Skip to content

Commit dab1dfe

Browse files
Adds expr_2024 migration lit
This is adding a migration lint for the current (in the 2021 edition and previous) to move expr to expr_2021 from expr Co-Developed-by: Eric Holk Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent f6e4703 commit dab1dfe

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ lint_lintpass_by_hand = implementing `LintPass` by hand
443443
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
444444
.note = the macro is defined here
445445
446+
lint_macro_expr_fragment_specifier_2024_migration =
447+
the `expr` fragment specifier will accept more expressions in the 2024 edition.
448+
.suggestion = to keep the existing behavior, use the `expr_2021` fragment specifier.
446449
lint_macro_is_private = macro `{$ident}` is private
447450
448451
lint_macro_rule_never_used = rule #{$n} of macro `{$name}` is never used

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ mod late;
5858
mod let_underscore;
5959
mod levels;
6060
mod lints;
61+
mod macro_expr_fragment_specifier_2024_migration;
6162
mod map_unit_fn;
6263
mod methods;
6364
mod multiple_supertrait_upcastable;
@@ -95,6 +96,7 @@ use impl_trait_overcaptures::ImplTraitOvercaptures;
9596
use internal::*;
9697
use invalid_from_utf8::*;
9798
use let_underscore::*;
99+
use macro_expr_fragment_specifier_2024_migration::*;
98100
use map_unit_fn::*;
99101
use methods::*;
100102
use multiple_supertrait_upcastable::*;
@@ -168,6 +170,7 @@ early_lint_methods!(
168170
IncompleteInternalFeatures: IncompleteInternalFeatures,
169171
RedundantSemicolons: RedundantSemicolons,
170172
UnusedDocComment: UnusedDocComment,
173+
Expr2024: Expr2024,
171174
]
172175
]
173176
);

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,126 @@
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,no_run
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+
/// FIXME: change this: You can manually change the identifiers from `expr` to `expr2021`,
52+
/// or use a [raw identifier], for example `r#gen`, to transition to a new edition.
53+
///
54+
/// This lint solves the problem automatically. It is "allow" by default
55+
/// because the code is perfectly valid in older editions. The [`cargo
56+
/// fix`] tool with the `--edition` flag will switch this lint to "warn"
57+
/// and automatically apply the suggested fix from the compiler (which is
58+
/// to use a raw identifier). This provides a completely automated way to
59+
/// update old code for a new edition.
60+
///
61+
/// [editions]: https://doc.rust-lang.org/edition-guide/
62+
/// [raw identifier]: https://doc.rust-lang.org/reference/identifiers.html
63+
/// [`cargo fix`]: https://doc.rust-lang.org/cargo/commands/cargo-fix.html
64+
pub EDITION_2024_EXPR_FRAGMENT_SPECIFIER,
65+
Allow,
66+
"The `expr` fragment specifier will accept more expressions in the 2024 edition. \
67+
To keep the existing behavior, use the `expr_2021` fragment specifier.",
68+
@future_incompatible = FutureIncompatibleInfo {
69+
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
70+
reference: "issue #123742 <https://github.com/rust-lang/rust/issues/123742>",
71+
};
72+
}
73+
74+
declare_lint_pass!(Expr2024 => [EDITION_2024_EXPR_FRAGMENT_SPECIFIER,]);
75+
76+
impl Expr2024 {
77+
fn check_tokens(&mut self, cx: &crate::EarlyContext<'_>, tokens: &TokenStream) {
78+
let mut prev_dollar = false;
79+
for tt in tokens.trees() {
80+
match tt {
81+
TokenTree::Token(token, _) => {
82+
if token.kind == TokenKind::Dollar {
83+
prev_dollar = true;
84+
continue;
85+
} else {
86+
if !prev_dollar {
87+
self.check_ident_token(cx, token);
88+
}
89+
}
90+
}
91+
TokenTree::Delimited(.., tts) => self.check_tokens(cx, tts),
92+
}
93+
prev_dollar = false;
94+
}
95+
}
96+
97+
fn check_ident_token(&mut self, cx: &crate::EarlyContext<'_>, token: &Token) {
98+
debug!("check_ident_token: {:?}", token);
99+
let (sym, edition) = match token.kind {
100+
TokenKind::Ident(sym, _) => (sym, Edition::Edition2024),
101+
_ => return,
102+
};
103+
104+
debug!("token.span.edition(): {:?}", token.span.edition());
105+
if token.span.edition() >= edition {
106+
return;
107+
}
108+
109+
if sym != sym::expr {
110+
return;
111+
}
112+
113+
debug!("emitting lint");
114+
cx.builder.emit_span_lint(
115+
&EDITION_2024_EXPR_FRAGMENT_SPECIFIER,
116+
token.span.into(),
117+
MacroExprFragment2024 { suggestion: token.span },
118+
);
119+
}
120+
}
121+
122+
impl EarlyLintPass for Expr2024 {
123+
fn check_mac_def(&mut self, cx: &crate::EarlyContext<'_>, mc: &rustc_ast::MacroDef) {
124+
self.check_tokens(cx, &mc.body.tokens);
125+
}
126+
}

0 commit comments

Comments
 (0)