Skip to content

Commit 72c7a90

Browse files
authored
Merge pull request #2946 from dotty-staging/fix-#2928
Fix #2928: Add special mode for computing the type of a LHS
2 parents 076df81 + 4f624fc commit 72c7a90

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

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

tests/neg/i2928.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Box[T22](var v: T22)
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
val s = new Box[String]("")
6+
val i = new Box[Int](3)
7+
8+
var box: Box[_] = s
9+
val sv = box.v
10+
box = i
11+
box.v = sv // error
12+
13+
val c: Int = i.v
14+
}
15+
}

0 commit comments

Comments
 (0)