diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index cda643fee469..37c2ccb366e3 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1509,11 +1509,11 @@ class Typer extends Namer checkNoDoubleDefs(cls) val impl1 = cpy.Template(impl)(constr1, parents1, self1, body1) .withType(dummy.termRef) - checkVariance(impl1) if (!cls.is(AbstractOrTrait) && !ctx.isAfterTyper) checkRealizableBounds(cls, cdef.namePos) if (cls.is(Case) && cls.derivesFrom(defn.EnumClass)) checkEnum(cdef, cls) val cdef1 = assignType(cpy.TypeDef(cdef)(name, impl1), cls) + checkVariance(cdef1) if (ctx.phase.isTyper && cdef1.tpe.derivesFrom(defn.DynamicClass) && !ctx.dynamicsEnabled) { val isRequired = parents1.exists(_.tpe.isRef(defn.DynamicClass)) ctx.featureWarning(nme.dynamics.toString, "extension of type scala.Dynamic", isScala2Feature = true, diff --git a/compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala b/compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala index 39d635863426..1a83a642a044 100644 --- a/compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala +++ b/compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala @@ -93,8 +93,8 @@ class VarianceChecker()(implicit ctx: Context) { this(status, tp.resultType) // params will be checked in their TypeDef or ValDef nodes. case AnnotatedType(_, annot) if annot.symbol == defn.UncheckedVarianceAnnot => status - //case tp: ClassInfo => - // ??? not clear what to do here yet. presumably, it's all checked at local typedefs + case tp: ClassInfo => + foldOver(status, tp.classParents) case _ => foldOver(status, tp) } @@ -132,14 +132,16 @@ class VarianceChecker()(implicit ctx: Context) { ctx.debuglog(s"Skipping variance check of ${sym.showDcl}") case tree: TypeDef => checkVariance(sym, tree.pos) + tree.rhs match { + case rhs: Template => traverseChildren(rhs) + case _ => + } case tree: ValDef => checkVariance(sym, tree.pos) case DefDef(_, tparams, vparamss, _, _) => checkVariance(sym, tree.pos) tparams foreach traverse vparamss foreach (_ foreach traverse) - case Template(_, _, _, body) => - traverseChildren(tree) case _ => } } diff --git a/tests/neg/i2973.scala b/tests/neg/i2973.scala new file mode 100644 index 000000000000..db351f428e78 --- /dev/null +++ b/tests/neg/i2973.scala @@ -0,0 +1,2 @@ +class Foo[U] +class Bar[-T] extends Foo[T] // error: contravariant type T occurs in invariant position