Skip to content

Commit d6cf057

Browse files
committed
Deduplicate patches before applying them to sources (#24215)
Fixes #24213 - deduplicate patches by content and span. Similar issues were spotted for code `using` rewrites resulting in invalid `using using` introduced by compiler. Issue might arise if some reason compiler typechecks the same code twice [Cherry-picked 2af7454]
1 parent 39ebf6b commit d6cf057

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ object Rewrites {
4343
pbuf.filterInPlace(x => !p(x.span))
4444

4545
def apply(cs: Array[Char]): Array[Char] = {
46-
val delta = pbuf.map(_.delta).sum
47-
val patches = pbuf.toList.sortBy(_.span.start)
46+
val patches = pbuf.toList.distinct.sortBy(_.span.start)
47+
val delta = patches.map(_.delta).sum
4848
if (patches.nonEmpty)
4949
patches.reduceLeft {(p1, p2) =>
5050
assert(p1.span.end <= p2.span.start, s"overlapping patches in $source: $p1 and $p2")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class CompilationTests {
8989
compileFile("tests/rewrites/implicit-to-given.scala", defaultOptions.and("-rewrite", "-Yimplicit-to-given")),
9090
compileFile("tests/rewrites/i22792.scala", defaultOptions.and("-rewrite")),
9191
compileFile("tests/rewrites/i23449.scala", defaultOptions.and("-rewrite", "-source:3.4-migration")),
92+
compileFile("tests/rewrites/i24213.scala", defaultOptions.and("-rewrite", "-source:3.4-migration")),
9293
).checkRewrites()
9394
}
9495

tests/rewrites/i24213.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def Test =
2+
try ()
3+
catch {
4+
case x: Throwable if x.getMessage `contains` "error" => ???
5+
}

tests/rewrites/i24213.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def Test =
2+
try ()
3+
catch {
4+
case x: Throwable if x.getMessage contains "error" => ???
5+
}

0 commit comments

Comments
 (0)