Skip to content

Commit

Permalink
Parse const_closures syntax.
Browse files Browse the repository at this point in the history
Enables parsing of the syntax for `#![features(const_closures)]` introduced in rust-lang/rust#106004
  • Loading branch information
onestacked committed Jan 19, 2023
1 parent 93c8ae0 commit 5aaa7df
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
8 changes: 5 additions & 3 deletions crates/parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub(super) fn atom_expr(
m.complete(p, BLOCK_EXPR)
}

T![static] | T![async] | T![move] | T![|] => closure_expr(p),
T![const] | T![static] | T![async] | T![move] | T![|] => closure_expr(p),
T![for] if la == T![<] => closure_expr(p),
T![for] => for_expr(p, None),

Expand Down Expand Up @@ -255,7 +255,7 @@ fn array_expr(p: &mut Parser<'_>) -> CompletedMarker {
// }
fn closure_expr(p: &mut Parser<'_>) -> CompletedMarker {
assert!(match p.current() {
T![static] | T![async] | T![move] | T![|] => true,
T![const] | T![static] | T![async] | T![move] | T![|] => true,
T![for] => p.nth(1) == T![<],
_ => false,
});
Expand All @@ -265,7 +265,9 @@ fn closure_expr(p: &mut Parser<'_>) -> CompletedMarker {
if p.at(T![for]) {
types::for_binder(p);
}

// test const_closure
// fn main() { let cl = const || _ = 0; }
p.eat(T![const]);
p.eat(T![static]);
p.eat(T![async]);
p.eat(T![move]);
Expand Down
42 changes: 42 additions & 0 deletions crates/parser/test_data/parser/inline/ok/0205_const_closure.rast
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "main"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
LET_STMT
LET_KW "let"
WHITESPACE " "
IDENT_PAT
NAME
IDENT "cl"
WHITESPACE " "
EQ "="
WHITESPACE " "
CLOSURE_EXPR
CONST_KW "const"
WHITESPACE " "
PARAM_LIST
PIPE "|"
PIPE "|"
WHITESPACE " "
BIN_EXPR
UNDERSCORE_EXPR
UNDERSCORE "_"
WHITESPACE " "
EQ "="
WHITESPACE " "
LITERAL
INT_NUMBER "0"
SEMICOLON ";"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() { let cl = const || _ = 0; }
2 changes: 1 addition & 1 deletion crates/syntax/rust.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ FieldExpr =
Attr* Expr '.' NameRef

ClosureExpr =
Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'? ParamList RetType?
Attr* ('for' GenericParamList)? 'const'? 'static'? 'async'? 'move'? ParamList RetType?
body:Expr

IfExpr =
Expand Down

0 comments on commit 5aaa7df

Please sign in to comment.