Skip to content

Commit 337fc94

Browse files
oderskyBlaisorblade
authored andcommitted
Fix #2973: Check variances of class parents
1 parent 7655503 commit 337fc94

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1509,11 +1509,11 @@ class Typer extends Namer
15091509
checkNoDoubleDefs(cls)
15101510
val impl1 = cpy.Template(impl)(constr1, parents1, self1, body1)
15111511
.withType(dummy.termRef)
1512-
checkVariance(impl1)
15131512
if (!cls.is(AbstractOrTrait) && !ctx.isAfterTyper)
15141513
checkRealizableBounds(cls, cdef.namePos)
15151514
if (cls.is(Case) && cls.derivesFrom(defn.EnumClass)) checkEnum(cdef, cls)
15161515
val cdef1 = assignType(cpy.TypeDef(cdef)(name, impl1), cls)
1516+
checkVariance(cdef1)
15171517
if (ctx.phase.isTyper && cdef1.tpe.derivesFrom(defn.DynamicClass) && !ctx.dynamicsEnabled) {
15181518
val isRequired = parents1.exists(_.tpe.isRef(defn.DynamicClass))
15191519
ctx.featureWarning(nme.dynamics.toString, "extension of type scala.Dynamic", isScala2Feature = true,

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ class VarianceChecker()(implicit ctx: Context) {
9393
this(status, tp.resultType) // params will be checked in their TypeDef or ValDef nodes.
9494
case AnnotatedType(_, annot) if annot.symbol == defn.UncheckedVarianceAnnot =>
9595
status
96-
//case tp: ClassInfo =>
97-
// ??? not clear what to do here yet. presumably, it's all checked at local typedefs
96+
case tp: ClassInfo =>
97+
foldOver(status, tp.classParents)
9898
case _ =>
9999
foldOver(status, tp)
100100
}
@@ -132,14 +132,16 @@ class VarianceChecker()(implicit ctx: Context) {
132132
ctx.debuglog(s"Skipping variance check of ${sym.showDcl}")
133133
case tree: TypeDef =>
134134
checkVariance(sym, tree.pos)
135+
tree.rhs match {
136+
case rhs: Template => traverseChildren(rhs)
137+
case _ =>
138+
}
135139
case tree: ValDef =>
136140
checkVariance(sym, tree.pos)
137141
case DefDef(_, tparams, vparamss, _, _) =>
138142
checkVariance(sym, tree.pos)
139143
tparams foreach traverse
140144
vparamss foreach (_ foreach traverse)
141-
case Template(_, _, _, body) =>
142-
traverseChildren(tree)
143145
case _ =>
144146
}
145147
}

tests/neg/i2973.scala

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Foo[U]
2+
class Bar[-T] extends Foo[T] // error: contravariant type T occurs in invariant position

0 commit comments

Comments
 (0)