Skip to content

Commit e38cbc7

Browse files
authored
Rollup merge of #92835 - iwanders:issue-66450-improve-cfg-error-message, r=nagisa
Improve error message for key="value" cfg arguments. Hi, I ran into difficulties using the `--cfg` flag syntax, first hit when googling for the error was issue #66450. Reading that issue, it sounded like the best way to improve the experience was to improve the error message, this is low risk and doesn't introduce any additional argument parsing. The issue mentions that it is entirely dependent on the shell, while this may be true, I think guiding the the user into the realization that the quotes may need to be escaped is helpful. The two suggested escapings both work in Bash and in the Windows command prompt. fyi `@ehuss`
2 parents 701a833 + af87248 commit e38cbc7

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

compiler/rustc_interface/src/interface.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,16 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
124124
Err(errs) => errs.into_iter().for_each(|mut err| err.cancel()),
125125
}
126126

127-
error!(r#"expected `key` or `key="value"`"#);
127+
// If the user tried to use a key="value" flag, but is missing the quotes, provide
128+
// a hint about how to resolve this.
129+
if s.contains("=") && !s.contains("=\"") && !s.ends_with("\"") {
130+
error!(concat!(
131+
r#"expected `key` or `key="value"`, ensure escaping is appropriate"#,
132+
r#" for your shell, try 'key="value"' or key=\"value\""#
133+
));
134+
} else {
135+
error!(r#"expected `key` or `key="value"`"#);
136+
}
128137
})
129138
.collect::<CrateConfig>();
130139
cfg.into_iter().map(|(a, b)| (a.to_string(), b.map(|b| b.to_string()))).collect()
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// compile-flags: --cfg a(b=c)
2-
// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`)
2+
// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")
33
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`)
1+
error: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")
22

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Test for missing quotes around value, issue #66450.
2+
// compile-flags: --cfg key=value
3+
// error-pattern: invalid `--cfg` argument: `key=value` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: invalid `--cfg` argument: `key=value` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")
2+

0 commit comments

Comments
 (0)