Skip to content

Commit

Permalink
Allow naming expr_2021 in all editions
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 19, 2024
1 parent 50d1efa commit 3e8898a
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 59 deletions.
19 changes: 14 additions & 5 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,11 @@ pub enum NonterminalKind {
PatWithOr,
Expr,
/// Matches an expression using the rules from edition 2021 and earlier.
Expr2021,
Expr2021 {
/// Keep track of whether the user used `:expr` or `:expr_2021` and we inferred it from the
/// edition of the span. This is used for diagnostics AND feature gating.
inferred: bool,
},
Ty,
Ident,
Lifetime,
Expand Down Expand Up @@ -913,8 +917,13 @@ impl NonterminalKind {
Edition::Edition2021 | Edition::Edition2024 => NonterminalKind::PatWithOr,
},
sym::pat_param => NonterminalKind::PatParam { inferred: false },
sym::expr => NonterminalKind::Expr,
sym::expr_2021 if edition().at_least_rust_2021() => NonterminalKind::Expr2021,
sym::expr => match edition() {
Edition::Edition2015 | Edition::Edition2018 | Edition::Edition2021 => {
NonterminalKind::Expr2021 { inferred: true }
}
Edition::Edition2024 => NonterminalKind::Expr,
},
sym::expr_2021 => NonterminalKind::Expr2021 { inferred: false },
sym::ty => NonterminalKind::Ty,
sym::ident => NonterminalKind::Ident,
sym::lifetime => NonterminalKind::Lifetime,
Expand All @@ -933,8 +942,8 @@ impl NonterminalKind {
NonterminalKind::Stmt => sym::stmt,
NonterminalKind::PatParam { inferred: false } => sym::pat_param,
NonterminalKind::PatParam { inferred: true } | NonterminalKind::PatWithOr => sym::pat,
NonterminalKind::Expr => sym::expr,
NonterminalKind::Expr2021 => sym::expr_2021,
NonterminalKind::Expr | NonterminalKind::Expr2021 { inferred: true } => sym::expr,
NonterminalKind::Expr2021 { inferred: false } => sym::expr_2021,
NonterminalKind::Ty => sym::ty,
NonterminalKind::Ident => sym::ident,
NonterminalKind::Lifetime => sym::lifetime,
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,9 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
// maintain
IsInFollow::Yes
}
NonterminalKind::Stmt | NonterminalKind::Expr | NonterminalKind::Expr2021 => {
NonterminalKind::Stmt
| NonterminalKind::Expr
| NonterminalKind::Expr2021 { inferred: _ } => {
const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
match tok {
TokenTree::Token(token) => match token.kind {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ pub(super) fn parse(
);
token::NonterminalKind::Ident
});
if kind == token::NonterminalKind::Expr2021
if kind
== (token::NonterminalKind::Expr2021 { inferred: false })
&& !features.expr_fragment_specifier_2024
{
rustc_session::parse::feature_err(
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a> Parser<'a> {
}

match kind {
NonterminalKind::Expr2021 => {
NonterminalKind::Expr2021 { inferred: _ } => {
token.can_begin_expr()
// This exception is here for backwards compatibility.
&& !token.is_keyword(kw::Let)
Expand All @@ -47,7 +47,6 @@ impl<'a> Parser<'a> {
token.can_begin_expr()
// This exception is here for backwards compatibility.
&& !token.is_keyword(kw::Let)
&& (!token.is_keyword(kw::Const) || token.span.edition().at_least_rust_2024())
}
NonterminalKind::Ty => token.can_begin_type(),
NonterminalKind::Ident => get_macro_ident(token).is_some(),
Expand Down Expand Up @@ -149,7 +148,7 @@ impl<'a> Parser<'a> {
})?)
}

NonterminalKind::Expr | NonterminalKind::Expr2021 => {
NonterminalKind::Expr | NonterminalKind::Expr2021 { inferred: _ } => {
NtExpr(self.parse_expr_force_collect()?)
}
NonterminalKind::Literal => {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/macros/expr_2021_implicit_in_2024.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//@ compile-flags: --edition=2024 -Zunstable-options
//@ aux-build:expr_2021_implicit.rs

//@ check-pass

extern crate expr_2021_implicit;

// Makes sure that a `:expr` fragment matcher defined in a edition 2021 crate
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/macros/expr_2021_implicit_in_2024.stderr

This file was deleted.

13 changes: 0 additions & 13 deletions tests/ui/macros/expr_2021_old_edition.rs

This file was deleted.

26 changes: 0 additions & 26 deletions tests/ui/macros/expr_2021_old_edition.stderr

This file was deleted.

0 comments on commit 3e8898a

Please sign in to comment.