Skip to content

Commit

Permalink
Rollup merge of rust-lang#124869 - compiler-errors:keyword, r=Nilstrieb
Browse files Browse the repository at this point in the history
Make sure we don't deny macro vars w keyword names

`$async:ident`, etc are all valid.

Fixes rust-lang#124862
  • Loading branch information
matthiaskrgr authored May 8, 2024
2 parents 6b92d9a + 1d9d671 commit cae0a91
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 34 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::{
},
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
};
use ast::token::TokenKind;
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::visit::{FnCtxt, FnKind};
use rustc_ast::{self as ast, *};
Expand Down Expand Up @@ -1869,16 +1870,24 @@ struct UnderMacro(bool);

impl KeywordIdents {
fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: &TokenStream) {
// Check if the preceding token is `$`, because we want to allow `$async`, etc.
let mut prev_dollar = false;
for tt in tokens.trees() {
match tt {
// Only report non-raw idents.
TokenTree::Token(token, _) => {
if let Some((ident, token::IdentIsRaw::No)) = token.ident() {
self.check_ident_token(cx, UnderMacro(true), ident);
if !prev_dollar {
self.check_ident_token(cx, UnderMacro(true), ident);
}
} else if token.kind == TokenKind::Dollar {
prev_dollar = true;
continue;
}
}
TokenTree::Delimited(.., tts) => self.check_tokens(cx, tts),
}
prev_dollar = false;
}
}

Expand Down
8 changes: 3 additions & 5 deletions tests/ui/rust-2018/async-ident.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ fn r#async() {} //~ ERROR async

macro_rules! foo {
($foo:ident) => {};
($r#async:expr, r#async) => {};
($async:expr, r#async) => {};
//~^ ERROR async
//~| ERROR async
//~| WARN this is accepted in the current edition
//~| WARN this is accepted in the current edition
}

foo!(r#async);
//~^ ERROR async
//~| WARN this is accepted in the current edition
//~^ ERROR async
//~| WARN this is accepted in the current edition

mod dont_lint_raw {
fn r#async() {}
Expand Down
6 changes: 2 additions & 4 deletions tests/ui/rust-2018/async-ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ macro_rules! foo {
($foo:ident) => {};
($async:expr, async) => {};
//~^ ERROR async
//~| ERROR async
//~| WARN this is accepted in the current edition
//~| WARN this is accepted in the current edition
}

foo!(async);
//~^ ERROR async
//~| WARN this is accepted in the current edition
//~^ ERROR async
//~| WARN this is accepted in the current edition

mod dont_lint_raw {
fn r#async() {}
Expand Down
35 changes: 13 additions & 22 deletions tests/ui/rust-2018/async-ident.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ LL | #![deny(keyword_idents)]
| ^^^^^^^^^^^^^^
= note: `#[deny(keyword_idents_2018)]` implied by `#[deny(keyword_idents)]`

error: `async` is a keyword in the 2018 edition
--> $DIR/async-ident.rs:12:7
|
LL | ($async:expr, async) => {};
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#async`
|
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>

error: `async` is a keyword in the 2018 edition
--> $DIR/async-ident.rs:12:19
|
Expand All @@ -32,7 +23,7 @@ LL | ($async:expr, async) => {};
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>

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

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

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

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

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

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

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

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

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

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

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

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

error: aborting due to 15 previous errors
error: aborting due to 14 previous errors

13 changes: 13 additions & 0 deletions tests/ui/rust-2024/gen-kw-in-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ check-pass

#![deny(keyword_idents_2024)]

macro_rules! foo {
($gen:expr) => {
$gen
};
}

fn main() {
foo!(println!("hello, world"));
}
11 changes: 10 additions & 1 deletion tests/ui/rust-2024/gen-kw.e2015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ LL | let gen = r#gen;
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>

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

error: aborting due to 3 previous errors

11 changes: 10 additions & 1 deletion tests/ui/rust-2024/gen-kw.e2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ LL | let gen = r#gen;
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>

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

error: aborting due to 3 previous errors

9 changes: 9 additions & 0 deletions tests/ui/rust-2024/gen-kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ fn main() {
//[e2015]~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
}

macro_rules! t {
() => { mod test { fn gen() {} } }
//~^ ERROR `gen` is a keyword in the 2024 edition
//[e2015]~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
}

t!();

0 comments on commit cae0a91

Please sign in to comment.