Skip to content
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

Unsoundness of variance checking with regard to constructors. #9549

Closed
scabug opened this issue Nov 5, 2015 · 3 comments
Closed

Unsoundness of variance checking with regard to constructors. #9549

scabug opened this issue Nov 5, 2015 · 3 comments

Comments

@scabug
Copy link

scabug commented Nov 5, 2015

Consider:

    class C[+A] {

      private[this] var y: A = _
      def getY: A = y

      class Inner(x: A) {
        y = x
      }
    }

    object Test {

      def main(args: Array[String]) = {
        val x = new C[String]
        val y: C[Any] = x
        val i = new y.Inner(1)
        val s: String = x.getY
        println(s)
      }
    }

This demonstrates an unsoundness in the variance checking of scalac.
Scalac excludes symbols owned by constructors from the
checking. This is unsound, as can be demonstrated by compiling the test
and observing output of the program run:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at Test$.main(variances-constr.scala:17)
at Test.main(variances-constr.scala)

Dotty allows this code only under -language:Scala2 and issues a migration warning.

@scabug
Copy link
Author

scabug commented Nov 5, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9549?orig=1
Reporter: @odersky
See #8737

@scabug
Copy link
Author

scabug commented Nov 6, 2015

@retronym said:
I guess the "ignore usages in constructors" rule was intended to allow a constructor to accept parameters of the type a covariant type parameter of a directly enclosing class.

class C[+A] { def this(a: A) = this() }

@scabug
Copy link
Author

scabug commented Nov 8, 2015

@retronym said:
Merging with your previous reports of the same issue, #8737

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants