From 90330b5330954d0e60b44c934e14f6323a759e1d Mon Sep 17 00:00:00 2001 From: Ivor Wanders Date: Wed, 12 Jan 2022 19:04:16 -0500 Subject: [PATCH 1/5] Improve error message for cfg arguments. This provides the user with a helpful error message in case a key="value" message was specified but couldn't be parsed. --- compiler/rustc_interface/src/interface.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 3804e100307..8fa1e18df3c 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -124,7 +124,10 @@ macro_rules! error { Err(errs) => errs.into_iter().for_each(|mut err| err.cancel()), } - error!(r#"expected `key` or `key="value"`"#); + error!(concat!( + r#"expected `key` or `key="value"`, ensure escaping is appropriate"#, + r#" for your shell, try 'key="value"' or key=\"value\""# + )); }) .collect::(); cfg.into_iter().map(|(a, b)| (a.to_string(), b.map(|b| b.to_string()))).collect() From db56c6d53adef72067f83f3d34b2881afe6b5d74 Mon Sep 17 00:00:00 2001 From: Ivor Wanders Date: Wed, 12 Jan 2022 19:50:04 -0500 Subject: [PATCH 2/5] Update unit tests to accomodate for improved error. --- src/test/ui/conditional-compilation/cfg-arg-invalid-1.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-1.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr | 2 +- src/test/ui/conditional-compilation/cfg-empty-codemap.rs | 2 +- src/test/ui/conditional-compilation/cfg-empty-codemap.stderr | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-1.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-1.rs index 36dd78dd2b1..d20e79b9db3 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-1.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-1.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg a(b=c) -// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`) +// 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\") fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-1.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-1.stderr index 1e7922a9ff1..3a12e978680 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-1.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-1.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`) +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\") diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs index 48d656a4a28..660d5d1b1ea 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg a{b} -// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`) +// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr index b92e1fd3d97..2e141137129 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`) +error: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs index e7dfa17b4b6..d8b3c0284cc 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg a(b) -// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`) +// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr index 6853a69b9eb..52685d7b4a2 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`) +error: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs index 9fa726f93e3..4a0d116e644 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg a{ -// error-pattern: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`) +// error-pattern: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr index 7d2087b4b71..618d12aba68 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`) +error: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs index 1d7fa788534..7c5531606fa 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg ) -// error-pattern: invalid `--cfg` argument: `)` (expected `key` or `key="value"`) +// error-pattern: invalid `--cfg` argument: `)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr index 7bb1814127b..16bb552f0d9 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `)` (expected `key` or `key="value"`) +error: invalid `--cfg` argument: `)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") diff --git a/src/test/ui/conditional-compilation/cfg-empty-codemap.rs b/src/test/ui/conditional-compilation/cfg-empty-codemap.rs index 9e34cacf74d..c23d2cee924 100644 --- a/src/test/ui/conditional-compilation/cfg-empty-codemap.rs +++ b/src/test/ui/conditional-compilation/cfg-empty-codemap.rs @@ -2,7 +2,7 @@ // compile-flags: --cfg "" -// error-pattern: invalid `--cfg` argument: `""` (expected `key` or `key="value"`) +// error-pattern: invalid `--cfg` argument: `""` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") pub fn main() { } diff --git a/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr b/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr index 128e3cd7306..55e3dc131ae 100644 --- a/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr +++ b/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `""` (expected `key` or `key="value"`) +error: invalid `--cfg` argument: `""` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") From 32be3489a52271520e6bc6c6653ce7457292c48b Mon Sep 17 00:00:00 2001 From: Ivor Wanders Date: Sun, 16 Jan 2022 11:04:11 -0500 Subject: [PATCH 3/5] Add unit test for cfg-arg without quotes. --- src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs | 3 +++ src/test/ui/conditional-compilation/cfg-arg-invalid-9.stderr | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs create mode 100644 src/test/ui/conditional-compilation/cfg-arg-invalid-9.stderr diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs new file mode 100644 index 00000000000..51d8c4b885c --- /dev/null +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs @@ -0,0 +1,3 @@ +// compile-flags: --cfg key=value +// 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\") +fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-9.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-9.stderr new file mode 100644 index 00000000000..985b5252258 --- /dev/null +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-9.stderr @@ -0,0 +1,2 @@ +error: invalid `--cfg` argument: `key=value` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") + From 1531d26fa3896869818b40d6771d8b3cdaf2f6eb Mon Sep 17 00:00:00 2001 From: Ivor Wanders Date: Sun, 16 Jan 2022 11:43:16 -0500 Subject: [PATCH 4/5] Limit scope of the hint about escaping. --- compiler/rustc_interface/src/interface.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 8fa1e18df3c..26343561959 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -124,10 +124,16 @@ macro_rules! error { Err(errs) => errs.into_iter().for_each(|mut err| err.cancel()), } - error!(concat!( - r#"expected `key` or `key="value"`, ensure escaping is appropriate"#, - r#" for your shell, try 'key="value"' or key=\"value\""# - )); + // If the user tried to use a key="value" flag, but is missing the quotes, provide + // a hint about how to resolve this. + if s.contains("=") && !s.contains("=\"") && !s.ends_with("\"") { + error!(concat!( + r#"expected `key` or `key="value"`, ensure escaping is appropriate"#, + r#" for your shell, try 'key="value"' or key=\"value\""# + )); + } else { + error!(r#"expected `key` or `key="value"`"#); + } }) .collect::(); cfg.into_iter().map(|(a, b)| (a.to_string(), b.map(|b| b.to_string()))).collect() From af87248037a8c86f580c469ce7d2a10c0b12f2fd Mon Sep 17 00:00:00 2001 From: Ivor Wanders Date: Sun, 16 Jan 2022 11:44:10 -0500 Subject: [PATCH 5/5] Update unit tests to match more limited hint about escaping. --- src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr | 2 +- src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs | 1 + src/test/ui/conditional-compilation/cfg-empty-codemap.rs | 2 +- src/test/ui/conditional-compilation/cfg-empty-codemap.stderr | 2 +- 11 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs index 660d5d1b1ea..48d656a4a28 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg a{b} -// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`) fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr index 2e141137129..b92e1fd3d97 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-2.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +error: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`) diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs index d8b3c0284cc..e7dfa17b4b6 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg a(b) -// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`) fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr index 52685d7b4a2..6853a69b9eb 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-4.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +error: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`) diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs index 4a0d116e644..9fa726f93e3 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg a{ -// error-pattern: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +// error-pattern: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`) fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr index 618d12aba68..7d2087b4b71 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +error: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`) diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs index 7c5531606fa..1d7fa788534 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.rs @@ -1,3 +1,3 @@ // compile-flags: --cfg ) -// error-pattern: invalid `--cfg` argument: `)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +// error-pattern: invalid `--cfg` argument: `)` (expected `key` or `key="value"`) fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr index 16bb552f0d9..7bb1814127b 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-8.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +error: invalid `--cfg` argument: `)` (expected `key` or `key="value"`) diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs index 51d8c4b885c..628b335c873 100644 --- a/src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs +++ b/src/test/ui/conditional-compilation/cfg-arg-invalid-9.rs @@ -1,3 +1,4 @@ +// Test for missing quotes around value, issue #66450. // compile-flags: --cfg key=value // 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\") fn main() {} diff --git a/src/test/ui/conditional-compilation/cfg-empty-codemap.rs b/src/test/ui/conditional-compilation/cfg-empty-codemap.rs index c23d2cee924..9e34cacf74d 100644 --- a/src/test/ui/conditional-compilation/cfg-empty-codemap.rs +++ b/src/test/ui/conditional-compilation/cfg-empty-codemap.rs @@ -2,7 +2,7 @@ // compile-flags: --cfg "" -// error-pattern: invalid `--cfg` argument: `""` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +// error-pattern: invalid `--cfg` argument: `""` (expected `key` or `key="value"`) pub fn main() { } diff --git a/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr b/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr index 55e3dc131ae..128e3cd7306 100644 --- a/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr +++ b/src/test/ui/conditional-compilation/cfg-empty-codemap.stderr @@ -1,2 +1,2 @@ -error: invalid `--cfg` argument: `""` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\") +error: invalid `--cfg` argument: `""` (expected `key` or `key="value"`)