Skip to content

Commit cfe5a9c

Browse files
committed
refactor: make sure the patch logic isn't duplicated
1 parent b01f963 commit cfe5a9c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,13 +2901,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
29012901
case closure(_, _, _) =>
29022902
case _ =>
29032903
val recovered = typed(qual)(using ctx.fresh.setExploreTyperState())
2904-
report.errorOrMigrationWarning(
2905-
OnlyFunctionsCanBeFollowedByUnderscore(recovered.tpe.widen, tree), tree.srcPos, from = `3.0`
2906-
)
2904+
val msg = OnlyFunctionsCanBeFollowedByUnderscore(recovered.tpe.widen, tree)
2905+
report.errorOrMigrationWarning(msg, tree.srcPos, from = `3.0`)
29072906
if (migrateTo3) {
29082907
// Under -rewrite, patch `x _` to `(() => x)`
2909-
patch(Span(tree.span.start), "(() => ")
2910-
patch(Span(qual.span.end, tree.span.end), ")")
2908+
msg.actions
2909+
.flatMap(_.patches)
2910+
.map(actionPatch => patch(actionPatch.srcPos.span, actionPatch.replacement))
29112911
return typed(untpd.Function(Nil, qual), pt)
29122912
}
29132913
}
@@ -3906,10 +3906,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39063906
def adaptNoArgsUnappliedMethod(wtp: MethodType, functionExpected: Boolean, arity: Int): Tree = {
39073907
/** Is reference to this symbol `f` automatically expanded to `f()`? */
39083908
def isAutoApplied(sym: Symbol): Boolean =
3909+
lazy val msg = MissingEmptyArgumentList(sym.show, tree)
3910+
39093911
sym.isConstructor
39103912
|| sym.matchNullaryLoosely
3911-
|| Feature.warnOnMigration(MissingEmptyArgumentList(sym.show, tree), tree.srcPos, version = `3.0`)
3912-
&& { patch(tree.span.endPos, "()"); true }
3913+
|| Feature.warnOnMigration(msg, tree.srcPos, version = `3.0`)
3914+
&& {
3915+
msg.actions
3916+
.flatMap(_.patches)
3917+
.map(actionPatch => patch(actionPatch.srcPos.span, actionPatch.replacement))
3918+
true
3919+
}
39133920

39143921
/** If this is a selection prototype of the form `.apply(...): R`, return the nested
39153922
* function prototype `(...)R`. Otherwise `pt`.

0 commit comments

Comments
 (0)