Skip to content

Commit

Permalink
Add revert with log function to std lib (#6717)
Browse files Browse the repository at this point in the history
## 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 <[email protected]>
  • Loading branch information
SwayStar123 and sdankel authored Nov 14, 2024
1 parent 861ca10 commit f7fa086
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
7 changes: 7 additions & 0 deletions sway-lib-std/src/error_signals.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion sway-lib-std/src/prelude.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 33 additions & 1 deletion sway-lib-std/src/revert.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<T>(value: T) {
log(value);
revert(REVERT_WITH_LOG_SIGNAL)
}

#[cfg(experimental_new_encoding = true)]
pub fn revert_with_log<T>(value: T)
where
T: AbiEncode,
{
log(value);
revert(REVERT_WITH_LOG_SIGNAL)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit f7fa086

Please sign in to comment.