Skip to content

Commit 2d4b74e

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 <[email protected]>
1 parent 5974fe8 commit 2d4b74e

File tree

10 files changed

+49
-3
lines changed

10 files changed

+49
-3
lines changed

compiler/rustc_ast/src/token.rs

+5
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,8 @@ pub enum NonterminalKind {
876876
},
877877
PatWithOr,
878878
Expr,
879+
/// Matches an expression using the rules from edition 2021 and earlier.
880+
Expr2021,
879881
Ty,
880882
Ident,
881883
Lifetime,
@@ -905,6 +907,8 @@ impl NonterminalKind {
905907
},
906908
sym::pat_param => NonterminalKind::PatParam { inferred: false },
907909
sym::expr => NonterminalKind::Expr,
910+
// FIXME: gate this on the feature flag
911+
sym::expr_2021 if edition() >= Edition::Edition2024 => NonterminalKind::Expr2021,
908912
sym::ty => NonterminalKind::Ty,
909913
sym::ident => NonterminalKind::Ident,
910914
sym::lifetime => NonterminalKind::Lifetime,
@@ -924,6 +928,7 @@ impl NonterminalKind {
924928
NonterminalKind::PatParam { inferred: false } => sym::pat_param,
925929
NonterminalKind::PatParam { inferred: true } | NonterminalKind::PatWithOr => sym::pat,
926930
NonterminalKind::Expr => sym::expr,
931+
NonterminalKind::Expr2021 => sym::expr_2021,
927932
NonterminalKind::Ty => sym::ty,
928933
NonterminalKind::Ident => sym::ident,
929934
NonterminalKind::Lifetime => sym::lifetime,

compiler/rustc_expand/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ expand_explain_doc_comment_inner =
4242
expand_explain_doc_comment_outer =
4343
outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match
4444
45+
expand_expr_2021_is_experimental =
46+
expr_2021 is experimental
47+
4548
expand_expr_repeat_no_syntax_vars =
4649
attempted to repeat an expression containing no syntax variables matched as repeating at this depth
4750

compiler/rustc_expand/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,10 @@ pub struct ExpectedParenOrBrace<'a> {
456456
pub span: Span,
457457
pub token: Cow<'a, str>,
458458
}
459+
460+
#[derive(Diagnostic)]
461+
#[diag(expand_expr_2021_is_experimental)]
462+
pub struct Expr2021IsExperimental {
463+
#[primary_span]
464+
pub span: Span,
465+
}

compiler/rustc_expand/src/mbe/macro_rules.rs

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

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
}

compiler/rustc_feature/src/unstable.rs

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

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())?)

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ symbols! {
769769
explicit_tail_calls,
770770
export_name,
771771
expr,
772+
expr_2021,
773+
expr_fragment_specifier_2024,
772774
extended_key_value_attributes,
773775
extended_varargs_abi_support,
774776
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)