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