From 36c58b7368ccb1e14573ed5ad3f281fae2c9085d Mon Sep 17 00:00:00 2001 From: royalpinto007 Date: Mon, 9 Sep 2024 16:20:51 +0530 Subject: [PATCH] fix Bugzilla 24745 - improve associative array syntax error message Signed-off-by: royalpinto007 --- compiler/src/dmd/parse.d | 15 ++++++++++++++- compiler/test/fail_compilation/test24745.d | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 compiler/test/fail_compilation/test24745.d diff --git a/compiler/src/dmd/parse.d b/compiler/src/dmd/parse.d index ea903bdb22..3e145be549 100644 --- a/compiler/src/dmd/parse.d +++ b/compiler/src/dmd/parse.d @@ -6812,7 +6812,6 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer { case TOK.identifier: { - if (commaExpected) error("comma expected separating field initializers"); const t = peek(&token); @@ -6846,6 +6845,20 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer default: if (commaExpected) error("comma expected separating field initializers"); + const t = peek(&token); + if (t.value == TOK.colon) + { + error("incorrect syntax for associative array, expected `[]`, found `{}`"); + while (token.value != TOK.rightCurly && token.value != TOK.endOfFile) + { + nextToken(); + } + if (token.value == TOK.rightCurly) + { + nextToken(); + } + break; + } auto value = parseInitializer(); _is.addInit(null, value); commaExpected = true; diff --git a/compiler/test/fail_compilation/test24745.d b/compiler/test/fail_compilation/test24745.d new file mode 100644 index 0000000000..6d9c335bb4 --- /dev/null +++ b/compiler/test/fail_compilation/test24745.d @@ -0,0 +1,13 @@ +// https://issues.dlang.org/show_bug.cgi?id=24745 + +/* +TEST_OUTPUT: +--- +fail_compilation/test24745.d(12): Error: incorrect syntax for associative array, expected `[]`, found `{}` +--- +*/ + +void main() +{ + int[int] f = {1: 1, 2: 2}; +}