about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2024-09-18 11:01:43 +0200
committerUrgau <urgau@numericable.fr>2024-09-18 11:07:32 +0200
commit89f04c2521e8ca27457f84c5be7fb81ec1a5a223 (patch)
tree5f723f64b11b148220591cea23c232405206ab73
parent46b0f8bafcf11fa1d6b6c172f4f5c1b95c11ebdb (diff)
downloadrust-89f04c2521e8ca27457f84c5be7fb81ec1a5a223.tar.gz
rust-89f04c2521e8ca27457f84c5be7fb81ec1a5a223.zip
Improve handling of raw-idents in check-cfg
-rw-r--r--compiler/rustc_lint/src/context/diagnostics/check_cfg.rs14
-rw-r--r--compiler/rustc_lint/src/lints.rs3
-rw-r--r--tests/ui/check-cfg/raw-keywords.edition2015.stderr40
-rw-r--r--tests/ui/check-cfg/raw-keywords.edition2021.stderr40
-rw-r--r--tests/ui/check-cfg/raw-keywords.rs40
5 files changed, 133 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs
index ddaa819df14..38c4e48928f 100644
--- a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs
+++ b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs
@@ -2,6 +2,7 @@ use rustc_middle::bug;
 use rustc_session::config::ExpectedValues;
 use rustc_session::Session;
 use rustc_span::edit_distance::find_best_match_for_name;
+use rustc_span::symbol::Ident;
 use rustc_span::{sym, Span, Symbol};
 
 use crate::lints;
@@ -30,7 +31,7 @@ enum EscapeQuotes {
     No,
 }
 
-fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
+fn to_check_cfg_arg(name: Ident, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
     if let Some(value) = value {
         let value = str::escape_debug(value.as_str()).to_string();
         let values = match quotes {
@@ -110,6 +111,7 @@ pub(super) fn unexpected_cfg_name(
                 }
             };
 
+            let best_match = Ident::new(best_match, name_span);
             if let Some((value, value_span)) = value {
                 if best_match_values.contains(&Some(value)) {
                     lints::unexpected_cfg_name::CodeSuggestion::SimilarNameAndValue {
@@ -163,6 +165,8 @@ pub(super) fn unexpected_cfg_name(
         };
         let expected_names = if !possibilities.is_empty() {
             let (possibilities, and_more) = sort_and_truncate_possibilities(sess, possibilities);
+            let possibilities: Vec<_> =
+                possibilities.into_iter().map(|s| Ident::new(s, name_span)).collect();
             Some(lints::unexpected_cfg_name::ExpectedNames {
                 possibilities: possibilities.into(),
                 and_more,
@@ -176,7 +180,9 @@ pub(super) fn unexpected_cfg_name(
         }
     };
 
-    let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
+    let inst = |escape_quotes| {
+        to_check_cfg_arg(Ident::new(name, name_span), value.map(|(v, _s)| v), escape_quotes)
+    };
 
     let invocation_help = if is_from_cargo {
         let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None };
@@ -273,7 +279,9 @@ pub(super) fn unexpected_cfg_value(
         || (matches!(sess.psess.unstable_features, rustc_feature::UnstableFeatures::Cheat)
             && !sess.opts.unstable_opts.ui_testing);
 
-    let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
+    let inst = |escape_quotes| {
+        to_check_cfg_arg(Ident::new(name, name_span), value.map(|(v, _s)| v), escape_quotes)
+    };
 
     let invocation_help = if is_from_cargo {
         let help = if name == sym::feature {
diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs
index a2ccb93347a..7fabfc9a784 100644
--- a/compiler/rustc_lint/src/lints.rs
+++ b/compiler/rustc_lint/src/lints.rs
@@ -2180,6 +2180,7 @@ pub(crate) struct UnexpectedCfgName {
 pub(crate) mod unexpected_cfg_name {
     use rustc_errors::DiagSymbolList;
     use rustc_macros::Subdiagnostic;
+    use rustc_span::symbol::Ident;
     use rustc_span::{Span, Symbol};
 
     #[derive(Subdiagnostic)]
@@ -2260,7 +2261,7 @@ pub(crate) mod unexpected_cfg_name {
     #[derive(Subdiagnostic)]
     #[help_once(lint_unexpected_cfg_name_expected_names)]
     pub(crate) struct ExpectedNames {
-        pub possibilities: DiagSymbolList,
+        pub possibilities: DiagSymbolList<Ident>,
         pub and_more: usize,
     }
 
diff --git a/tests/ui/check-cfg/raw-keywords.edition2015.stderr b/tests/ui/check-cfg/raw-keywords.edition2015.stderr
new file mode 100644
index 00000000000..ab7e77686ee
--- /dev/null
+++ b/tests/ui/check-cfg/raw-keywords.edition2015.stderr
@@ -0,0 +1,40 @@
+warning: unexpected `cfg` condition name: `tru`
+  --> $DIR/raw-keywords.rs:14:7
+   |
+LL | #[cfg(tru)]
+   |       ^^^ help: there is a config with a similar name: `r#true`
+   |
+   = help: to expect this configuration use `--check-cfg=cfg(tru)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+   = note: `#[warn(unexpected_cfgs)]` on by default
+
+warning: unexpected `cfg` condition name: `r#false`
+  --> $DIR/raw-keywords.rs:19:7
+   |
+LL | #[cfg(r#false)]
+   |       ^^^^^^^
+   |
+   = help: expected names are: `async`, `clippy`, `debug_assertions`, `doc`, `doctest`, `edition2015`, `edition2021`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `r#true`, `ub_checks`, `unix`, and `windows`
+   = help: to expect this configuration use `--check-cfg=cfg(r#false)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+
+warning: unexpected `cfg` condition name: `await`
+  --> $DIR/raw-keywords.rs:27:29
+   |
+LL | #[cfg_attr(edition2015, cfg(await))]
+   |                             ^^^^^
+   |
+   = help: to expect this configuration use `--check-cfg=cfg(await)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+
+warning: unexpected `cfg` condition name: `raw`
+  --> $DIR/raw-keywords.rs:33:7
+   |
+LL | #[cfg(r#raw)]
+   |       ^^^^^
+   |
+   = help: to expect this configuration use `--check-cfg=cfg(raw)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+
+warning: 4 warnings emitted
+
diff --git a/tests/ui/check-cfg/raw-keywords.edition2021.stderr b/tests/ui/check-cfg/raw-keywords.edition2021.stderr
new file mode 100644
index 00000000000..1ae1cad4e6b
--- /dev/null
+++ b/tests/ui/check-cfg/raw-keywords.edition2021.stderr
@@ -0,0 +1,40 @@
+warning: unexpected `cfg` condition name: `tru`
+  --> $DIR/raw-keywords.rs:14:7
+   |
+LL | #[cfg(tru)]
+   |       ^^^ help: there is a config with a similar name: `r#true`
+   |
+   = help: to expect this configuration use `--check-cfg=cfg(tru)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+   = note: `#[warn(unexpected_cfgs)]` on by default
+
+warning: unexpected `cfg` condition name: `r#false`
+  --> $DIR/raw-keywords.rs:19:7
+   |
+LL | #[cfg(r#false)]
+   |       ^^^^^^^
+   |
+   = help: expected names are: `r#async`, `clippy`, `debug_assertions`, `doc`, `doctest`, `edition2015`, `edition2021`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `r#true`, `ub_checks`, `unix`, and `windows`
+   = help: to expect this configuration use `--check-cfg=cfg(r#false)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+
+warning: unexpected `cfg` condition name: `r#await`
+  --> $DIR/raw-keywords.rs:28:29
+   |
+LL | #[cfg_attr(edition2021, cfg(r#await))]
+   |                             ^^^^^^^
+   |
+   = help: to expect this configuration use `--check-cfg=cfg(r#await)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+
+warning: unexpected `cfg` condition name: `raw`
+  --> $DIR/raw-keywords.rs:33:7
+   |
+LL | #[cfg(r#raw)]
+   |       ^^^^^
+   |
+   = help: to expect this configuration use `--check-cfg=cfg(raw)`
+   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
+
+warning: 4 warnings emitted
+
diff --git a/tests/ui/check-cfg/raw-keywords.rs b/tests/ui/check-cfg/raw-keywords.rs
new file mode 100644
index 00000000000..5de13240d7e
--- /dev/null
+++ b/tests/ui/check-cfg/raw-keywords.rs
@@ -0,0 +1,40 @@
+// This test check that using raw keywords works with --cfg and --check-cfg
+// and that the diagnostics suggestions are coherent
+//
+//@ check-pass
+//@ no-auto-check-cfg
+//@ compile-flags: --cfg=true --cfg=async --check-cfg=cfg(r#true,r#async,edition2015,edition2021)
+//
+//@ revisions: edition2015 edition2021
+//@ [edition2021] compile-flags: --edition 2021
+
+#[cfg(r#true)]
+fn foo() {}
+
+#[cfg(tru)]
+//~^ WARNING unexpected `cfg` condition name: `tru`
+//~^^ SUGGESTION r#true
+fn foo() {}
+
+#[cfg(r#false)]
+//~^ WARNING unexpected `cfg` condition name: `r#false`
+fn foo() {}
+
+#[cfg_attr(edition2015, cfg(async))]
+#[cfg_attr(edition2021, cfg(r#async))]
+fn bar() {}
+
+#[cfg_attr(edition2015, cfg(await))]
+#[cfg_attr(edition2021, cfg(r#await))]
+//[edition2015]~^^ WARNING unexpected `cfg` condition name: `await`
+//[edition2021]~^^ WARNING unexpected `cfg` condition name: `r#await`
+fn zoo() {}
+
+#[cfg(r#raw)]
+//~^ WARNING unexpected `cfg` condition name: `raw`
+fn foo() {}
+
+fn main() {
+    foo();
+    bar();
+}