Skip to content
Browse files

Merge pull request #699 from retronym/ticket/5162

SI-5162 Exclude super.foo from the erasure cast of SI-4283
  • Loading branch information...
2 parents 30ef129 + 6177066 commit 0a07bb962f87053e217dc34b88f5e46e046bd95a @adriaanm committed Jun 11, 2012
View
27 src/compiler/scala/tools/nsc/transform/Erasure.scala
@@ -1042,18 +1042,27 @@ abstract class Erasure extends AddInterfaces
assert(overridden != NoSymbol, tree.symbol)
tree.symbol = overridden
}
+
def isAccessible(sym: Symbol) = localTyper.context.isAccessible(sym, sym.owner.thisType)
if (!isAccessible(owner) && qual.tpe != null) {
- // Todo: Figure out how qual.tpe could be null in the check above (it does appear in build where SwingWorker.this
- // has a null type).
- val qualSym = qual.tpe.widen.typeSymbol
- if (isAccessible(qualSym) && !qualSym.isPackageClass && !qualSym.isPackageObjectClass) {
- // insert cast to prevent illegal access error (see #4283)
- // util.trace("insert erasure cast ") (*/
- treeCopy.Select(tree, gen.mkAttributedCast(qual, qual.tpe.widen), name) //)
- } else tree
+ qual match {
+ case Super(_, _) =>
+ // Insert a cast here at your peril -- see SI-5162. Bail out if the target method is defined in
+ // Java, otherwise, we'd get an IllegalAccessError at runtime. If the target method is defined in
+ // Scala, however, we should have access.
+ if (owner.isJavaDefined) unit.error(tree.pos, s"Unable to access ${tree.symbol.fullLocationString} with a super reference.")
+ tree
+ case _ =>
+ // Todo: Figure out how qual.tpe could be null in the check above (it does appear in build where SwingWorker.this
+ // has a null type).
+ val qualSym = qual.tpe.widen.typeSymbol
+ if (isAccessible(qualSym) && !qualSym.isPackageClass && !qualSym.isPackageObjectClass) {
+ // insert cast to prevent illegal access error (see #4283)
+ // util.trace("insert erasure cast ") (*/
+ treeCopy.Select(tree, gen.mkAttributedCast(qual, qual.tpe.widen), name) //)
+ } else tree
+ }
} else tree
-
case Template(parents, self, body) =>
assert(!currentOwner.isImplClass)
//Console.println("checking no dble defs " + tree)//DEBUG
View
4 test/files/neg/t4283b.check
@@ -0,0 +1,4 @@
+Test.scala:2: error: Unable to access method f in class AbstractFoo with a super reference.
+ override def f(): Int = super.f()
+ ^
+one error found
View
5 test/files/neg/t4283b/AbstractFoo.java
@@ -0,0 +1,5 @@
+package test;
+
+/* package private */ class AbstractFoo {
+ public int f() { return 2; }
+}
View
0 test/pending/run/t4283/ScalaBipp.scala → test/files/neg/t4283b/ScalaBipp.scala
File renamed without changes.
View
3 test/files/neg/t4283b/Test.scala
@@ -0,0 +1,3 @@
+object Derived extends test.ScalaBipp {
+ override def f(): Int = super.f()
+}
View
5 test/files/run/t4283.check
@@ -0,0 +1,5 @@
+2
+2
+1
+1
+1
View
0 test/pending/run/t4283/AbstractFoo.java → test/files/run/t4283/AbstractFoo.java
File renamed without changes.
View
5 test/files/run/t4283/ScalaBipp.scala
@@ -0,0 +1,5 @@
+package test
+
+class ScalaBipp extends AbstractFoo {
+ def make: Option[ScalaBipp] = Option(this)
+}
View
5 test/pending/run/t4283/IllegalAccess.scala → test/files/run/t4283/Test.scala
@@ -1,8 +1,7 @@
-package other
+object Test {
-object IllegalAccess {
def main(args: Array[String]) {
- val x = (new test.ScalaBipp).make.get.asInstanceOf[test.ScalaBipp].f()
+ val x = (new test.ScalaBipp).make.get.asInstanceOf[test.ScalaBipp].f()
println(x)
val y = (new test.ScalaBipp).make.get.f()
println(y)
View
19 test/files/run/t5162.scala
@@ -0,0 +1,19 @@
+// In run, rather than pos, to check for problems like SI-4283
+object O1 {
+ private[O1] class Base {
+ def foo: Int = 0
+ }
+ class Mediator extends Base
+}
+
+object O2 {
+ class Derived extends O1.Mediator {
+ override def foo: Int = super.foo
+ }
+}
+
+object Test {
+ def main(args: Array[String]) {
+ new O2.Derived().foo
+ }
+}

0 comments on commit 0a07bb9

Please sign in to comment.
Something went wrong with that request. Please try again.