Skip to content

Commit 415e6d5

Browse files
committed
Better error recovery in comma-separated lists
1 parent 24fdf53 commit 415e6d5

File tree

4 files changed

+89
-29
lines changed

4 files changed

+89
-29
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -553,19 +553,28 @@ object Parsers {
553553
def inDefScopeBraces[T](body: => T, rewriteWithColon: Boolean = false): T =
554554
inBracesOrIndented(body, rewriteWithColon)
555555

556-
/** part { `separator` part }
557-
*/
558-
def tokenSeparated[T](separator: Int, part: () => T): List[T] = {
556+
/** part { `,` part }
557+
* @param expectedEnd If set to something other than [[EMPTY]],
558+
* assume this comma separated list must be followed by this token.
559+
* If the parser consumes a `part` that is not followed by a comma or this expected
560+
* token, issue a syntax error and try to recover at the next safe point.
561+
*/
562+
def commaSeparated[T](part: () => T, expectedEnd: Token = EMPTY): List[T] = {
559563
val ts = new ListBuffer[T] += part()
560-
while (in.token == separator) {
564+
while (in.token == COMMA) {
561565
in.nextToken()
562566
ts += part()
563567
}
568+
if (expectedEnd != EMPTY && in.token != expectedEnd) {
569+
// As a side effect, will skip to the nearest safe point, which might be a comma
570+
syntaxErrorOrIncomplete(ExpectedTokenButFound(expectedEnd, in.token))
571+
if (in.token == COMMA) {
572+
ts ++= commaSeparated(part, expectedEnd)
573+
}
574+
}
564575
ts.toList
565576
}
566577

567-
def commaSeparated[T](part: () => T): List[T] = tokenSeparated(COMMA, part)
568-
569578
def inSepRegion[T](f: Region => Region)(op: => T): T =
570579
val cur = in.currentRegion
571580
in.currentRegion = f(cur)
@@ -1509,7 +1518,7 @@ object Parsers {
15091518
/** FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
15101519
*/
15111520
def funParamClause(): List[ValDef] =
1512-
inParens(commaSeparated(() => typedFunParam(in.offset, ident())))
1521+
inParens(commaSeparated(() => typedFunParam(in.offset, ident()), RPAREN))
15131522

15141523
def funParamClauses(): List[List[ValDef]] =
15151524
if in.token == LPAREN then funParamClause() :: funParamClauses() else Nil
@@ -1622,7 +1631,7 @@ object Parsers {
16221631
else
16231632
def singletonArgs(t: Tree): Tree =
16241633
if in.token == LPAREN && in.featureEnabled(Feature.dependent)
1625-
then singletonArgs(AppliedTypeTree(t, inParens(commaSeparated(singleton))))
1634+
then singletonArgs(AppliedTypeTree(t, inParens(commaSeparated(singleton, RPAREN))))
16261635
else t
16271636
singletonArgs(simpleType1())
16281637

@@ -1638,7 +1647,7 @@ object Parsers {
16381647
def simpleType1() = simpleTypeRest {
16391648
if in.token == LPAREN then
16401649
atSpan(in.offset) {
1641-
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true)))
1650+
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true, RPAREN)))
16421651
}
16431652
else if in.token == LBRACE then
16441653
atSpan(in.offset) { RefinedTypeTree(EmptyTree, refinement(indentOK = false)) }
@@ -1722,7 +1731,7 @@ object Parsers {
17221731
* | NamedTypeArg {`,' NamedTypeArg}
17231732
* NamedTypeArg ::= id `=' Type
17241733
*/
1725-
def argTypes(namedOK: Boolean, wildOK: Boolean): List[Tree] = {
1734+
def argTypes(namedOK: Boolean, wildOK: Boolean, expectedEnd: Token): List[Tree] = {
17261735

17271736
def argType() = {
17281737
val t = typ()
@@ -1739,7 +1748,7 @@ object Parsers {
17391748
val rest =
17401749
if (in.token == COMMA) {
17411750
in.nextToken()
1742-
commaSeparated(arg)
1751+
commaSeparated(arg, expectedEnd)
17431752
}
17441753
else Nil
17451754
first :: rest
@@ -1752,7 +1761,7 @@ object Parsers {
17521761
case firstArg =>
17531762
otherArgs(firstArg, () => argType())
17541763
}
1755-
else commaSeparated(() => argType())
1764+
else commaSeparated(() => argType(), expectedEnd)
17561765
}
17571766

17581767
/** FunArgType ::= Type | `=>' Type
@@ -1781,7 +1790,7 @@ object Parsers {
17811790
/** TypeArgs ::= `[' Type {`,' Type} `]'
17821791
* NamedTypeArgs ::= `[' NamedTypeArg {`,' NamedTypeArg} `]'
17831792
*/
1784-
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK))
1793+
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK, RBRACKET))
17851794

17861795
/** Refinement ::= `{' RefineStatSeq `}'
17871796
*/
@@ -2145,7 +2154,7 @@ object Parsers {
21452154
var mods1 = mods
21462155
if isErased then mods1 = addModifier(mods1)
21472156
try
2148-
commaSeparated(() => binding(mods1))
2157+
commaSeparated(() => binding(mods1), RPAREN)
21492158
finally
21502159
accept(RPAREN)
21512160
else {
@@ -2376,7 +2385,7 @@ object Parsers {
23762385
/** ExprsInParens ::= ExprInParens {`,' ExprInParens}
23772386
*/
23782387
def exprsInParensOpt(): List[Tree] =
2379-
if (in.token == RPAREN) Nil else commaSeparated(exprInParens)
2388+
if (in.token == RPAREN) Nil else commaSeparated(exprInParens, RPAREN)
23802389

23812390
/** ParArgumentExprs ::= `(' [‘using’] [ExprsInParens] `)'
23822391
* | `(' [ExprsInParens `,'] PostfixExpr `*' ')'
@@ -2386,9 +2395,9 @@ object Parsers {
23862395
(Nil, false)
23872396
else if isIdent(nme.using) then
23882397
in.nextToken()
2389-
(commaSeparated(argumentExpr), true)
2398+
(commaSeparated(argumentExpr, RPAREN), true)
23902399
else
2391-
(commaSeparated(argumentExpr), false)
2400+
(commaSeparated(argumentExpr, RPAREN), false)
23922401
}
23932402

23942403
/** ArgumentExprs ::= ParArgumentExprs
@@ -2532,7 +2541,7 @@ object Parsers {
25322541
if (leading == LBRACE || in.token == CASE)
25332542
enumerators()
25342543
else {
2535-
val pats = patternsOpt()
2544+
val pats = patternsOpt(EMPTY)
25362545
val pat =
25372546
if (in.token == RPAREN || pats.length > 1) {
25382547
wrappedEnums = false
@@ -2724,7 +2733,7 @@ object Parsers {
27242733
case USCORE =>
27252734
wildcardIdent()
27262735
case LPAREN =>
2727-
atSpan(in.offset) { makeTupleOrParens(inParens(patternsOpt())) }
2736+
atSpan(in.offset) { makeTupleOrParens(inParens(patternsOpt(RPAREN))) }
27282737
case QUOTE =>
27292738
simpleExpr(Location.InPattern)
27302739
case XMLSTART =>
@@ -2759,17 +2768,17 @@ object Parsers {
27592768

27602769
/** Patterns ::= Pattern [`,' Pattern]
27612770
*/
2762-
def patterns(location: Location = Location.InPattern): List[Tree] =
2763-
commaSeparated(() => pattern(location))
2771+
def patterns(expectedEnd: Token = EMPTY, location: Location = Location.InPattern): List[Tree] =
2772+
commaSeparated(() => pattern(location), expectedEnd)
27642773

2765-
def patternsOpt(location: Location = Location.InPattern): List[Tree] =
2766-
if (in.token == RPAREN) Nil else patterns(location)
2774+
def patternsOpt(expectedEnd: Token, location: Location = Location.InPattern): List[Tree] =
2775+
if (in.token == RPAREN) Nil else patterns(expectedEnd, location)
27672776

27682777
/** ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
27692778
* | ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
27702779
*/
27712780
def argumentPatterns(): List[Tree] =
2772-
inParens(patternsOpt(Location.InPatternArgs))
2781+
inParens(patternsOpt(RPAREN, Location.InPatternArgs))
27732782

27742783
/* -------- MODIFIERS and ANNOTATIONS ------------------------------------------- */
27752784

@@ -2950,7 +2959,7 @@ object Parsers {
29502959
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
29512960
}
29522961
}
2953-
commaSeparated(() => typeParam())
2962+
commaSeparated(() => typeParam(), RBRACKET)
29542963
}
29552964

29562965
def typeParamClauseOpt(ownerKind: ParamOwner.Value): List[TypeDef] =
@@ -2959,7 +2968,7 @@ object Parsers {
29592968
/** ContextTypes ::= FunArgType {‘,’ FunArgType}
29602969
*/
29612970
def contextTypes(ofClass: Boolean, nparams: Int, impliedMods: Modifiers): List[ValDef] =
2962-
val tps = commaSeparated(funArgType)
2971+
val tps = commaSeparated(funArgType, RPAREN)
29632972
var counter = nparams
29642973
def nextIdx = { counter += 1; counter }
29652974
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param
@@ -3063,7 +3072,7 @@ object Parsers {
30633072
!impliedMods.is(Given)
30643073
|| startParamTokens.contains(in.token)
30653074
|| isIdent && (in.name == nme.inline || in.lookahead.isColon())
3066-
if isParams then commaSeparated(() => param())
3075+
if isParams then commaSeparated(() => param(), RPAREN)
30673076
else contextTypes(ofClass, nparams, impliedMods)
30683077
checkVarArgsRules(clause)
30693078
clause
@@ -3755,7 +3764,7 @@ object Parsers {
37553764
val derived =
37563765
if (isIdent(nme.derives)) {
37573766
in.nextToken()
3758-
tokenSeparated(COMMA, () => convertToTypeId(qualId()))
3767+
commaSeparated(() => convertToTypeId(qualId()))
37593768
}
37603769
else Nil
37613770
possibleTemplateStart()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:3:21 ----------------------------------------------------
2+
3 | def foo(x: Int = 5 6, y Int = 7, z: Int 5, x = 5): Unit = () // error // error // error // error
3+
| ^
4+
| ')' expected, but integer literal found
5+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:3:26 ----------------------------------------------------
6+
3 | def foo(x: Int = 5 6, y Int = 7, z: Int 5, x = 5): Unit = () // error // error // error // error
7+
| ^^^
8+
| ':' expected, but identifier found
9+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:3:42 ----------------------------------------------------
10+
3 | def foo(x: Int = 5 6, y Int = 7, z: Int 5, x = 5): Unit = () // error // error // error // error
11+
| ^
12+
| ')' expected, but integer literal found
13+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:3:47 ----------------------------------------------------
14+
3 | def foo(x: Int = 5 6, y Int = 7, z: Int 5, x = 5): Unit = () // error // error // error // error
15+
| ^
16+
| ':' expected, but '=' found
17+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:11:16 ---------------------------------------------------
18+
11 | case Plus(4 1) => // error
19+
| ^
20+
| ')' expected, but integer literal found
21+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:12:16 ---------------------------------------------------
22+
12 | case Plus(4 5 6 7, 1, 2 3) => // error // error
23+
| ^
24+
| ')' expected, but integer literal found
25+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:12:28 ---------------------------------------------------
26+
12 | case Plus(4 5 6 7, 1, 2 3) => // error // error
27+
| ^
28+
| ')' expected, but integer literal found
29+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:14:12 ---------------------------------------------------
30+
14 | val x: A[T=Int, T=Int] = ??? // error // error
31+
| ^
32+
| ']' expected, but '=' found
33+
-- [E040] Syntax Error: tests/neg/comma-separated-errors.scala:14:19 ---------------------------------------------------
34+
14 | val x: A[T=Int, T=Int] = ??? // error // error
35+
| ^
36+
| ']' expected, but '=' found
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class A[T]
2+
object o {
3+
def foo(x: Int = 5 6, y Int = 7, z: Int 5, x = 5): Unit = () // error // error // error // error
4+
5+
case class Plus(a: Int, b: Int)
6+
7+
object Plus {
8+
def unapply(r: Int): Plus = Plus(r - 1, 1)
9+
}
10+
5 match {
11+
case Plus(4 1) => // error
12+
case Plus(4 5 6 7, 1, 2 3) => // error // error
13+
}
14+
val x: A[T=Int, T=Int] = ??? // error // error
15+
}

tests/neg/i1679.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class A[T]
22
object o {
33
// Testing compiler crash, this test should be modified when named type argument are completely implemented
4-
val x: A[T=Int, T=Int] = ??? // error: ']' expected, but '=' found // error
4+
val x: A[T=Int, T=Int] = ??? // error: ']' expected, but '=' found // error: ']' expected, but '=' found
55
}

0 commit comments

Comments
 (0)