Skip to content

Commit

Permalink
Auto merge of #124679 - Urgau:check-cfg-structured-cli-errors, r=nnet…
Browse files Browse the repository at this point in the history
…hercote

Improve check-cfg CLI errors with more structured diagnostics

This PR improve check-cfg CLI errors with more structured diagnostics.

In particular it now shows the statement where the error occurred, what kind lit it is, as well as pointing users to the doc for more details.

`@rustbot` label +F-check-cfg
  • Loading branch information
bors committed May 6, 2024
2 parents d287f3e + 228496e commit 69f53f5
Show file tree
Hide file tree
Showing 28 changed files with 151 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4162,6 +4162,7 @@ dependencies = [
"rustc_ast",
"rustc_ast_lowering",
"rustc_ast_passes",
"rustc_ast_pretty",
"rustc_attr",
"rustc_borrowck",
"rustc_builtin_macros",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl LitKind {

pub fn descr(self) -> &'static str {
match self {
Bool => panic!("literal token contains `Lit::Bool`"),
Bool => "boolean",
Byte => "byte",
Char => "char",
Integer => "integer",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
51 changes: 41 additions & 10 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,45 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
);
let filename = FileName::cfg_spec_source_code(&s);

const VISIT: &str =
"visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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()
}
};
}

Expand Down Expand Up @@ -183,7 +214,7 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> 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()
Expand All @@ -202,25 +233,25 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> 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(...)`");
}
}

Expand Down
5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.any_values.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.anything_else.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

6 changes: 6 additions & 0 deletions tests/ui/check-cfg/invalid-arguments.boolean.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

6 changes: 5 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.cfg_none.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.giberich.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.mixed_any.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.mixed_values_any.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.multiple_any.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.multiple_values.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

6 changes: 5 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.none_not_empty.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

6 changes: 5 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.not_empty_any.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

3 changes: 2 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
//@ check-fail
//@ no-auto-check-cfg
//@ 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
Expand All @@ -11,6 +11,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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

5 changes: 4 additions & 1 deletion tests/ui/check-cfg/invalid-arguments.unterminated.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

0 comments on commit 69f53f5

Please sign in to comment.