@@ -556,6 +556,13 @@ trait ConstraintHandling {
556556 inst
557557 end approximation
558558
559+ private def isTransparent (tp : Type , traitOnly : Boolean )(using Context ): Boolean = tp match
560+ case AndType (tp1, tp2) =>
561+ isTransparent(tp1, traitOnly) && isTransparent(tp2, traitOnly)
562+ case _ =>
563+ val cls = tp.underlyingClassRef(refinementOK = false ).typeSymbol
564+ cls.isTransparentClass && (! traitOnly || cls.is(Trait ))
565+
559566 /** If `tp` is an intersection such that some operands are transparent trait instances
560567 * and others are not, replace as many transparent trait instances as possible with Any
561568 * as long as the result is still a subtype of `bound`. But fall back to the
@@ -568,18 +575,17 @@ trait ConstraintHandling {
568575 var dropped : List [Type ] = List () // the types dropped so far, last one on top
569576
570577 def dropOneTransparentTrait (tp : Type ): Type =
571- val tpd = tp.dealias
572- if tpd.typeSymbol.isTransparentTrait && ! tpd.isLambdaSub && ! kept.contains(tpd) then
573- dropped = tpd :: dropped
578+ if isTransparent(tp, traitOnly = true ) && ! kept.contains(tp) then
579+ dropped = tp :: dropped
574580 defn.AnyType
575- else tpd match
581+ else tp match
576582 case AndType (tp1, tp2) =>
577583 val tp1w = dropOneTransparentTrait(tp1)
578584 if tp1w ne tp1 then tp1w & tp2
579585 else
580586 val tp2w = dropOneTransparentTrait(tp2)
581587 if tp2w ne tp2 then tp1 & tp2w
582- else tpd
588+ else tp
583589 case _ =>
584590 tp
585591
@@ -654,7 +660,16 @@ trait ConstraintHandling {
654660
655661 val wideInst =
656662 if isSingleton(bound) then inst
657- else dropTransparentTraits(widenIrreducible(widenOr(widenSingle(inst))), bound)
663+ else
664+ val widenedFromSingle = widenSingle(inst)
665+ val widenedFromUnion = widenOr(widenedFromSingle)
666+ val widened =
667+ if (widenedFromUnion ne widenedFromSingle) && isTransparent(widenedFromUnion, traitOnly = false ) then
668+ widenedFromSingle
669+ else
670+ dropTransparentTraits(widenedFromUnion, bound)
671+ widenIrreducible(widened)
672+
658673 wideInst match
659674 case wideInst : TypeRef if wideInst.symbol.is(Module ) =>
660675 TermRef (wideInst.prefix, wideInst.symbol.sourceModule)
0 commit comments