@@ -890,12 +890,26 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
890
890
/** Produce a typed qual.unapply or qual.unapplySeq tree, or
891
891
* else if this fails follow a type alias and try again.
892
892
*/
893
- val unapplyFn = trySelectUnapply(qual) { sel =>
893
+ var unapplyFn = trySelectUnapply(qual) { sel =>
894
894
val qual1 = followTypeAlias(qual)
895
895
if (qual1.isEmpty) notAnExtractor(sel)
896
896
else trySelectUnapply(qual1)(_ => notAnExtractor(sel))
897
897
}
898
898
899
+ /** Add a `Bind` node for each `bound` symbol in a type application `unapp` */
900
+ def addBinders (unapp : Tree , bound : List [Symbol ]) = unapp match {
901
+ case TypeApply (fn, args) =>
902
+ def addBinder (arg : Tree ) = arg.tpe.stripTypeVar match {
903
+ case ref : TypeRef if bound.contains(ref.symbol) =>
904
+ tpd.Bind (ref.symbol, Ident (ref))
905
+ case _ =>
906
+ arg
907
+ }
908
+ tpd.cpy.TypeApply (unapp)(fn, args.mapConserve(addBinder))
909
+ case _ =>
910
+ unapp
911
+ }
912
+
899
913
def fromScala2x = unapplyFn.symbol.exists && (unapplyFn.symbol.owner is Scala2x )
900
914
901
915
/** Is `subtp` a subtype of `tp` or of some generalization of `tp`?
@@ -922,27 +936,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
922
936
unapp.println(i " case 1 $unapplyArgType ${ctx.typerState.constraint}" )
923
937
selType
924
938
} else if (isSubTypeOfParent(unapplyArgType, selType)(ctx.addMode(Mode .GADTflexible ))) {
925
- maximizeType(unapplyArgType) match {
926
- case Some (tvar) =>
927
- def msg =
928
- ex """ There is no best instantiation of pattern type $unapplyArgType
929
- |that makes it a subtype of selector type $selType.
930
- |Non-variant type variable ${tvar.origin} cannot be uniquely instantiated. """
931
- if (fromScala2x) {
932
- // We can't issue an error here, because in Scala 2, ::[B] is invariant
933
- // whereas List[+T] is covariant. According to the strict rule, a pattern
934
- // match of a List[C] against a case x :: xs is illegal, because
935
- // B cannot be uniquely instantiated. Of course :: should have been
936
- // covariant in the first place, but in the Scala libraries it isn't.
937
- // So for now we allow these kinds of patterns, even though they
938
- // can open unsoundness holes. See SI-7952 for an example of the hole this opens.
939
- if (ctx.settings.verbose.value) ctx.warning(msg, tree.pos)
940
- } else {
941
- unapp.println(s " ${unapplyFn.symbol.owner} ${unapplyFn.symbol.owner is Scala2x }" )
942
- ctx.strictWarning(msg, tree.pos)
943
- }
944
- case _ =>
945
- }
939
+ val patternBound = maximizeType(unapplyArgType, tree.pos, fromScala2x)
940
+ if (patternBound.nonEmpty) unapplyFn = addBinders(unapplyFn, patternBound)
946
941
unapp.println(i " case 2 $unapplyArgType ${ctx.typerState.constraint}" )
947
942
unapplyArgType
948
943
} else {
0 commit comments