rust-analyzer/ungrammar

Closure parameter list

dtolnay opened this issue · 3 comments

The ClosureExpr node is currently defined in a way that would imply parentheses around the parameters () body, rather than vertical pipes || body.

ungrammar/rust.ungram

Lines 423 to 425 in 07d5806

ClosureExpr =
Attr* 'static'? 'async'? 'move'? ParamList RetType?
body:Expr

ungrammar/rust.ungram

Lines 135 to 139 in 07d5806

ParamList =
'('(
SelfParam
| (SelfParam ',')? (Param (',' Param)* ','?)?
)')'

Yeah, I’ve noticed that as well. I am debating between just allowing | in Params, or using separate node types for closures.

I don‘t have a super great intuition for where to reuse nodes and where to use more refined types, but I lean towards reusing.

In Syn we use Signature for all instances of function signatures involving fn and a different representation for ExprClosure, since the argument grammar is pretty different anyway beyond just the delimiters. For example you can always get the Type of a fn parameter from syntax, but not of a closure parameter.

For example you can always get the Type of a fn parameter from syntax, but not of a closure parameter.

Which is slightly different for rust-analyzer, where everything is optional. In fn foo(x: ) { } there will be a Param node in the CST. So, any processing of params syntax needs to deal with missing types anyway, so there‘s less incentive to try to lift „is parameter optional“ to the type level.