From 1e280d03886e3b20fa28661b0d25b6b11dec56f3 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 5 Nov 2015 14:13:14 +0100 Subject: Handle variance unsoundness in scalac The included test pos-special/variances-constr.scala 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. --- tests/pos-special/variances-constr.scala | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/pos-special/variances-constr.scala (limited to 'tests/pos-special') diff --git a/tests/pos-special/variances-constr.scala b/tests/pos-special/variances-constr.scala new file mode 100644 index 000000000..b210b0440 --- /dev/null +++ b/tests/pos-special/variances-constr.scala @@ -0,0 +1,21 @@ +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) + } +} + -- cgit v1.2.3