Skip to content

Commit ef6478b

Browse files
Add expr_2021 nonterminal and feature flag
This commit adds a new nonterminal `expr_2021` in macro patterns, and `expr_fragment_specifier_2024` feature flag. For now, `expr` and `expr_2021` are treated the same, but in future PRs we will update `expr` to match to new grammar. Co-authored-by: Vincezo Palazzo <vincenzopalazzodev@gmail.com>
1 parent 030a12c commit ef6478b

File tree

10 files changed

+48
-3
lines changed

10 files changed

+48
-3
lines changed

Diff for: compiler/rustc_ast/src/token.rs

+4
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,8 @@ pub enum NonterminalKind {
878878
},
879879
PatWithOr,
880880
Expr,
881+
/// Matches an expression using the rules from edition 2021 and earlier.
882+
Expr2021,
881883
Ty,
882884
Ident,
883885
Lifetime,
@@ -907,6 +909,7 @@ impl NonterminalKind {
907909
},
908910
sym::pat_param => NonterminalKind::PatParam { inferred: false },
909911
sym::expr => NonterminalKind::Expr,
912+
sym::expr_2021 if edition() >= Edition::Edition2024 => NonterminalKind::Expr2021,
910913
sym::ty => NonterminalKind::Ty,
911914
sym::ident => NonterminalKind::Ident,
912915
sym::lifetime => NonterminalKind::Lifetime,
@@ -926,6 +929,7 @@ impl NonterminalKind {
926929
NonterminalKind::PatParam { inferred: false } => sym::pat_param,
927930
NonterminalKind::PatParam { inferred: true } | NonterminalKind::PatWithOr => sym::pat,
928931
NonterminalKind::Expr => sym::expr,
932+
NonterminalKind::Expr2021 => sym::expr_2021,
929933
NonterminalKind::Ty => sym::ty,
930934
NonterminalKind::Ident => sym::ident,
931935
NonterminalKind::Lifetime => sym::lifetime,

Diff for: compiler/rustc_expand/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ expand_explain_doc_comment_inner =
3939
expand_explain_doc_comment_outer =
4040
outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match
4141
42+
expand_expr_2021_is_experimental =
43+
expr_2021 is experimental
44+
4245
expand_expr_repeat_no_syntax_vars =
4346
attempted to repeat an expression containing no syntax variables matched as repeating at this depth
4447

Diff for: compiler/rustc_expand/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,10 @@ pub struct ExpectedParenOrBrace<'a> {
433433
pub span: Span,
434434
pub token: Cow<'a, str>,
435435
}
436+
437+
#[derive(Diagnostic)]
438+
#[diag(expand_expr_2021_is_experimental)]
439+
pub struct Expr2021IsExperimental {
440+
#[primary_span]
441+
pub span: Span,
442+
}

Diff for: compiler/rustc_expand/src/mbe/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
12901290
// maintain
12911291
IsInFollow::Yes
12921292
}
1293-
NonterminalKind::Stmt | NonterminalKind::Expr => {
1293+
NonterminalKind::Stmt | NonterminalKind::Expr | NonterminalKind::Expr2021 => {
12941294
const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
12951295
match tok {
12961296
TokenTree::Token(token) => match token.kind {

Diff for: compiler/rustc_expand/src/mbe/quoted.rs

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ pub(super) fn parse(
9292
token::NonterminalKind::Ident
9393
},
9494
);
95+
if kind == token::NonterminalKind::Expr2021
96+
&& !features.expr_fragment_specifier_2024
97+
{
98+
sess.dcx()
99+
.emit_err(errors::Expr2021IsExperimental { span });
100+
}
95101
result.push(TokenTree::MetaVarDecl(span, ident, Some(kind)));
96102
continue;
97103
}

Diff for: compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ declare_features! (
458458
(unstable, exhaustive_patterns, "1.13.0", Some(51085)),
459459
/// Allows explicit tail calls via `become` expression.
460460
(incomplete, explicit_tail_calls, "1.72.0", Some(112788)),
461+
/// Uses 2024 rules for matching `expr` fragments in macros. Also enables `expr_2021` fragment.
462+
(incomplete, expr_fragment_specifier_2024, "CURRENT_RUSTC_VERSION", Some(123742)),
461463
/// Allows using `efiapi`, `sysv64` and `win64` as calling convention
462464
/// for functions with varargs.
463465
(unstable, extended_varargs_abi_support, "1.65.0", Some(100189)),

Diff for: compiler/rustc_parse/src/parser/nonterminal.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a> Parser<'a> {
3737
}
3838

3939
match kind {
40-
NonterminalKind::Expr => {
40+
NonterminalKind::Expr | NonterminalKind::Expr2021 => {
4141
token.can_begin_expr()
4242
// This exception is here for backwards compatibility.
4343
&& !token.is_keyword(kw::Let)
@@ -145,7 +145,9 @@ impl<'a> Parser<'a> {
145145
})?)
146146
}
147147

148-
NonterminalKind::Expr => NtExpr(self.parse_expr_force_collect()?),
148+
NonterminalKind::Expr | NonterminalKind::Expr2021 => {
149+
NtExpr(self.parse_expr_force_collect()?)
150+
}
149151
NonterminalKind::Literal => {
150152
// The `:literal` matcher does not support attributes
151153
NtLiteral(self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?)

Diff for: compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,8 @@ symbols! {
782782
explicit_tail_calls,
783783
export_name,
784784
expr,
785+
expr_2021,
786+
expr_fragment_specifier_2024,
785787
extended_key_value_attributes,
786788
extended_varargs_abi_support,
787789
extern_absolute_paths,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: --edition=2024 -Z unstable-options
2+
3+
macro_rules! m {
4+
($e:expr_2021) => { //~ ERROR: expr_2021 is experimental
5+
$e
6+
};
7+
}
8+
9+
fn main() {
10+
m!(());
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expr_2021 is experimental
2+
--> $DIR/feature-gate-expr_fragment_specifier_2024.rs:4:6
3+
|
4+
LL | ($e:expr_2021) => {
5+
| ^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)