Skip to content

Commit

Permalink
refactor: add in a Rewrites.applyAction helper
Browse files Browse the repository at this point in the history
This also has a small change where instead of applying all the actions
if we are doing a rewrite, we only actually take the first one

[Cherry-picked e97fea5]
  • Loading branch information
ckipp01 authored and Kordyjan committed Nov 29, 2023
1 parent 9e0ba4f commit 23fb8d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dotty.tools.dotc.util.SourcePosition;

import java.io.OutputStreamWriter
import java.nio.charset.StandardCharsets.UTF_8
import dotty.tools.dotc.reporting.CodeAction

/** Handles rewriting of Scala2 files to Dotty */
object Rewrites {
Expand Down Expand Up @@ -99,6 +100,14 @@ object Rewrites {
report.echo(s"[patched file ${source.file.path}]")
rewrites.patched(source).writeBack()
}

/** Given a CodeAction take the patches and apply them.
*
* @param action The CodeAction containing the patches
*/
def applyAction(action: CodeAction)(using Context): Unit =
action.patches.foreach: actionPatch =>
patch(actionPatch.srcPos.span, actionPatch.replacement)
}

/** A completely encapsulated class representing rewrite state, used
Expand Down
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import cc.CheckCaptures
import config.Config

import scala.annotation.constructorOnly
import dotty.tools.dotc.rewrites.Rewrites

object Typer {

Expand Down Expand Up @@ -2882,8 +2883,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if (migrateTo3) {
// Under -rewrite, patch `x _` to `(() => x)`
msg.actions
.flatMap(_.patches)
.map(actionPatch => patch(actionPatch.srcPos.span, actionPatch.replacement))
.headOption
.foreach(Rewrites.applyAction)
return typed(untpd.Function(Nil, qual), pt)
}
}
Expand Down Expand Up @@ -3887,8 +3888,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
|| Feature.warnOnMigration(msg, tree.srcPos, version = `3.0`)
&& {
msg.actions
.flatMap(_.patches)
.map(actionPatch => patch(actionPatch.srcPos.span, actionPatch.replacement))
.headOption
.foreach(Rewrites.applyAction)
true
}

Expand Down

0 comments on commit 23fb8d3

Please sign in to comment.