Skip to content

Commit cdf9a41

Browse files
authored
Rollup merge of rust-lang#125627 - vincenzopalazzo:macros/cargo-fix-expr2024, r=eholk
migration lint for `expr2024` for the edition 2024 This is adding a migration lint for the current (in the 2021 edition and previous) to move expr to expr_2021 from expr Issue rust-lang#123742 I created also a repository to test out the migration https://github.com/vincenzopalazzo/expr2024-cargo-fix-migration Co-Developed-by: `@eholk`
2 parents cb8a7ea + 6366acc commit cdf9a41

7 files changed

+207
-0
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ lint_lintpass_by_hand = implementing `LintPass` by hand
441441
lint_macro_expanded_macro_exports_accessed_by_absolute_paths = macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
442442
.note = the macro is defined here
443443
444+
lint_macro_expr_fragment_specifier_2024_migration =
445+
the `expr` fragment specifier will accept more expressions in the 2024 edition.
446+
.suggestion = to keep the existing behavior, use the `expr_2021` fragment specifier.
444447
lint_macro_is_private = macro `{$ident}` is private
445448
446449
lint_macro_rule_never_used = rule #{$n} of macro `{$name}` is never used

Diff for: compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ mod late;
6060
mod let_underscore;
6161
mod levels;
6262
mod lints;
63+
mod macro_expr_fragment_specifier_2024_migration;
6364
mod map_unit_fn;
6465
mod methods;
6566
mod multiple_supertrait_upcastable;
@@ -97,6 +98,7 @@ use impl_trait_overcaptures::ImplTraitOvercaptures;
9798
use internal::*;
9899
use invalid_from_utf8::*;
99100
use let_underscore::*;
101+
use macro_expr_fragment_specifier_2024_migration::*;
100102
use map_unit_fn::*;
101103
use methods::*;
102104
use multiple_supertrait_upcastable::*;
@@ -170,6 +172,7 @@ early_lint_methods!(
170172
IncompleteInternalFeatures: IncompleteInternalFeatures,
171173
RedundantSemicolons: RedundantSemicolons,
172174
UnusedDocComment: UnusedDocComment,
175+
Expr2024: Expr2024,
173176
]
174177
]
175178
);

Diff for: compiler/rustc_lint/src/lints.rs

+7
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ pub struct BuiltinTypeAliasGenericBounds<'a, 'b> {
323323
pub sub: Option<SuggestChangingAssocTypes<'a, 'b>>,
324324
}
325325

