Skip to content

Preserve HKTypeLambdas representing kinds in wildApprox #6240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ object ProtoTypes {
wildApprox(tp.refinedInfo, theMap, seen))
case tp: AliasingBounds => // default case, inlined for speed
tp.derivedAlias(wildApprox(tp.alias, theMap, seen))
case tp @ TypeParamRef(hktl: HKTypeLambda, _) if representsKind(hktl) => tp
case tp @ TypeParamRef(poly, pnum) =>
def wildApproxBounds(bounds: TypeBounds) =
if (seen.contains(tp)) WildcardType
Expand Down Expand Up @@ -653,6 +654,18 @@ object ProtoTypes {

final def wildApprox(tp: Type)(implicit ctx: Context): Type = wildApprox(tp, null, Set.empty)

final def representsKind(tl: HKTypeLambda)(implicit ctx: Context): Boolean = {
tl.paramInfos.forall {
case TypeBounds(lo, hi) =>
def isKindBound(tp: Type): Boolean =
Inferencing.isFullyDefined(tp, ForceDegree.none) || (tp match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have some explanations about the logic here as a comment. In particular I did not understand why a fully defined bounds represents a kind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also worried about performance implications. isFullyDefined is not cheap, and wildApprox is a hotspot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't isKindBound also need to return true for AnyKind? If not, we could use a comment explaining why that's the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just finishing up with a different approach ... no need to continue reviewing this.

case tp: HKTypeLambda => representsKind(tp)
case _ => false
})
isKindBound(lo) && isKindBound(hi)
}
}

@sharable object AssignProto extends UncachedGroundType with MatchAlways

private[ProtoTypes] class WildApproxMap(val seen: Set[TypeParamRef])(implicit ctx: Context) extends TypeMap {
Expand Down
121 changes: 121 additions & 0 deletions tests/pos/i6238.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
object K1 {
class Foo[T]

class Bar[F[_]]
object Bar {
implicit def barF[F[_]](implicit fooF: Foo[Bar[F]]): Bar[F] = null
}

class A[T]
object A {
implicit def fooA[F[_[_]]](implicit barB: F[B]): Foo[F[A]] = null
}

class B[T]
object B {
implicit def fooB[F[_[_]]]: Foo[F[B]] = null
}
}

object K1U {
class Foo[T]

class Bar[F[_ <: Int]]
object Bar {
implicit def barF[F[_ <: Int]](implicit fooF: Foo[Bar[F]]): Bar[F] = null
}

class A[T <: Int]
object A {
implicit def fooA[F[_[_ <: Int]]](implicit barB: F[B]): Foo[F[A]] = null
}

class B[T <: Int]
object B {
implicit def fooB[F[_[_ <: Int]]]: Foo[F[B]] = null
}
}

object K1L {
class Foo[T]

class Bar[F[_ >: Int]]
object Bar {
implicit def barF[F[_ >: Int]](implicit fooF: Foo[Bar[F]]): Bar[F] = null
}

class A[T >: Int]
object A {
implicit def fooA[F[_[_ >: Int]]](implicit barB: F[B]): Foo[F[A]] = null
}

class B[T >: Int]
object B {
implicit def fooB[F[_[_ >: Int]]]: Foo[F[B]] = null
}
}

object K11 {
class Foo[T]

class Bar[F[_[_]]]
object Bar {
implicit def barF[F[_[_]]](implicit fooF: Foo[Bar[F]]): Bar[F] = null
}

class A[T[_]]
object A {
implicit def fooA[F[_[_[_]]]](implicit barB: F[B]): Foo[F[A]] = null
}

class B[T[_]]
object B {
implicit def fooB[F[_[_[_]]]]: Foo[F[B]] = null
}
}

object K2 {
class Foo[T]

class Bar[F[_, _]]
object Bar {
implicit def barF[F[_, _]](implicit fooF: Foo[Bar[F]]): Bar[F] = null
}

class A[T, U]
object A {
implicit def fooA[F[_[_, _]]](implicit barB: F[B]): Foo[F[A]] = null
}

class B[T, U]
object B {
implicit def fooB[F[_[_, _]]]: Foo[F[B]] = null
}
}

object Test {
{
import K1._
implicitly[Bar[A]]
}

{
import K1U._
implicitly[Bar[A]]
}

{
import K1L._
implicitly[Bar[A]]
}

{
import K11._
implicitly[Bar[A]]
}

{
import K2._
implicitly[Bar[A]]
}
}
50 changes: 50 additions & 0 deletions tests/run/implicit-functors2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
object Utils {
type Id[t] = t
type Const[c] = [t] => c
}

import Utils._

abstract class ErasedInstances { type FT }
class ErasedProductInstances(override val toString: String) extends ErasedInstances
class ErasedCoproductInstances(override val toString: String) extends ErasedInstances

object K1 {
type Instances[F[_[_]], T[_]] = ErasedInstances { type FT = F[T] ; type C = F }
}

class Functor[F[_]](override val toString: String)

object Functor {
inline def apply[F[_]](implicit ff: Functor[F]): Functor[F] = ff

implicit val functorId: Functor[Id] = new Functor("functorId")

implicit def functorNested[F[_], G[_]](implicit ff: Functor[F], fg: Functor[G]): Functor[[t] => F[G[t]]] = new Functor(s"functorNested($ff, $fg)")

implicit def functorGen[F[_]](implicit inst: K1.Instances[Functor, F]): Functor[F] = new Functor(s"functorGen($inst")

implicit def functorConst[T]: Functor[Const[T]] = new Functor(s"functorConst")
}

sealed trait Opt[+A]
object Opt {
implicit def optInstances[F[_[_]]](implicit fs: F[Sm], fn: F[[t] => Nn.type]): ErasedCoproductInstances { type FT = F[Opt] ; type C = F } =
new ErasedCoproductInstances(s"optInstances($fs, $fn)") { type FT = F[Opt] ; type C = F }
}

case class Sm[+A](value: A) extends Opt[A]
object Sm {
implicit def smInstances[F[_[_]]](implicit fi: F[Id]): ErasedProductInstances { type FT = F[Sm] ; type C = F } =
new ErasedProductInstances(s"smInstances($fi)") { type FT = F[Sm] ; type C = F }
}

case object Nn extends Opt[Nothing]

object Test extends App {
assert(Functor[Const[Nn.type]].toString == "functorConst")
assert(Functor[Sm].toString == "functorGen(smInstances(functorId)")
assert(Functor[Opt].toString == "functorGen(optInstances(functorGen(smInstances(functorId), functorConst)")
assert(Functor[[t] => Opt[Opt[t]]].toString == "functorNested(functorGen(optInstances(functorGen(smInstances(functorId), functorConst), functorGen(optInstances(functorGen(smInstances(functorId), functorConst))")
}