From 7c95446800a490cd3f72e4a49171130ec4ee42fb Mon Sep 17 00:00:00 2001 From: Paul Backus Date: Sun, 16 Mar 2025 20:31:39 -0400 Subject: [PATCH] Don't parse lambda as UDA without parentheses (#21009) This brings the compiler's behavior in line with the language spec. Fixes dlang/dlang.org#4137 --- compiler/src/dmd/parse.d | 16 +++++++++++++++- compiler/test/compilable/uda_lambda.d | 3 +++ compiler/test/fail_compilation/uda_lambda.d | 7 +++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 compiler/test/compilable/uda_lambda.d create mode 100644 compiler/test/fail_compilation/uda_lambda.d diff --git a/compiler/src/dmd/parse.d b/compiler/src/dmd/parse.d index 3d25464fc5..33f2a1e116 100644 --- a/compiler/src/dmd/parse.d +++ b/compiler/src/dmd/parse.d @@ -1319,7 +1319,21 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer // Allow identifier, template instantiation, or function call // for `@Argument` (single UDA) form. - AST.Expression exp = parsePrimaryExp(); + AST.Expression exp; + + { + const loc = token.loc; + Identifier id = token.ident; + nextToken(); + if (token.value == TOK.not) + { + auto tempinst = new AST.TemplateInstance(loc, id, parseTemplateArguments()); + exp = new AST.ScopeExp(loc, tempinst); + } + else + exp = new AST.IdentifierExp(loc, id); + } + if (token.value == TOK.leftParenthesis) { const loc = token.loc; diff --git a/compiler/test/compilable/uda_lambda.d b/compiler/test/compilable/uda_lambda.d new file mode 100644 index 0000000000..df331747a7 --- /dev/null +++ b/compiler/test/compilable/uda_lambda.d @@ -0,0 +1,3 @@ +enum UDA; +int fun() @UDA => 7; +static assert(fun() == 7); diff --git a/compiler/test/fail_compilation/uda_lambda.d b/compiler/test/fail_compilation/uda_lambda.d new file mode 100644 index 0000000000..bd3ff49e0e --- /dev/null +++ b/compiler/test/fail_compilation/uda_lambda.d @@ -0,0 +1,7 @@ +/+ +TEST_OUTPUT: +--- +fail_compilation/uda_lambda.d(7): Error: declaration expected, not `=>` +--- ++/ +@a => 7 int b;