FuelLabs/sway · error · anyhow::Error

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

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

When collecting Sway tests, forc inspects the should_revert attribute argument of a #[test] function and requires it to be a string literal whose content parses as a u64 (e.g. "42"); a code-specific revert becomes TestPassCondition::ShouldRevert(u64). Non-string arguments, or strings that are not a plain decimal u64, produce this error naming the test.

Source

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

        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())
        {
            Some(should_revert_arg) => {
                match should_revert_arg.get_string_opt(&Handler::default()) {
                    Ok(should_revert_arg_value) => TestPassCondition::ShouldRevert(
                        should_revert_arg_value
                            .map(|val| val.parse::<u64>())
                            .transpose()
                            .map_err(|_| {
                                anyhow!(get_invalid_revert_code_error_msg(
                                    &test_function_decl.name,
                                    should_revert_arg
                                ))
                            })?,
                    ),
                    Err(_) => bail!(get_invalid_revert_code_error_msg(
                        &test_function_decl.name,
                        should_revert_arg
                    )),
                }
            }
            None => TestPassCondition::ShouldNotRevert,
        };

        let file_path =
            Arc::new(engines.se().get_path(span.source_id().ok_or_else(|| {
                anyhow!("Missing span for test \"{}\".", test_function_decl.name)
            })?));

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Quote the value and use decimal digits: #[test(should_revert = "42")]
  2. If any revert code is acceptable, drop the value entirely: #[test(should_revert)]
  3. Remove hex prefixes, underscores and whitespace - u64::from_str only accepts plain decimal digits

Example fix

# before
#[test(should_revert = 42)]
fn test_reverts() { revert(42); }

# after
#[test(should_revert = "42")]
fn test_reverts() { revert(42); }
Defensive patterns

Strategy: validation

Validate before calling

// before invoking forc test, scan sources for should_revert assignments and
// require the value to be a quoted decimal u64 string
static BAD_REVERT: Lazy<Regex> =
    Lazy::new(|| Regex::new(r#"should_revert\s*=\s*([^"\s][^\s]*)"#).unwrap());

fn revert_args_are_valid(sway_source: &str) -> bool {
    BAD_REVERT.captures_iter(sway_source)
        .all(|c| c[1].parse::<u64>().is_ok())
}

Type guard

fn is_valid_revert_code(value: &str) -> bool {
    value.parse::<u64>().is_ok()
}

Prevention

When it happens

Trigger: Declaring #[test(should_revert = 42)] (unquoted integer), #[test(should_revert = "0x2a")] (hex), or any argument whose get_string_opt() or subsequent parse::<u64>() fails, then running forc test / forc build.

Common situations: Porting tests from syntaxes where revert codes are numeric literals; copy-pasting hex revert codes from transaction receipts; sway versions where attribute argument quoting rules changed.

Related errors


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