Make sure we don't deny macro vars w keyword names

This commit is contained in:
Michael Goulet 2024-05-07 19:13:33 -04:00
parent 14081a255d
commit 1d9d6715ae
8 changed files with 70 additions and 34 deletions

View File

@ -40,6 +40,7 @@
},
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 @@ fn check_pat_post(&mut self, _cx: &EarlyContext<'_>, pat: &ast::Pat) {
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;
}
}

View File

@ -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() {}

View File

@ -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() {}

View File

@ -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
|
@ -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`
@ -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`
@ -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`
@ -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`
@ -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`
@ -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`
@ -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`
@ -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`
@ -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`
@ -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`
@ -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`
@ -131,7 +122,7 @@ 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`
@ -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 <https://github.com/rust-lang/rust/issues/49716>
error: aborting due to 15 previous errors
error: aborting due to 14 previous errors

View File

@ -0,0 +1,13 @@
//@ check-pass
#![deny(keyword_idents_2024)]
macro_rules! foo {
($gen:expr) => {
$gen
};
}
fn main() {
foo!(println!("hello, world"));
}

View File

@ -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

View File

@ -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

View File

@ -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!();