From 6c67dd70832cc0eef3f8f1a9cec37e01b82996a8 Mon Sep 17 00:00:00 2001 From: royalpinto007 Date: Mon, 9 Sep 2024 16:20:51 +0530 Subject: [PATCH] fix 24745 - improve error message when creating an associative array with the wrong syntax Signed-off-by: royalpinto007 fix 24745 - issue line Signed-off-by: royalpinto007 --- compiler/src/dmd/parse.d | 4 ++-- compiler/test/fail_compilation/test24745.d | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 compiler/test/fail_compilation/test24745.d diff --git a/compiler/src/dmd/parse.d b/compiler/src/dmd/parse.d index a7a930335ad5..fa24cb3280af 100644 --- a/compiler/src/dmd/parse.d +++ b/compiler/src/dmd/parse.d @@ -6791,7 +6791,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer { if (commaExpected) - error("comma expected separating field initializers"); + error("incorrect syntax for associative array, expected `[]`, found `{}`"); const t = peek(&token); Identifier id; if (t.value == TOK.colon) @@ -6822,7 +6822,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer default: if (commaExpected) - error("comma expected separating field initializers"); + error("incorrect syntax for associative array, expected `[]`, found `{}`"); 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 000000000000..9c7ea59929a3 --- /dev/null +++ b/compiler/test/fail_compilation/test24745.d @@ -0,0 +1,18 @@ +// https://issues.dlang.org/show_bug.cgi?id=24745 + +/* +TEST_OUTPUT: +--- +fail_compilation/test24745.d(17): Error: incorrect syntax for associative array, expected `[]`, found `{}` +fail_compilation/test24745.d(17): Error: expression expected, not `:` +fail_compilation/test24745.d(17): Error: incorrect syntax for associative array, expected `[]`, found `{}` +fail_compilation/test24745.d(17): Error: incorrect syntax for associative array, expected `[]`, found `{}` +fail_compilation/test24745.d(17): Error: expression expected, not `:` +fail_compilation/test24745.d(17): Error: incorrect syntax for associative array, expected `[]`, found `{}` +--- +*/ + +void main() +{ + int[int] f = {1: 1, 2: 2}; +}