Skip to content

Commit bf0d185

Browse files
committed
Make useVariances tunable
Needs to be passed as a parameter since Namer does not construct a bounds immediately.
1 parent 99b48be commit bf0d185

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3564,29 +3564,32 @@ object Types {
35643564
apply(syntheticParamNames(n))(
35653565
pt => List.fill(n)(TypeBounds.empty), pt => defn.AnyType)
35663566

3567+
override def fromParams[PI <: ParamInfo.Of[TypeName]](params: List[PI], resultType: Type)(implicit ctx: Context): Type =
3568+
fromParams(params, resultType, useVariances = resultType.isInstanceOf[TypeBounds])
3569+
35673570
/** Distributes Lambda inside type bounds. Examples:
35683571
*
35693572
* type T[X] = U becomes type T = [X] -> U
35703573
* type T[X] <: U becomes type T >: Nothing <: ([X] -> U)
35713574
* type T[X] >: L <: U becomes type T >: ([X] -> L) <: ([X] -> U)
35723575
*/
3573-
override def fromParams[PI <: ParamInfo.Of[TypeName]](params: List[PI], resultType: Type)(implicit ctx: Context): Type = {
3574-
def expand(tp: Type, useVariances: Boolean) = params match
3575-
case (param: Symbol) :: _ if useVariances =>
3576+
def fromParams[PI <: ParamInfo.Of[TypeName]](params: List[PI], resultType: Type, useVariances: Boolean)(implicit ctx: Context): Type = {
3577+
def expand(tp: Type, useVariances: Boolean) =
3578+
if params.nonEmpty then
35763579
apply(params.map(_.paramName), params.map(_.paramVariance))(
35773580
tl => params.map(param => toPInfo(tl.integrate(params, param.paramInfo))),
3578-
tl => tl.integrate(params, tp.resultType))
3579-
case _ =>
3581+
tl => tl.integrate(params, tp))
3582+
else
35803583
super.fromParams(params, tp)
35813584
resultType match {
35823585
case rt: AliasingBounds =>
3583-
rt.derivedAlias(expand(rt.alias, true))
3586+
rt.derivedAlias(expand(rt.alias, useVariances))
35843587
case rt @ TypeBounds(lo, hi) =>
35853588
rt.derivedTypeBounds(
35863589
if (lo.isRef(defn.NothingClass)) lo else expand(lo, false),
3587-
expand(hi, true))
3590+
expand(hi, useVariances))
35883591
case rt =>
3589-
expand(rt, false)
3592+
expand(rt, useVariances)
35903593
}
35913594
}
35923595
}

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ class Namer { typer: Typer =>
15051505
}
15061506

15071507
def typeDefSig(tdef: TypeDef, sym: Symbol, tparamSyms: List[TypeSymbol])(implicit ctx: Context): Type = {
1508-
def abstracted(tp: Type): Type = HKTypeLambda.fromParams(tparamSyms, tp)
1508+
def abstracted(tp: Type): Type = HKTypeLambda.fromParams(tparamSyms, tp, useVariances = true)
15091509
val dummyInfo1 = abstracted(TypeBounds.empty)
15101510
sym.info = dummyInfo1
15111511
sym.setFlag(Provisional)

compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ object VarianceChecker {
2828
def checkLambda(tree: tpd.LambdaTypeTree)(implicit ctx: Context): Unit = {
2929
def checkType(tl: HKTypeLambda): Unit = {
3030
val checkOK = new TypeAccumulator[Boolean] {
31+
def paramVarianceSign(tref: TypeParamRef) =
32+
tl.typeParams(tref.paramNum).paramVarianceSign
3133
def error(tref: TypeParamRef) = {
32-
val VariantName(paramName, v) = tl.paramNames(tref.paramNum).toTermName
34+
val paramName = tl.paramNames(tref.paramNum).toTermName
35+
val v = paramVarianceSign(tref)
3336
val paramVarianceStr = if (v == 0) "contra" else "co"
3437
val occursStr = variance match {
3538
case -1 => "contra"
@@ -45,7 +48,7 @@ object VarianceChecker {
4548
def apply(x: Boolean, t: Type) = x && {
4649
t match {
4750
case tref: TypeParamRef if tref.binder `eq` tl =>
48-
val v = tl.typeParams(tref.paramNum).paramVarianceSign
51+
val v = paramVarianceSign(tref)
4952
varianceConforms(variance, v) || { error(tref); false }
5053
case AnnotatedType(_, annot) if annot.symbol == defn.UncheckedVarianceAnnot =>
5154
x

0 commit comments

Comments
 (0)