Skip to content

Commit eecd1b2

Browse files
committed
fix Issue 21347 - Parser never stops looking for ',' on array literal syntax error
When a comma is missing after an element in an array literal or a subscript argument list, the parser used to keep consuming tokens past the syntax error all the way to the end of file, producing a long cascade of spurious errors instead of a small, focused set. Break out of the parsing loop as soon as a required comma is missing, after reporting the original error, instead of continuing to loop until the closing bracket or end of file is reached. Fixes Bugzilla Issue 21347
1 parent 52c8c25 commit eecd1b2

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

compiler/src/dmd/parse.d

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9024,7 +9024,12 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
90249024
values.push(e);
90259025
if (token.value == TOK.rightBracket)
90269026
break;
9027-
check(TOK.comma);
9027+
if (token.value != TOK.comma)
9028+
{
9029+
check(TOK.comma);
9030+
break;
9031+
}
9032+
nextToken();
90289033
}
90299034
check(loc, TOK.rightBracket);
90309035

@@ -9441,7 +9446,12 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
94419446
arguments.push(index);
94429447
if (token.value == TOK.rightBracket)
94439448
break;
9444-
check(TOK.comma);
9449+
if (token.value != TOK.comma)
9450+
{
9451+
check(TOK.comma);
9452+
break;
9453+
}
9454+
nextToken();
94459455
}
94469456
check(TOK.rightBracket);
94479457
inBrackets--;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* TEST_OUTPUT:
2+
---
3+
fail_compilation/fail21347.d(19): Error: expression expected, not `;`
4+
fail_compilation/fail21347.d(20): Error: found `}` when expecting `,`
5+
fail_compilation/fail21347.d(19): Error: found `End of File` when expecting `]`
6+
fail_compilation/fail21347.d(19): Error: found `End of File` when expecting `)`
7+
fail_compilation/fail21347.d(21): Error: found `End of File` when expecting `;` following expression
8+
fail_compilation/fail21347.d(19): expression: `[(__error)]`
9+
fail_compilation/fail21347.d(21): Error: matching `}` expected following compound statement, not `End of File`
10+
fail_compilation/fail21347.d(18): unmatched `{`
11+
---
12+
*/
13+
14+
// https://github.com/dlang/dmd/issues/21347
15+
// diagnostic: Parser never stops looking for ',' on array literal syntax error
16+
17+
void test21347()
18+
{
19+
([;
20+
}

compiler/test/fail_compilation/fail315.d

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/*
22
TEST_OUTPUT:
33
---
4-
fail_compilation/fail315.d-mixin-17(17): Error: found `;` when expecting `,`
5-
fail_compilation/fail315.d-mixin-17(17): Error: expression expected, not `}`
6-
fail_compilation/fail315.d-mixin-17(17): Error: found `End of File` when expecting `,`
7-
fail_compilation/fail315.d-mixin-17(17): Error: found `End of File` when expecting `]`
8-
fail_compilation/fail315.d-mixin-17(17): Error: found `End of File` when expecting `;` following `return` statement
9-
fail_compilation/fail315.d-mixin-17(17): Error: matching `}` expected following compound statement, not `End of File`
10-
fail_compilation/fail315.d-mixin-17(17): unmatched `{`
11-
fail_compilation/fail315.d(22): Error: template instance `fail315.foo!()` error instantiating
4+
fail_compilation/fail315.d-mixin-15(15): Error: found `;` when expecting `,`
5+
fail_compilation/fail315.d-mixin-15(15): Error: found `}` when expecting `]`
6+
fail_compilation/fail315.d-mixin-15(15): Error: found `End of File` when expecting `;` following `return` statement
7+
fail_compilation/fail315.d-mixin-15(15): Error: matching `}` expected following compound statement, not `End of File`
8+
fail_compilation/fail315.d-mixin-15(15): unmatched `{`
9+
fail_compilation/fail315.d(20): Error: template instance `fail315.foo!()` error instantiating
1210
---
1311
*/
1412

0 commit comments

Comments
 (0)