@@ -1125,6 +1125,20 @@ class Typer extends Namer
1125
1125
newTypeVar(apply(bounds.orElse(TypeBounds .empty)).bounds)
1126
1126
case _ => mapOver(t)
1127
1127
}
1128
+ def extractInUnion (t : Type ): Seq [Type ] = t match {
1129
+ case t : OrType =>
1130
+ extractInUnion(t.tp1) ++ extractInUnion(t.tp2)
1131
+ case t : TypeParamRef =>
1132
+ extractInUnion(ctx.typerState.constraint.entry(t).bounds.hi)
1133
+ case t if defn.isNonRefinedFunction(t) =>
1134
+ Seq (t)
1135
+ case SAMType (_ : MethodType ) =>
1136
+ Seq (t)
1137
+ case _ =>
1138
+ Nil
1139
+ }
1140
+ def defaultResult = (List .tabulate(defaultArity)(alwaysWildcardType), untpd.TypeTree ())
1141
+
1128
1142
val pt1 = pt.stripTypeVar.dealias
1129
1143
if (pt1 ne pt1.dropDependentRefinement)
1130
1144
&& defn.isContextFunctionType(pt1.nonPrivateMember(nme.apply).info.finalResultType)
@@ -1133,22 +1147,25 @@ class Typer extends Namer
1133
1147
i """ Implementation restriction: Expected result type $pt1
1134
1148
|is a curried dependent context function type. Such types are not yet supported. """ ,
1135
1149
tree.srcPos)
1136
- pt1 match {
1150
+
1151
+ val elems = extractInUnion(pt1)
1152
+ if elems.length != 1 then
1153
+ // The union type containing multiple function types is ignored
1154
+ defaultResult
1155
+ else elems.head match {
1137
1156
case pt1 if defn.isNonRefinedFunction(pt1) =>
1138
1157
// if expected parameter type(s) are wildcards, approximate from below.
1139
1158
// if expected result type is a wildcard, approximate from above.
1140
1159
// this can type the greatest set of admissible closures.
1141
1160
(pt1.argTypesLo.init, typeTree(interpolateWildcards(pt1.argTypesHi.last)))
1142
1161
case SAMType (sam @ MethodTpe (_, formals, restpe)) =>
1143
1162
(formals,
1144
- if (sam.isResultDependent)
1145
- untpd.DependentTypeTree (syms => restpe.substParams(sam, syms.map(_.termRef)))
1146
- else
1147
- typeTree(restpe))
1148
- case tp : TypeParamRef =>
1149
- decomposeProtoFunction(ctx.typerState.constraint.entry(tp).bounds.hi, defaultArity, tree)
1163
+ if (sam.isResultDependent)
1164
+ untpd.DependentTypeTree (syms => restpe.substParams(sam, syms.map(_.termRef)))
1165
+ else
1166
+ typeTree(restpe))
1150
1167
case _ =>
1151
- ( List .tabulate(defaultArity)(alwaysWildcardType), untpd. TypeTree ())
1168
+ defaultResult
1152
1169
}
1153
1170
}
1154
1171
@@ -1393,14 +1410,22 @@ class Typer extends Namer
1393
1410
}
1394
1411
1395
1412
def typedClosure (tree : untpd.Closure , pt : Type )(using Context ): Tree = {
1413
+ def extractInUnion (t : Type ): Seq [Type ] = t match {
1414
+ case t : OrType =>
1415
+ extractInUnion(t.tp1) ++ extractInUnion(t.tp2)
1416
+ case SAMType (_) =>
1417
+ Seq (t)
1418
+ case _ =>
1419
+ Nil
1420
+ }
1396
1421
val env1 = tree.env mapconserve (typed(_))
1397
1422
val meth1 = typedUnadapted(tree.meth)
1398
1423
val target =
1399
1424
if (tree.tpt.isEmpty)
1400
1425
meth1.tpe.widen match {
1401
1426
case mt : MethodType =>
1402
- pt.stripNull match {
1403
- case pt @ SAMType (sam)
1427
+ extractInUnion(pt) match {
1428
+ case Seq ( pt @ SAMType (sam) )
1404
1429
if ! defn.isFunctionType(pt) && mt <:< sam =>
1405
1430
// SAMs of the form C[?] where C is a class cannot be conversion targets.
1406
1431
// The resulting class `class $anon extends C[?] {...}` would be illegal,
0 commit comments