Skip to content

Commit 462a646

Browse files
WojciechMazurtgodzik
authored andcommitted
Deduplicate patches before applying them to sources (scala#24215)
Fixes scala#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
1 parent b19f330 commit 462a646

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-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")

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)