Skip to content

Commit 3e8898a

Browse files
Allow naming expr_2021 in all editions
1 parent 50d1efa commit 3e8898a

File tree

8 files changed

+23
-59
lines changed

8 files changed

+23
-59
lines changed

compiler/rustc_ast/src/token.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,11 @@ pub enum NonterminalKind {
884884
PatWithOr,
885885
Expr,
886886
/// Matches an expression using the rules from edition 2021 and earlier.
887-
Expr2021,
887+
Expr2021 {
888+
/// Keep track of whether the user used `:expr` or `:expr_2021` and we inferred it from the
889+
/// edition of the span. This is used for diagnostics AND feature gating.
890+
inferred: bool,
891+
},
888892
Ty,
889893
Ident,
890894
Lifetime,
@@ -913,8 +917,13 @@ impl NonterminalKind {
913917
Edition::Edition2021 | Edition::Edition2024 => NonterminalKind::PatWithOr,
914918
},
915919
sym::pat_param => NonterminalKind::PatParam { inferred: false },
916-
sym::expr => NonterminalKind::Expr,
917-
sym::expr_2021 if edition().at_least_rust_2021() => NonterminalKind::Expr2021,
920+
sym::expr => match edition() {
921+
Edition::Edition2015 | Edition::Edition2018 | Edition::Edition2021 => {
922+
NonterminalKind::Expr2021 { inferred: true }
923+
}
924+
Edition::Edition2024 => NonterminalKind::Expr,
925+
},
926+
sym::expr_2021 => NonterminalKind::Expr2021 { inferred: false },
918927
sym::ty => NonterminalKind::Ty,
919928
sym::ident => NonterminalKind::Ident,
920929
sym::lifetime => NonterminalKind::Lifetime,
@@ -933,8 +942,8 @@ impl NonterminalKind {
933942
NonterminalKind::Stmt => sym::stmt,
934943
NonterminalKind::PatParam { inferred: false } => sym::pat_param,
935944
NonterminalKind::PatParam { inferred: true } | NonterminalKind::PatWithOr => sym::pat,
936-
NonterminalKind::Expr => sym::expr,
937-
NonterminalKind::Expr2021 => sym::expr_2021,
945+
NonterminalKind::Expr | NonterminalKind::Expr2021 { inferred: true } => sym::expr,
946+
NonterminalKind::Expr2021 { inferred: false } => sym::expr_2021,
938947
NonterminalKind::Ty => sym::ty,
939948
NonterminalKind::Ident => sym::ident,
940949
NonterminalKind::Lifetime => sym::lifetime,

compiler/rustc_expand/src/mbe/macro_rules.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,9 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
12921292
// maintain
12931293
IsInFollow::Yes
12941294
}
1295-
NonterminalKind::Stmt | NonterminalKind::Expr | NonterminalKind::Expr2021 => {
1295+
NonterminalKind::Stmt
1296+
| NonterminalKind::Expr
1297+
| NonterminalKind::Expr2021 { inferred: _ } => {
12961298
const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
12971299
match tok {
12981300
TokenTree::Token(token) => match token.kind {

compiler/rustc_expand/src/mbe/quoted.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ pub(super) fn parse(
113113
);
114114
token::NonterminalKind::Ident
115115
});
116-
if kind == token::NonterminalKind::Expr2021
116+
if kind
117+
== (token::NonterminalKind::Expr2021 { inferred: false })
117118
&& !features.expr_fragment_specifier_2024
118119
{
119120
rustc_session::parse::feature_err(

compiler/rustc_parse/src/parser/nonterminal.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a> Parser<'a> {
3636
}
3737

3838
match kind {
39-
NonterminalKind::Expr2021 => {
39+
NonterminalKind::Expr2021 { inferred: _ } => {
4040
token.can_begin_expr()
4141
// This exception is here for backwards compatibility.
4242
&& !token.is_keyword(kw::Let)
@@ -47,7 +47,6 @@ impl<'a> Parser<'a> {
4747
token.can_begin_expr()
4848
// This exception is here for backwards compatibility.
4949
&& !token.is_keyword(kw::Let)
50-
&& (!token.is_keyword(kw::Const) || token.span.edition().at_least_rust_2024())
5150
}
5251
NonterminalKind::Ty => token.can_begin_type(),
5352
NonterminalKind::Ident => get_macro_ident(token).is_some(),
@@ -149,7 +148,7 @@ impl<'a> Parser<'a> {
149148
})?)
150149
}
151150

152-
NonterminalKind::Expr | NonterminalKind::Expr2021 => {
151+
NonterminalKind::Expr | NonterminalKind::Expr2021 { inferred: _ } => {
153152
NtExpr(self.parse_expr_force_collect()?)
154153
}
155154
NonterminalKind::Literal => {

tests/ui/macros/expr_2021_implicit_in_2024.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//@ compile-flags: --edition=2024 -Zunstable-options
22
//@ aux-build:expr_2021_implicit.rs
33

4+
//@ check-pass
5+
46
extern crate expr_2021_implicit;
57

68
// Makes sure that a `:expr` fragment matcher defined in a edition 2021 crate

tests/ui/macros/expr_2021_implicit_in_2024.stderr

-10
This file was deleted.

tests/ui/macros/expr_2021_old_edition.rs

-13
This file was deleted.

tests/ui/macros/expr_2021_old_edition.stderr

-26
This file was deleted.

0 commit comments

Comments
 (0)