Skip to content

Commit

Permalink
Undo patch of double-block apply
Browse files Browse the repository at this point in the history
If `f{}{}` is candidate for rewrite, unpatch it.

We don't know exact spans, so just check endpoints to remove.
  • Loading branch information
som-snytt authored and bracevac committed Nov 19, 2024
1 parent 73168ee commit 7a57867
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import ScriptParsers.*
import Decorators.*
import util.Chars
import scala.annotation.tailrec
import rewrites.Rewrites.{patch, overlapsPatch}
import rewrites.Rewrites.{overlapsPatch, patch, unpatch}
import reporting.*
import config.Feature
import config.Feature.{sourceVersion, migrateTo3}
Expand Down Expand Up @@ -2779,6 +2779,12 @@ object Parsers {
simpleExprRest(tapp, location, canApply = true)
case LPAREN | LBRACE | INDENT if canApply =>
val app = atSpan(startOffset(t), in.offset) { mkApply(t, argumentExprs()) }
if in.rewriteToIndent then
app match
case Apply(Apply(_, List(Block(_, _))), List(blk @ Block(_, _))) =>
unpatch(blk.srcPos.sourcePos.source, Span(blk.span.start, blk.span.start + 1))
unpatch(blk.srcPos.sourcePos.source, Span(blk.span.end, blk.span.end + 1))
case _ =>
simpleExprRest(app, location, canApply = true)
case USCORE =>
atSpan(startOffset(t), in.skipToken()) { PostfixOp(t, Ident(nme.WILDCARD)) }
Expand Down
17 changes: 16 additions & 1 deletion compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ object Rewrites {
case class ActionPatch(srcPos: SourcePosition, replacement: String)

private class Patches(source: SourceFile) {
private[Rewrites] val pbuf = new mutable.ListBuffer[Patch]()
private[Rewrites] val pbuf = mutable.ListBuffer.empty[Patch]

def addPatch(span: Span, replacement: String): Unit =
pbuf += Patch(span, replacement)

// remove patches which match either end point
def removePatch(span: Span): Unit =
def p(other: Span): Boolean = span.start == other.start || span.end == other.end
pbuf.filterInPlace(x => !p(x.span))

def apply(cs: Array[Char]): Array[Char] = {
val delta = pbuf.map(_.delta).sum
val patches = pbuf.toList.sortBy(_.span.start)
Expand Down Expand Up @@ -87,6 +92,16 @@ object Rewrites {
def patch(span: Span, replacement: String)(using Context): Unit =
patch(ctx.compilationUnit.source, span, replacement)

/** Delete patches matching the given span,
* where a match has the same start or end offset.
*/
def unpatch(source: SourceFile, span: Span)(using Context): Unit =
if ctx.reporter != Reporter.NoReporter // NoReporter is used for syntax highlighting
then ctx.settings.rewrite.value.foreach: rewrites =>
rewrites.patched
.get(source)
.foreach(_.removePatch(span))

/** Does `span` overlap with a patch region of `source`? */
def overlapsPatch(source: SourceFile, span: Span)(using Context): Boolean =
ctx.settings.rewrite.value.exists(rewrites =>
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CompilationTests {
compileDir("tests/rewrites/annotation-named-pararamters", defaultOptions.and("-rewrite", "-source:3.6-migration")),
compileFile("tests/rewrites/i21418.scala", unindentOptions.and("-rewrite", "-source:3.5-migration")),
compileFile("tests/rewrites/infix-named-args.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
compileFile("tests/rewrites/i21382.scala", defaultOptions.and("-indent", "-rewrite")),
).checkRewrites()
}

Expand Down
8 changes: 8 additions & 0 deletions tests/rewrites/i21382.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def check(element: Any)(expected: String): Unit = ???

def test =
check {
???
}{
42.toString
}

0 comments on commit 7a57867

Please sign in to comment.