Skip to content

Commit 866885c

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 Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 47243b3 commit 866885c

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

compiler/rustc_parse/src/parser/nonterminal.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,18 @@ impl<'a> Parser<'a> {
4646
&& !token.is_keyword(kw::Const)
4747
}
4848
NonterminalKind::Expr(Expr) => {
49-
token.can_begin_expr()
49+
// To avoid changing the API of `can_begin_expr` we keep the logic
50+
// related ot the `Expr` inside this block. In the future we may
51+
// want to refactor `can_begin_expr` to take a `ExprKind` parameter
52+
// to make more complex logic easier to implement.
53+
//
54+
// In the Edition 2024, `_` will be considered an expression, so
55+
// we need to allow it here in the case the `token.can_begin_expr()` return
56+
// false.
57+
//
58+
// Due that the logic of `can_begin_expr` is used somewhere else, we need to
59+
// reduce the scope where the `_` is considered an expression.
60+
(token.can_begin_expr() || token.is_keyword(kw::Underscore))
5061
// This exception is here for backwards compatibility.
5162
&& !token.is_keyword(kw::Let)
5263
}
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)