From 343d322117ecafad253de7ead30e25aa24400d40 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 6 Dec 2024 16:29:29 -0800 Subject: [PATCH 1/2] Add test for 'gen in more positions --- tests/ui/rust-2024/gen-kw.e2015.stderr | 22 ++++++++++++++++++++-- tests/ui/rust-2024/gen-kw.e2018.stderr | 22 ++++++++++++++++++++-- tests/ui/rust-2024/gen-kw.rs | 13 ++++++++++++- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/tests/ui/rust-2024/gen-kw.e2015.stderr b/tests/ui/rust-2024/gen-kw.e2015.stderr index ff552f663c741..b71e20b040a71 100644 --- a/tests/ui/rust-2024/gen-kw.e2015.stderr +++ b/tests/ui/rust-2024/gen-kw.e2015.stderr @@ -34,11 +34,29 @@ LL | () => { mod test { fn gen() {} } } error: `gen` is a keyword in the 2024 edition --> $DIR/gen-kw.rs:25:9 | -LL | fn test<'gen>() {} +LL | fn test<'gen>(_: &'gen i32) {} | ^^^^ 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 4 previous errors +error: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:30:10 + | +LL | struct S<'gen>(&'gen i32); + | ^^^^ 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: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:36:6 + | +LL | impl<'gen> Tr for S<'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 6 previous errors diff --git a/tests/ui/rust-2024/gen-kw.e2018.stderr b/tests/ui/rust-2024/gen-kw.e2018.stderr index efa812069c386..0327893bc0f4a 100644 --- a/tests/ui/rust-2024/gen-kw.e2018.stderr +++ b/tests/ui/rust-2024/gen-kw.e2018.stderr @@ -34,11 +34,29 @@ LL | () => { mod test { fn gen() {} } } error: `gen` is a keyword in the 2024 edition --> $DIR/gen-kw.rs:25:9 | -LL | fn test<'gen>() {} +LL | fn test<'gen>(_: &'gen i32) {} | ^^^^ 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 4 previous errors +error: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:30:10 + | +LL | struct S<'gen>(&'gen i32); + | ^^^^ 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: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:36:6 + | +LL | impl<'gen> Tr for S<'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 6 previous errors diff --git a/tests/ui/rust-2024/gen-kw.rs b/tests/ui/rust-2024/gen-kw.rs index 5a658470c0a8e..60d64694ab785 100644 --- a/tests/ui/rust-2024/gen-kw.rs +++ b/tests/ui/rust-2024/gen-kw.rs @@ -22,7 +22,18 @@ macro_rules! t { //[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024! } -fn test<'gen>() {} +fn test<'gen>(_: &'gen i32) {} +//~^ 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! + +struct S<'gen>(&'gen i32); +//~^ 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! + +trait Tr {} +impl<'gen> Tr for S<'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! From 27f50f91fe1a63dc9e07fb44f21d4f345c157357 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 6 Dec 2024 16:45:43 -0800 Subject: [PATCH 2/2] Support checking lifetimes in early lint pass This is intended to fix keyword_idents_2024 which was not checking for `'gen` in most positions. --- compiler/rustc_lint/src/builtin.rs | 3 +++ compiler/rustc_lint/src/early.rs | 1 + compiler/rustc_lint/src/passes.rs | 1 + tests/ui/rust-2024/gen-kw.e2015.stderr | 33 +++++++++++++++++++++++--- tests/ui/rust-2024/gen-kw.e2018.stderr | 33 +++++++++++++++++++++++--- tests/ui/rust-2024/gen-kw.rs | 9 +++++++ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ec08519892203..a1417bffe3a1f 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1913,6 +1913,9 @@ impl EarlyLintPass for KeywordIdents { self.check_ident_token(cx, UnderMacro(false), *ident, ""); } } + fn check_lifetime(&mut self, cx: &EarlyContext<'_>, lt: &ast::Lifetime) { + self.check_ident_token(cx, UnderMacro(false), lt.ident.without_first_quote(), "'"); + } } declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMENTS]); diff --git a/compiler/rustc_lint/src/early.rs b/compiler/rustc_lint/src/early.rs index 4f3184f1d7cff..77dd32db14999 100644 --- a/compiler/rustc_lint/src/early.rs +++ b/compiler/rustc_lint/src/early.rs @@ -245,6 +245,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> fn visit_lifetime(&mut self, lt: &'a ast::Lifetime, _: ast_visit::LifetimeCtxt) { self.check_id(lt.id); + lint_callback!(self, check_lifetime, lt); } fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) { diff --git a/compiler/rustc_lint/src/passes.rs b/compiler/rustc_lint/src/passes.rs index 9d84d36e779e8..44ebeafffc539 100644 --- a/compiler/rustc_lint/src/passes.rs +++ b/compiler/rustc_lint/src/passes.rs @@ -137,6 +137,7 @@ macro_rules! early_lint_methods { $macro!($args, [ fn check_param(a: &rustc_ast::Param); fn check_ident(a: &rustc_span::symbol::Ident); + fn check_lifetime(a: &rustc_ast::Lifetime); fn check_crate(a: &rustc_ast::Crate); fn check_crate_post(a: &rustc_ast::Crate); fn check_item(a: &rustc_ast::Item); diff --git a/tests/ui/rust-2024/gen-kw.e2015.stderr b/tests/ui/rust-2024/gen-kw.e2015.stderr index b71e20b040a71..17efa2f38ffff 100644 --- a/tests/ui/rust-2024/gen-kw.e2015.stderr +++ b/tests/ui/rust-2024/gen-kw.e2015.stderr @@ -41,7 +41,16 @@ LL | fn test<'gen>(_: &'gen i32) {} = note: for more information, see issue #49716 error: `gen` is a keyword in the 2024 edition - --> $DIR/gen-kw.rs:30:10 + --> $DIR/gen-kw.rs:25:19 + | +LL | fn test<'gen>(_: &'gen i32) {} + | ^^^^ 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: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:33:10 | LL | struct S<'gen>(&'gen i32); | ^^^^ help: you can use a raw identifier to stay compatible: `'r#gen` @@ -50,7 +59,16 @@ LL | struct S<'gen>(&'gen i32); = note: for more information, see issue #49716 error: `gen` is a keyword in the 2024 edition - --> $DIR/gen-kw.rs:36:6 + --> $DIR/gen-kw.rs:33:17 + | +LL | struct S<'gen>(&'gen i32); + | ^^^^ 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: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:42:6 | LL | impl<'gen> Tr for S<'gen> {} | ^^^^ help: you can use a raw identifier to stay compatible: `'r#gen` @@ -58,5 +76,14 @@ LL | impl<'gen> Tr for S<'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 6 previous errors +error: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:42:21 + | +LL | impl<'gen> Tr for S<'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 9 previous errors diff --git a/tests/ui/rust-2024/gen-kw.e2018.stderr b/tests/ui/rust-2024/gen-kw.e2018.stderr index 0327893bc0f4a..f89d08cee0576 100644 --- a/tests/ui/rust-2024/gen-kw.e2018.stderr +++ b/tests/ui/rust-2024/gen-kw.e2018.stderr @@ -41,7 +41,16 @@ LL | fn test<'gen>(_: &'gen i32) {} = note: for more information, see issue #49716 error: `gen` is a keyword in the 2024 edition - --> $DIR/gen-kw.rs:30:10 + --> $DIR/gen-kw.rs:25:19 + | +LL | fn test<'gen>(_: &'gen i32) {} + | ^^^^ 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: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:33:10 | LL | struct S<'gen>(&'gen i32); | ^^^^ help: you can use a raw identifier to stay compatible: `'r#gen` @@ -50,7 +59,16 @@ LL | struct S<'gen>(&'gen i32); = note: for more information, see issue #49716 error: `gen` is a keyword in the 2024 edition - --> $DIR/gen-kw.rs:36:6 + --> $DIR/gen-kw.rs:33:17 + | +LL | struct S<'gen>(&'gen i32); + | ^^^^ 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: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:42:6 | LL | impl<'gen> Tr for S<'gen> {} | ^^^^ help: you can use a raw identifier to stay compatible: `'r#gen` @@ -58,5 +76,14 @@ LL | impl<'gen> Tr for S<'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 6 previous errors +error: `gen` is a keyword in the 2024 edition + --> $DIR/gen-kw.rs:42:21 + | +LL | impl<'gen> Tr for S<'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 9 previous errors diff --git a/tests/ui/rust-2024/gen-kw.rs b/tests/ui/rust-2024/gen-kw.rs index 60d64694ab785..fb68accc7d06e 100644 --- a/tests/ui/rust-2024/gen-kw.rs +++ b/tests/ui/rust-2024/gen-kw.rs @@ -24,18 +24,27 @@ macro_rules! t { fn test<'gen>(_: &'gen i32) {} //~^ ERROR `gen` is a keyword in the 2024 edition +//~| 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! +//[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! //[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024! struct S<'gen>(&'gen i32); //~^ ERROR `gen` is a keyword in the 2024 edition +//~| 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! //[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! +//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024! trait Tr {} impl<'gen> Tr for S<'gen> {} //~^ ERROR `gen` is a keyword in the 2024 edition +//~| 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! //[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! +//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024! t!();