Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make edition dependent :expr macro fragment act like the edition-dependent :pat fragment does #126700

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated -- but I wonder if we can come up with a more descriptive name. expr_2021 gives me the vibe that its new to 2021 (i.e. >=2021) not legacy to 2021 (i.e. <=2021).

I quite like PatParam and PatWithOr naming scheme since it makes it less about edition and more about what it's parsing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be awesome. I couldn't come up with a good succinct name yet, it's kinda hard lol ngl and the grammar may still change, too (cc #126697). Note that we're already tracking this under Unresolved Questions in #123742.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the _2021 suffix's meaning is non-obvious, and even after learning what it means it I keep having trouble remembering the meaning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been thinking about a good name for a while now because I often get confused when reading the code a week later.

However, I think expr_2021 is one of the best names we can choose. If we update the documentation to explain that expr_2021 includes all features up to the 2021 edition, and expr includes all features of the current edition (up to 2024 in this case), it would be clear.

This approach can be applied retroactively as well. We do not have an expr_2018 because there were no changes in the semantics of expr at that time.

Do you think your concern can be addressed by updating the documentation to clearly explain this way of thinking?

I am also considering that if we give another name without referencing the edition, and if expr keeps changing in the next edition, keeping track of and explaining all the different expr types will be a mess, in my opinion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use the year as a suffix for saying "the moment in was introduced", that would mean we would have Expr2015 and Expr2024; that would follow "the vibe that its new to 2024".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, are we agreeing that Expr2021 would include all features up to the 2021 edition, and Expr would continue to represent the current edition's features?

Or are you proposing a slightly different idea from what we are currently implementing?

By the way, I am happy with either approach, but I think it is important for users that we clearly indicate the edition where the expr_* is necessary to maintain old behavior.

/// 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
9 changes: 9 additions & 0 deletions tests/ui/macros/auxiliary/expr_2021_implicit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ edition:2021

#[macro_export]
macro_rules! m {
($expr:expr) => {
compile_error!("did not expect an expression to be parsed");
};
(const { }) => {};
}
12 changes: 12 additions & 0 deletions tests/ui/macros/expr_2021_implicit_in_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ 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
// still parses like an `expr_2021` fragment matcher in a 2024 user crate.
expr_2021_implicit::m!(const {});

fn main() {}
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.

Loading