diff --git a/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs b/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs index 642badfeadf..4144d0b085c 100644 --- a/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs +++ b/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs @@ -4943,8 +4943,13 @@ pub fn cfg_eval( } }, _ => { - // Already checked with `AttributeKind::expected_args_*` - unreachable!("cfg attribute should only have the `target` or the `program_type` argument"); + return Err(handler.emit_err( + ConvertParseTreeError::InvalidCfgArg { + span: arg.span(), + value: arg.name.as_str().to_string(), + } + .into(), + )); } } } diff --git a/sway-error/src/convert_parse_tree_error.rs b/sway-error/src/convert_parse_tree_error.rs index 2fdfdc58d75..6c909e2016e 100644 --- a/sway-error/src/convert_parse_tree_error.rs +++ b/sway-error/src/convert_parse_tree_error.rs @@ -119,6 +119,8 @@ pub enum ConvertParseTreeError { ExpectedCfgProgramTypeArgValue { span: Span }, #[error("Expected \"true\" or \"false\" for experimental_new_encoding")] ExpectedExperimentalNewEncodingArgValue { span: Span }, + #[error("Unexpected attribute value: \"{value}\" for attribute: \"cfg\"")] + InvalidCfgArg { span: Span, value: String }, } impl Spanned for ConvertParseTreeError { @@ -182,6 +184,7 @@ impl Spanned for ConvertParseTreeError { ConvertParseTreeError::InvalidCfgProgramTypeArgValue { span, .. } => span.clone(), ConvertParseTreeError::ExpectedCfgProgramTypeArgValue { span } => span.clone(), ConvertParseTreeError::ExpectedExperimentalNewEncodingArgValue { span } => span.clone(), + ConvertParseTreeError::InvalidCfgArg { span, .. } => span.clone(), } } } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/Forc.lock new file mode 100644 index 00000000000..1cef6b3f7a1 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/Forc.lock @@ -0,0 +1,3 @@ +[[package]] +name = "invalid_cfg_arg" +source = "member" diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/Forc.toml new file mode 100644 index 00000000000..582fa8b5e47 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/Forc.toml @@ -0,0 +1,6 @@ +[project] +name = "invalid_cfg_arg" +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +implicit-std = false diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/src/main.sw new file mode 100644 index 00000000000..c9ad0835d76 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/src/main.sw @@ -0,0 +1,2 @@ +predicate; +#[cfg(c)] a diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/test.toml new file mode 100644 index 00000000000..1f48bdf0556 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/invalid_cfg_arg/test.toml @@ -0,0 +1,10 @@ +category = "fail" + +# check: $()#[cfg(c)] a +# nextln: $()Unexpected attribute value: "c" for attribute: "cfg" expected value "target" or "program_type" or "experimental_new_encoding" + +# check: $()#[cfg(c)] a +# nextln: $()Expected an item. + +# check: $()#[cfg(c)] a +# nextln: $()Unexpected attribute value: "c" for attribute: "cfg" \ No newline at end of file