Skip to content

Commit df27dcd

Browse files
authoredNov 17, 2020
Merge pull request #10328 from dotty-staging/fix-#9992
Fix #9992: Reset toplevel indent region width on left indent
2 parents 3c02d0c + 956d0c1 commit df27dcd

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed
 

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,15 @@ 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+
else if !isLeadingInfixOperator() && !statCtdTokens.contains(lastToken) then
476476
currentRegion match
477477
case r: Indented =>
478478
currentRegion = r.enclosing
479479
insert(OUTDENT, offset)
480480
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)))
481+
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
483482
case _ =>
484483

485484
else if lastWidth < nextWidth
@@ -1318,7 +1317,7 @@ object Scanners {
13181317
/* Initialization: read first char, then first token */
13191318
nextChar()
13201319
nextToken()
1321-
currentRegion = Indented(indentWidth(offset), Set(), EMPTY, null)
1320+
currentRegion = topLevelRegion(indentWidth(offset))
13221321
}
13231322
// end Scanner
13241323

@@ -1359,6 +1358,8 @@ object Scanners {
13591358
case class Indented(width: IndentWidth, others: Set[IndentWidth], prefix: Token, outer: Region | Null) extends Region:
13601359
knownWidth = width
13611360

1361+
def topLevelRegion(width: IndentWidth) = Indented(width, Set(), EMPTY, null)
1362+
13621363
enum IndentWidth {
13631364
case Run(ch: Char, n: Int)
13641365
case Conc(l: IndentWidth, r: Run)

‎tests/pos/i9992.scala

+5
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

‎tests/pos/opaques-queue.scala

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Elem
2+
trait QueueSignature:
3+
type Queue
4+
def empty: Queue
5+
def append(q: Queue, e: Elem): Queue
6+
def pop(q: Queue): Option[(Elem, Queue)]
7+
val QueueModule: QueueSignature =
8+
object QueueImpl extends QueueSignature:
9+
type Queue = (List[Elem], List[Elem])
10+
def empty = (Nil, Nil)
11+
def append(q: Queue, e: Elem): Queue = (q._1, e :: q._2)
12+
def pop(q: Queue): Option[(Elem, Queue)] = q match
13+
case (Nil, Nil) => None
14+
case (x :: xs, ys) => Some((x, (xs, ys)))
15+
case (Nil, ys) => pop((ys.reverse, Nil))
16+
QueueImpl
17+
18+
object queues:
19+
opaque type Queue = (List[Elem], List[Elem])
20+
def empty = (Nil, Nil)
21+
def append(q: Queue, e: Elem): Queue = (q._1, e :: q._2)
22+
def pop(q: Queue): Option[(Elem, Queue)] = q match
23+
case (Nil, Nil) => None
24+
case (x :: xs, ys) => Some((x, (xs, ys)))
25+
case (Nil, ys) => pop((ys.reverse, Nil))

0 commit comments

Comments
 (0)