Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo patch of double-block apply #21982

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading