Skip to content

Commit 9d0bfba

Browse files
committed
Fix #17187: allow patches with same span
1 parent 3e6a261 commit 9d0bfba

File tree

6 files changed

+91
-8
lines changed

6 files changed

+91
-8
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,11 @@ object Parsers {
715715
val t = enclosed(INDENT, body)
716716
if needsBraces(t) then
717717
patch(source, Span(startOpening, endOpening), " {")
718-
patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
718+
val next = in.next
719+
def closedByEndMarker =
720+
next.token == END && (next.offset - next.lineOffset) == indentWidth.toPrefix.size
721+
if closedByEndMarker then patch(source, Span(next.offset), "} // ")
722+
else patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
719723
t
720724
end indentedToBraces
721725

@@ -1422,9 +1426,6 @@ object Parsers {
14221426
val start = in.skipToken()
14231427
if stats.isEmpty || !matchesAndSetEnd(stats.last) then
14241428
syntaxError(em"misaligned end marker", Span(start, in.lastCharOffset))
1425-
else if overlapsPatch(source, Span(start, start)) then
1426-
patch(source, Span(start, start), "")
1427-
patch(source, Span(start, in.lastCharOffset), s"} // end $endName")
14281429
in.token = IDENTIFIER // Leaving it as the original token can confuse newline insertion
14291430
in.nextToken()
14301431
end checkEndMarker

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

+1-4
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/test/dotty/tools/dotc/CompilationTests.scala

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class CompilationTests {
8181
compileFile("tests/rewrites/i9632.scala", defaultOptions.and("-indent", "-rewrite")),
8282
compileFile("tests/rewrites/i11895.scala", defaultOptions.and("-indent", "-rewrite")),
8383
compileFile("tests/rewrites/i12340.scala", unindentOptions.and("-rewrite")),
84+
compileFile("tests/rewrites/i17187.scala", unindentOptions.and("-rewrite")),
8485
).checkRewrites()
8586
}
8687

tests/rewrites/i12340.scala

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

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

+33
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)