Skip to content

Commit 505d13f

Browse files
committed
Emit error when using path-segment keyword as cfg pred
1 parent ce6daf3 commit 505d13f

File tree

4 files changed

+142
-3
lines changed

4 files changed

+142
-3
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ pub(crate) fn parse_cfg_entry<S: Stage>(
6969
}
7070
},
7171
a @ (ArgParser::NoArgs | ArgParser::NameValue(_)) => {
72-
let Some(name) = meta.path().word_sym() else {
72+
let Some(name) = meta.path().word_sym().filter(|s| !s.is_path_segment_keyword())
73+
else {
7374
cx.emit_err(session_diagnostics::CfgPredicateIdentifier {
7475
span: meta.path().span(),
7576
});
@@ -148,7 +149,7 @@ fn parse_cfg_entry_target<S: Stage>(
148149
};
149150

150151
// Then, parse it as a name-value item
151-
let Some(name) = sub_item.path().word_sym() else {
152+
let Some(name) = sub_item.path().word_sym().filter(|s| !s.is_path_segment_keyword()) else {
152153
cx.emit_err(session_diagnostics::CfgPredicateIdentifier {
153154
span: sub_item.path().span(),
154155
});

compiler/rustc_attr_parsing/src/attributes/cfg_old.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ pub fn eval_condition(
220220
}
221221
}
222222
}
223-
MetaItemKind::Word | MetaItemKind::NameValue(..) if cfg.path.segments.len() != 1 => {
223+
MetaItemKind::Word | MetaItemKind::NameValue(..)
224+
if cfg.path.segments.len() != 1
225+
|| cfg.path.segments[0].ident.is_path_segment_keyword() =>
226+
{
224227
dcx.emit_err(session_diagnostics::CfgPredicateIdentifier { span: cfg.path.span });
225228
true
226229
}

tests/ui/cfg/path-kw-as-pred.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
macro_rules! foo {
2+
() => {
3+
#[cfg($crate)] //~ ERROR `cfg` predicate key must be an identifier
4+
#[cfg_attr($crate, path = "foo")] //~ ERROR `cfg` predicate key must be an identifier
5+
mod _x {}
6+
7+
cfg!($crate); //~ ERROR `cfg` predicate key must be an identifier
8+
};
9+
}
10+
11+
#[cfg(crate)] //~ ERROR `cfg` predicate key must be an identifier
12+
#[cfg(super)] //~ ERROR `cfg` predicate key must be an identifier
13+
#[cfg(self)] //~ ERROR `cfg` predicate key must be an identifier
14+
#[cfg(Self)] //~ ERROR `cfg` predicate key must be an identifier
15+
#[cfg_attr(crate, path = "foo")] //~ ERROR `cfg` predicate key must be an identifier
16+
#[cfg_attr(super, path = "foo")] //~ ERROR `cfg` predicate key must be an identifier
17+
#[cfg_attr(self, path = "foo")] //~ ERROR `cfg` predicate key must be an identifier
18+
#[cfg_attr(Self, path = "foo")] //~ ERROR `cfg` predicate key must be an identifier
19+
mod _y {}
20+
21+
fn main() {
22+
foo!();
23+
24+
cfg!(crate); //~ ERROR `cfg` predicate key must be an identifier
25+
cfg!(super); //~ ERROR `cfg` predicate key must be an identifier
26+
cfg!(self); //~ ERROR `cfg` predicate key must be an identifier
27+
cfg!(Self); //~ ERROR `cfg` predicate key must be an identifier
28+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
error: `cfg` predicate key must be an identifier
2+
--> $DIR/path-kw-as-pred.rs:11:7
3+
|
4+
LL | #[cfg(crate)]
5+
| ^^^^^
6+
7+
error: `cfg` predicate key must be an identifier
8+
--> $DIR/path-kw-as-pred.rs:12:7
9+
|
10+
LL | #[cfg(super)]
11+
| ^^^^^
12+
13+
error: `cfg` predicate key must be an identifier
14+
--> $DIR/path-kw-as-pred.rs:13:7
15+
|
16+
LL | #[cfg(self)]
17+
| ^^^^
18+
19+
error: `cfg` predicate key must be an identifier
20+
--> $DIR/path-kw-as-pred.rs:14:7
21+
|
22+
LL | #[cfg(Self)]
23+
| ^^^^
24+
25+
error: `cfg` predicate key must be an identifier
26+
--> $DIR/path-kw-as-pred.rs:15:12
27+
|
28+
LL | #[cfg_attr(crate, path = "foo")]
29+
| ^^^^^
30+
31+
error: `cfg` predicate key must be an identifier
32+
--> $DIR/path-kw-as-pred.rs:16:12
33+
|
34+
LL | #[cfg_attr(super, path = "foo")]
35+
| ^^^^^
36+
37+
error: `cfg` predicate key must be an identifier
38+
--> $DIR/path-kw-as-pred.rs:17:12
39+
|
40+
LL | #[cfg_attr(self, path = "foo")]
41+
| ^^^^
42+
43+
error: `cfg` predicate key must be an identifier
44+
--> $DIR/path-kw-as-pred.rs:18:12
45+
|
46+
LL | #[cfg_attr(Self, path = "foo")]
47+
| ^^^^
48+
49+
error: `cfg` predicate key must be an identifier
50+
--> $DIR/path-kw-as-pred.rs:3:15
51+
|
52+
LL | #[cfg($crate)]
53+
| ^^^^^^
54+
...
55+
LL | foo!();
56+
| ------ in this macro invocation
57+
|
58+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
59+
60+
error: `cfg` predicate key must be an identifier
61+
--> $DIR/path-kw-as-pred.rs:4:20
62+
|
63+
LL | #[cfg_attr($crate, path = "foo")]
64+
| ^^^^^^
65+
...
66+
LL | foo!();
67+
| ------ in this macro invocation
68+
|
69+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
70+
71+
error: `cfg` predicate key must be an identifier
72+
--> $DIR/path-kw-as-pred.rs:7:14
73+
|
74+
LL | cfg!($crate);
75+
| ^^^^^^
76+
...
77+
LL | foo!();
78+
| ------ in this macro invocation
79+
|
80+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
81+
82+
error: `cfg` predicate key must be an identifier
83+
--> $DIR/path-kw-as-pred.rs:24:10
84+
|
85+
LL | cfg!(crate);
86+
| ^^^^^
87+
88+
error: `cfg` predicate key must be an identifier
89+
--> $DIR/path-kw-as-pred.rs:25:10
90+
|
91+
LL | cfg!(super);
92+
| ^^^^^
93+
94+
error: `cfg` predicate key must be an identifier
95+
--> $DIR/path-kw-as-pred.rs:26:10
96+
|
97+
LL | cfg!(self);
98+
| ^^^^
99+
100+
error: `cfg` predicate key must be an identifier
101+
--> $DIR/path-kw-as-pred.rs:27:10
102+
|
103+
LL | cfg!(Self);
104+
| ^^^^
105+
106+
error: aborting due to 15 previous errors
107+

0 commit comments

Comments
 (0)