Skip to content

Commit 3acd910

Browse files
authored
Rollup merge of #126697 - vincenzopalazzo:macros/find_the_expression_tok, r=eholk,compiler-errors
[RFC] mbe: 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 Issue #123742 r? `@eholk`
2 parents 20379e4 + 79ef91e commit 3acd910

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

compiler/rustc_parse/src/parser/nonterminal.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,24 @@ impl<'a> Parser<'a> {
3939
}
4040

4141
match kind {
42+
// `expr_2021` and earlier
4243
NonterminalKind::Expr(Expr2021 { .. }) => {
4344
token.can_begin_expr()
4445
// This exception is here for backwards compatibility.
4546
&& !token.is_keyword(kw::Let)
4647
// This exception is here for backwards compatibility.
4748
&& !token.is_keyword(kw::Const)
4849
}
50+
// Current edition expressions
4951
NonterminalKind::Expr(Expr) => {
50-
token.can_begin_expr()
52+
// In Edition 2024, `_` is considered an expression, so we
53+
// need to allow it here because `token.can_begin_expr()` does
54+
// not consider `_` to be an expression.
55+
//
56+
// Because `can_begin_expr` is used elsewhere, we need to reduce
57+
// the scope of where the `_` is considered an expression to
58+
// just macro parsing code.
59+
(token.can_begin_expr() || token.is_keyword(kw::Underscore))
5160
// This exception is here for backwards compatibility.
5261
&& !token.is_keyword(kw::Let)
5362
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: no rules expected the token `_`
2+
--> $DIR/expr_2024_underscore_expr.rs:22:12
3+
|
4+
LL | macro_rules! m2021 {
5+
| ------------------ when calling this macro
6+
...
7+
LL | m2021!(_);
8+
| ^ no rules expected this token in macro call
9+
|
10+
note: while trying to match meta-variable `$e:expr_2021`
11+
--> $DIR/expr_2024_underscore_expr.rs:10:6
12+
|
13+
LL | ($e:expr_2021) => {
14+
| ^^^^^^^^^^^^
15+
16+
error: no rules expected the token `_`
17+
--> $DIR/expr_2024_underscore_expr.rs:23:12
18+
|
19+
LL | macro_rules! m2024 {
20+
| ------------------ when calling this macro
21+
...
22+
LL | m2024!(_);
23+
| ^ no rules expected this token in macro call
24+
|
25+
note: while trying to match meta-variable `$e:expr`
26+
--> $DIR/expr_2024_underscore_expr.rs:16:6
27+
|
28+
LL | ($e:expr) => {
29+
| ^^^^^^^
30+
31+
error: aborting due to 2 previous errors
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: no rules expected the token `_`
2+
--> $DIR/expr_2024_underscore_expr.rs:22:12
3+
|
4+
LL | macro_rules! m2021 {
5+
| ------------------ when calling this macro
6+
...
7+
LL | m2021!(_);
8+
| ^ no rules expected this token in macro call
9+
|
10+
note: while trying to match meta-variable `$e:expr_2021`
11+
--> $DIR/expr_2024_underscore_expr.rs:10:6
12+
|
13+
LL | ($e:expr_2021) => {
14+
| ^^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ revisions: edi2021 edi2024
2+
//@[edi2024]compile-flags: --edition=2024 -Z unstable-options
3+
//@[edi2021]compile-flags: --edition=2021
4+
// This test ensures that the `_` tok is considered an
5+
// expression on edition 2024.
6+
#![feature(expr_fragment_specifier_2024)]
7+
#![allow(incomplete_features)]
8+
9+
macro_rules! m2021 {
10+
($e:expr_2021) => {
11+
$e = 1;
12+
};
13+
}
14+
15+
macro_rules! m2024 {
16+
($e:expr) => {
17+
$e = 1;
18+
};
19+
}
20+
21+
fn main() {
22+
m2021!(_); //~ ERROR: no rules expected the token `_`
23+
m2024!(_); //[edi2021]~ ERROR: no rules expected the token `_`
24+
}

0 commit comments

Comments
 (0)