Skip to content

Commit 5e9d681

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

File tree

4 files changed

+88
-29
lines changed

4 files changed

+88
-29
lines changed

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -553,19 +553,27 @@ 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+
syntaxErrorOrIncomplete(ExpectedTokenButFound(expectedEnd, in.token))
570+
if (in.token == COMMA) {
571+
ts ++= commaSeparated(part, expectedEnd)
572+
}
573+
}
564574
ts.toList
565575
}
566576

567-
def commaSeparated[T](part: () => T): List[T] = tokenSeparated(COMMA, part)
568-
569577
def inSepRegion[T](f: Region => Region)(op: => T): T =
570578
val cur = in.currentRegion
571579
in.currentRegion = f(cur)
@@ -1509,7 +1517,7 @@ object Parsers {
15091517
/** FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
15101518
*/
15111519
def funParamClause(): List[ValDef] =
1512-
inParens(commaSeparated(() => typedFunParam(in.offset, ident())))
1520+
inParens(commaSeparated(() => typedFunParam(in.offset, ident()), RPAREN))
15131521

15141522
def funParamClauses(): List[List[ValDef]] =
15151523
if in.token == LPAREN then funParamClause() :: funParamClauses() else Nil
@@ -1622,7 +1630,7 @@ object Parsers {
16221630
else
16231631
def singletonArgs(t: Tree): Tree =
16241632
if in.token == LPAREN && in.featureEnabled(Feature.dependent)
1625-
then singletonArgs(AppliedTypeTree(t, inParens(commaSeparated(singleton))))
1633+
then singletonArgs(AppliedTypeTree(t, inParens(commaSeparated(singleton, RPAREN))))
16261634
else t
16271635
singletonArgs(simpleType1())
16281636

@@ -1638,7 +1646,7 @@ object Parsers {
16381646
def simpleType1() = simpleTypeRest {
16391647
if in.token == LPAREN then
16401648
atSpan(in.offset) {
1641-
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true)))
1649+
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true, RPAREN)))
16421650
}
16431651
else if in.token == LBRACE then
16441652
atSpan(in.offset) { RefinedTypeTree(EmptyTree, refinement(indentOK = false)) }
@@ -1722,7 +1730,7 @@ object Parsers {
17221730
* | NamedTypeArg {`,' NamedTypeArg}
17231731
* NamedTypeArg ::= id `=' Type
17241732
*/
1725-
def argTypes(namedOK: Boolean, wildOK: Boolean): List[Tree] = {
1733+
def argTypes(namedOK: Boolean, wildOK: Boolean, expectedEnd: Token): List[Tree] = {
17261734

17271735
def argType() = {
17281736
val t = typ()
@@ -1739,7 +1747,7 @@ object Parsers {
17391747
val rest =
17401748
if (in.token == COMMA) {
17411749
in.nextToken()
1742-
commaSeparated(arg)
1750+
commaSeparated(arg, expectedEnd)
17431751
}
17441752
else Nil
17451753
first :: rest
@@ -1752,7 +1760,7 @@ object Parsers {
17521760
case firstArg =>
17531761
otherArgs(firstArg, () => argType())
17541762
}
1755-
else commaSeparated(() => argType())
1763+
else commaSeparated(() => argType(), expectedEnd)
17561764
}
17571765

17581766
/** FunArgType ::= Type | `=>' Type
@@ -1781,7 +1789,7 @@ object Parsers {
17811789
/** TypeArgs ::= `[' Type {`,' Type} `]'
17821790
* NamedTypeArgs ::= `[' NamedTypeArg {`,' NamedTypeArg} `]'
17831791
*/
1784-
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK))
1792+
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK, RBRACKET))
17851793

17861794
/** Refinement ::= `{' RefineStatSeq `}'
17871795
*/
@@ -2145,7 +2153,7 @@ object Parsers {
21452153
var mods1 = mods
21462154
if isErased then mods1 = addModifier(mods1)
21472155
try
2148-
commaSeparated(() => binding(mods1))
2156+
commaSeparated(() => binding(mods1), RPAREN)
21492157
finally
21502158
accept(RPAREN)
21512159
else {
@@ -2376,7 +2384,7 @@ object Parsers {
23762384
/** ExprsInParens ::= ExprInParens {`,' ExprInParens}
23772385
*/
23782386
def exprsInParensOpt(): List[Tree] =
2379-
if (in.token == RPAREN) Nil else commaSeparated(exprInParens)
2387+
if (in.token == RPAREN) Nil else commaSeparated(exprInParens, RPAREN)
23802388

23812389
/** ParArgumentExprs ::= `(' [‘using’] [ExprsInParens] `)'
23822390
* | `(' [ExprsInParens `,'] PostfixExpr `*' ')'
@@ -2386,9 +2394,9 @@ object Parsers {
23862394
(Nil, false)
23872395
else if isIdent(nme.using) then
23882396
in.nextToken()
2389-
(commaSeparated(argumentExpr), true)
2397+
(commaSeparated(argumentExpr, RPAREN), true)
23902398
else
2391-
(commaSeparated(argumentExpr), false)
2399+
(commaSeparated(argumentExpr, RPAREN), false)
23922400
}
23932401

23942402
/** ArgumentExprs ::= ParArgumentExprs
@@ -2532,7 +2540,7 @@ object Parsers {
25322540
if (leading == LBRACE || in.token == CASE)
25332541
enumerators()
25342542
else {
2535-
val pats = patternsOpt()
2543+
val pats = patternsOpt(EMPTY)
25362544
val pat =
25372545
if (in.token == RPAREN || pats.length > 1) {
25382546
wrappedEnums = false
@@ -2724,7 +2732,7 @@ object Parsers {
27242732
case USCORE =>
27252733
wildcardIdent()
27262734
case LPAREN =>
2727-
atSpan(in.offset) { makeTupleOrParens(inParens(patternsOpt())) }
2735+
atSpan(in.offset) { makeTupleOrParens(inParens(patternsOpt(RPAREN))) }
27282736
case QUOTE =>
27292737
simpleExpr(Location.InPattern)
27302738
case XMLSTART =>
@@ -2759,17 +2767,17 @@ object Parsers {
27592767

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

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

27682776
/** ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
27692777
* | ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
27702778
*/
27712779
def argumentPatterns(): List[Tree] =
2772-
inParens(patternsOpt(Location.InPatternArgs))
2780+
inParens(patternsOpt(RPAREN, Location.InPatternArgs))
27732781

27742782
/* -------- MODIFIERS and ANNOTATIONS ------------------------------------------- */
27752783

@@ -2950,7 +2958,7 @@ object Parsers {
29502958
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
29512959
}
29522960
}
2953-
commaSeparated(() => typeParam())
2961+
commaSeparated(() => typeParam(), RBRACKET)
29542962
}
29552963

29562964
def typeParamClauseOpt(ownerKind: ParamOwner.Value): List[TypeDef] =
@@ -2959,7 +2967,7 @@ object Parsers {
29592967
/** ContextTypes ::= FunArgType {‘,’ FunArgType}
29602968
*/
29612969
def contextTypes(ofClass: Boolean, nparams: Int, impliedMods: Modifiers): List[ValDef] =
2962-
val tps = commaSeparated(funArgType)
2970+
val tps = commaSeparated(funArgType, RPAREN)
29632971
var counter = nparams
29642972
def nextIdx = { counter += 1; counter }
29652973
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param
@@ -3063,7 +3071,7 @@ object Parsers {
30633071
!impliedMods.is(Given)
30643072
|| startParamTokens.contains(in.token)
30653073
|| isIdent && (in.name == nme.inline || in.lookahead.isColon())
3066-
if isParams then commaSeparated(() => param())
3074+
if isParams then commaSeparated(() => param(), RPAREN)
30673075
else contextTypes(ofClass, nparams, impliedMods)
30683076
checkVarArgsRules(clause)
30693077
clause
@@ -3755,7 +3763,7 @@ object Parsers {
37553763
val derived =
37563764
if (isIdent(nme.derives)) {
37573765
in.nextToken()
3758-
tokenSeparated(COMMA, () => convertToTypeId(qualId()))
3766+
commaSeparated(() => convertToTypeId(qualId()))
37593767
}
37603768
else Nil
37613769
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)