Skip to content

Commit

Permalink
Backport "Fix #17187: allow patches with same span" (#17476)
Browse files Browse the repository at this point in the history
Backports #17366
  • Loading branch information
Kordyjan authored May 12, 2023
2 parents 4f2e819 + c1028a2 commit 6629e13
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 9 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 @@ -704,7 +704,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 @@ -1411,9 +1415,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: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/util/Spans.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ object Spans {
|| containsInner(this, that.end)
|| containsInner(that, this.start)
|| containsInner(that, this.end)
|| this.start == that.start && this.end == that.end // exact match in one point
)
}

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 @@ -83,6 +83,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 6629e13

Please sign in to comment.