Skip to content

Fix #9992: Reset toplevel indent region width on left indent #10328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,15 @@ object Scanners {
else if indentIsSignificant then
if nextWidth < lastWidth
|| nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH) && token != CASE then
if !currentRegion.isOutermost &&
!isLeadingInfixOperator() &&
!statCtdTokens.contains(lastToken) then
if currentRegion.isOutermost then
if nextWidth < lastWidth then currentRegion = topLevelRegion(nextWidth)
else if !isLeadingInfixOperator() && !statCtdTokens.contains(lastToken) then
currentRegion match
case r: Indented =>
currentRegion = r.enclosing
insert(OUTDENT, offset)
case r: InBraces if !closingRegionTokens.contains(token) =>
report.warning("Line is indented too far to the left, or a `}` is missing",
source.atSpan(Span(offset)))
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
case _ =>

else if lastWidth < nextWidth
Expand Down Expand Up @@ -1316,7 +1315,7 @@ object Scanners {
/* Initialization: read first char, then first token */
nextChar()
nextToken()
currentRegion = Indented(indentWidth(offset), Set(), EMPTY, null)
currentRegion = topLevelRegion(indentWidth(offset))
}
// end Scanner

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

def topLevelRegion(width: IndentWidth) = Indented(width, Set(), EMPTY, null)

enum IndentWidth {
case Run(ch: Char, n: Int)
case Conc(l: IndentWidth, r: Run)
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i9992.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.concurrent._

@main def test(): Unit =
def test = ()
test
25 changes: 25 additions & 0 deletions tests/pos/opaques-queue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Elem
trait QueueSignature:
type Queue
def empty: Queue
def append(q: Queue, e: Elem): Queue
def pop(q: Queue): Option[(Elem, Queue)]
val QueueModule: QueueSignature =
object QueueImpl extends QueueSignature:
type Queue = (List[Elem], List[Elem])
def empty = (Nil, Nil)
def append(q: Queue, e: Elem): Queue = (q._1, e :: q._2)
def pop(q: Queue): Option[(Elem, Queue)] = q match
case (Nil, Nil) => None
case (x :: xs, ys) => Some((x, (xs, ys)))
case (Nil, ys) => pop((ys.reverse, Nil))
QueueImpl

object queues:
opaque type Queue = (List[Elem], List[Elem])
def empty = (Nil, Nil)
def append(q: Queue, e: Elem): Queue = (q._1, e :: q._2)
def pop(q: Queue): Option[(Elem, Queue)] = q match
case (Nil, Nil) => None
case (x :: xs, ys) => Some((x, (xs, ys)))
case (Nil, ys) => pop((ys.reverse, Nil))