diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ba316e5eeb041..b9be92b89afdf 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -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, *}; @@ -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; } } diff --git a/tests/ui/rust-2018/async-ident.fixed b/tests/ui/rust-2018/async-ident.fixed index 4e31f674435b8..639a8a245fb41 100644 --- a/tests/ui/rust-2018/async-ident.fixed +++ b/tests/ui/rust-2018/async-ident.fixed @@ -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() {} diff --git a/tests/ui/rust-2018/async-ident.rs b/tests/ui/rust-2018/async-ident.rs index 4c5134a292321..7921f05f48126 100644 --- a/tests/ui/rust-2018/async-ident.rs +++ b/tests/ui/rust-2018/async-ident.rs @@ -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() {} diff --git a/tests/ui/rust-2018/async-ident.stderr b/tests/ui/rust-2018/async-ident.stderr index 5b8d8184f4f3d..4ab061dd6f599 100644 --- a/tests/ui/rust-2018/async-ident.stderr +++ b/tests/ui/rust-2018/async-ident.stderr @@ -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 - error: `async` is a keyword in the 2018 edition --> $DIR/async-ident.rs:12:19 | @@ -32,7 +23,7 @@ LL | ($async:expr, async) => {}; = note: for more information, see issue #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` @@ -41,7 +32,7 @@ LL | foo!(async); = note: for more information, see issue #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` @@ -50,7 +41,7 @@ LL | trait async {} = note: for more information, see issue #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` @@ -59,7 +50,7 @@ LL | impl async for MyStruct {} = note: for more information, see issue #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` @@ -68,7 +59,7 @@ LL | static async: u32 = 0; = note: for more information, see issue #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` @@ -77,7 +68,7 @@ LL | const async: u32 = 0; = note: for more information, see issue #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` @@ -86,7 +77,7 @@ LL | impl Foo { fn async() {} } = note: for more information, see issue #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` @@ -95,7 +86,7 @@ LL | struct async {} = note: for more information, see issue #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` @@ -104,7 +95,7 @@ LL | let async: async = async {}; = note: for more information, see issue #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` @@ -113,7 +104,7 @@ LL | let async: async = async {}; = note: for more information, see issue #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` @@ -122,7 +113,7 @@ LL | let async: async = async {}; = note: for more information, see issue #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` @@ -131,7 +122,7 @@ LL | () => (pub fn async() {}) = note: for more information, see issue #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` @@ -139,5 +130,5 @@ LL | (async) => (1) = 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 -error: aborting due to 15 previous errors +error: aborting due to 14 previous errors diff --git a/tests/ui/rust-2024/gen-kw-in-macro.rs b/tests/ui/rust-2024/gen-kw-in-macro.rs new file mode 100644 index 0000000000000..3ccbe05b226bc --- /dev/null +++ b/tests/ui/rust-2024/gen-kw-in-macro.rs @@ -0,0 +1,13 @@ +//@ check-pass + +#![deny(keyword_idents_2024)] + +macro_rules! foo { + ($gen:expr) => { + $gen + }; +} + +fn main() { + foo!(println!("hello, world")); +} diff --git a/tests/ui/rust-2024/gen-kw.e2015.stderr b/tests/ui/rust-2024/gen-kw.e2015.stderr index b12363184b713..b1074f77e0089 100644 --- a/tests/ui/rust-2024/gen-kw.e2015.stderr +++ b/tests/ui/rust-2024/gen-kw.e2015.stderr @@ -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 -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 + +error: aborting due to 3 previous errors diff --git a/tests/ui/rust-2024/gen-kw.e2018.stderr b/tests/ui/rust-2024/gen-kw.e2018.stderr index e10fc4c45120c..b902cff7fdbc3 100644 --- a/tests/ui/rust-2024/gen-kw.e2018.stderr +++ b/tests/ui/rust-2024/gen-kw.e2018.stderr @@ -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 -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 + +error: aborting due to 3 previous errors diff --git a/tests/ui/rust-2024/gen-kw.rs b/tests/ui/rust-2024/gen-kw.rs index 3d2a3f9516558..04251cbcac47b 100644 --- a/tests/ui/rust-2024/gen-kw.rs +++ b/tests/ui/rust-2024/gen-kw.rs @@ -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!();