Skip to content

Commit a55d063

Browse files
Macros: match const { ... } with expr nonterminal in edition 2024
Co-authored-by: Eric Holk <[email protected]> Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 73303c3 commit a55d063

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

compiler/rustc_parse/src/parser/nonterminal.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_ast::token::{self, Delimiter, Nonterminal, Nonterminal::*, Nonterminal
33
use rustc_ast::HasTokens;
44
use rustc_ast_pretty::pprust;
55
use rustc_errors::PResult;
6+
use rustc_span::edition::Edition;
67
use rustc_span::symbol::{kw, Ident};
78

89
use crate::errors::UnexpectedNonterminal;
@@ -37,13 +38,19 @@ impl<'a> Parser<'a> {
3738
}
3839

3940
match kind {
40-
NonterminalKind::Expr | NonterminalKind::Expr2021 => {
41+
NonterminalKind::Expr2021 => {
4142
token.can_begin_expr()
4243
// This exception is here for backwards compatibility.
4344
&& !token.is_keyword(kw::Let)
4445
// This exception is here for backwards compatibility.
4546
&& !token.is_keyword(kw::Const)
4647
}
48+
NonterminalKind::Expr => {
49+
token.can_begin_expr()
50+
// This exception is here for backwards compatibility.
51+
&& !token.is_keyword(kw::Let)
52+
&& (token.span.edition() >= Edition::Edition2024 || !token.is_keyword(kw::Const))
53+
}
4754
NonterminalKind::Ty => token.can_begin_type(),
4855
NonterminalKind::Ident => get_macro_ident(token).is_some(),
4956
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: no rules expected the token `const`
2+
--> $DIR/expr_2021_inline_const.rs:21:12
3+
|
4+
LL | macro_rules! m2021 {
5+
| ------------------ when calling this macro
6+
...
7+
LL | m2021!(const { 1 });
8+
| ^^^^^ no rules expected this token in macro call
9+
|
10+
note: while trying to match meta-variable `$e:expr_2021`
11+
--> $DIR/expr_2021_inline_const.rs:10:6
12+
|
13+
LL | ($e:expr_2021) => {
14+
| ^^^^^^^^^^^^
15+
16+
error: no rules expected the token `const`
17+
--> $DIR/expr_2021_inline_const.rs:22:12
18+
|
19+
LL | macro_rules! m2024 {
20+
| ------------------ when calling this macro
21+
...
22+
LL | m2024!(const { 1 });
23+
| ^^^^^ no rules expected this token in macro call
24+
|
25+
note: while trying to match meta-variable `$e:expr`
26+
--> $DIR/expr_2021_inline_const.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 `const`
2+
--> $DIR/expr_2021_inline_const.rs:21:12
3+
|
4+
LL | macro_rules! m2021 {
5+
| ------------------ when calling this macro
6+
...
7+
LL | m2021!(const { 1 });
8+
| ^^^^^ no rules expected this token in macro call
9+
|
10+
note: while trying to match meta-variable `$e:expr_2021`
11+
--> $DIR/expr_2021_inline_const.rs:10:6
12+
|
13+
LL | ($e:expr_2021) => {
14+
| ^^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ revisions: edi2021 edi2024
2+
//@[edi2024]compile-flags: --edition=2024 -Z unstable-options
3+
//@[edi2021]compile-flags: --edition=2021
4+
5+
// This test ensures that the inline const match only on edition 2024
6+
#![feature(expr_fragment_specifier_2024)]
7+
#![allow(incomplete_features)]
8+
9+
macro_rules! m2021 {
10+
($e:expr_2021) => {
11+
$e
12+
};
13+
}
14+
15+
macro_rules! m2024 {
16+
($e:expr) => {
17+
$e
18+
};
19+
}
20+
fn main() {
21+
m2021!(const { 1 }); //~ ERROR: no rules expected the token `const`
22+
m2024!(const { 1 }); //[edi2021]~ ERROR: no rules expected the token `const`
23+
}

0 commit comments

Comments
 (0)