Skip to content

Commit 276fa19

Browse files
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 Co-authored-by: Eric Holk <[email protected]> Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 47243b3 commit 276fa19

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
@@ -38,15 +38,24 @@ impl<'a> Parser<'a> {
3838
}
3939

4040
match kind {
41+
// `expr_2021` and earlier
4142
NonterminalKind::Expr(Expr2021 { .. }) => {
4243
token.can_begin_expr()
4344
// This exception is here for backwards compatibility.
4445
&& !token.is_keyword(kw::Let)
4546
// This exception is here for backwards compatibility.
4647
&& !token.is_keyword(kw::Const)
4748
}
49+
// Current edition expressions
4850
NonterminalKind::Expr(Expr) => {
49-
token.can_begin_expr()
51+
// In Edition 2024, `_` will be considered an expression, so we
52+
// need to allow it here because `token.can_begin_expr()` does
53+
// not consider `_` to be an expression.
54+
//
55+
// Because `can_begin_expr` is used elsewhere, we need to reduce
56+
// the scope of where the `_` is considered an expression to
57+
// just macro parsing code.
58+
(token.can_begin_expr() || token.is_keyword(kw::Underscore))
5059
// This exception is here for backwards compatibility.
5160
&& !token.is_keyword(kw::Let)
5261
}
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:23: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:11: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)