{"record":{"id":"7ea2f30135e459d5","repo":"FuelLabs/sway","slug":"invalid-revert-code-for-test-a-revert-code","errorCode":null,"errorMessage":"Invalid revert code for test \\\"{}\\\".\nA revert code must be a string containing a \\\"u64\\\", e.g.: \\\"42\\\".\nThe invalid revert code was: {}.","messagePattern":"Invalid revert code for test \\\\\"(.+?)\\\\\"\\.\nA revert code must be a string containing a \\\\\"u64\\\\\", e\\.g\\.: \\\\\"42\\\\\"\\.\nThe invalid revert code was: (.+?)\\.","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"forc-pkg/src/pkg.rs","lineNumber":2097,"sourceCode":"\n        let Some(test_attr) = test_function_decl.attributes.test() else {\n            unreachable!(\"`test_function_decl` is guaranteed to be a test function and it must have a `#[test]` attribute\");\n        };\n\n        let pass_condition = match test_attr\n            .args\n            .iter()\n            // Last \"should_revert\" argument wins ;-)\n            .rfind(|arg| arg.is_test_should_revert())\n        {\n            Some(should_revert_arg) => {\n                match should_revert_arg.get_string_opt(&Handler::default()) {\n                    Ok(should_revert_arg_value) => TestPassCondition::ShouldRevert(\n                        should_revert_arg_value\n                            .map(|val| val.parse::<u64>())\n                            .transpose()\n                            .map_err(|_| {\n                                anyhow!(get_invalid_revert_code_error_msg(\n                                    &test_function_decl.name,\n                                    should_revert_arg\n                                ))\n                            })?,\n                    ),\n                    Err(_) => bail!(get_invalid_revert_code_error_msg(\n                        &test_function_decl.name,\n                        should_revert_arg\n                    )),\n                }\n            }\n            None => TestPassCondition::ShouldNotRevert,\n        };\n\n        let file_path =\n            Arc::new(engines.se().get_path(span.source_id().ok_or_else(|| {\n                anyhow!(\"Missing span for test \\\"{}\\\".\", test_function_decl.name)\n            })?));","sourceCodeStart":2079,"sourceCodeEnd":2115,"githubUrl":"https://github.com/FuelLabs/sway/blob/47e5e902faa42baf652dd6a0c88cd23390c1a614/forc-pkg/src/pkg.rs#L2079-L2115","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Quote the value and use decimal digits: #[test(should_revert = \"42\")]","If any revert code is acceptable, drop the value entirely: #[test(should_revert)]","Remove hex prefixes, underscores and whitespace - u64::from_str only accepts plain decimal digits"],"exampleFix":"# before\n#[test(should_revert = 42)]\nfn test_reverts() { revert(42); }\n\n# after\n#[test(should_revert = \"42\")]\nfn test_reverts() { revert(42); }","handlingStrategy":"validation","validationCode":"// before invoking forc test, scan sources for should_revert assignments and\n// require the value to be a quoted decimal u64 string\nstatic BAD_REVERT: Lazy<Regex> =\n    Lazy::new(|| Regex::new(r#\"should_revert\\s*=\\s*([^\"\\s][^\\s]*)\"#).unwrap());\n\nfn revert_args_are_valid(sway_source: &str) -> bool {\n    BAD_REVERT.captures_iter(sway_source)\n        .all(|c| c[1].parse::<u64>().is_ok())\n}","typeGuard":"fn is_valid_revert_code(value: &str) -> bool {\n    value.parse::<u64>().is_ok()\n}","tryCatchPattern":null,"preventionTips":["Always quote revert codes as decimal strings","Prefer bare should_revert unless the exact code matters","Add a CI lint that rejects non-string should_revert values in .sw files"],"tags":["sway","testing","forc","parsing"],"backgroundTag":null,"analyzedSha":"47e5e902faa42baf652dd6a0c88cd23390c1a614","analyzedAt":"2026-08-16T07:57:45.555Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}