Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #699 from retronym/ticket/5162

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

0 comments on commit 0a07bb9

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