Skip to content

Commit 317b08c

Browse files
committed
Better error recovery in comma-separated lists
1 parent 3d06d94 commit 317b08c

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
@@ -562,19 +562,28 @@ object Parsers {
562562
def inDefScopeBraces[T](body: => T, rewriteWithColon: Boolean = false): T =
563563
inBracesOrIndented(body, rewriteWithColon)
564564

565-
/** part { `separator` part }
566-
*/
567-
def tokenSeparated[T](separator: Int, part: () => T): List[T] = {
565+
/** part { `,` part }
566+
* @param expectedEnd If set to something other than [[EMPTY]],
567+
* assume this comma separated list must be followed by this token.
568+
* If the parser consumes a `part` that is not followed by a comma or this expected
569+
* token, issue a syntax error and try to recover at the next safe point.
570+
*/
571+
def commaSeparated[T](part: () => T, expectedEnd: Token = EMPTY): List[T] = {
568572
val ts = new ListBuffer[T] += part()
569-
while (in.token == separator) {
573+
while (in.token == COMMA) {
570574
in.nextToken()
571575
ts += part()
572576
}
577+
if (expectedEnd != EMPTY && in.token != expectedEnd) {
578+
// As a side effect, will skip to the nearest safe point, which might be a comma
579+
syntaxErrorOrIncomplete(ExpectedTokenButFound(expectedEnd, in.token))
580+
if (in.token == COMMA) {
581+
ts ++= commaSeparated(part, expectedEnd)
582+
}
583+
}
573584
ts.toList
574585
}
575586

576-
def commaSeparated[T](part: () => T): List[T] = tokenSeparated(COMMA, part)
577-
578587
def inSepRegion[T](f: Region => Region)(op: => T): T =
579588
val cur = in.currentRegion
580589
in.currentRegion = f(cur)
@@ -1519,7 +1528,7 @@ object Parsers {
15191528
/** FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
15201529
*/
15211530
def funParamClause(): List[ValDef] =
1522-
inParens(commaSeparated(() => typedFunParam(in.offset, ident())))
1531+
inParens(commaSeparated(() => typedFunParam(in.offset, ident()), RPAREN))
15231532

15241533
def funParamClauses(): List[List[ValDef]] =
15251534
if in.token == LPAREN then funParamClause() :: funParamClauses() else Nil
@@ -1631,7 +1640,7 @@ object Parsers {
16311640
else
16321641
def singletonArgs(t: Tree): Tree =
16331642
if in.token == LPAREN && in.featureEnabled(Feature.dependent)
1634-
then singletonArgs(AppliedTypeTree(t, inParens(commaSeparated(singleton))))
1643+
then singletonArgs(AppliedTypeTree(t, inParens(commaSeparated(singleton, RPAREN))))
16351644
else t
16361645
singletonArgs(simpleType1())
16371646

@@ -1647,7 +1656,7 @@ object Parsers {
16471656
def simpleType1() = simpleTypeRest {
16481657
if in.token == LPAREN then
16491658
atSpan(in.offset) {
1650-
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true)))
1659+
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true, RPAREN)))
16511660
}
16521661
else if in.token == LBRACE then
16531662
atSpan(in.offset) { RefinedTypeTree(EmptyTree, refinement(indentOK = false)) }
@@ -1731,7 +1740,7 @@ object Parsers {
17311740
* | NamedTypeArg {`,' NamedTypeArg}
17321741
* NamedTypeArg ::= id `=' Type
17331742
*/
1734-
def argTypes(namedOK: Boolean, wildOK: Boolean): List[Tree] = {
1743+
def argTypes(namedOK: Boolean, wildOK: Boolean, expectedEnd: Token): List[Tree] = {
17351744

17361745
def argType() = {
17371746
val t = typ()
@@ -1748,7 +1757,7 @@ object Parsers {
17481757
val rest =
17491758
if (in.token == COMMA) {
17501759
in.nextToken()
1751-
commaSeparated(arg)
1760+
commaSeparated(arg, expectedEnd)
17521761
}
17531762
else Nil
17541763
first :: rest
@@ -1761,7 +1770,7 @@ object Parsers {
17611770
case firstArg =>
17621771
otherArgs(firstArg, () => argType())
17631772
}
1764-
else commaSeparated(() => argType())
1773+
else commaSeparated(() => argType(), expectedEnd)
17651774
}
17661775

17671776
/** FunArgType ::= Type | `=>' Type
@@ -1790,7 +1799,7 @@ object Parsers {
17901799
/** TypeArgs ::= `[' Type {`,' Type} `]'
17911800
* NamedTypeArgs ::= `[' NamedTypeArg {`,' NamedTypeArg} `]'
17921801
*/
1793-
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK))
1802+
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK, RBRACKET))
17941803

17951804
/** Refinement ::= `{' RefineStatSeq `}'
17961805
*/
@@ -2154,7 +2163,7 @@ object Parsers {
21542163
var mods1 = mods
21552164
if isErased then mods1 = addModifier(mods1)
21562165
try
2157-
commaSeparated(() => binding(mods1))
2166+
commaSeparated(() => binding(mods1), RPAREN)
21582167
finally
21592168
accept(RPAREN)
21602169
else {
@@ -2384,7 +2393,7 @@ object Parsers {
23842393
/** ExprsInParens ::= ExprInParens {`,' ExprInParens}
23852394
*/
23862395
def exprsInParensOpt(): List[Tree] =
2387-
if (in.token == RPAREN) Nil else commaSeparated(exprInParens)
2396+
if (in.token == RPAREN) Nil else commaSeparated(exprInParens, RPAREN)
23882397

23892398
/** ParArgumentExprs ::= `(' [‘using’] [ExprsInParens] `)'
23902399
* | `(' [ExprsInParens `,'] PostfixExpr `*' ')'
@@ -2394,9 +2403,9 @@ object Parsers {
23942403
(Nil, false)
23952404
else if isIdent(nme.using) then
23962405
in.nextToken()
2397-
(commaSeparated(argumentExpr), true)
2406+
(commaSeparated(argumentExpr, RPAREN), true)
23982407
else
2399-
(commaSeparated(argumentExpr), false)
2408+
(commaSeparated(argumentExpr, RPAREN), false)
24002409
}
24012410

24022411
/** ArgumentExprs ::= ParArgumentExprs
@@ -2540,7 +2549,7 @@ object Parsers {
25402549
if (leading == LBRACE || in.token == CASE)
25412550
enumerators()
25422551
else {
2543-
val pats = patternsOpt()
2552+
val pats = patternsOpt(EMPTY)
25442553
val pat =
25452554
if (in.token == RPAREN || pats.length > 1) {
25462555
wrappedEnums = false
@@ -2732,7 +2741,7 @@ object Parsers {
27322741
case USCORE =>
27332742
wildcardIdent()
27342743
case LPAREN =>
2735-
atSpan(in.offset) { makeTupleOrParens(inParens(patternsOpt())) }
2744+
atSpan(in.offset) { makeTupleOrParens(inParens(patternsOpt(RPAREN))) }
27362745
case QUOTE =>
27372746
simpleExpr(Location.InPattern)
27382747
case XMLSTART =>
@@ -2768,17 +2777,17 @@ object Parsers {
27682777

27692778
/** Patterns ::= Pattern [`,' Pattern]
27702779
*/
2771-
def patterns(location: Location = Location.InPattern): List[Tree] =
2772-
commaSeparated(() => pattern(location))
2780+
def patterns(expectedEnd: Token = EMPTY, location: Location = Location.InPattern): List[Tree] =
2781+
commaSeparated(() => pattern(location), expectedEnd)
27732782

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

27772786
/** ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
27782787
* | ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
27792788
*/
27802789
def argumentPatterns(): List[Tree] =
2781-
inParens(patternsOpt(Location.InPatternArgs))
2790+
inParens(patternsOpt(RPAREN, Location.InPatternArgs))
27822791

27832792
/* -------- MODIFIERS and ANNOTATIONS ------------------------------------------- */
27842793

@@ -2959,7 +2968,7 @@ object Parsers {
29592968
TypeDef(name, lambdaAbstract(hkparams, bounds)).withMods(mods)
29602969
}
29612970
}
2962-
commaSeparated(() => typeParam())
2971+
commaSeparated(() => typeParam(), RBRACKET)
29632972
}
29642973

29652974
def typeParamClauseOpt(ownerKind: ParamOwner): List[TypeDef] =
@@ -2968,7 +2977,7 @@ object Parsers {
29682977
/** ContextTypes ::= FunArgType {‘,’ FunArgType}
29692978
*/
29702979
def contextTypes(ofClass: Boolean, nparams: Int, impliedMods: Modifiers): List[ValDef] =
2971-
val tps = commaSeparated(funArgType)
2980+
val tps = commaSeparated(funArgType, RPAREN)
29722981
var counter = nparams
29732982
def nextIdx = { counter += 1; counter }
29742983
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param
@@ -3072,7 +3081,7 @@ object Parsers {
30723081
!impliedMods.is(Given)
30733082
|| startParamTokens.contains(in.token)
30743083
|| isIdent && (in.name == nme.inline || in.lookahead.isColon())
3075-
if isParams then commaSeparated(() => param())
3084+
if isParams then commaSeparated(() => param(), RPAREN)
30763085
else contextTypes(ofClass, nparams, impliedMods)
30773086
checkVarArgsRules(clause)
30783087
clause
@@ -3766,7 +3775,7 @@ object Parsers {
37663775
val derived =
37673776
if (isIdent(nme.derives)) {
37683777
in.nextToken()
3769-
tokenSeparated(COMMA, () => convertToTypeId(qualId()))
3778+
commaSeparated(() => convertToTypeId(qualId()))
37703779
}
37713780
else Nil
37723781
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)