diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 605afc07cc72..6c494db78c7f 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -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 @@ -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 diff --git a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala index 96e88e5c68ae..f2dfac88d464 100644 --- a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala +++ b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala @@ -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 diff --git a/compiler/src/dotty/tools/dotc/util/Spans.scala b/compiler/src/dotty/tools/dotc/util/Spans.scala index ba537e9aec01..e1487408f36b 100644 --- a/compiler/src/dotty/tools/dotc/util/Spans.scala +++ b/compiler/src/dotty/tools/dotc/util/Spans.scala @@ -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 ) } diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index fdbd9216f1b7..8d9e28d415c1 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -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() } diff --git a/tests/rewrites/i12340.check b/tests/rewrites/i12340.check index c6cb9af8bb57..3ee2867f3e62 100644 --- a/tests/rewrites/i12340.check +++ b/tests/rewrites/i12340.check @@ -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) diff --git a/tests/rewrites/i12340.scala b/tests/rewrites/i12340.scala index bf907ef9f276..10fcdd256f5b 100644 --- a/tests/rewrites/i12340.scala +++ b/tests/rewrites/i12340.scala @@ -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) diff --git a/tests/rewrites/i17187.check b/tests/rewrites/i17187.check new file mode 100644 index 000000000000..1e6a07c79738 --- /dev/null +++ b/tests/rewrites/i17187.check @@ -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 + } +} diff --git a/tests/rewrites/i17187.scala b/tests/rewrites/i17187.scala new file mode 100644 index 000000000000..e6a55e00b39a --- /dev/null +++ b/tests/rewrites/i17187.scala @@ -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