Skip to content

Commit 78900fb

Browse files
ckipp01Kordyjan
authored andcommitted
refactor: add in a Rewrites.applyAction helper
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]
1 parent 606e7d4 commit 78900fb

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotty.tools.dotc.util.SourcePosition;
1111

1212
import java.io.OutputStreamWriter
1313
import java.nio.charset.StandardCharsets.UTF_8
14+
import dotty.tools.dotc.reporting.CodeAction
1415

1516
/** Handles rewriting of Scala2 files to Dotty */
1617
object Rewrites {
@@ -99,6 +100,14 @@ object Rewrites {
99100
report.echo(s"[patched file ${source.file.path}]")
100101
rewrites.patched(source).writeBack()
101102
}
103+
104+
/** Given a CodeAction take the patches and apply them.
105+
*
106+
* @param action The CodeAction containing the patches
107+
*/
108+
def applyAction(action: CodeAction)(using Context): Unit =
109+
action.patches.foreach: actionPatch =>
110+
patch(actionPatch.srcPos.span, actionPatch.replacement)
102111
}
103112

104113
/** A completely encapsulated class representing rewrite state, used

compiler/src/dotty/tools/dotc/typer/Typer.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import cc.CheckCaptures
5454
import config.Config
5555

5656
import scala.annotation.constructorOnly
57+
import dotty.tools.dotc.rewrites.Rewrites
5758

5859
object Typer {
5960

@@ -2882,8 +2883,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
28822883
if (migrateTo3) {
28832884
// Under -rewrite, patch `x _` to `(() => x)`
28842885
msg.actions
2885-
.flatMap(_.patches)
2886-
.map(actionPatch => patch(actionPatch.srcPos.span, actionPatch.replacement))
2886+
.headOption
2887+
.foreach(Rewrites.applyAction)
28872888
return typed(untpd.Function(Nil, qual), pt)
28882889
}
28892890
}
@@ -3887,8 +3888,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
38873888
|| Feature.warnOnMigration(msg, tree.srcPos, version = `3.0`)
38883889
&& {
38893890
msg.actions
3890-
.flatMap(_.patches)
3891-
.map(actionPatch => patch(actionPatch.srcPos.span, actionPatch.replacement))
3891+
.headOption
3892+
.foreach(Rewrites.applyAction)
38923893
true
38933894
}
38943895

0 commit comments

Comments
 (0)