From 26dfa54ef31687e4c3e5b85be476ee5759f77950 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 18 Aug 2013 12:32:17 +0200 Subject: SI-7763 Avoid dropping casts in erasure 466b7d29f avoided quadratic complexity in Erasure's treatment of chained `asInstanceOf` calls. It did so by using the typechecked qualifier, rather than discarding it. However, that also dropped the cast altogether! In many cases this was masked by later inclusion of a cast to the expected type by `adaptToType`: at scala.tools.nsc.transform.Erasure$Eraser.cast(Erasure.scala:636) at scala.tools.nsc.transform.Erasure$Eraser.scala$tools$nsc$transform$Erasure$Eraser$$adaptToType(Erasure.scala:665) at scala.tools.nsc.transform.Erasure$Eraser.adapt(Erasure.scala:766) at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5352) This commit re-wraps the typechecked `qual` in its original `.asInstanceOf[T]` to preserve semantics while avoiding the big-O blowup. The test includes the compiler option `-Ynooptimize` because dead code elimination *also* thinks that this cast is superfluous. I'll follow up on that problem seprately. --- test/files/run/t7763.flags | 1 + test/files/run/t7763.scala | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/files/run/t7763.flags create mode 100644 test/files/run/t7763.scala (limited to 'test/files/run') diff --git a/test/files/run/t7763.flags b/test/files/run/t7763.flags new file mode 100644 index 0000000000..7f4215d1bc --- /dev/null +++ b/test/files/run/t7763.flags @@ -0,0 +1 @@ +-Ynooptimize \ No newline at end of file diff --git a/test/files/run/t7763.scala b/test/files/run/t7763.scala new file mode 100644 index 0000000000..638077e64a --- /dev/null +++ b/test/files/run/t7763.scala @@ -0,0 +1,20 @@ +object Test { + class A; class B + def main(args: Array[String]) { + def noExpectedType() { + a().asInstanceOf[B] // cast elided! + } + def withExpectedType(): B = { + a().asInstanceOf[B] + } + def test(a: => Any) = try { + a + sys.error("no CCE!") + } catch {case _: ClassCastException => } + + test(noExpectedType()) + test(withExpectedType()) + } + + def a(): Object = new A +} -- cgit v1.2.3