Bug 24525 – auto ref lambda exp not parsed if used as left-most expression in an expression statement
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-04-28T00:18:12Z
Last change time
2024-05-02T00:20:38Z
Keywords
pull, rejects-valid
Assigned to
Nick Treleaven
Creator
basile-z
Comments
Comment #0 by b2.temp — 2024-04-28T00:18:12Z
## test case
```d
void main()
{
int a;
(auto ref () {return a;}()) = 0; // OK
auto ref () {return a;}() = 0; // NG
}
```
## output
(many parser errors)
## notes
- Only observed for the 3rd rule (https://dlang.org/spec/expression.html#function_literals).
- Must be that the parser takes the path of a declaration,
Comment #1 by nick — 2024-04-28T18:30:11Z
Under https://dlang.org/spec/statement.html#NonEmptyStatementNoCaseNoDefault grammar block it says:
> Any ambiguities in the grammar between Statements and Declarations are resolved by the declarations taking precedence.
For:
> ref () {return a;}() = 0;
A declaration `ref () {return a;}` gets parsed, not an ExpressionStatement. So I think this issue is invalid.
It is unfortunate that the function literal syntax starting `ref` or `auto ref` was added, when the function/delegate keyword starting forms can already handle those.
Comment #2 by nick — 2024-04-28T18:31:58Z
> A declaration `ref () {return a;}` gets parsed
Sorry, that may be what is happening, but there is no identifier for the declaration, so it should try to parse an ExpressionStatement.
Comment #3 by b2.temp — 2024-04-29T10:01:44Z
> Any ambiguities in the grammar between Statements and Declarations are resolved by the declarations taking precedence.
yes, I remember the spec about decl/stmt ambiguities but here I think there are none.
The parser should lookup after `auto`. If next is `(` or if next two are `ref` then `(`, then it should takes the path of expression.
> It is unfortunate that the function literal syntax starting `ref` or `auto ref` was added, when the function/delegate keyword starting forms can already handle those.
Indeed. But I'll show you the context:
```
void main()
{
int a,b,c,d,cond;
auto ref () {
switch (cond)
{
case 1: return a;
case 2: return b;
case 3: return c;
default: return d;
}
}() = 0;
}
```
Here, given the size of the lambda, you try to avoid the verbose solution (you can put the lambda between parens however, but according to the specs, that should work w/o).
Comment #4 by dlang-bot — 2024-05-01T09:52:08Z
@ntrel created dlang/dmd pull request #16431 "Fix Bugzilla 24525 - ref lambda not parsed at start of ExpressionStat…" fixing this issue:
- Fix Bugzilla 24525 - ref lambda not parsed at start of ExpressionStatement
https://github.com/dlang/dmd/pull/16431
Comment #5 by dlang-bot — 2024-05-02T00:20:38Z
dlang/dmd pull request #16431 "Fix Bugzilla 24525 - ref lambda not parsed at start of ExpressionStat…" was merged into master:
- 3c3be0716da1b6d2cdbe2ca6517a8b1cec6d7ae2 by Nick Treleaven:
Fix Bugzilla 24525 - ref lambda not parsed at start of ExpressionStatement
https://github.com/dlang/dmd/pull/16431