Skip to content

Commit 92007e5

Browse files
committed
Fix #2928: Add special mode for computing the type of a LHS
To fix #2928, we need to compute the member type of an assigment's left-hand side using negative variance.
1 parent 16e6c9b commit 92007e5

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
586586
lhsCore.tpe match {
587587
case ref: TermRef =>
588588
val lhsVal = lhsCore.denot.suchThat(!_.is(Method))
589-
if (canAssign(lhsVal.symbol))
590-
assignType(cpy.Assign(tree)(lhs1, typed(tree.rhs, lhsVal.info)))
589+
if (canAssign(lhsVal.symbol)) {
590+
// lhsBounds: (T .. Any) as seen from lhs prefix, where T is the type of lhsVal.symbol
591+
// This ensures we do the as-seen-from on T with variance -1. Test case neg/i2928.scala
592+
val lhsBounds =
593+
TypeBounds.lower(lhsVal.symbol.info).asSeenFrom(ref.prefix, lhsVal.symbol.owner)
594+
assignType(cpy.Assign(tree)(lhs1, typed(tree.rhs, lhsBounds.loBound)))
595+
}
591596
else {
592597
val pre = ref.prefix
593598
val setterName = ref.name.setterName

0 commit comments

Comments
 (0)