Skip to content

Commit f001b6f

Browse files
committed
Enforce indentation off-side rule
Ref lampepfl/dotty 10671 Currently class A: val x = 1 val y = 2 is admitted. This becomes confusing since this looseness ends up not catching things like class Test: test("hello") assert(1 == 1) where `assert(1 == 1)` is actually not passed into `test(...)(...)`. Following the convention of indentation language like Python, we should reject superfluous indentation, which can be defined as having two or more indentation widths within a region.
1 parent 830d1b8 commit f001b6f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,16 @@ object Scanners {
496496
currentRegion match {
497497
case Indented(curWidth, others, prefix, outer)
498498
if curWidth < nextWidth && !others.contains(nextWidth) && nextWidth != lastWidth =>
499-
if (token == OUTDENT)
499+
if token == OUTDENT then
500500
errorButContinue(
501501
i"""The start of this line does not match any of the previous indentation widths.
502502
|Indentation width of current line : $nextWidth
503503
|This falls between previous widths: $curWidth and $lastWidth""")
504+
else if others.nonEmpty then
505+
errorButContinue(
506+
i"""Unexpected indentation:
507+
|Indentation width of current line: $nextWidth
508+
|Expected indentation : ${others.head}""")
504509
else
505510
currentRegion = Indented(curWidth, others + nextWidth, prefix, outer)
506511
case _ =>

0 commit comments

Comments
 (0)