Skip to content

Commit 1d9d671

Browse files
Make sure we don't deny macro vars w keyword names
1 parent 14081a2 commit 1d9d671

File tree

8 files changed

+70
-34
lines changed

8 files changed

+70
-34
lines changed

compiler/rustc_lint/src/builtin.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::{
4040
},
4141
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
4242
};
43+
use ast::token::TokenKind;
4344
use rustc_ast::tokenstream::{TokenStream, TokenTree};
4445
use rustc_ast::visit::{FnCtxt, FnKind};
4546
use rustc_ast::{self as ast, *};
@@ -1869,16 +1870,24 @@ struct UnderMacro(bool);
18691870

18701871
impl KeywordIdents {
18711872
fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: &TokenStream) {
1873+
// Check if the preceding token is `$`, because we want to allow `$async`, etc.
1874+
let mut prev_dollar = false;
18721875
for tt in tokens.trees() {
18731876
match tt {
18741877
// Only report non-raw idents.
18751878
TokenTree::Token(token, _) => {
18761879
if let Some((ident, token::IdentIsRaw::No)) = token.ident() {
1877-
self.check_ident_token(cx, UnderMacro(true), ident);
1880+
if !prev_dollar {
1881+
self.check_ident_token(cx, UnderMacro(true), ident);
1882+
}
1883+
} else if token.kind == TokenKind::Dollar {
1884+
prev_dollar = true;
1885+
continue;
18781886
}
18791887
}
18801888
TokenTree::Delimited(.., tts) => self.check_tokens(cx, tts),
18811889
}
1890+
prev_dollar = false;
18821891
}
18831892
}
18841893

tests/ui/rust-2018/async-ident.fixed

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ fn r#async() {} //~ ERROR async
99

