Skip to content

Fix #7567: Variance checking fixes #7573

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

Merged
merged 1 commit into from
Nov 18, 2019
Merged
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
30 changes: 13 additions & 17 deletions compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,21 @@ class VarianceChecker()(implicit ctx: Context) {
private object Validator extends TypeAccumulator[Option[VarianceError]] {
private var base: Symbol = _

/** Is no variance checking needed within definition of `base`? */
def ignoreVarianceIn(base: Symbol): Boolean = (
base.isTerm
|| base.is(Package)
|| base.isAllOf(PrivateLocal)
)

/** The variance of a symbol occurrence of `tvar` seen at the level of the definition of `base`.
* The search proceeds from `base` to the owner of `tvar`.
* Initially the state is covariant, but it might change along the search.
*/
def relativeVariance(tvar: Symbol, base: Symbol, v: Variance = Covariant): Variance = /*trace(i"relative variance of $tvar wrt $base, so far: $v")*/
if (base == tvar.owner) v
else if (base.is(Param) && base.owner.isTerm)
if base == tvar.owner then
v
else if base.is(Param) && base.owner.isTerm && !base.owner.isAllOf(PrivateLocal) then
relativeVariance(tvar, paramOuter(base.owner), flip(v))
else if (ignoreVarianceIn(base.owner)) Bivariant
else if (base.isAliasType) relativeVariance(tvar, base.owner, Invariant)
else relativeVariance(tvar, base.owner, v)
else if base.owner.isTerm || base.owner.is(Package) || base.isAllOf(PrivateLocal) then
Bivariant
else if base.isAliasType then
relativeVariance(tvar, base.owner, Invariant)
else
relativeVariance(tvar, base.owner, v)

/** The next level to take into account when determining the
* relative variance with a method parameter as base. The method
Expand Down Expand Up @@ -189,11 +186,10 @@ class VarianceChecker()(implicit ctx: Context) {
override def traverse(tree: Tree)(implicit ctx: Context) = {
def sym = tree.symbol
// No variance check for private/protected[this] methods/values.
def skip =
!sym.exists ||
sym.isAllOf(PrivateLocal) ||
sym.name.is(InlineAccessorName) || // TODO: should we exclude all synthetic members?
sym.is(TypeParam) && sym.owner.isClass // already taken care of in primary constructor of class
def skip = !sym.exists
|| sym.name.is(InlineAccessorName) // TODO: should we exclude all synthetic members?
|| sym.isAllOf(LocalParamAccessor) // local class parameters are construction only
|| sym.is(TypeParam) && sym.owner.isClass // already taken care of in primary constructor of class
try tree match {
case defn: MemberDef if skip =>
ctx.debuglog(s"Skipping variance check of ${sym.showDcl}")
Expand Down
13 changes: 13 additions & 0 deletions tests/neg/i7567.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class A
class B extends A
class C extends A

object Foo {
private[this] class Bar[+T](var x: T) // error: covariant type T occurs in contravariant position in type T of value x_=
def foo: B = {
val barB: Bar[B] = new Bar(new B)
val barA: Bar[A] = barB
barA.x = new C
barB.x
}
}