Skip to content

Commit 8724085

Browse files
committed
Parse const_closures syntax.
Enables parsing of the syntax for `#![features(const_closures)]` introduced in rust-lang/rust#106004
1 parent 93c8ae0 commit 8724085

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

crates/parser/src/grammar/expressions/atom.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub(super) fn atom_expr(
152152
m.complete(p, BLOCK_EXPR)
153153
}
154154

155-
T![static] | T![async] | T![move] | T![|] => closure_expr(p),
155+
T![const] | T![static] | T![async] | T![move] | T![|] => closure_expr(p),
156156
T![for] if la == T![<] => closure_expr(p),
157157
T![for] => for_expr(p, None),
158158

@@ -255,7 +255,7 @@ fn array_expr(p: &mut Parser<'_>) -> CompletedMarker {
255255
// }
256256
fn closure_expr(p: &mut Parser<'_>) -> CompletedMarker {
257257
assert!(match p.current() {
258-
T![static] | T![async] | T![move] | T![|] => true,
258+
T![const] | T![static] | T![async] | T![move] | T![|] => true,
259259
T![for] => p.nth(1) == T![<],
260260
_ => false,
261261
});
@@ -265,7 +265,9 @@ fn closure_expr(p: &mut Parser<'_>) -> CompletedMarker {
265265
if p.at(T![for]) {
266266
types::for_binder(p);
267267
}
268-
268+
// test const_closure
269+
// fn main() { let cl = const || _ = 0; }
270+
p.eat(T![const]);
269271
p.eat(T![static]);
270272
p.eat(T![async]);
271273
p.eat(T![move]);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "main"
7+
PARAM_LIST
8+
L_PAREN "("
9+
R_PAREN ")"
10+
WHITESPACE " "
11+
BLOCK_EXPR
12+
STMT_LIST
13+
L_CURLY "{"
14+
WHITESPACE " "
15+
LET_STMT
16+
LET_KW "let"
17+
WHITESPACE " "
18+
IDENT_PAT
19+
NAME
20+
IDENT "cl"
21+
WHITESPACE " "
22+
EQ "="
23+
WHITESPACE " "
24+
CLOSURE_EXPR
25+
CONST_KW "const"
26+
WHITESPACE " "
27+
PARAM_LIST
28+
PIPE "|"
29+
PIPE "|"
30+
WHITESPACE " "
31+
BIN_EXPR
32+
UNDERSCORE_EXPR
33+
UNDERSCORE "_"
34+
WHITESPACE " "
35+
EQ "="
36+
WHITESPACE " "
37+
LITERAL
38+
INT_NUMBER "0"
39+
SEMICOLON ";"
40+
WHITESPACE " "
41+
R_CURLY "}"
42+
WHITESPACE "\n"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() { let cl = const || _ = 0; }

crates/syntax/rust.ungram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ FieldExpr =
452452
Attr* Expr '.' NameRef
453453

454454
ClosureExpr =
455-
Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'? ParamList RetType?
455+
Attr* ('for' GenericParamList)? 'const'? 'static'? 'async'? 'move'? ParamList RetType?
456456
body:Expr
457457

458458
IfExpr =

crates/syntax/src/ast/generated/nodes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ impl ast::HasAttrs for ClosureExpr {}
842842
impl ClosureExpr {
843843
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
844844
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
845+
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
845846
pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) }
846847
pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
847848
pub fn move_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![move]) }

0 commit comments

Comments
 (0)