Skip to content

Commit

Permalink
fix Bugzilla 24745 - improve associative array syntax error message
Browse files Browse the repository at this point in the history
Signed-off-by: royalpinto007 <[email protected]>

fix 24745 - issue line

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): update test cases, error messages

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): message for default comma case

Signed-off-by: royalpinto007 <[email protected]>
  • Loading branch information
royalpinto007 committed Nov 15, 2024
1 parent a555e13 commit 16276df
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -6846,6 +6845,11 @@ 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 `{}`");
}
auto value = parseInitializer();
_is.addInit(null, value);
commaExpected = true;
Expand Down
20 changes: 20 additions & 0 deletions compiler/test/fail_compilation/test24745.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://issues.dlang.org/show_bug.cgi?id=24745

/*
TEST_OUTPUT:
---
fail_compilation/test24745.d(19): Error: incorrect syntax for associative array, expected `[]`, found `{}`
fail_compilation/test24745.d(19): Error: comma expected separating field initializers
fail_compilation/test24745.d(19): Error: expression expected, not `:`
fail_compilation/test24745.d(19): Error: comma expected separating field initializers
fail_compilation/test24745.d(19): Error: incorrect syntax for associative array, expected `[]`, found `{}`
fail_compilation/test24745.d(19): Error: comma expected separating field initializers
fail_compilation/test24745.d(19): Error: expression expected, not `:`
fail_compilation/test24745.d(19): Error: comma expected separating field initializers
---
*/

void main()
{
int[int] f = {1: 1, 2: 2};
}

0 comments on commit 16276df

Please sign in to comment.