FuelLabs/sway · error · anyhow::Error

Invalid revert code for test "{}". A revert code must be a s

Error message

Invalid revert code for test "{}".
A revert code must be a string containing a "u64", e.g.: "42".
The invalid revert code was: {}.

What it means

While collecting tests for a package, PkgTestEntry::from_decl reads the last `should_revert` argument of a `#[test]` attribute. The value must be a string literal parseable as a decimal u64 (`val.parse::<u64>()`), or omitted entirely (bare `#[test(should_revert)]`). This anyhow error names the offending test and echoes the invalid value when the string fails to parse (or when get_string_opt reports the argument is not a string).

Source

Thrown at forc-pkg/src/pkg.rs:2071

}

impl PkgEntryKind {
    /// Returns `Some` if the `PkgEntryKind` is `Test`.
    pub fn test(&self) -> Option<&PkgTestEntry> {
        match self {
            PkgEntryKind::Test(test) => Some(test),
            _ => None,
        }
    }
}

impl PkgTestEntry {
    fn from_decl(decl_ref: &DeclRefFunction, engines: &Engines) -> Result<Self> {
        fn get_invalid_revert_code_error_msg(
            test_function_name: &Ident,
            should_revert_arg: &AttributeArg,
        ) -> String {
            format!("Invalid revert code for test \"{}\".\nA revert code must be a string containing a \"u64\", e.g.: \"42\".\nThe invalid revert code was: {}.",
                test_function_name,
                should_revert_arg.value.as_ref().expect("`get_string_opt` returned either a value or an error, which means that the invalid value must exist").span().as_str(),
            )
        }

        let span = decl_ref.span();
        let test_function_decl = engines.de().get_function(decl_ref);

        let Some(test_attr) = test_function_decl.attributes.test() else {
            unreachable!("`test_function_decl` is guaranteed to be a test function and it must have a `#[test]` attribute");
        };

        let pass_condition = match test_attr
            .args
            .iter()
            // Last "should_revert" argument wins ;-)
            .rfind(|arg| arg.is_test_should_revert())
        {

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Use a decimal u64 in a string: `#[test(should_revert = "42")]`.
  2. If any revert is acceptable, drop the value: `#[test(should_revert)]`.
  3. Convert hex codes to decimal before embedding (0x2a -> "42").

Example fix

// before
#[test(should_revert = "0x2a")]
fn test_revert() { revert(42); }
// after
#[test(should_revert = "42")]
fn test_revert() { revert(42); }
Defensive patterns

Strategy: validation

Validate before calling

# CI lint: every should_revert value must be a decimal u64 string
grep -RnoE '#\[test\(should_revert *= *"[^"]*"\)\]' --include='*.sw' . \
  | sed -E 's/.*"([^"]*)"\)$/\1/' | grep -qvE '^[0-9]+$' \
  && { echo 'non-u64 should_revert value found' >&2; exit 1; } || true

Prevention

When it happens

Trigger: `#[test(should_revert = "0x2a")]` (hex not accepted by u64::from_str); `should_revert = ""` or `should_revert = "42a"`; passing a non-string literal as the revert code; duplicated should_revert args where the last one is malformed (last one wins).

Common situations: Porting tests from syntax/docs that show hex revert codes; copy-pasting contract error codes (often hex) into should_revert; typos in the numeric string; using a bare u64 literal instead of a quoted string.

Related errors


AI-assisted analysis of FuelLabs/sway@47e5e902fa (2026-08-16). Data as JSON: /api/errors/9dccdfe9179ad023. Report an issue: GitHub.