@@ -1107,6 +1107,20 @@ class Typer extends Namer
1107
1107
newTypeVar(apply(bounds.orElse(TypeBounds .empty)).bounds)
1108
1108
case _ => mapOver(t)
1109
1109
}
1110
+ def extractInUnion (t : Type ): Seq [Type ] = t match {
1111
+ case t : OrType =>
1112
+ extractInUnion(t.tp1) ++ extractInUnion(t.tp2)
1113
+ case t : TypeParamRef =>
1114
+ extractInUnion(ctx.typerState.constraint.entry(t).bounds.hi)
1115
+ case t if defn.isNonRefinedFunction(t) =>
1116
+ Seq (t)
1117
+ case SAMType (_ : MethodType ) =>
1118
+ Seq (t)
1119
+ case _ =>
1120
+ Nil
1121
+ }
1122
+ def defaultResult = (List .tabulate(defaultArity)(alwaysWildcardType), untpd.TypeTree ())
1123
+
1110
1124
val pt1 = pt.stripTypeVar.dealias
1111
1125
if (pt1 ne pt1.dropDependentRefinement)
1112
1126
&& defn.isContextFunctionType(pt1.nonPrivateMember(nme.apply).info.finalResultType)
@@ -1115,22 +1129,25 @@ class Typer extends Namer
1115
1129
i """ Implementation restriction: Expected result type $pt1
1116
1130
|is a curried dependent context function type. Such types are not yet supported. """ ,
1117
1131
tree.srcPos)
1118
- pt1 match {
1132
+
1133
+ val elems = extractInUnion(pt1)
1134
+ if elems.length != 1 then
1135
+ // The union type containing multiple function types is ignored
1136
+ defaultResult
1137
+ else elems.head match {
1119
1138
case pt1 if defn.isNonRefinedFunction(pt1) =>
1120
1139
// if expected parameter type(s) are wildcards, approximate from below.
1121
1140
// if expected result type is a wildcard, approximate from above.
1122
1141
// this can type the greatest set of admissible closures.
1123
1142
(pt1.argTypesLo.init, typeTree(interpolateWildcards(pt1.argTypesHi.last)))
1124
1143
case SAMType (sam @ MethodTpe (_, formals, restpe)) =>
1125
1144
(formals,
1126
- if (sam.isResultDependent)
1127
- untpd.DependentTypeTree (syms => restpe.substParams(sam, syms.map(_.termRef)))
1128
- else
1129
- typeTree(restpe))
1130
- case tp : TypeParamRef =>
1131
- decomposeProtoFunction(ctx.typerState.constraint.entry(tp).bounds.hi, defaultArity, tree)
1145
+ if (sam.isResultDependent)
1146
+ untpd.DependentTypeTree (syms => restpe.substParams(sam, syms.map(_.termRef)))
1147
+ else
1148
+ typeTree(restpe))
1132
1149
case _ =>
1133
- ( List .tabulate(defaultArity)(alwaysWildcardType), untpd. TypeTree ())
1150
+ defaultResult
1134
1151
}
1135
1152
}
1136
1153
@@ -1375,14 +1392,22 @@ class Typer extends Namer
1375
1392
}
1376
1393
1377
1394
def typedClosure (tree : untpd.Closure , pt : Type )(using Context ): Tree = {
1395
+ def extractInUnion (t : Type ): Seq [Type ] = t match {
1396
+ case t : OrType =>
1397
+ extractInUnion(t.tp1) ++ extractInUnion(t.tp2)
1398
+ case SAMType (_) =>
1399
+ Seq (t)
1400
+ case _ =>
1401
+ Nil
1402
+ }
1378
1403
val env1 = tree.env mapconserve (typed(_))
1379
1404
val meth1 = typedUnadapted(tree.meth)
1380
1405
val target =
1381
1406
if (tree.tpt.isEmpty)
1382
1407
meth1.tpe.widen match {
1383
1408
case mt : MethodType =>
1384
- pt.stripNull match {
1385
- case pt @ SAMType (sam)
1409
+ extractInUnion(pt) match {
1410
+ case Seq ( pt @ SAMType (sam) )
1386
1411
if ! defn.isFunctionType(pt) && mt <:< sam =>
1387
1412
// SAMs of the form C[?] where C is a class cannot be conversion targets.
1388
1413
// The resulting class `class $anon extends C[?] {...}` would be illegal,
0 commit comments