Skip to content

Commit 176a30f

Browse files
oderskysmarter
authored andcommitted
Fix #2494: avoid infinite loop in parser
1 parent abad0ad commit 176a30f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ object Parsers {
164164
def isDefIntro(allowedMods: BitSet) =
165165
in.token == AT || (allowedMods contains in.token) || (defIntroTokens contains in.token)
166166

167+
def isCaseIntro =
168+
in.token == AT || (modifierTokensOrCase contains in.token)
169+
167170
def isStatSep: Boolean =
168171
in.token == NEWLINE || in.token == NEWLINES || in.token == SEMI
169172

@@ -2244,12 +2247,18 @@ object Parsers {
22442247
Thicket(clsDef :: modDef :: Nil)
22452248
}
22462249

2247-
/** EnumCaseStats = EnumCaseStat {semi EnumCaseStat */
2250+
/** EnumCaseStats = EnumCaseStat {semi EnumCaseStat} */
22482251
def enumCaseStats(): List[DefTree] = {
22492252
val cases = new ListBuffer[DefTree] += enumCaseStat()
2250-
while (in.token != RBRACE && in.token != EOF) {
2253+
var exitOnError = false
2254+
while (!isStatSeqEnd && !exitOnError) {
22512255
acceptStatSep()
2252-
cases += enumCaseStat()
2256+
if (isCaseIntro)
2257+
cases += enumCaseStat()
2258+
else if (!isStatSep) {
2259+
exitOnError = mustStartStat
2260+
syntaxErrorOrIncomplete("illegal start of case")
2261+
}
22532262
}
22542263
cases.toList
22552264
}

tests/neg/i2494.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
enum
2+
object // error // error

0 commit comments

Comments
 (0)