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]>
  • Loading branch information
royalpinto007 authored and thewilsonator committed Nov 17, 2024
1 parent f5f7bae commit 36c58b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 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,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;
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/fail_compilation/test24745.d
Original file line number Diff line number Diff line change
@@ -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};
}

0 comments on commit 36c58b7

Please sign in to comment.