Skip to content

Commit c6f16e0

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 <vincenzopalazzodev@gmail.com>
1 parent f25e92b commit c6f16e0

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

compiler/rustc_ast/src/token.rs

+3
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ pub fn ident_can_begin_expr(name: Symbol, span: Span, is_raw: IdentIsRaw) -> boo
216216
kw::Static,
217217
]
218218
.contains(&name)
219+
// Please note the order of the check here, the edition is not check on every
220+
// token by only on the possible one.
221+
|| (kw::Underscore == name && span.edition().at_least_rust_2024())
219222
}
220223

221224
fn ident_can_begin_type(name: Symbol, span: Span, is_raw: IdentIsRaw) -> bool {

compiler/rustc_parse/src/parser/nonterminal.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ impl<'a> Parser<'a> {
4444
&& !token.is_keyword(kw::Let)
4545
// This exception is here for backwards compatibility.
4646
&& !token.is_keyword(kw::Const)
47+
// FIXME(vincenzopalazzo): query less time the edition
48+
&& (token.span.edition().at_least_rust_2024() && !token.is_keyword(kw::Underscore) )
4749
}
4850
NonterminalKind::Expr(Expr) => {
4951
token.can_begin_expr()
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: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: no rules expected the token `_`
17+
--> $DIR/expr_2024_underscore_expr.rs:24: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:17: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)