Skip to content

Commit 6629e13

Browse files
authored
Backport "Fix #17187: allow patches with same span" (#17476)
Backports #17366
2 parents 4f2e819 + c1028a2 commit 6629e13

File tree

8 files changed

+100
-9
lines changed

8 files changed

+100
-9
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,11 @@ object Parsers {
704704
val t = enclosed(INDENT, body)
705705
if needsBraces(t) then
706706
patch(source, Span(startOpening, endOpening), " {")
707-
patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
707+
val next = in.next
708+
def closedByEndMarker =
709+
next.token == END && (next.offset - next.lineOffset) == indentWidth.toPrefix.size
710+
if closedByEndMarker then patch(source, Span(next.offset), "} // ")
711+
else patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
708712
t
709713
end indentedToBraces
710714

@@ -1411,9 +1415,6 @@ object Parsers {
14111415
val start = in.skipToken()
14121416
if stats.isEmpty || !matchesAndSetEnd(stats.last) then
14131417
syntaxError(em"misaligned end marker", Span(start, in.lastCharOffset))
1414-
else if overlapsPatch(source, Span(start, start)) then
1415-
patch(source, Span(start, start), "")
1416-
patch(source, Span(start, in.lastCharOffset), s"} // end $endName")
14171418
in.token = IDENTIFIER // Leaving it as the original token can confuse newline insertion
14181419
in.nextToken()
14191420
end checkEndMarker

compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ object Rewrites {
2323
private[Rewrites] val pbuf = new mutable.ListBuffer[Patch]()
2424

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

3128
def apply(cs: Array[Char]): Array[Char] = {
3229
val delta = pbuf.map(_.delta).sum

compiler/src/dotty/tools/dotc/util/Spans.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ object Spans {
8686
|| containsInner(this, that.end)
8787
|| containsInner(that, this.start)
8888
|| containsInner(that, this.end)
89-
|| this.start == that.start && this.end == that.end // exact match in one point
9089
)
9190
}
9291

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class CompilationTests {
8383
compileFile("tests/rewrites/i9632.scala", defaultOptions.and("-indent", "-rewrite")),
8484
compileFile("tests/rewrites/i11895.scala", defaultOptions.and("-indent", "-rewrite")),
8585
compileFile("tests/rewrites/i12340.scala", unindentOptions.and("-rewrite")),
86+
compileFile("tests/rewrites/i17187.scala", unindentOptions.and("-rewrite")),
8687
).checkRewrites()
8788
}
8889

tests/rewrites/i12340.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ class C {
33
def f = 42
44
} // end C
55

6+
class A {
7+
class B {
8+
class C {
9+
def foo = 42
10+
}
11+
12+
} // end B
13+
}
14+
615
def f(i: Int) = {
716
if i < 42 then
817
println(i)

tests/rewrites/i12340.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ class C:
33
def f = 42
44
end C
55

6+
class A:
7+
class B:
8+
class C:
9+
def foo = 42
10+
11+
end B
12+
613
def f(i: Int) =
714
if i < 42 then
815
println(i)

tests/rewrites/i17187.check

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
object A {
3+
object B {
4+
def a = 2
5+
}
6+
}
7+
8+
def m1 = {
9+
def b = {
10+
def c = 2
11+
}
12+
}
13+
14+
def m2 =
15+
if true then {
16+
val x = 3
17+
if (false)
18+
x
19+
else {
20+
val y = 4
21+
y
22+
}
23+
}
24+
25+
def m3 =
26+
try {
27+
val n2 = 21
28+
val n1 = 4
29+
n2 / n1
30+
}
31+
catch {
32+
case _ => 4
33+
}
34+
35+
def m4 = {
36+
val n2 = 21
37+
try {
38+
val n1 = 4
39+
n2 / n1
40+
}
41+
catch {
42+
case _ => 4
43+
}
44+
}

tests/rewrites/i17187.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
object A:
3+
object B:
4+
def a = 2
5+
6+
def m1 =
7+
def b =
8+
def c = 2
9+
10+
def m2 =
11+
if true then
12+
val x = 3
13+
if (false)
14+
x
15+
else
16+
val y = 4
17+
y
18+
19+
def m3 =
20+
try
21+
val n2 = 21
22+
val n1 = 4
23+
n2 / n1
24+
catch
25+
case _ => 4
26+
27+
def m4 =
28+
val n2 = 21
29+
try
30+
val n1 = 4
31+
n2 / n1
32+
catch
33+
case _ => 4

0 commit comments

Comments
 (0)