@@ -43,18 +43,16 @@ import config.Printers.{gadts, typr}
4343import config .Feature
4444import config .Feature .{sourceVersion , migrateTo3 }
4545import config .SourceVersion .*
46- import rewrites .Rewrites .patch
46+ import rewrites .Rewrites , Rewrites .patch
4747import staging .StagingLevel
4848import reporting .*
4949import Nullables .*
5050import NullOpsDecorator .*
5151import cc .CheckCaptures
5252import config .Config
5353import config .MigrationVersion
54- import Migrations .*
5554
5655import scala .annotation .constructorOnly
57- import dotty .tools .dotc .rewrites .Rewrites
5856
5957object Typer {
6058
@@ -128,7 +126,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
128126 with Dynamic
129127 with Checking
130128 with QuotesAndSplices
131- with Deriving {
129+ with Deriving
130+ with Migrations {
132131
133132 import Typer .*
134133 import tpd .{cpy => _ , _ }
@@ -159,6 +158,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
159158 // Overridden in derived typers
160159 def newLikeThis (nestingLevel : Int ): Typer = new Typer (nestingLevel)
161160
161+ // Overridden to do nothing in derived typers
162+ protected def migrate [T ](migration : => T , disabled : => T = ()): T = migration
163+
162164 /** Find the type of an identifier with given `name` in given context `ctx`.
163165 * @param name the name of the identifier
164166 * @param pt the expected type
@@ -2979,48 +2981,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
29792981 else tree1
29802982 }
29812983
2982- def typedAsFunction (tree : untpd.PostfixOp , pt : Type )(using Context ): Tree = {
2983- val untpd .PostfixOp (qual, Ident (nme.WILDCARD )) = tree : @ unchecked
2984- val pt1 = if (defn.isFunctionNType(pt)) pt else AnyFunctionProto
2985- val nestedCtx = ctx.fresh.setNewTyperState()
2986- val res = typed(qual, pt1)(using nestedCtx)
2987- res match {
2988- case closure(_, _, _) =>
2989- case _ =>
2990- val recovered = typed(qual)(using ctx.fresh.setExploreTyperState())
2991- val msg = OnlyFunctionsCanBeFollowedByUnderscore (recovered.tpe.widen, tree)
2992- report.errorOrMigrationWarning(msg, tree.srcPos, MigrationVersion .Scala2to3 )
2993- if MigrationVersion .Scala2to3 .needsPatch then
2994- // Under -rewrite, patch `x _` to `(() => x)`
2995- msg.actions
2996- .headOption
2997- .foreach(Rewrites .applyAction)
2998- return typed(untpd.Function (Nil , qual), pt)
2999- }
3000- nestedCtx.typerState.commit()
3001-
3002- lazy val (prefix, suffix) = res match {
3003- case Block (mdef @ DefDef (_, vparams :: Nil , _, _) :: Nil , _ : Closure ) =>
3004- val arity = vparams.length
3005- if (arity > 0 ) (" " , " " ) else (" (() => " , " ())" )
3006- case _ =>
3007- (" (() => " , " )" )
3008- }
3009- def remedy =
3010- if ((prefix ++ suffix).isEmpty) " simply leave out the trailing ` _`"
3011- else s " use ` $prefix<function> $suffix` instead "
3012- def rewrite = Message .rewriteNotice(" This construct" , `3.4-migration`)
3013- report.errorOrMigrationWarning(
3014- em """ The syntax `<function> _` is no longer supported;
3015- |you can $remedy$rewrite""" ,
3016- tree.srcPos,
3017- MigrationVersion .FunctionUnderscore )
3018- if MigrationVersion .FunctionUnderscore .needsPatch then
3019- patch(Span (tree.span.start), prefix)
3020- patch(Span (qual.span.end, tree.span.end), suffix)
3021-
3022- res
3023- }
2984+ override def typedAsFunction (tree : untpd.PostfixOp , pt : Type )(using Context ): Tree =
2985+ migrate(super .typedAsFunction(tree, pt), throw new AssertionError (" can't retype a PostfixOp" ))
30242986
30252987 /** Translate infix operation expression `l op r` to
30262988 *
@@ -3138,7 +3100,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
31383100 case tree : untpd.TypeDef =>
31393101 // separate method to keep dispatching method `typedNamed` short which might help the JIT
31403102 def typedTypeOrClassDef : Tree =
3141- migrateKindProjectorQMark( tree, sym)
3103+ migrate(kindProjectorQMark( tree, sym) )
31423104 if tree.isClassDef then
31433105 typedClassDef(tree, sym.asClass)(using ctx.localContext(tree, sym))
31443106 else
@@ -3814,7 +3776,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
38143776 case wtp : MethodOrPoly =>
38153777 def methodStr = methPart(tree).symbol.showLocated
38163778 if matchingApply(wtp, pt) then
3817- migrateContextBoundParams( tree, wtp, pt)
3779+ migrate(contextBoundParams( tree, wtp, pt) )
38183780 if needsTupledDual(wtp, pt) then adapt(tree, pt.tupledDual, locked)
38193781 else tree
38203782 else if wtp.isContextualMethod then
0 commit comments