@@ -73,7 +73,7 @@ trait PatternTypeConstrainer { self: TypeComparer =>
7373 * scrutinee and pattern types. This does not apply if the pattern type is only applied to type variables,
7474 * in which case the subtyping relationship "heals" the type.
7575 */
76- def constrainPatternType (pat : Type , scrut : Type , forceInvariantRefinement : Boolean = false ): Boolean = trace(i " constrainPatternType( $scrut , $pat ) " , gadts) {
76+ def constrainPatternType (pat : Type , scrut : Type , forceInvariantRefinement : Boolean = false ): Boolean = trace(i " constrainPatternType( $pat , $scrut ) " , gadts) {
7777
7878 def classesMayBeCompatible : Boolean = {
7979 import Flags ._
@@ -231,41 +231,32 @@ trait PatternTypeConstrainer { self: TypeComparer =>
231231 * (see `RefChecks#checkCaseClassInheritanceInvariant`).
232232 */
233233 def constrainSimplePatternType (patternTp : Type , scrutineeTp : Type , forceInvariantRefinement : Boolean ): Boolean = {
234+ val debug = noPrinter
234235 def refinementIsInvariant (tp : Type ): Boolean = tp match {
235236 case tp : SingletonType => true
236237 case tp : ClassInfo => tp.cls.is(Final ) || tp.cls.is(Case )
237238 case tp : TypeProxy => refinementIsInvariant(tp.superType)
238239 case _ => false
239240 }
240241
241- def widenVariantParams (tp : Type ) = tp match {
242- case tp @ AppliedType (tycon, args) =>
243- val args1 = args.zipWithConserve(tycon.typeParams)((arg, tparam) =>
244- if (tparam.paramVarianceSign != 0 ) TypeBounds .empty else arg
245- )
246- tp.derivedAppliedType(tycon, args1)
247- case tp =>
248- tp
249- }
250-
251242 val patternCls = patternTp.classSymbol
252243 val scrutineeCls = scrutineeTp.classSymbol
253244
254245 // NOTE: we already know that there is a derives-from relationship in either direction
255246 val upcastPattern =
256247 patternCls.derivesFrom(scrutineeCls)
257248
258- val pt = if upcastPattern then patternTp.baseType(scrutineeCls) else patternTp
259- val tp = if ! upcastPattern then scrutineeTp.baseType(patternCls) else scrutineeTp
249+ val pat = if upcastPattern then patternTp.baseType(scrutineeCls) else patternTp
250+ val scr = if ! upcastPattern then scrutineeTp.baseType(patternCls) else scrutineeTp
260251
261252 val assumeInvariantRefinement =
262253 migrateTo3 || forceInvariantRefinement || refinementIsInvariant(patternTp)
263254
264- trace(i " constraining simple pattern type $tp >:< $pt " , gadts, (res : Boolean ) => i " $res gadt = ${ctx.gadt}" ) {
265- (tp, pt ) match {
266- case (AppliedType (tyconS, argsS), AppliedType (tyconP, argsP)) =>
255+ trace(i " constraining simple pattern type $pat >:< $scr assume= $assumeInvariantRefinement " , gadts, (res : Boolean ) => i " $res gadt = ${ctx.gadt}" ) {
256+ (scr, pat ) match {
257+ case (AppliedType (tyconS, argsS), AppliedType (tyconP, argsP)) if tyconP.frozen_ =:= (tyconS) =>
267258 val saved = state.nn.constraint
268- val result =
259+ val success =
269260 ctx.gadtState.rollbackGadtUnless {
270261 tyconS.typeParams.lazyZip(argsS).lazyZip(argsP).forall { (param, argS, argP) =>
271262 val variance = param.paramVarianceSign
@@ -277,15 +268,21 @@ trait PatternTypeConstrainer { self: TypeComparer =>
277268 val TypeBounds (loS, hiS) = argS.bounds
278269 val TypeBounds (loP, hiP) = argP.bounds
279270 var res = true
280- if variance < 1 then res &&= isSubType(loS, hiP)
281- if variance > - 1 then res &&= isSubType(loP, hiS)
271+ if ctx.mode.is(Mode .Type ) then
272+ if variance > - 1 then res &&= isSubType(loS, hiP).showing(i " $loS <: $hiP = $result v= $variance argS= $argS argP= $argP" , debug)
273+ if variance < 1 then res &&= isSubType(loP, hiS).showing(i " $hiS >: $loP = $result v= $variance argS= $argS argP= $argP" , debug)
274+ else
275+ if variance < 1 then res &&= isSubType(loS, hiP).showing(i " $hiP >: $loS = $result v= $variance argP= $argP argS= $argS" , debug)
276+ if variance > - 1 then res &&= isSubType(loP, hiS).showing(i " $loP <: $hiS = $result v= $variance argP= $argP argS= $argS" , debug)
282277 res
283278 else true
284279 }
285280 }
286- if ! result then
281+ if ! success then
287282 constraint = saved
288- result
283+ success
284+ case (scr : TypeRef , _) if ctx.mode.is(Mode .Type ) && ctx.gadt.contains(scr.symbol) =>
285+ isSubType(scrutineeTp, patternTp).showing(i " $scrutineeTp <: $patternTp = $result" , debug)
289286 case _ =>
290287 // Give up if we don't get AppliedType, e.g. if we upcasted to Any.
291288 // Note that this doesn't mean that patternTp, scrutineeTp cannot possibly
0 commit comments