Skip to content

Commit 7c95446

Browse files
authored
Don't parse lambda as UDA without parentheses (dlang#21009)
This brings the compiler's behavior in line with the language spec. Fixes dlang/dlang.org#4137
1 parent 726c50e commit 7c95446

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

compiler/src/dmd/parse.d

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,21 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
13191319

13201320
// Allow identifier, template instantiation, or function call
13211321
// for `@Argument` (single UDA) form.
1322-
AST.Expression exp = parsePrimaryExp();
1322+
AST.Expression exp;
1323+
1324+
{
1325+
const loc = token.loc;
1326+
Identifier id = token.ident;
1327+
nextToken();
1328+
if (token.value == TOK.not)
1329+
{
1330+
auto tempinst = new AST.TemplateInstance(loc, id, parseTemplateArguments());
1331+
exp = new AST.ScopeExp(loc, tempinst);
1332+
}
1333+
else
1334+
exp = new AST.IdentifierExp(loc, id);
1335+
}
1336+
13231337
if (token.value == TOK.leftParenthesis)
13241338
{
13251339
const loc = token.loc;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
enum UDA;
2+
int fun() @UDA => 7;
3+
static assert(fun() == 7);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/+
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation/uda_lambda.d(7): Error: declaration expected, not `=>`
5+
---
6+
+/
7+
@a => 7 int b;

0 commit comments

Comments
 (0)