Skip to content

Commit 2215ff8

Browse files
committed
Fix #9992: Reset toplevel indent region width on left indent
If we start with a toplevel region with greater than 0 indent, and we find a following line with less indentation, reset the current indentation width to that smaller number.
1 parent 93f24a0 commit 2215ff8

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,16 @@ object Scanners {
470470
else if indentIsSignificant then
471471
if nextWidth < lastWidth
472472
|| nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH) && token != CASE then
473-
if !currentRegion.isOutermost &&
474-
!isLeadingInfixOperator() &&
475-
!statCtdTokens.contains(lastToken) then
473+
if currentRegion.isOutermost then
474+
if nextWidth < lastWidth then currentRegion = topLevelRegion(nextWidth)
475+
// report.error("Line is indented too far to the left", sourcePos())
476+
else if !isLeadingInfixOperator() && !statCtdTokens.contains(lastToken) then
476477
currentRegion match
477478
case r: Indented =>
478479
currentRegion = r.enclosing
479480
insert(OUTDENT, offset)
480481
case r: InBraces if !closingRegionTokens.contains(token) =>
481-
report.warning("Line is indented too far to the left, or a `}` is missing",
482-
source.atSpan(Span(offset)))
482+
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
483483
case _ =>
484484

485485
else if lastWidth < nextWidth
@@ -1316,7 +1316,7 @@ object Scanners {
13161316
/* Initialization: read first char, then first token */
13171317
nextChar()
13181318
nextToken()
1319-
currentRegion = Indented(indentWidth(offset), Set(), EMPTY, null)
1319+
currentRegion = topLevelRegion(indentWidth(offset))
13201320
}
13211321
// end Scanner
13221322

@@ -1357,6 +1357,8 @@ object Scanners {
13571357
case class Indented(width: IndentWidth, others: Set[IndentWidth], prefix: Token, outer: Region | Null) extends Region:
13581358
knownWidth = width
13591359

1360+
def topLevelRegion(width: IndentWidth) = Indented(width, Set(), EMPTY, null)
1361+
13601362
enum IndentWidth {
13611363
case Run(ch: Char, n: Int)
13621364
case Conc(l: IndentWidth, r: Run)

tests/pos/i9992.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.concurrent._
2+
3+
@main def test(): Unit =
4+
def test = ()
5+
test

0 commit comments

Comments
 (0)