Skip to content

Fix #2973: Check variances of class parents #4057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This visit sees the parent classes, but stripped of any annotations!

case _ =>
foldOver(status, tp)
}
Expand Down Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On tests/pos/Iterable.scala we get an error inside this call (going through tp.classParents), because sym contains the parens but without annotations!

tree.rhs match {
case rhs: Template => traverseChildren(rhs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here traverseChildren sees parents (in the form of a constructor call), but this Traverser ignores terms and only looks at definitions...

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 _ =>
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/neg/i2973.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Foo[U]
class Bar[-T] extends Foo[T] // error: contravariant type T occurs in invariant position