Skip to content

Commit b0883b5

Browse files
authored
Require indent after colon at EOL (#16466)
Require indented definitions after a `:` at the end of a line where an argument is expected. Fixes #16452
2 parents 1c308a5 + 675ae0c commit b0883b5

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ object Parsers {
944944
lookahead.isArrow
945945
&& {
946946
lookahead.nextToken()
947-
lookahead.token == INDENT
947+
lookahead.token == INDENT || lookahead.token == EOF
948948
}
949949
lookahead.nextToken()
950950
if lookahead.isIdent || lookahead.token == USCORE then
@@ -1340,11 +1340,16 @@ object Parsers {
13401340
// note: next is defined here because current == NEWLINE
13411341
if (in.token == NEWLINE && p(in.next.token)) newLineOpt()
13421342

1343-
def colonAtEOLOpt(): Unit = {
1343+
def acceptIndent() =
1344+
if in.token != INDENT then
1345+
syntaxErrorOrIncomplete(em"indented definitions expected, ${in} found")
1346+
1347+
def colonAtEOLOpt(): Unit =
13441348
possibleColonOffset = in.lastOffset
13451349
in.observeColonEOL(inTemplate = false)
1346-
if in.token == COLONeol then in.nextToken()
1347-
}
1350+
if in.token == COLONeol then
1351+
in.nextToken()
1352+
acceptIndent()
13481353

13491354
def argumentStart(): Unit =
13501355
colonAtEOLOpt()
@@ -1364,8 +1369,7 @@ object Parsers {
13641369
if in.lookahead.token == END then in.token = NEWLINE
13651370
else
13661371
in.nextToken()
1367-
if in.token != INDENT && in.token != LBRACE then
1368-
syntaxErrorOrIncomplete(em"indented definitions expected, ${in} found")
1372+
if in.token != LBRACE then acceptIndent()
13691373
else
13701374
newLineOptWhenFollowedBy(LBRACE)
13711375

tests/neg/i16452.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i16452.scala:2:8 -----------------------------------------------------------------------------------
2+
2 |// error
3+
| ^
4+
| indented definitions expected, eof found

tests/neg/i16452.scala

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val x = Seq(1, 2, 3).map:
2+
// error

0 commit comments

Comments
 (0)