diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index ca20fe086716..2a609e1deec9 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -4650,7 +4650,13 @@ object Types extends TypeUtils { cachedSuper = tycon match case tycon: HKTypeLambda => defn.AnyType case tycon: TypeRef if tycon.symbol.isClass => tycon - case tycon: TypeProxy => tycon.superType.applyIfParameterized(args) + case tycon: TypeProxy => + if validSuper != Nowhere && args.exists(_.isProvisional) then + // applyIfParameterized may perform eta-reduction leading to different + // variance annotations depending on the instantiation of type params + // see tests/pos/typeclass-encoding3b.scala:348 for an example + validSuper = Nowhere + tycon.superType.applyIfParameterized(args) case _ => defn.AnyType cachedSuper @@ -4678,8 +4684,8 @@ object Types extends TypeUtils { */ override def underlyingMatchType(using Context): Type = if ctx.period != validUnderlyingMatch then - validUnderlyingMatch = if tycon.isProvisional then Nowhere else ctx.period cachedUnderlyingMatch = superType.underlyingMatchType + validUnderlyingMatch = validSuper cachedUnderlyingMatch override def tryNormalize(using Context): Type = tycon.stripTypeVar match { diff --git a/tests/neg/typeclass-encoding3.scala b/tests/pos/typeclass-encoding3b.scala similarity index 95% rename from tests/neg/typeclass-encoding3.scala rename to tests/pos/typeclass-encoding3b.scala index ff403314cd1a..8ff416728718 100644 --- a/tests/neg/typeclass-encoding3.scala +++ b/tests/pos/typeclass-encoding3b.scala @@ -345,5 +345,12 @@ object functors { } MonadFlatten.flattened(List(List(1, 2, 3), List(4, 5))) // ok, synthesizes (using ListMonad) - MonadFlatten.flattened(List(List(1, 2, 3), List(4, 5)))(using ListMonad) // error + MonadFlatten.flattened(List(List(1, 2, 3), List(4, 5)))(using ListMonad) // was an error + /* + Before the changes, when checking `ListMonad <:< functors.Monad.Impl[T]` + we eventually got to the comparison `[X] =>> T[X] <:< [+X] =>> List[X]` + because the `This` type member of `ListMonad` has a covariance annotation. + This failed the variance conformance checks despite the fact that T had been instantiated to List, + since it had been substituted into the refinement (and cached) before its instantiation. + */ } \ No newline at end of file