Parse const_closures syntax.

Enables parsing of the syntax for `#![features(const_closures)]` introduced in https://github.com/rust-lang/rust/pull/106004
This commit is contained in:
onestacked 2023-01-19 15:40:07 +01:00
parent 93c8ae08dd
commit 872408500b
5 changed files with 50 additions and 4 deletions

View file

@ -152,7 +152,7 @@ pub(super) fn atom_expr(
m.complete(p, BLOCK_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] if la == T![<] => closure_expr(p),
T![for] => for_expr(p, None), T![for] => for_expr(p, None),
@ -255,7 +255,7 @@ fn array_expr(p: &mut Parser<'_>) -> CompletedMarker {
// } // }
fn closure_expr(p: &mut Parser<'_>) -> CompletedMarker { fn closure_expr(p: &mut Parser<'_>) -> CompletedMarker {
assert!(match p.current() { 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![<], T![for] => p.nth(1) == T![<],
_ => false, _ => false,
}); });
@ -265,7 +265,9 @@ fn closure_expr(p: &mut Parser<'_>) -> CompletedMarker {
if p.at(T![for]) { if p.at(T![for]) {
types::for_binder(p); types::for_binder(p);
} }
// test const_closure
// fn main() { let cl = const || _ = 0; }
p.eat(T![const]);
p.eat(T![static]); p.eat(T![static]);
p.eat(T![async]); p.eat(T![async]);
p.eat(T![move]); p.eat(T![move]);

View file

@ -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"

View file

@ -0,0 +1 @@
fn main() { let cl = const || _ = 0; }

View file

@ -452,7 +452,7 @@ FieldExpr =
Attr* Expr '.' NameRef Attr* Expr '.' NameRef
ClosureExpr = ClosureExpr =
Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'? ParamList RetType? Attr* ('for' GenericParamList)? 'const'? 'static'? 'async'? 'move'? ParamList RetType?
body:Expr body:Expr
IfExpr = IfExpr =

View file

@ -842,6 +842,7 @@ impl ast::HasAttrs for ClosureExpr {}
impl ClosureExpr { impl ClosureExpr {
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) } pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) } pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) }
pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
pub fn move_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![move]) } pub fn move_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![move]) }