From 5aaa7df7d39d203b316983d5efb0e0d5e709cd81 Mon Sep 17 00:00:00 2001 From: onestacked Date: Thu, 19 Jan 2023 15:38:41 +0100 Subject: [PATCH] Parse const_closures syntax. Enables parsing of the syntax for `#![features(const_closures)]` introduced in https://github.com/rust-lang/rust/pull/106004 --- crates/parser/src/grammar/expressions/atom.rs | 8 ++-- .../parser/inline/ok/0205_const_closure.rast | 42 +++++++++++++++++++ .../parser/inline/ok/0205_const_closure.rs | 1 + crates/syntax/rust.ungram | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 crates/parser/test_data/parser/inline/ok/0205_const_closure.rast create mode 100644 crates/parser/test_data/parser/inline/ok/0205_const_closure.rs diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs index efa3997353bf..a23f900b7386 100644 --- a/crates/parser/src/grammar/expressions/atom.rs +++ b/crates/parser/src/grammar/expressions/atom.rs @@ -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), @@ -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, }); @@ -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]); diff --git a/crates/parser/test_data/parser/inline/ok/0205_const_closure.rast b/crates/parser/test_data/parser/inline/ok/0205_const_closure.rast new file mode 100644 index 000000000000..06442a1d0f1f --- /dev/null +++ b/crates/parser/test_data/parser/inline/ok/0205_const_closure.rast @@ -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" diff --git a/crates/parser/test_data/parser/inline/ok/0205_const_closure.rs b/crates/parser/test_data/parser/inline/ok/0205_const_closure.rs new file mode 100644 index 000000000000..0c05cc70bd37 --- /dev/null +++ b/crates/parser/test_data/parser/inline/ok/0205_const_closure.rs @@ -0,0 +1 @@ +fn main() { let cl = const || _ = 0; } diff --git a/crates/syntax/rust.ungram b/crates/syntax/rust.ungram index 2c67586a3905..36ad5fddfd0c 100644 --- a/crates/syntax/rust.ungram +++ b/crates/syntax/rust.ungram @@ -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 =