From 16b33a922a1ec8c96c9b0d07bc786211dcd336a8 Mon Sep 17 00:00:00 2001 From: Eugene Flesselle Date: Wed, 5 Jun 2024 15:10:46 +0200 Subject: [PATCH 1/5] Mark AppliedType cachedSuper valid Nowhere when using provisional args --- compiler/src/dotty/tools/dotc/core/Types.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index ca4834558d9a..ea2774b81c85 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -4643,7 +4643,9 @@ 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 validSuper = Nowhere + tycon.superType.applyIfParameterized(args) case _ => defn.AnyType cachedSuper From 59b0f3ae413556ea537c442bb25f93b2a8487245 Mon Sep 17 00:00:00 2001 From: Eugene Flesselle Date: Wed, 5 Jun 2024 15:11:06 +0200 Subject: [PATCH 2/5] Reclassify test --- .../typeclass-encoding3b.scala} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{neg/typeclass-encoding3.scala => pos/typeclass-encoding3b.scala} (99%) diff --git a/tests/neg/typeclass-encoding3.scala b/tests/pos/typeclass-encoding3b.scala similarity index 99% rename from tests/neg/typeclass-encoding3.scala rename to tests/pos/typeclass-encoding3b.scala index ff403314cd1a..84db4c4b5045 100644 --- a/tests/neg/typeclass-encoding3.scala +++ b/tests/pos/typeclass-encoding3b.scala @@ -345,5 +345,5 @@ 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) } \ No newline at end of file From eaa673d5ca2cea3d0c5671898db99b6059c32fe6 Mon Sep 17 00:00:00 2001 From: Eugene Flesselle Date: Thu, 6 Jun 2024 22:20:53 +0200 Subject: [PATCH 3/5] Add explanation doc --- compiler/src/dotty/tools/dotc/core/Types.scala | 6 +++++- tests/pos/typeclass-encoding3b.scala | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index ea2774b81c85..b9379300344e 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -4644,7 +4644,11 @@ object Types extends TypeUtils { case tycon: HKTypeLambda => defn.AnyType case tycon: TypeRef if tycon.symbol.isClass => tycon case tycon: TypeProxy => - if validSuper != Nowhere && args.exists(_.isProvisional) then validSuper = Nowhere + 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 diff --git a/tests/pos/typeclass-encoding3b.scala b/tests/pos/typeclass-encoding3b.scala index 84db4c4b5045..2d5111c4313b 100644 --- a/tests/pos/typeclass-encoding3b.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) + MonadFlatten.flattened(List(List(1, 2, 3), List(4, 5)))(using ListMonad) // was an error + /* + When checking `ListMonad <:< functors.Monad.Impl[T]` + we eventually get to the comparison `[X] =>> T[X] <:< [+X] =>> List[X]` + because the `This` type member of `ListMonad` has a covariance annotation. + This fails the variance conformance checks despite the fact that T has been instantiated to List, + since it has been substituted into the refinement (and cached) before its instantiation. + */ } \ No newline at end of file From ee5481408b09e95ce9becaea510be8687ff27c87 Mon Sep 17 00:00:00 2001 From: Eugene Flesselle Date: Thu, 6 Jun 2024 22:22:24 +0200 Subject: [PATCH 4/5] Keep `validUnderlyingMatch` inline with `validSuper` --- compiler/src/dotty/tools/dotc/core/Types.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index b9379300344e..bd3fa6e6a3dd 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -4677,8 +4677,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 { From 81a118488124bbe009556be5e9a54910de544e67 Mon Sep 17 00:00:00 2001 From: Eugene Flesselle Date: Fri, 7 Jun 2024 11:03:49 +0200 Subject: [PATCH 5/5] Amend doc --- tests/pos/typeclass-encoding3b.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/pos/typeclass-encoding3b.scala b/tests/pos/typeclass-encoding3b.scala index 2d5111c4313b..8ff416728718 100644 --- a/tests/pos/typeclass-encoding3b.scala +++ b/tests/pos/typeclass-encoding3b.scala @@ -347,10 +347,10 @@ 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) // was an error /* - When checking `ListMonad <:< functors.Monad.Impl[T]` - we eventually get to the comparison `[X] =>> T[X] <:< [+X] =>> List[X]` + 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 fails the variance conformance checks despite the fact that T has been instantiated to List, - since it has been substituted into the refinement (and cached) before its instantiation. + 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