From f7fa086f5a7a720fe41f5e5314b3e5ada3c3cb4d Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Thu, 14 Nov 2024 18:31:39 +0530 Subject: [PATCH] Add revert with log function to std lib (#6717) ## Description Adds a `revert_with_log` function that reverts unconditionally with a message Closes #6629 ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com> --- sway-lib-std/src/error_signals.sw | 7 ++++ sway-lib-std/src/prelude.sw | 2 +- sway-lib-std/src/revert.sw | 34 ++++++++++++++++++- .../revert_inline_tests/src/main.sw | 5 +++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/sway-lib-std/src/error_signals.sw b/sway-lib-std/src/error_signals.sw index bbc4f6f291b..87f86699378 100644 --- a/sway-lib-std/src/error_signals.sw +++ b/sway-lib-std/src/error_signals.sw @@ -35,3 +35,10 @@ pub const FAILED_ASSERT_SIGNAL = 0xffff_ffff_ffff_0004; /// /// The value is: 18446744073709486085 pub const FAILED_ASSERT_NE_SIGNAL = 0xffff_ffff_ffff_0005; + +/// A revert with this value signals that it was caused by a call to `std::revert::revert_with_log`. +/// +/// # Additional Information +/// +/// The value is: 18446744073709486086 +pub const REVERT_WITH_LOG_SIGNAL = 0xffff_ffff_ffff_0006; diff --git a/sway-lib-std/src/prelude.sw b/sway-lib-std/src/prelude.sw index 6f207cc7b45..f8d42cf0044 100644 --- a/sway-lib-std/src/prelude.sw +++ b/sway-lib-std/src/prelude.sw @@ -21,7 +21,7 @@ pub use ::vec::{Vec, VecIter}; pub use ::assert::{assert, assert_eq, assert_ne}; pub use ::option::Option::{self, *}; pub use ::result::Result::{self, *}; -pub use ::revert::{require, revert}; +pub use ::revert::{require, revert, revert_with_log}; // Convert pub use ::convert::From; diff --git a/sway-lib-std/src/revert.sw b/sway-lib-std/src/revert.sw index ba6ddeadf88..f53dd5378d2 100644 --- a/sway-lib-std/src/revert.sw +++ b/sway-lib-std/src/revert.sw @@ -2,7 +2,7 @@ library; use ::logging::log; -use ::error_signals::FAILED_REQUIRE_SIGNAL; +use ::error_signals::{FAILED_REQUIRE_SIGNAL, REVERT_WITH_LOG_SIGNAL}; /// Will either panic or revert with a given number depending on the context. /// @@ -71,3 +71,35 @@ where revert(FAILED_REQUIRE_SIGNAL) } } + +/// Reverts unconditionally and logs `value`. +/// +/// # Arguments +/// +/// * `value`: [T] - The value which will be logged. +/// +/// # Reverts +/// +/// * Reverts unconditionally. +/// +/// # Examples +/// +/// ```sway +/// fn foo() { +/// revert_with_log("Example error message"); +/// } +/// ``` +#[cfg(experimental_new_encoding = false)] +pub fn revert_with_log(value: T) { + log(value); + revert(REVERT_WITH_LOG_SIGNAL) +} + +#[cfg(experimental_new_encoding = true)] +pub fn revert_with_log(value: T) +where + T: AbiEncode, +{ + log(value); + revert(REVERT_WITH_LOG_SIGNAL) +} diff --git a/test/src/in_language_tests/test_programs/revert_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/revert_inline_tests/src/main.sw index cff1e49fc0e..64c74e30b63 100644 --- a/test/src/in_language_tests/test_programs/revert_inline_tests/src/main.sw +++ b/test/src/in_language_tests/test_programs/revert_inline_tests/src/main.sw @@ -14,3 +14,8 @@ fn revert_revert_require() { fn pass_revert_require() { require(true, "error"); } + +#[test(should_revert)] +fn revert_revert_with_log() { + revert_with_log("error") +} \ No newline at end of file