From c6f16e0afdb81716fac28adfed8a8245f87ce14d Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Fri, 31 May 2024 16:47:30 +0000 Subject: [PATCH] rustc_parser: consider the in 2024 an expression This commit is adding the possibility to parse the `_` as an expression inside the esition 2024. Link: https://rust-lang.zulipchat.com/#narrow/stream/404510-wg-macros/topic/supporting.20.60_.60.20expressions Signed-off-by: Vincenzo Palazzo --- compiler/rustc_ast/src/token.rs | 3 ++ .../rustc_parse/src/parser/nonterminal.rs | 2 ++ .../expr_2024_underscore_expr.edi2021.stderr | 32 +++++++++++++++++++ .../expr_2024_underscore_expr.edi2024.stderr | 17 ++++++++++ tests/ui/macros/expr_2024_underscore_expr.rs | 24 ++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 tests/ui/macros/expr_2024_underscore_expr.edi2021.stderr create mode 100644 tests/ui/macros/expr_2024_underscore_expr.edi2024.stderr create mode 100644 tests/ui/macros/expr_2024_underscore_expr.rs diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index efe1956615216..4dd198e0d0e09 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -216,6 +216,9 @@ pub fn ident_can_begin_expr(name: Symbol, span: Span, is_raw: IdentIsRaw) -> boo kw::Static, ] .contains(&name) + // Please note the order of the check here, the edition is not check on every + // token by only on the possible one. + || (kw::Underscore == name && span.edition().at_least_rust_2024()) } fn ident_can_begin_type(name: Symbol, span: Span, is_raw: IdentIsRaw) -> bool { diff --git a/compiler/rustc_parse/src/parser/nonterminal.rs b/compiler/rustc_parse/src/parser/nonterminal.rs index 4a78b427832c5..4b1ac7f9ef78b 100644 --- a/compiler/rustc_parse/src/parser/nonterminal.rs +++ b/compiler/rustc_parse/src/parser/nonterminal.rs @@ -44,6 +44,8 @@ impl<'a> Parser<'a> { && !token.is_keyword(kw::Let) // This exception is here for backwards compatibility. && !token.is_keyword(kw::Const) + // FIXME(vincenzopalazzo): query less time the edition + && (token.span.edition().at_least_rust_2024() && !token.is_keyword(kw::Underscore) ) } NonterminalKind::Expr(Expr) => { token.can_begin_expr() diff --git a/tests/ui/macros/expr_2024_underscore_expr.edi2021.stderr b/tests/ui/macros/expr_2024_underscore_expr.edi2021.stderr new file mode 100644 index 0000000000000..09b7de2a5f19c --- /dev/null +++ b/tests/ui/macros/expr_2024_underscore_expr.edi2021.stderr @@ -0,0 +1,32 @@ +error: no rules expected the token `_` + --> $DIR/expr_2024_underscore_expr.rs:23:12 + | +LL | macro_rules! m2021 { + | ------------------ when calling this macro +... +LL | m2021!(_); + | ^ no rules expected this token in macro call + | +note: while trying to match meta-variable `$e:expr_2021` + --> $DIR/expr_2024_underscore_expr.rs:11:6 + | +LL | ($e:expr_2021) => { + | ^^^^^^^^^^^^ + +error: no rules expected the token `_` + --> $DIR/expr_2024_underscore_expr.rs:24:12 + | +LL | macro_rules! m2024 { + | ------------------ when calling this macro +... +LL | m2024!(_); + | ^ no rules expected this token in macro call + | +note: while trying to match meta-variable `$e:expr` + --> $DIR/expr_2024_underscore_expr.rs:17:6 + | +LL | ($e:expr) => { + | ^^^^^^^ + +error: aborting due to 2 previous errors + diff --git a/tests/ui/macros/expr_2024_underscore_expr.edi2024.stderr b/tests/ui/macros/expr_2024_underscore_expr.edi2024.stderr new file mode 100644 index 0000000000000..e6c2bef10e299 --- /dev/null +++ b/tests/ui/macros/expr_2024_underscore_expr.edi2024.stderr @@ -0,0 +1,17 @@ +error: no rules expected the token `_` + --> $DIR/expr_2024_underscore_expr.rs:23:12 + | +LL | macro_rules! m2021 { + | ------------------ when calling this macro +... +LL | m2021!(_); + | ^ no rules expected this token in macro call + | +note: while trying to match meta-variable `$e:expr_2021` + --> $DIR/expr_2024_underscore_expr.rs:11:6 + | +LL | ($e:expr_2021) => { + | ^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/macros/expr_2024_underscore_expr.rs b/tests/ui/macros/expr_2024_underscore_expr.rs new file mode 100644 index 0000000000000..b2129bf154f7e --- /dev/null +++ b/tests/ui/macros/expr_2024_underscore_expr.rs @@ -0,0 +1,24 @@ +//@ revisions: edi2021 edi2024 +//@[edi2024]compile-flags: --edition=2024 -Z unstable-options +//@[edi2021]compile-flags: --edition=2021 +// This test ensures that the `_` tok is considered an +// expression on edition 2024. +#![feature(expr_fragment_specifier_2024)] +#![allow(incomplete_features)] + +macro_rules! m2021 { + ($e:expr_2021) => { + $e = 1; + }; +} + +macro_rules! m2024 { + ($e:expr) => { + $e = 1; + }; +} + +fn main() { + m2021!(_); //~ ERROR: no rules expected the token `_` + m2024!(_); //[edi2021]~ ERROR: no rules expected the token `_` +}