diff --git a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala index 9b6b115a7b17..5bea0fb66ed0 100644 --- a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala +++ b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala @@ -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 { @@ -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 diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index b5a0ed2917d6..76b7b6add57c 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -54,6 +54,7 @@ import cc.CheckCaptures import config.Config import scala.annotation.constructorOnly +import dotty.tools.dotc.rewrites.Rewrites object Typer { @@ -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) } } @@ -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 }