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 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
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)
}
22 changes: 20 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,22 @@ class Namer { typer: Typer =>
}
sym.info = dummyInfo2

// Treat the parameters of an upper type lambda bound 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 =>
def recur(tp: Type): Type = tp match
case tp: HKTypeLambda if !tp.isDeclaredVarianceLambda =>
tp.withVariances(tp.paramNames.map(alwaysInvariant))
.derivedLambdaType(resType = recur(tp.resType))
case tp => tp
tp.derivedTypeBounds(tp.lo, recur(tp.hi))
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)
}
11 changes: 11 additions & 0 deletions tests/neg/i9061.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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
case class Covarify2[+F[+X] <: ([A] =>> Any), +A](fa: F[Int][A]) // error: covariant type A occurs in invariant position in type F[A] of value fa
case class Covarify3[+F <: [X] =>> [A] =>> Any, +A](fa: F[Int][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"))
}