Skip to content

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

Closed
liufengyun opened this issue Jul 27, 2017 · 2 comments · Fixed by #2946
Closed

Unsound type widening for setters with wildcard types #2928

liufengyun opened this issue Jul 27, 2017 · 2 comments · Fixed by #2946

Comments

@liufengyun
Copy link
Contributor

This might be a known problem. The following code compiles in dotc and produces run-time exception. Scalac compiler rejects the code.

class Box[T](var v: T)

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.v
   box = i
   box.v = sv

   val c: Int = i.v
 }
}
@smarter
Copy link
Member

smarter commented Jul 28, 2017

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 box.v = sv in your example, but doesn't currently.

@smarter smarter changed the title Restrict wildcard types to immutable vals? Unsound type widening for setters with wildcard types Jul 28, 2017
@smarter
Copy link
Member

smarter commented Jul 28, 2017

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.

odersky added a commit to dotty-staging/dotty that referenced this issue Aug 2, 2017
To fix scala#2928, we need to compute the member type of an assigment's
left-hand side using negative variance.
odersky added a commit to dotty-staging/dotty that referenced this issue Aug 2, 2017
To fix scala#2928, we need to compute the member type of an assigment's
left-hand side using negative variance.
odersky added a commit to dotty-staging/dotty that referenced this issue Aug 16, 2017
To fix scala#2928, we need to compute the member type of an assigment's
left-hand side using negative variance.
odersky added a commit that referenced this issue Aug 21, 2017
Fix #2928: Add special mode for computing the type of a LHS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants