Skip to content

Commit

Permalink
Fix scala#17187: allow patches with same span
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed Apr 28, 2023
1 parent 3e6a261 commit 48a610c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 8 deletions.
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,11 @@ object Parsers {
val t = enclosed(INDENT, body)
if needsBraces(t) then
patch(source, Span(startOpening, endOpening), " {")
patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
val next = in.next
def closedByEndMarker =
next.token == END && (next.offset - next.lineOffset) == indentWidth.toPrefix.size
if closedByEndMarker then patch(source, Span(next.offset), "} // ")
else patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
t
end indentedToBraces

Expand Down Expand Up @@ -1422,9 +1426,6 @@ object Parsers {
val start = in.skipToken()
if stats.isEmpty || !matchesAndSetEnd(stats.last) then
syntaxError(em"misaligned end marker", Span(start, in.lastCharOffset))
else if overlapsPatch(source, Span(start, start)) then
patch(source, Span(start, start), "")
patch(source, Span(start, in.lastCharOffset), s"} // end $endName")
in.token = IDENTIFIER // Leaving it as the original token can confuse newline insertion
in.nextToken()
end checkEndMarker
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ object Rewrites {
private[Rewrites] val pbuf = new mutable.ListBuffer[Patch]()

def addPatch(span: Span, replacement: String): Unit =
pbuf.indexWhere(p => p.span.start == span.start && p.span.end == span.end) match {
case i if i >= 0 => pbuf.update(i, Patch(span, replacement))
case _ => pbuf += Patch(span, replacement)
}
pbuf += Patch(span, replacement)

def apply(cs: Array[Char]): Array[Char] = {
val delta = pbuf.map(_.delta).sum
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class CompilationTests {
compileFile("tests/rewrites/i9632.scala", defaultOptions.and("-indent", "-rewrite")),
compileFile("tests/rewrites/i11895.scala", defaultOptions.and("-indent", "-rewrite")),
compileFile("tests/rewrites/i12340.scala", unindentOptions.and("-rewrite")),
compileFile("tests/rewrites/i17187.scala", unindentOptions.and("-rewrite")),
).checkRewrites()
}

Expand Down
9 changes: 9 additions & 0 deletions tests/rewrites/i12340.check
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ class C {
def f = 42
} // end C

class A {
class B {
class C {
def foo = 42
}

} // end B
}

def f(i: Int) = {
if i < 42 then
println(i)
Expand Down
7 changes: 7 additions & 0 deletions tests/rewrites/i12340.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ class C:
def f = 42
end C

class A:
class B:
class C:
def foo = 42

end B

def f(i: Int) =
if i < 42 then
println(i)
Expand Down
44 changes: 44 additions & 0 deletions tests/rewrites/i17187.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

object A {
object B {
def a = 2
}
}

def m1 = {
def b = {
def c = 2
}
}

def m2 =
if true then {
val x = 3
if (false)
x
else {
val y = 4
y
}
}

def m3 =
try {
val n2 = 21
val n1 = 4
n2 / n1
}
catch {
case _ => 4
}

def m4 = {
val n2 = 21
try {
val n1 = 4
n2 / n1
}
catch {
case _ => 4
}
}
33 changes: 33 additions & 0 deletions tests/rewrites/i17187.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

object A:
object B:
def a = 2

def m1 =
def b =
def c = 2

def m2 =
if true then
val x = 3
if (false)
x
else
val y = 4
y

def m3 =
try
val n2 = 21
val n1 = 4
n2 / n1
catch
case _ => 4

def m4 =
val n2 = 21
try
val n1 = 4
n2 / n1
catch
case _ => 4

0 comments on commit 48a610c

Please sign in to comment.