-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Unsound type widening for setters with wildcard types #2928
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
Comments
This seems to be an issue with unsound type widening somewhere, and it's specific to var setters, e.g. the following fails to typecheck as expected: class Box[T](private[this] var x: T) {
def get: T = x
def put(elem: T) = x = elem
}
object Test {
def main(args: Array[String]): Unit = {
val s = new Box[String]("")
val i = new Box[Int](3)
var box: Box[_] = s
val sv = box.get
box = i
box.put(sv)
val c: Int = i.get
}
} 14 | box.put(sv)
| ^^
| found: Box[_]#T(sv)
| required: ?1.T
|
| where: ?1 is an unknown value of type Box[_] The same thing should happen for |
See https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/TypeOps.scala#L21-L50 for an explanation of how skolemization is done, for some reason this seems to not be done correctly for var setters. |
To fix scala#2928, we need to compute the member type of an assigment's left-hand side using negative variance.
To fix scala#2928, we need to compute the member type of an assigment's left-hand side using negative variance.
To fix scala#2928, we need to compute the member type of an assigment's left-hand side using negative variance.
Fix #2928: Add special mode for computing the type of a LHS
This might be a known problem. The following code compiles in
dotc
and produces run-time exception. Scalac compiler rejects the code.The text was updated successfully, but these errors were encountered: