Skip to content

Commit b897386

Browse files
committed
Avoid automatism for passing variances to PolyTypes
We used to "fill-in" with zeroes if variances were missing. I now think that;'s too error-prone. Better define all variances explicitly.
1 parent 17469b8 commit b897386

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scala.annotation.{ switch, meta }
99
import scala.collection.{ mutable, immutable }
1010
import PartialFunction._
1111
import collection.mutable
12+
import util.common.alwaysZero
1213
import scala.reflect.api.{ Universe => ApiUniverse }
1314

1415
object Definitions {
@@ -152,7 +153,7 @@ class Definitions {
152153
resultTypeFn: PolyType => Type, flags: FlagSet = EmptyFlags) = {
153154
val tparamNames = tpnme.syntheticTypeParamNames(typeParamCount)
154155
val tparamBounds = tparamNames map (_ => TypeBounds.empty)
155-
val ptype = PolyType(tparamNames)(_ => tparamBounds, resultTypeFn)
156+
val ptype = PolyType(tparamNames, tparamNames.map(alwaysZero))(_ => tparamBounds, resultTypeFn)
156157
enterMethod(cls, name, ptype, flags)
157158
}
158159

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,7 @@ object Types {
26032603
case t => mapOver(t)
26042604
}
26052605
}
2606-
PolyType(paramNames ++ that.paramNames)(
2606+
PolyType(paramNames ++ that.paramNames, variances ++ that.variances)(
26072607
x => this.paramBounds.mapConserve(_.subst(this, x).bounds) ++
26082608
that.paramBounds.mapConserve(shift(_).subst(that, x).bounds),
26092609
x => shift(that.resultType).subst(that, x).subst(this, x))
@@ -2634,11 +2634,10 @@ object Types {
26342634
}
26352635

26362636
object PolyType {
2637-
def apply(paramNames: List[TypeName], variances: List[Int] = Nil)(
2637+
def apply(paramNames: List[TypeName], variances: List[Int])(
26382638
paramBoundsExp: PolyType => List[TypeBounds],
26392639
resultTypeExp: PolyType => Type)(implicit ctx: Context): PolyType = {
2640-
val vs = if (variances.isEmpty) paramNames.map(alwaysZero) else variances
2641-
unique(new PolyType(paramNames, vs)(paramBoundsExp, resultTypeExp))
2640+
unique(new PolyType(paramNames, variances)(paramBoundsExp, resultTypeExp))
26422641
}
26432642

26442643
def unapply(tl: PolyType): Some[(List[LambdaParam], Type)] =

compiler/src/dotty/tools/dotc/transform/FullParameterization.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ trait FullParameterization {
101101
}
102102
val ctparams = if (abstractOverClass) clazz.typeParams else Nil
103103
val ctnames = ctparams.map(_.name.unexpandedName)
104+
val ctvariances = ctparams.map(_.variance)
104105

105106
/** The method result type */
106107
def resultType(mapClassParams: Type => Type) = {
@@ -122,14 +123,14 @@ trait FullParameterization {
122123

123124
info match {
124125
case info: PolyType =>
125-
PolyType(info.paramNames ++ ctnames)(
126+
PolyType(info.paramNames ++ ctnames, info.variances ++ ctvariances)(
126127
pt =>
127128
(info.paramBounds.map(mapClassParams(_, pt).bounds) ++
128129
mappedClassBounds(pt)).mapConserve(_.subst(info, pt).bounds),
129130
pt => resultType(mapClassParams(_, pt)).subst(info, pt))
130131
case _ =>
131132
if (ctparams.isEmpty) resultType(identity)
132-
else PolyType(ctnames)(mappedClassBounds, pt => resultType(mapClassParams(_, pt)))
133+
else PolyType(ctnames, ctvariances)(mappedClassBounds, pt => resultType(mapClassParams(_, pt)))
133134
}
134135
}
135136

0 commit comments

Comments
 (0)