Skip to content

Commit f39d5b1

Browse files
committed
Improve check-cfg CLI errors with more structured diagnostics
1 parent 1364b41 commit f39d5b1

27 files changed

+150
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,7 @@ dependencies = [
40934093
"rustc_ast",
40944094
"rustc_ast_lowering",
40954095
"rustc_ast_passes",
4096+
"rustc_ast_pretty",
40964097
"rustc_attr",
40974098
"rustc_borrowck",
40984099
"rustc_builtin_macros",

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rustc-rayon-core = { version = "0.5.0", optional = true }
1010
rustc_ast = { path = "../rustc_ast" }
1111
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
1212
rustc_ast_passes = { path = "../rustc_ast_passes" }
13+
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1314
rustc_attr = { path = "../rustc_attr" }
1415
rustc_borrowck = { path = "../rustc_borrowck" }
1516
rustc_builtin_macros = { path = "../rustc_builtin_macros" }

compiler/rustc_interface/src/interface.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,45 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
120120
);
121121
let filename = FileName::cfg_spec_source_code(&s);
122122

123+
const VISIT: &str =
124+
"visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details";
125+
123126
macro_rules! error {
124127
($reason:expr) => {
125128
#[allow(rustc::untranslatable_diagnostic)]
126129
#[allow(rustc::diagnostic_outside_of_impl)]
127-
dcx.fatal(format!(
128-
concat!("invalid `--check-cfg` argument: `{}` (", $reason, ")"),
129-
s
130-
))
130+
{
131+
let mut diag =
132+
dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
133+
diag.note($reason);
134+
diag.note(VISIT);
135+
diag.emit()
136+
}
137+
};
138+
(in $arg:expr, $reason:expr) => {
139+
#[allow(rustc::untranslatable_diagnostic)]
140+
#[allow(rustc::diagnostic_outside_of_impl)]
141+
{
142+
let mut diag =
143+
dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
144+
145+
let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string($arg);
146+
if let Some(lit) = $arg.lit() {
147+
let (lit_kind_article, lit_kind_descr) = {
148+
let lit_kind = lit.as_token_lit().kind;
149+
(lit_kind.article(), lit_kind.descr())
150+
};
151+
diag.note(format!(
152+
"`{pparg}` is {lit_kind_article} {lit_kind_descr} literal"
153+
));
154+
} else {
155+
diag.note(format!("`{pparg}` is invalid"));
156+
}
157+
158+
diag.note($reason);
159+
diag.note(VISIT);
160+
diag.emit()
161+
}
131162
};
132163
}
133164

@@ -183,7 +214,7 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
183214
}
184215
any_specified = true;
185216
if !args.is_empty() {
186-
error!("`any()` must be empty");
217+
error!(in arg, "`any()` takes no argument");
187218
}
188219
} else if arg.has_name(sym::values)
189220
&& let Some(args) = arg.meta_item_list()
@@ -202,25 +233,25 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
202233
&& let Some(args) = arg.meta_item_list()
203234
{
204235
if values_any_specified {
205-
error!("`any()` in `values()` cannot be specified multiple times");
236+
error!(in arg, "`any()` in `values()` cannot be specified multiple times");
206237
}
207238
values_any_specified = true;
208239
if !args.is_empty() {
209-
error!("`any()` must be empty");
240+
error!(in arg, "`any()` in `values()` takes no argument");
210241
}
211242
} else if arg.has_name(sym::none)
212243
&& let Some(args) = arg.meta_item_list()
213244
{
214245
values.insert(None);
215246
if !args.is_empty() {
216-
error!("`none()` must be empty");
247+
error!(in arg, "`none()` in `values()` takes no argument");
217248
}
218249
} else {
219-
error!("`values()` arguments must be string literals, `none()` or `any()`");
250+
error!(in arg, "`values()` arguments must be string literals, `none()` or `any()`");
220251
}
221252
}
222253
} else {
223-
error!("`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
254+
error!(in arg, "`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
224255
}
225256
}
226257

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
error: invalid `--check-cfg` argument: `cfg(any(),values())` (`values()` cannot be specified before the names)
1+
error: invalid `--check-cfg` argument: `cfg(any(),values())`
2+
|
3+
= note: `values()` cannot be specified before the names
4+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
25

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
error: invalid `--check-cfg` argument: `anything_else(...)` (expected `cfg(name, values("value1", "value2", ... "valueN"))`)
1+
error: invalid `--check-cfg` argument: `anything_else(...)`
2+
|
3+
= note: expected `cfg(name, values("value1", "value2", ... "valueN"))`
4+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
25

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: invalid `--check-cfg` argument: `cfg(true)`
2+
|
3+
= note: `true` is a boolean literal
4+
= note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)`
5+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
6+
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
error: invalid `--check-cfg` argument: `cfg(none())` (`cfg()` arguments must be simple identifiers, `any()` or `values(...)`)
1+
error: invalid `--check-cfg` argument: `cfg(none())`
2+
|
3+
= note: `none()` is invalid
4+
= note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)`
5+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
26

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
error: invalid `--check-cfg` argument: `cfg(...)` (expected `cfg(name, values("value1", "value2", ... "valueN"))`)
1+
error: invalid `--check-cfg` argument: `cfg(...)`
2+
|
3+
= note: expected `cfg(name, values("value1", "value2", ... "valueN"))`
4+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
25

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
error: invalid `--check-cfg` argument: `cfg(foo,values(bar))` (`values()` arguments must be string literals, `none()` or `any()`)
1+
error: invalid `--check-cfg` argument: `cfg(foo,values(bar))`
2+
|
3+
= note: `bar` is invalid
4+
= note: `values()` arguments must be string literals, `none()` or `any()`
5+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
26

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
error: invalid `--check-cfg` argument: `cfg(foo,values("bar",bar,"bar"))` (`values()` arguments must be string literals, `none()` or `any()`)
1+
error: invalid `--check-cfg` argument: `cfg(foo,values("bar",bar,"bar"))`
2+
|
3+
= note: `bar` is invalid
4+
= note: `values()` arguments must be string literals, `none()` or `any()`
5+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
26

0 commit comments

Comments
 (0)