@@ -53,7 +53,6 @@ import config.MigrationVersion
53
53
import transform .CheckUnused .OriginalName
54
54
55
55
import scala .annotation .constructorOnly
56
- import dotty .tools .dotc .ast .desugar .PolyFunctionApply
57
56
58
57
object Typer {
59
58
@@ -1958,7 +1957,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1958
1957
untpd.InLambdaTypeTree (isResult = true , (tsyms, vsyms) =>
1959
1958
mt.resultType.substParams(mt, vsyms.map(_.termRef)).substParams(poly, tsyms.map(_.typeRef)))
1960
1959
val desugared @ Block (List (defdef), _) = desugar.makeClosure(tparams, inferredVParams, body, resultTpt, tree.span)
1961
- defdef.putAttachment(PolyFunctionApply , () )
1960
+ defdef.putAttachment(desugar. PolyFunctionApply , List .empty )
1962
1961
typed(desugared, pt)
1963
1962
else
1964
1963
val msg =
@@ -1967,7 +1966,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1967
1966
errorTree(EmptyTree , msg, tree.srcPos)
1968
1967
case _ =>
1969
1968
val desugared @ Block (List (defdef), _) = desugar.makeClosure(tparams, vparams, body, untpd.TypeTree (), tree.span)
1970
- defdef.putAttachment(PolyFunctionApply , () )
1969
+ defdef.putAttachment(desugar. PolyFunctionApply , List .empty )
1971
1970
typed(desugared, pt)
1972
1971
end typedPolyFunctionValue
1973
1972
@@ -3580,30 +3579,57 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3580
3579
case xtree => typedUnnamed(xtree)
3581
3580
3582
3581
val unsimplifiedType = result.tpe
3583
- simplify(result, pt, locked)
3584
- result .tpe.stripTypeVar match
3582
+ val result1 = simplify(result, pt, locked)
3583
+ result1 .tpe.stripTypeVar match
3585
3584
case e : ErrorType if ! unsimplifiedType.isErroneous => errorTree(xtree, e.msg, xtree.srcPos)
3586
- case _ => result
3585
+ case _ => result1
3587
3586
catch case ex : TypeError =>
3588
3587
handleTypeError(ex)
3589
3588
}
3590
3589
}
3591
3590
3591
+ private def pushDownDeferredEvidenceParams (tpe : Type , params : List [untpd.ValDef ], span : Span )(using Context ): Type = tpe.dealias match {
3592
+ case tpe : MethodType =>
3593
+ MethodType (tpe.paramNames)(paramNames => tpe.paramInfos, _ => pushDownDeferredEvidenceParams(tpe.resultType, params, span))
3594
+ case tpe : PolyType =>
3595
+ PolyType (tpe.paramNames)(paramNames => tpe.paramInfos, _ => pushDownDeferredEvidenceParams(tpe.resultType, params, span))
3596
+ case tpe : RefinedType =>
3597
+ // TODO(kπ): Doesn't seem right, but the PolyFunction ends up being a refinement
3598
+ RefinedType (pushDownDeferredEvidenceParams(tpe.parent, params, span), tpe.refinedName, pushDownDeferredEvidenceParams(tpe.refinedInfo, params, span))
3599
+ case tpe @ AppliedType (tycon, args) if defn.isFunctionType(tpe) && args.size > 1 =>
3600
+ AppliedType (tpe.tycon, args.init :+ pushDownDeferredEvidenceParams(args.last, params, span))
3601
+ case tpe =>
3602
+ val paramNames = params.map(_.name)
3603
+ val paramTpts = params.map(_.tpt)
3604
+ val paramsErased = params.map(_.mods.flags.is(Erased ))
3605
+ val ctxFunction = desugar.makeContextualFunction(paramTpts, paramNames, untpd.TypedSplice (TypeTree (tpe.dealias)), paramsErased).withSpan(span)
3606
+ typed(ctxFunction).tpe
3607
+ }
3608
+
3609
+ private def addDownDeferredEvidenceParams (tree : Tree , pt : Type )(using Context ): (Tree , Type ) = {
3610
+ tree.getAttachment(desugar.PolyFunctionApply ) match
3611
+ case Some (params) if params.nonEmpty =>
3612
+ tree.removeAttachment(desugar.PolyFunctionApply )
3613
+ val tpe = pushDownDeferredEvidenceParams(tree.tpe, params, tree.span)
3614
+ TypeTree (tpe).withSpan(tree.span) -> tpe
3615
+ case _ => tree -> pt
3616
+ }
3617
+
3592
3618
/** Interpolate and simplify the type of the given tree. */
3593
- protected def simplify (tree : Tree , pt : Type , locked : TypeVars )(using Context ): tree.type =
3594
- if ! tree.denot.isOverloaded then // for overloaded trees: resolve overloading before simplifying
3595
- if ! tree.tpe.widen.isInstanceOf [MethodOrPoly ] // wait with simplifying until method is fully applied
3596
- || tree.isDef // ... unless tree is a definition
3619
+ protected def simplify (tree : Tree , pt : Type , locked : TypeVars )(using Context ): Tree =
3620
+ val (tree1, pt1) = addDownDeferredEvidenceParams(tree, pt)
3621
+ if ! tree1.denot.isOverloaded then // for overloaded trees: resolve overloading before simplifying
3622
+ if ! tree1.tpe.widen.isInstanceOf [MethodOrPoly ] // wait with simplifying until method is fully applied
3623
+ || tree1.isDef // ... unless tree is a definition
3597
3624
then
3598
- interpolateTypeVars(tree, pt , locked)
3599
- val simplified = tree .tpe.simplified
3600
- if ! MatchType .thatReducesUsingGadt(tree .tpe) then // needs a GADT cast. i15743
3625
+ interpolateTypeVars(tree1, pt1 , locked)
3626
+ val simplified = tree1 .tpe.simplified
3627
+ if ! MatchType .thatReducesUsingGadt(tree1 .tpe) then // needs a GADT cast. i15743
3601
3628
tree.overwriteType(simplified)
3602
- tree
3629
+ tree1
3603
3630
3604
3631
protected def makeContextualFunction (tree : untpd.Tree , pt : Type )(using Context ): Tree = {
3605
3632
val defn .FunctionOf (formals, _, true ) = pt.dropDependentRefinement: @ unchecked
3606
- println(i " make contextual function $tree / $pt" )
3607
3633
val paramNamesOrNil = pt match
3608
3634
case RefinedType (_, _, rinfo : MethodType ) => rinfo.paramNames
3609
3635
case _ => Nil
0 commit comments