diff --git a/Cargo.lock b/Cargo.lock index 7e4db4fdd53e..3e3060b6f967 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4095,6 +4095,7 @@ dependencies = [ "rustc_ast", "rustc_ast_lowering", "rustc_ast_passes", + "rustc_ast_pretty", "rustc_attr", "rustc_borrowck", "rustc_builtin_macros", diff --git a/compiler/rustc_interface/Cargo.toml b/compiler/rustc_interface/Cargo.toml index 0e90836145ef..4b3b0728f385 100644 --- a/compiler/rustc_interface/Cargo.toml +++ b/compiler/rustc_interface/Cargo.toml @@ -10,6 +10,7 @@ rustc-rayon-core = { version = "0.5.0", optional = true } rustc_ast = { path = "../rustc_ast" } rustc_ast_lowering = { path = "../rustc_ast_lowering" } rustc_ast_passes = { path = "../rustc_ast_passes" } +rustc_ast_pretty = { path = "../rustc_ast_pretty" } rustc_attr = { path = "../rustc_attr" } rustc_borrowck = { path = "../rustc_borrowck" } rustc_builtin_macros = { path = "../rustc_builtin_macros" } diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index c5b81dbd6791..55304bbbd922 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -120,14 +120,45 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec) -> CheckCfg { ); let filename = FileName::cfg_spec_source_code(&s); + const VISIT: &str = + "visit for more details"; + macro_rules! error { ($reason:expr) => { #[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::diagnostic_outside_of_impl)] - dcx.fatal(format!( - concat!("invalid `--check-cfg` argument: `{}` (", $reason, ")"), - s - )) + { + let mut diag = + dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`")); + diag.note($reason); + diag.note(VISIT); + diag.emit() + } + }; + (in $arg:expr, $reason:expr) => { + #[allow(rustc::untranslatable_diagnostic)] + #[allow(rustc::diagnostic_outside_of_impl)] + { + let mut diag = + dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`")); + + let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string($arg); + if let Some(lit) = $arg.lit() { + let (lit_kind_article, lit_kind_descr) = { + let lit_kind = lit.as_token_lit().kind; + (lit_kind.article(), lit_kind.descr()) + }; + diag.note(format!( + "`{pparg}` is {lit_kind_article} {lit_kind_descr} literal" + )); + } else { + diag.note(format!("`{pparg}` is invalid")); + } + + diag.note($reason); + diag.note(VISIT); + diag.emit() + } }; } @@ -183,7 +214,7 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec) -> CheckCfg { } any_specified = true; if !args.is_empty() { - error!("`any()` must be empty"); + error!(in arg, "`any()` takes no argument"); } } else if arg.has_name(sym::values) && let Some(args) = arg.meta_item_list() @@ -202,25 +233,25 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec) -> CheckCfg { && let Some(args) = arg.meta_item_list() { if values_any_specified { - error!("`any()` in `values()` cannot be specified multiple times"); + error!(in arg, "`any()` in `values()` cannot be specified multiple times"); } values_any_specified = true; if !args.is_empty() { - error!("`any()` must be empty"); + error!(in arg, "`any()` in `values()` takes no argument"); } } else if arg.has_name(sym::none) && let Some(args) = arg.meta_item_list() { values.insert(None); if !args.is_empty() { - error!("`none()` must be empty"); + error!(in arg, "`none()` in `values()` takes no argument"); } } else { - error!("`values()` arguments must be string literals, `none()` or `any()`"); + error!(in arg, "`values()` arguments must be string literals, `none()` or `any()`"); } } } else { - error!("`cfg()` arguments must be simple identifiers, `any()` or `values(...)`"); + error!(in arg, "`cfg()` arguments must be simple identifiers, `any()` or `values(...)`"); } } diff --git a/tests/ui/check-cfg/invalid-arguments.any_values.stderr b/tests/ui/check-cfg/invalid-arguments.any_values.stderr index f9a9c4a6e132..65ef5155fa1a 100644 --- a/tests/ui/check-cfg/invalid-arguments.any_values.stderr +++ b/tests/ui/check-cfg/invalid-arguments.any_values.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(any(),values())` (`values()` cannot be specified before the names) +error: invalid `--check-cfg` argument: `cfg(any(),values())` + | + = note: `values()` cannot be specified before the names + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.anything_else.stderr b/tests/ui/check-cfg/invalid-arguments.anything_else.stderr index 925664bb3fc6..f3bc0b782e22 100644 --- a/tests/ui/check-cfg/invalid-arguments.anything_else.stderr +++ b/tests/ui/check-cfg/invalid-arguments.anything_else.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `anything_else(...)` (expected `cfg(name, values("value1", "value2", ... "valueN"))`) +error: invalid `--check-cfg` argument: `anything_else(...)` + | + = note: expected `cfg(name, values("value1", "value2", ... "valueN"))` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.boolean.stderr b/tests/ui/check-cfg/invalid-arguments.boolean.stderr new file mode 100644 index 000000000000..18734de9dac0 --- /dev/null +++ b/tests/ui/check-cfg/invalid-arguments.boolean.stderr @@ -0,0 +1,6 @@ +error: invalid `--check-cfg` argument: `cfg(true)` + | + = note: `true` is a boolean literal + = note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)` + = note: visit for more details + diff --git a/tests/ui/check-cfg/invalid-arguments.cfg_none.stderr b/tests/ui/check-cfg/invalid-arguments.cfg_none.stderr index 7992dbdff005..ef464260c29d 100644 --- a/tests/ui/check-cfg/invalid-arguments.cfg_none.stderr +++ b/tests/ui/check-cfg/invalid-arguments.cfg_none.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(none())` (`cfg()` arguments must be simple identifiers, `any()` or `values(...)`) +error: invalid `--check-cfg` argument: `cfg(none())` + | + = note: `none()` is invalid + = note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.giberich.stderr b/tests/ui/check-cfg/invalid-arguments.giberich.stderr index d427033fcc26..3e3501454918 100644 --- a/tests/ui/check-cfg/invalid-arguments.giberich.stderr +++ b/tests/ui/check-cfg/invalid-arguments.giberich.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(...)` (expected `cfg(name, values("value1", "value2", ... "valueN"))`) +error: invalid `--check-cfg` argument: `cfg(...)` + | + = note: expected `cfg(name, values("value1", "value2", ... "valueN"))` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.ident_in_values_1.stderr b/tests/ui/check-cfg/invalid-arguments.ident_in_values_1.stderr index 90308bdcd235..cfedb7ed5174 100644 --- a/tests/ui/check-cfg/invalid-arguments.ident_in_values_1.stderr +++ b/tests/ui/check-cfg/invalid-arguments.ident_in_values_1.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values(bar))` (`values()` arguments must be string literals, `none()` or `any()`) +error: invalid `--check-cfg` argument: `cfg(foo,values(bar))` + | + = note: `bar` is invalid + = note: `values()` arguments must be string literals, `none()` or `any()` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.ident_in_values_2.stderr b/tests/ui/check-cfg/invalid-arguments.ident_in_values_2.stderr index 16f92a504a5f..ba194862284d 100644 --- a/tests/ui/check-cfg/invalid-arguments.ident_in_values_2.stderr +++ b/tests/ui/check-cfg/invalid-arguments.ident_in_values_2.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values("bar",bar,"bar"))` (`values()` arguments must be string literals, `none()` or `any()`) +error: invalid `--check-cfg` argument: `cfg(foo,values("bar",bar,"bar"))` + | + = note: `bar` is invalid + = note: `values()` arguments must be string literals, `none()` or `any()` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.mixed_any.stderr b/tests/ui/check-cfg/invalid-arguments.mixed_any.stderr index 9239f8cce945..512ecbe9edaf 100644 --- a/tests/ui/check-cfg/invalid-arguments.mixed_any.stderr +++ b/tests/ui/check-cfg/invalid-arguments.mixed_any.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(any(),values(any()))` (`values()` cannot be specified before the names) +error: invalid `--check-cfg` argument: `cfg(any(),values(any()))` + | + = note: `values()` cannot be specified before the names + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.mixed_values_any.stderr b/tests/ui/check-cfg/invalid-arguments.mixed_values_any.stderr index 4c406143d080..2d59b12097d7 100644 --- a/tests/ui/check-cfg/invalid-arguments.mixed_values_any.stderr +++ b/tests/ui/check-cfg/invalid-arguments.mixed_values_any.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values("bar",any()))` (`values()` arguments cannot specify string literals and `any()` at the same time) +error: invalid `--check-cfg` argument: `cfg(foo,values("bar",any()))` + | + = note: `values()` arguments cannot specify string literals and `any()` at the same time + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.multiple_any.stderr b/tests/ui/check-cfg/invalid-arguments.multiple_any.stderr index 6f1db1b13c31..6eb63de42524 100644 --- a/tests/ui/check-cfg/invalid-arguments.multiple_any.stderr +++ b/tests/ui/check-cfg/invalid-arguments.multiple_any.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(any(),any())` (`any()` cannot be specified multiple times) +error: invalid `--check-cfg` argument: `cfg(any(),any())` + | + = note: `any()` cannot be specified multiple times + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.multiple_values.stderr b/tests/ui/check-cfg/invalid-arguments.multiple_values.stderr index bce305b09c30..06060078bc02 100644 --- a/tests/ui/check-cfg/invalid-arguments.multiple_values.stderr +++ b/tests/ui/check-cfg/invalid-arguments.multiple_values.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values(),values())` (`values()` cannot be specified multiple times) +error: invalid `--check-cfg` argument: `cfg(foo,values(),values())` + | + = note: `values()` cannot be specified multiple times + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.multiple_values_any.stderr b/tests/ui/check-cfg/invalid-arguments.multiple_values_any.stderr index 748ce231af73..72554ac3ead8 100644 --- a/tests/ui/check-cfg/invalid-arguments.multiple_values_any.stderr +++ b/tests/ui/check-cfg/invalid-arguments.multiple_values_any.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values(any(),any()))` (`any()` in `values()` cannot be specified multiple times) +error: invalid `--check-cfg` argument: `cfg(foo,values(any(),any()))` + | + = note: `any()` is invalid + = note: `any()` in `values()` cannot be specified multiple times + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.none_not_empty.stderr b/tests/ui/check-cfg/invalid-arguments.none_not_empty.stderr index 0a6c6ffd42f2..6e9d87bace2f 100644 --- a/tests/ui/check-cfg/invalid-arguments.none_not_empty.stderr +++ b/tests/ui/check-cfg/invalid-arguments.none_not_empty.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values(none("test")))` (`none()` must be empty) +error: invalid `--check-cfg` argument: `cfg(foo,values(none("test")))` + | + = note: `none("test")` is invalid + = note: `none()` in `values()` takes no argument + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.not_empty_any.stderr b/tests/ui/check-cfg/invalid-arguments.not_empty_any.stderr index daf38147fe53..35eb1949ad7c 100644 --- a/tests/ui/check-cfg/invalid-arguments.not_empty_any.stderr +++ b/tests/ui/check-cfg/invalid-arguments.not_empty_any.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(any(foo))` (`any()` must be empty) +error: invalid `--check-cfg` argument: `cfg(any(foo))` + | + = note: `any(foo)` is invalid + = note: `any()` takes no argument + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.not_empty_values_any.stderr b/tests/ui/check-cfg/invalid-arguments.not_empty_values_any.stderr index 79f83e802ca5..cc41d21bec6e 100644 --- a/tests/ui/check-cfg/invalid-arguments.not_empty_values_any.stderr +++ b/tests/ui/check-cfg/invalid-arguments.not_empty_values_any.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values(any(bar)))` (`any()` must be empty) +error: invalid `--check-cfg` argument: `cfg(foo,values(any(bar)))` + | + = note: `any(bar)` is invalid + = note: `any()` in `values()` takes no argument + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.rs b/tests/ui/check-cfg/invalid-arguments.rs index 84087a16e80e..5141bc11bd7a 100644 --- a/tests/ui/check-cfg/invalid-arguments.rs +++ b/tests/ui/check-cfg/invalid-arguments.rs @@ -1,7 +1,7 @@ // Check that invalid --check-cfg are rejected // //@ check-fail -//@ revisions: anything_else +//@ revisions: anything_else boolean //@ revisions: string_for_name_1 string_for_name_2 multiple_any multiple_values //@ revisions: multiple_values_any not_empty_any not_empty_values_any //@ revisions: values_any_missing_values values_any_before_ident ident_in_values_1 @@ -10,6 +10,7 @@ //@ revisions: none_not_empty cfg_none // //@ [anything_else]compile-flags: --check-cfg=anything_else(...) +//@ [boolean]compile-flags: --check-cfg=cfg(true) //@ [string_for_name_1]compile-flags: --check-cfg=cfg("NOT_IDENT") //@ [string_for_name_2]compile-flags: --check-cfg=cfg(foo,"NOT_IDENT",bar) //@ [multiple_any]compile-flags: --check-cfg=cfg(any(),any()) diff --git a/tests/ui/check-cfg/invalid-arguments.string_for_name_1.stderr b/tests/ui/check-cfg/invalid-arguments.string_for_name_1.stderr index c6f6834ffd30..7022b709b64a 100644 --- a/tests/ui/check-cfg/invalid-arguments.string_for_name_1.stderr +++ b/tests/ui/check-cfg/invalid-arguments.string_for_name_1.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg("NOT_IDENT")` (`cfg()` arguments must be simple identifiers, `any()` or `values(...)`) +error: invalid `--check-cfg` argument: `cfg("NOT_IDENT")` + | + = note: `"NOT_IDENT"` is a string literal + = note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.string_for_name_2.stderr b/tests/ui/check-cfg/invalid-arguments.string_for_name_2.stderr index ab3dc86cd1ad..ea96b913907a 100644 --- a/tests/ui/check-cfg/invalid-arguments.string_for_name_2.stderr +++ b/tests/ui/check-cfg/invalid-arguments.string_for_name_2.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,"NOT_IDENT",bar)` (`cfg()` arguments must be simple identifiers, `any()` or `values(...)`) +error: invalid `--check-cfg` argument: `cfg(foo,"NOT_IDENT",bar)` + | + = note: `"NOT_IDENT"` is a string literal + = note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_1.stderr b/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_1.stderr index c04b15ec265c..f82c520c62a6 100644 --- a/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_1.stderr +++ b/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_1.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `abc()` (expected `cfg(name, values("value1", "value2", ... "valueN"))`) +error: invalid `--check-cfg` argument: `abc()` + | + = note: expected `cfg(name, values("value1", "value2", ... "valueN"))` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_2.stderr b/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_2.stderr index cee65f9887b9..ee62c25b41cb 100644 --- a/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_2.stderr +++ b/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_2.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,test())` (`cfg()` arguments must be simple identifiers, `any()` or `values(...)`) +error: invalid `--check-cfg` argument: `cfg(foo,test())` + | + = note: `test()` is invalid + = note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_3.stderr b/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_3.stderr index a023779b35a7..a8ab9cc9d303 100644 --- a/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_3.stderr +++ b/tests/ui/check-cfg/invalid-arguments.unknown_meta_item_3.stderr @@ -1,2 +1,6 @@ -error: invalid `--check-cfg` argument: `cfg(foo,values(test()))` (`values()` arguments must be string literals, `none()` or `any()`) +error: invalid `--check-cfg` argument: `cfg(foo,values(test()))` + | + = note: `test()` is invalid + = note: `values()` arguments must be string literals, `none()` or `any()` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.unterminated.stderr b/tests/ui/check-cfg/invalid-arguments.unterminated.stderr index 80161a6aa0fc..150e1d66426b 100644 --- a/tests/ui/check-cfg/invalid-arguments.unterminated.stderr +++ b/tests/ui/check-cfg/invalid-arguments.unterminated.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(` (expected `cfg(name, values("value1", "value2", ... "valueN"))`) +error: invalid `--check-cfg` argument: `cfg(` + | + = note: expected `cfg(name, values("value1", "value2", ... "valueN"))` + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.values_any_before_ident.stderr b/tests/ui/check-cfg/invalid-arguments.values_any_before_ident.stderr index fc93ec8fbdfd..7013bb8e0951 100644 --- a/tests/ui/check-cfg/invalid-arguments.values_any_before_ident.stderr +++ b/tests/ui/check-cfg/invalid-arguments.values_any_before_ident.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(values(any()),foo)` (`values()` cannot be specified before the names) +error: invalid `--check-cfg` argument: `cfg(values(any()),foo)` + | + = note: `values()` cannot be specified before the names + = note: visit for more details diff --git a/tests/ui/check-cfg/invalid-arguments.values_any_missing_values.stderr b/tests/ui/check-cfg/invalid-arguments.values_any_missing_values.stderr index f41672fcbdb6..ad1af73c6901 100644 --- a/tests/ui/check-cfg/invalid-arguments.values_any_missing_values.stderr +++ b/tests/ui/check-cfg/invalid-arguments.values_any_missing_values.stderr @@ -1,2 +1,5 @@ -error: invalid `--check-cfg` argument: `cfg(foo,any())` (`cfg(any())` can only be provided in isolation) +error: invalid `--check-cfg` argument: `cfg(foo,any())` + | + = note: `cfg(any())` can only be provided in isolation + = note: visit for more details