Skip to content

Commit 7acd75d

Browse files
oderskyBlaisorblade
authored andcommitted
Fix #2973: Check variances of class parents
1 parent a91d82a commit 7acd75d

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,11 +1572,11 @@ class Typer extends Namer
15721572
checkNoDoubleDeclaration(cls)
15731573
val impl1 = cpy.Template(impl)(constr1, parents1, self1, body1)
15741574
.withType(dummy.termRef)
1575-
checkVariance(impl1)
15761575
if (!cls.is(AbstractOrTrait) && !ctx.isAfterTyper)
15771576
checkRealizableBounds(cls, cdef.namePos)
15781577
if (cls.is(Case) && cls.derivesFrom(defn.EnumClass)) checkEnum(cdef, cls)
15791578
val cdef1 = assignType(cpy.TypeDef(cdef)(name, impl1), cls)
1579+
checkVariance(cdef1)
15801580
if (ctx.phase.isTyper && cdef1.tpe.derivesFrom(defn.DynamicClass) && !ctx.dynamicsEnabled) {
15811581
val isRequired = parents1.exists(_.tpe.isRef(defn.DynamicClass))
15821582
ctx.featureWarning(nme.dynamics.toString, "extension of type scala.Dynamic", isScala2Feature = true,

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class VarianceChecker()(implicit ctx: Context) {
9595
this(status, tp.resultType) // params will be checked in their TypeDef or ValDef nodes.
9696
case AnnotatedType(_, annot) if annot.symbol == defn.UncheckedVarianceAnnot =>
9797
status
98-
//case tp: ClassInfo =>
99-
// ??? not clear what to do here yet. presumably, it's all checked at local typedefs
98+
case tp: ClassInfo =>
99+
foldOver(status, tp.classParents)
100100
case _ =>
101101
foldOver(status, tp)
102102
}
@@ -142,14 +142,16 @@ class VarianceChecker()(implicit ctx: Context) {
142142
ctx.debuglog(s"Skipping variance check of ${sym.showDcl}")
143143
case tree: TypeDef =>
144144
checkVariance(sym, tree.pos)
145+
tree.rhs match {
146+
case rhs: Template => traverseChildren(rhs)
147+
case _ =>
148+
}
145149
case tree: ValDef =>
146150
checkVariance(sym, tree.pos)
147151
case DefDef(_, tparams, vparamss, _, _) =>
148152
checkVariance(sym, tree.pos)
149153
tparams foreach traverse
150154
vparamss foreach (_ foreach traverse)
151-
case Template(_, _, _, body) =>
152-
traverseChildren(tree)
153155
case _ =>
154156
}
155157
}

tests/neg/i2973.scala

Lines changed: 2 additions & 0 deletions
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)