Skip to content

Commit

Permalink
Undo patch of double-block apply (#21982)
Browse files Browse the repository at this point in the history
Fixes #21382
  • Loading branch information
bracevac authored Jan 13, 2025
2 parents c5bf0e0 + f6b49df commit 49839cd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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
15 changes: 15 additions & 0 deletions compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ object Rewrites {
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 @@ -81,6 +81,7 @@ class CompilationTests {
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/ambigious-named-tuple-assignment.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 49839cd

Please sign in to comment.