326+
#[derive(LintDiagnostic)]
327+
#[diag(lint_macro_expr_fragment_specifier_2024_migration)]
328+
pub struct MacroExprFragment2024 {
329+
#[suggestion(code = "expr_2021", applicability = "machine-applicable")]
330+
pub suggestion: Span,
331+
}
332+
326333
pub struct BuiltinTypeAliasGenericBoundsSuggestion {
327334
pub suggestions: Vec<(Span, String)>,
328335
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//! Migration code for the `expr_fragment_specifier_2024`
2+
//! rule.
3+
use tracing::debug;
4+
5+
use rustc_ast::token::Token;
6+
use rustc_ast::token::TokenKind;
7+
use rustc_ast::tokenstream::TokenStream;
8+
use rustc_ast::tokenstream::TokenTree;
9+
use rustc_session::declare_lint;
10+
use rustc_session::declare_lint_pass;
11+
use rustc_session::lint::FutureIncompatibilityReason;
12+
use rustc_span::edition::Edition;
13+
use rustc_span::sym;
14+
15+
use crate::lints::MacroExprFragment2024;
16+
use crate::EarlyLintPass;
17+
18+
declare_lint! {
19+
/// The `edition_2024_expr_fragment_specifier` lint detects the use of `expr` fragments
20+
/// during migration to the 2024 edition.
21+
///
22+
/// The `expr` fragment specifier will accept more expressions in the 2024 edition.
23+
/// To maintain the current behavior, use the `expr_2021` fragment specifier.
24+
///
25+
/// ### Example
26+
///
27+
/// ```rust,edition2021,compile_fail
28+
/// #![deny(edition_2024_expr_fragment_specifier)]
29+
/// macro_rules! m {
30+
/// ($e:expr) => {
31+
/// $e
32+
/// }
33+
/// }
34+
///
35+
/// fn main() {
36+
/// m!(1);
37+
/// }
38+
/// ```
39+
///
40+
/// {{produces}}
41+
///
42+
/// ### Explanation
43+
///
44+
/// Rust [editions] allow the language to evolve without breaking
45+
/// backwards compatibility. This lint catches code that uses new keywords
46+
/// that are added to the language that are used as identifiers (such as a
47+
/// variable name, function name, etc.). If you switch the compiler to a
48+
/// new edition without updating the code, then it will fail to compile if
49+
/// you are using a new keyword as an identifier.
50+
///
51+
/// This lint solves the problem automatically. It is "allow" by default
52+
/// because the code is perfectly valid in older editions. The [`cargo
53+
/// fix`] tool with the `--edition` flag will switch this lint to "warn"
54+
/// and automatically apply the suggested fix from the compiler (which is
55+
/// to use a raw identifier). This provides a completely automated way to
56+
/// update old code for a new edition.
57+
///
58+
/// [editions]: https://doc.rust-lang.org/edition-guide/
59+
/// [raw identifier]: https://doc.rust-lang.org/reference/identifiers.html
60+
/// [`cargo fix`]: https://doc.rust-lang.org/cargo/commands/cargo-fix.html
61+
pub EDITION_2024_EXPR_FRAGMENT_SPECIFIER,
62+
Allow,
63+
"The `expr` fragment specifier will accept more expressions in the 2024 edition. \
64+
To keep the existing behavior, use the `expr_2021` fragment specifier.",
65+
@future_incompatible = FutureIncompatibleInfo {
66+
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
67+
reference: "Migration Guide <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/macro-fragment-specifiers.html>",
68+
};
69+
}
70+
71+
declare_lint_pass!(Expr2024 => [EDITION_2024_EXPR_FRAGMENT_SPECIFIER,]);
72+
73+
impl Expr2024 {
74+
fn check_tokens(&mut self, cx: &crate::EarlyContext<'_>, tokens: &TokenStream) {
75+
let mut prev_dollar = false;
76+
for tt in tokens.trees() {
77+
match tt {
78+
TokenTree::Token(token, _) => {
79+
if token.kind == TokenKind::Dollar {
80+
prev_dollar = true;
81+
continue;
82+
} else {
83+
if !prev_dollar {
84+
self.check_ident_token(cx, token);
85+
}
86+
}
87+
}
88+
TokenTree::Delimited(.., tts) => self.check_tokens(cx, tts),
89+
}
90+
prev_dollar = false;
91+
}
92+
}
93+
94+
fn check_ident_token(&mut self, cx: &crate::EarlyContext<'_>, token: &Token) {
95+
debug!("check_ident_token: {:?}", token);
96+
let (sym, edition) = match token.kind {
97+
TokenKind::Ident(sym, _) => (sym, Edition::Edition2024),
98+
_ => return,
99+
};
100+
101+
debug!("token.span.edition(): {:?}", token.span.edition());
102+
if token.span.edition() >= edition {
103+
return;
104+
}
105+
106+
if sym != sym::expr {
107+
return;
108+
}
109+
110+
debug!("emitting lint");
111+
cx.builder.emit_span_lint(
112+
&EDITION_2024_EXPR_FRAGMENT_SPECIFIER,
113+
token.span.into(),
114+
MacroExprFragment2024 { suggestion: token.span },
115+
);
116+
}
117+
}
118+
119+
impl EarlyLintPass for Expr2024 {
120+
fn check_mac_def(&mut self, cx: &crate::EarlyContext<'_>, mc: &rustc_ast::MacroDef) {
121+
self.check_tokens(cx, &mc.body.tokens);
122+
}
123+
}

Diff for: tests/ui/macros/expr_2021_cargo_fix_edition.fixed

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ run-rustfix
2+
//@ check-pass
3+
//@ compile-flags: --edition=2021
4+
#![allow(incomplete_features)]
5+
#![feature(expr_fragment_specifier_2024)]
6+
#![warn(edition_2024_expr_fragment_specifier)]
7+
8+
macro_rules! m {
9+
($e:expr_2021) => { //~ WARN: the `expr` fragment specifier will accept more expressions in the 2024 edition.
10+
//~^ WARN: this changes meaning in Rust 2024
11+
$e
12+
};
13+
($($i:expr_2021)*) => { }; //~ WARN: the `expr` fragment specifier will accept more expressions in the 2024 edition.
14+
//~^ WARN: this changes meaning in Rust 2024
15+
}
16+
17+
fn main() {
18+
m!(());
19+
}

Diff for: tests/ui/macros/expr_2021_cargo_fix_edition.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ run-rustfix
2+
//@ check-pass
3+
//@ compile-flags: --edition=2021
4+
#![allow(incomplete_features)]
5+
#![feature(expr_fragment_specifier_2024)]
6+
#![warn(edition_2024_expr_fragment_specifier)]
7+
8+
macro_rules! m {
9+
($e:expr) => { //~ WARN: the `expr` fragment specifier will accept more expressions in the 2024 edition.
10+
//~^ WARN: this changes meaning in Rust 2024
11+
$e
12+
};
13+
($($i:expr)*) => { }; //~ WARN: the `expr` fragment specifier will accept more expressions in the 2024 edition.
14+
//~^ WARN: this changes meaning in Rust 2024
15+
}
16+
17+
fn main() {
18+
m!(());
19+
}

Diff for: tests/ui/macros/expr_2021_cargo_fix_edition.stderr

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
warning: the `expr` fragment specifier will accept more expressions in the 2024 edition.
2+
--> $DIR/expr_2021_cargo_fix_edition.rs:9:9
3+
|
4+
LL | ($e:expr) => {
5+
| ^^^^
6+
|
7+
= warning: this changes meaning in Rust 2024
8+
= note: for more information, see Migration Guide <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/macro-fragment-specifiers.html>
9+
note: the lint level is defined here
10+
--> $DIR/expr_2021_cargo_fix_edition.rs:6:9
11+
|
12+
LL | #![warn(edition_2024_expr_fragment_specifier)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
help: to keep the existing behavior, use the `expr_2021` fragment specifier.
15+
|
16+
LL | ($e:expr_2021) => {
17+
| ~~~~~~~~~
18+
19+
warning: the `expr` fragment specifier will accept more expressions in the 2024 edition.
20+
--> $DIR/expr_2021_cargo_fix_edition.rs:13:11
21+
|
22+
LL | ($($i:expr)*) => { };
23+
| ^^^^
24+
|
25+
= warning: this changes meaning in Rust 2024
26+
= note: for more information, see Migration Guide <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/macro-fragment-specifiers.html>
27+
help: to keep the existing behavior, use the `expr_2021` fragment specifier.
28+
|
29+
LL | ($($i:expr_2021)*) => { };
30+
| ~~~~~~~~~
31+
32+
warning: 2 warnings emitted
33+

0 commit comments

Comments
 (0)