1010
macro_rules! foo {
1111
($foo:ident) => {};
12-
($r#async:expr, r#async) => {};
12+
($async:expr, r#async) => {};
1313
//~^ ERROR async
14-
//~| ERROR async
15-
//~| WARN this is accepted in the current edition
1614
//~| WARN this is accepted in the current edition
1715
}
1816

1917
foo!(r#async);
20-
//~^ ERROR async
21-
//~| WARN this is accepted in the current edition
18+
//~^ ERROR async
19+
//~| WARN this is accepted in the current edition
2220

2321
mod dont_lint_raw {
2422
fn r#async() {}

tests/ui/rust-2018/async-ident.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ macro_rules! foo {
1111
($foo:ident) => {};
1212
($async:expr, async) => {};
1313
//~^ ERROR async
14-
//~| ERROR async
15-
//~| WARN this is accepted in the current edition
1614
//~| WARN this is accepted in the current edition
1715
}
1816

1917
foo!(async);
20-
//~^ ERROR async
21-
//~| WARN this is accepted in the current edition
18+
//~^ ERROR async
19+
//~| WARN this is accepted in the current edition
2220

2321
mod dont_lint_raw {
2422
fn r#async() {}

tests/ui/rust-2018/async-ident.stderr

+13-22
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ LL | #![deny(keyword_idents)]
1313
| ^^^^^^^^^^^^^^
1414
= note: `#[deny(keyword_idents_2018)]` implied by `#[deny(keyword_idents)]`
1515

16-
error: `async` is a keyword in the 2018 edition
17-
--> $DIR/async-ident.rs:12:7
18-
|
19-
LL | ($async:expr, async) => {};
20-
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
21-
|
22-
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
23-
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
24-
2516
error: `async` is a keyword in the 2018 edition
2617
--> $DIR/async-ident.rs:12:19
2718
|
@@ -32,7 +23,7 @@ LL | ($async:expr, async) => {};
3223
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
3324

3425
error: `async` is a keyword in the 2018 edition
35-
--> $DIR/async-ident.rs:19:6
26+
--> $DIR/async-ident.rs:17:6
3627
|
3728
LL | foo!(async);
3829
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -41,7 +32,7 @@ LL | foo!(async);
4132
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
4233

4334
error: `async` is a keyword in the 2018 edition
44-
--> $DIR/async-ident.rs:28:11
35+
--> $DIR/async-ident.rs:26:11
4536
|
4637
LL | trait async {}
4738
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -50,7 +41,7 @@ LL | trait async {}
5041
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
5142

5243
error: `async` is a keyword in the 2018 edition
53-
--> $DIR/async-ident.rs:32:10
44+
--> $DIR/async-ident.rs:30:10
5445
|
5546
LL | impl async for MyStruct {}
5647
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -59,7 +50,7 @@ LL | impl async for MyStruct {}
5950
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
6051

6152
error: `async` is a keyword in the 2018 edition
62-
--> $DIR/async-ident.rs:38:12
53+
--> $DIR/async-ident.rs:36:12
6354
|
6455
LL | static async: u32 = 0;
6556
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -68,7 +59,7 @@ LL | static async: u32 = 0;
6859
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
6960

7061
error: `async` is a keyword in the 2018 edition
71-
--> $DIR/async-ident.rs:44:11
62+
--> $DIR/async-ident.rs:42:11
7263
|
7364
LL | const async: u32 = 0;
7465
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -77,7 +68,7 @@ LL | const async: u32 = 0;
7768
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
7869

7970
error: `async` is a keyword in the 2018 edition
80-
--> $DIR/async-ident.rs:50:15
71+
--> $DIR/async-ident.rs:48:15
8172
|
8273
LL | impl Foo { fn async() {} }
8374
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -86,7 +77,7 @@ LL | impl Foo { fn async() {} }
8677
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
8778

8879
error: `async` is a keyword in the 2018 edition
89-
--> $DIR/async-ident.rs:55:12
80+
--> $DIR/async-ident.rs:53:12
9081
|
9182
LL | struct async {}
9283
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -95,7 +86,7 @@ LL | struct async {}
9586
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
9687

9788
error: `async` is a keyword in the 2018 edition
98-
--> $DIR/async-ident.rs:58:9
89+
--> $DIR/async-ident.rs:56:9
9990
|
10091
LL | let async: async = async {};
10192
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -104,7 +95,7 @@ LL | let async: async = async {};
10495
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
10596

10697
error: `async` is a keyword in the 2018 edition
107-
--> $DIR/async-ident.rs:58:16
98+
--> $DIR/async-ident.rs:56:16
10899
|
109100
LL | let async: async = async {};
110101
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -113,7 +104,7 @@ LL | let async: async = async {};
113104
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
114105

115106
error: `async` is a keyword in the 2018 edition
116-
--> $DIR/async-ident.rs:58:24
107+
--> $DIR/async-ident.rs:56:24
117108
|
118109
LL | let async: async = async {};
119110
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -122,7 +113,7 @@ LL | let async: async = async {};
122113
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
123114

124115
error: `async` is a keyword in the 2018 edition
125-
--> $DIR/async-ident.rs:69:19
116+
--> $DIR/async-ident.rs:67:19
126117
|
127118
LL | () => (pub fn async() {})
128119
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
@@ -131,13 +122,13 @@ LL | () => (pub fn async() {})
131122
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
132123

133124
error: `async` is a keyword in the 2018 edition
134-
--> $DIR/async-ident.rs:76:6
125+
--> $DIR/async-ident.rs:74:6
135126
|
136127
LL | (async) => (1)
137128
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
138129
|
139130
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
140131
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
141132

142-
error: aborting due to 15 previous errors
133+
error: aborting due to 14 previous errors
143134

tests/ui/rust-2024/gen-kw-in-macro.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ check-pass
2+
3+
#![deny(keyword_idents_2024)]
4+
5+
macro_rules! foo {
6+
($gen:expr) => {
7+
$gen
8+
};
9+
}
10+
11+
fn main() {
12+
foo!(println!("hello, world"));
13+
}

tests/ui/rust-2024/gen-kw.e2015.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@ LL | let gen = r#gen;
2222
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
2323
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
2424

25-
error: aborting due to 2 previous errors
25+
error: `gen` is a keyword in the 2024 edition
26+
--> $DIR/gen-kw.rs:19:27
27+
|
28+
LL | () => { mod test { fn gen() {} } }
29+
| ^^^ help: you can use a raw identifier to stay compatible: `r#gen`
30+
|
31+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
32+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
33+
34+
error: aborting due to 3 previous errors
2635

tests/ui/rust-2024/gen-kw.e2018.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@ LL | let gen = r#gen;
2222
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
2323
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
2424

25-
error: aborting due to 2 previous errors
25+
error: `gen` is a keyword in the 2024 edition
26+
--> $DIR/gen-kw.rs:19:27
27+
|
28+
LL | () => { mod test { fn gen() {} } }
29+
| ^^^ help: you can use a raw identifier to stay compatible: `r#gen`
30+
|
31+
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
32+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
33+
34+
error: aborting due to 3 previous errors
2635

tests/ui/rust-2024/gen-kw.rs

+9
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ fn main() {
1414
//[e2015]~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
1515
//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
1616
}
17+
18+
macro_rules! t {
19+
() => { mod test { fn gen() {} } }
20+
//~^ ERROR `gen` is a keyword in the 2024 edition
21+
//[e2015]~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
22+
//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
23+
}
24+
25+
t!();

0 commit comments

Comments
 (0)