Skip to content

Fix #9061: Treat type F <: [X] => G like type F[X] <: G. #9084

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

Merged
merged 2 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Variances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,6 @@ object Variances {
if (v > 0) "+"
else if (v < 0) "-"
else ""

val alwaysInvariant: Any => Invariant.type = Function.const(Invariant)
}
20 changes: 18 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import util.Spans._
import util.Property
import collection.mutable
import tpd.ListOfTreeDecorator
import Variances.alwaysInvariant
import config.{Config, Feature}
import config.Printers.typr
import Annotations._
Expand Down Expand Up @@ -968,7 +969,10 @@ class Namer { typer: Typer =>
override final def typeSig(sym: Symbol): Type =
val tparamSyms = completerTypeParams(sym)(ictx)
given ctx as Context = nestedCtx
def abstracted(tp: TypeBounds): TypeBounds = HKTypeLambda.boundsFromParams(tparamSyms, tp)

def abstracted(tp: TypeBounds): TypeBounds =
HKTypeLambda.boundsFromParams(tparamSyms, tp)

val dummyInfo1 = abstracted(TypeBounds.empty)
sym.info = dummyInfo1
sym.setFlag(Provisional)
Expand Down Expand Up @@ -998,8 +1002,20 @@ class Namer { typer: Typer =>
}
sym.info = dummyInfo2

// If this type does not have type parameters itself, treat the parameters
// of a type lambda on the RHS as non-variant. E.g.
// type F <: [X] =>> G and type F[X] <: G
// are treated alike.
def addVariances(tp: Type): Type = tp match
case tp @ TypeBounds(lo, hi) =>
tp.derivedTypeBounds(addVariances(lo), addVariances(hi))
case tp: HKTypeLambda if tparamSyms.isEmpty && !tp.isDeclaredVarianceLambda =>
Copy link
Member

Choose a reason for hiding this comment

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

This won't catch curried type lambdas like:

F[X] <: [A] =>> Any

which are also problematic:

case class Contra[-A](f: A => Int)

case class Covarify[+F[X] <: ([A] =>> Any), +A](fa: F[Int][A])

@main def main = {
  val x = Covarify[[X] =>> Contra, Int](Contra[Int](_ + 5))
  val y: Covarify[[X] =>> Contra, Any] = x
  println(y.fa.f("abc"))
}

tp.withVariances(tp.paramNames.map(alwaysInvariant))
case _ =>
tp

val rhs1 = typedAheadType(rhs)
val rhsBodyType: TypeBounds = rhs1.tpe.toBounds
val rhsBodyType: TypeBounds = addVariances(rhs1.tpe).toBounds
val unsafeInfo = if (isDerived) rhsBodyType else abstracted(rhsBodyType)

def opaqueToBounds(info: Type): Type =
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/util/common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import core.Types.WildcardType
/** Common values hoisted out for performance */
object common {

val alwaysTrue: Any => Boolean = Function.const(true) _
val alwaysFalse: Any => Boolean = Function.const(false) _
val alwaysZero: Any => Int = Function.const(0) _
val alwaysWildcardType: Any => WildcardType.type = Function.const(WildcardType) _
val alwaysTrue: Any => Boolean = Function.const(true)
val alwaysFalse: Any => Boolean = Function.const(false)
val alwaysZero: Any => Int = Function.const(0)
val alwaysWildcardType: Any => WildcardType.type = Function.const(WildcardType)
}
9 changes: 9 additions & 0 deletions tests/neg/i9061.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
case class Contra[-A](f: A => Int)

case class Covarify[+F <: ([A] =>> Any), +A](fa: F[A]) // error: covariant type A occurs in invariant position in type F[A] of value fa

@main def main = {
val x = Covarify[Contra, Int](Contra[Int](_ + 5))
val y: Covarify[Contra, Any] = x
println(y.fa.f("abc"))
}