@@ -499,28 +499,30 @@ object Inferencing {
499
499
propagate(accu(SimpleIdentityMap .empty, tp))
500
500
}
501
501
502
+ /** Run the transformation after dealiasing but return the original type if it was a no-op. */
503
+ private def derivedOnDealias (tp : Type )(transform : Type => Type )(using Context ) = {
504
+ val dealiased = tp.dealias
505
+ val transformed = transform(dealiased)
506
+ if transformed eq dealiased then tp // return the original type, not the result of dealiasing
507
+ else transformed
508
+ }
509
+
502
510
/** Replace every top-level occurrence of a wildcard type argument by
503
511
* a fresh skolem type. The skolem types are of the form $i.CAP, where
504
512
* $i is a skolem of type `scala.internal.TypeBox`, and `CAP` is its
505
513
* type member. See the documentation of `TypeBox` for a rationale why we do this.
506
514
*/
507
- def captureWildcards (tp : Type )(using Context ): Type = tp match {
515
+ def captureWildcards (tp : Type )(using Context ): Type = derivedOnDealias(tp) {
508
516
case tp @ AppliedType (tycon, args) if tp.hasWildcardArg =>
509
- tycon.typeParams match {
510
- case tparams @ ((_ : Symbol ) :: _) =>
511
- val boundss = tparams.map(_.paramInfo.substApprox(tparams.asInstanceOf [List [TypeSymbol ]], args))
512
- val args1 = args.zipWithConserve(boundss) { (arg, bounds) =>
513
- arg match {
514
- case TypeBounds (lo, hi) =>
515
- val skolem = SkolemType (defn.TypeBoxClass .typeRef.appliedTo(lo | bounds.loBound, hi & bounds.hiBound))
516
- TypeRef (skolem, defn.TypeBox_CAP )
517
- case arg => arg
518
- }
519
- }
520
- tp.derivedAppliedType(tycon, args1)
521
- case _ =>
522
- tp
517
+ val tparams = tycon.typeParamSymbols
518
+ val args1 = args.zipWithConserve(tparams.map(_.paramInfo.substApprox(tparams, args))) {
519
+ case (TypeBounds (lo, hi), bounds) =>
520
+ val skolem = SkolemType (defn.TypeBoxClass .typeRef.appliedTo(lo | bounds.loBound, hi & bounds.hiBound))
521
+ TypeRef (skolem, defn.TypeBox_CAP )
522
+ case (arg, _) =>
523
+ arg
523
524
}
525
+ if tparams.isEmpty then tp else tp.derivedAppliedType(tycon, args1)
524
526
case tp : AndOrType => tp.derivedAndOrType(captureWildcards(tp.tp1), captureWildcards(tp.tp2))
525
527
case tp : RefinedType => tp.derivedRefinedType(captureWildcards(tp.parent), tp.refinedName, tp.refinedInfo)
526
528
case tp : RecType => tp.derivedRecType(captureWildcards(tp.parent))
0 commit comments