Skip to content

Commit 00c4be3

Browse files
authored
Rollup merge of rust-lang#130507 - Urgau:check-cfg-raw-keywords, r=jieyouxu
Improve handling of raw-idents in check-cfg This PR improves the handling of raw-idents in the check-cfg diagnostics. In particular the list of expected names and the suggestion now correctly take into account the "keyword-ness" of the ident, and correctly prefix the ident with `r#` when necessary. `@rustbot` labels +F-check-cfg
2 parents 3443795 + 89f04c2 commit 00c4be3

File tree

5 files changed

+133
-4
lines changed

5 files changed

+133
-4
lines changed

Diff for: compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_middle::bug;
22
use rustc_session::config::ExpectedValues;
33
use rustc_session::Session;
44
use rustc_span::edit_distance::find_best_match_for_name;
5+
use rustc_span::symbol::Ident;
56
use rustc_span::{sym, Span, Symbol};
67

78
use crate::lints;
@@ -30,7 +31,7 @@ enum EscapeQuotes {
3031
No,
3132
}
3233

33-
fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
34+
fn to_check_cfg_arg(name: Ident, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
3435
if let Some(value) = value {
3536
let value = str::escape_debug(value.as_str()).to_string();
3637
let values = match quotes {
@@ -110,6 +111,7 @@ pub(super) fn unexpected_cfg_name(
110111
}
111112
};
112113

114+
let best_match = Ident::new(best_match, name_span);
113115
if let Some((value, value_span)) = value {
114116
if best_match_values.contains(&Some(value)) {
115117
lints::unexpected_cfg_name::CodeSuggestion::SimilarNameAndValue {
@@ -163,6 +165,8 @@ pub(super) fn unexpected_cfg_name(
163165
};
164166
let expected_names = if !possibilities.is_empty() {
165167
let (possibilities, and_more) = sort_and_truncate_possibilities(sess, possibilities);
168+
let possibilities: Vec<_> =
169+
possibilities.into_iter().map(|s| Ident::new(s, name_span)).collect();
166170
Some(lints::unexpected_cfg_name::ExpectedNames {
167171
possibilities: possibilities.into(),
168172
and_more,
@@ -176,7 +180,9 @@ pub(super) fn unexpected_cfg_name(
176180
}
177181
};
178182

179-
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
183+
let inst = |escape_quotes| {
184+
to_check_cfg_arg(Ident::new(name, name_span), value.map(|(v, _s)| v), escape_quotes)
185+
};
180186

181187
let invocation_help = if is_from_cargo {
182188
let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None };
@@ -273,7 +279,9 @@ pub(super) fn unexpected_cfg_value(
273279
|| (matches!(sess.psess.unstable_features, rustc_feature::UnstableFeatures::Cheat)
274280
&& !sess.opts.unstable_opts.ui_testing);
275281

276-
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
282+
let inst = |escape_quotes| {
283+
to_check_cfg_arg(Ident::new(name, name_span), value.map(|(v, _s)| v), escape_quotes)
284+
};
277285

278286
let invocation_help = if is_from_cargo {
279287
let help = if name == sym::feature {

Diff for: compiler/rustc_lint/src/lints.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,7 @@ pub(crate) struct UnexpectedCfgName {
21802180
pub(crate) mod unexpected_cfg_name {
21812181
use rustc_errors::DiagSymbolList;
21822182
use rustc_macros::Subdiagnostic;
2183+
use rustc_span::symbol::Ident;
21832184
use rustc_span::{Span, Symbol};
21842185

21852186
#[derive(Subdiagnostic)]
@@ -2260,7 +2261,7 @@ pub(crate) mod unexpected_cfg_name {
22602261
#[derive(Subdiagnostic)]
22612262
#[help_once(lint_unexpected_cfg_name_expected_names)]
22622263
pub(crate) struct ExpectedNames {
2263-
pub possibilities: DiagSymbolList,
2264+
pub possibilities: DiagSymbolList<Ident>,
22642265
pub and_more: usize,
22652266
}
22662267

Diff for: tests/ui/check-cfg/raw-keywords.edition2015.stderr

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
warning: unexpected `cfg` condition name: `tru`
2+
--> $DIR/raw-keywords.rs:14:7
3+
|
4+
LL | #[cfg(tru)]
5+
| ^^^ help: there is a config with a similar name: `r#true`
6+
|
7+
= help: to expect this configuration use `--check-cfg=cfg(tru)`
8+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
9+
= note: `#[warn(unexpected_cfgs)]` on by default
10+
11+
warning: unexpected `cfg` condition name: `r#false`
12+
--> $DIR/raw-keywords.rs:19:7
13+
|
14+
LL | #[cfg(r#false)]
15+
| ^^^^^^^
16+
|
17+
= 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`
18+
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
19+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
20+
21+
warning: unexpected `cfg` condition name: `await`
22+
--> $DIR/raw-keywords.rs:27:29
23+
|
24+
LL | #[cfg_attr(edition2015, cfg(await))]
25+
| ^^^^^
26+
|
27+
= help: to expect this configuration use `--check-cfg=cfg(await)`
28+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
29+
30+
warning: unexpected `cfg` condition name: `raw`
31+
--> $DIR/raw-keywords.rs:33:7
32+
|
33+
LL | #[cfg(r#raw)]
34+
| ^^^^^
35+
|
36+
= help: to expect this configuration use `--check-cfg=cfg(raw)`
37+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
38+
39+
warning: 4 warnings emitted
40+

Diff for: tests/ui/check-cfg/raw-keywords.edition2021.stderr

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
warning: unexpected `cfg` condition name: `tru`
2+
--> $DIR/raw-keywords.rs:14:7
3+
|
4+
LL | #[cfg(tru)]
5+
| ^^^ help: there is a config with a similar name: `r#true`
6+
|
7+
= help: to expect this configuration use `--check-cfg=cfg(tru)`
8+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
9+
= note: `#[warn(unexpected_cfgs)]` on by default
10+
11+
warning: unexpected `cfg` condition name: `r#false`
12+
--> $DIR/raw-keywords.rs:19:7
13+
|
14+
LL | #[cfg(r#false)]
15+
| ^^^^^^^
16+
|
17+
= 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`
18+
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
19+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
20+
21+
warning: unexpected `cfg` condition name: `r#await`
22+
--> $DIR/raw-keywords.rs:28:29
23+
|
24+
LL | #[cfg_attr(edition2021, cfg(r#await))]
25+
| ^^^^^^^
26+
|
27+
= help: to expect this configuration use `--check-cfg=cfg(r#await)`
28+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
29+
30+
warning: unexpected `cfg` condition name: `raw`
31+
--> $DIR/raw-keywords.rs:33:7
32+
|
33+
LL | #[cfg(r#raw)]
34+
| ^^^^^
35+
|
36+
= help: to expect this configuration use `--check-cfg=cfg(raw)`
37+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
38+
39+
warning: 4 warnings emitted
40+

Diff for: tests/ui/check-cfg/raw-keywords.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This test check that using raw keywords works with --cfg and --check-cfg
2+
// and that the diagnostics suggestions are coherent
3+
//
4+
//@ check-pass
5+
//@ no-auto-check-cfg
6+
//@ compile-flags: --cfg=true --cfg=async --check-cfg=cfg(r#true,r#async,edition2015,edition2021)
7+
//
8+
//@ revisions: edition2015 edition2021
9+
//@ [edition2021] compile-flags: --edition 2021
10+
11+
#[cfg(r#true)]
12+
fn foo() {}
13+
14+
#[cfg(tru)]
15+
//~^ WARNING unexpected `cfg` condition name: `tru`
16+
//~^^ SUGGESTION r#true
17+
fn foo() {}
18+
19+
#[cfg(r#false)]
20+
//~^ WARNING unexpected `cfg` condition name: `r#false`
21+
fn foo() {}
22+
23+
#[cfg_attr(edition2015, cfg(async))]
24+
#[cfg_attr(edition2021, cfg(r#async))]
25+
fn bar() {}
26+
27+
#[cfg_attr(edition2015, cfg(await))]
28+
#[cfg_attr(edition2021, cfg(r#await))]
29+
//[edition2015]~^^ WARNING unexpected `cfg` condition name: `await`
30+
//[edition2021]~^^ WARNING unexpected `cfg` condition name: `r#await`
31+
fn zoo() {}
32+
33+
#[cfg(r#raw)]
34+
//~^ WARNING unexpected `cfg` condition name: `raw`
35+
fn foo() {}
36+
37+
fn main() {
38+
foo();
39+
bar();
40+
}

0 commit comments

Comments
 (0)