Skip to content

DelayedInit transformation needs more awareness of access restrictions #5565

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

Closed
scabug opened this issue Mar 11, 2012 · 11 comments
Closed

DelayedInit transformation needs more awareness of access restrictions #5565

scabug opened this issue Mar 11, 2012 · 11 comments

Comments

@scabug
Copy link

scabug commented Mar 11, 2012

This is related to #1806
First, i just spent an hour trying to workaround this issue, so I'd expect someone would really appreciate if protected statics were just treated as public.
Second, I got this error message:
"Access to protected variable staticReference not permitted because enclosing class object SpringWebApplication in package ui is not a subclass of object WebApplication in package navigator7 where target is defined var webApplication = if (WebApplication.staticReference == null) {"
here is the definition:
object SpringWebApplication extends WebApplication{ }
it really looks like it is a subclass, but the message tells me it is not.
Instead of this lengthy explanation, I would prefer to see some steps of how to work around the problem, e.g. "Use reflection or write a java wrapper to access this field, because scala does not allow accessing protected static members"

@scabug
Copy link
Author

scabug commented Mar 11, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5565?orig=1
Reporter: @OlegYch
Assignee: @magarciaEPFL
Affected Versions: 2.9.1

@scabug
Copy link
Author

scabug commented Mar 11, 2012

@paulp said:
Please attach actual self-contained code, not vague references to unknown frameworks.

This is probably fixed in trunk, but without a test case there's no way for me to say.

@scabug
Copy link
Author

scabug commented Mar 11, 2012

@OlegYch said:
sorry, should have elaborated
here is testcase https://github.com/OlegYch/scala-testcases/tree/master/SI-5565
and here is scalac output

D:\test\>javac pack2\*.java
D:\test\>d:\Distrib\Coding\Java\scala\scala-2.9.1.final\bin\scalac.bat  pack2\*.java *.scala
test.scala:5: error: variable i in object base cannot be accessed in object pack2.base
 Access to protected variable i not permitted because
 enclosing class object test in package pack1 is not a subclass of
 object base in package pack2 where target is defined
  println(base.i)
               ^
one error found

with nightly build:

D:\test>d:\Distrib\Coding\Java\scala\scala-2.10.0-M2-0118-gf1c39dd981-2012-03-11\bin\scalac.bat pack2\*.java *.scala

D:\test>javac pack2\*.java

D:\test>d:\Distrib\Coding\Java\scala\scala-2.10.0-M2-0118-gf1c39dd981-2012-03-11\bin\scala.bat pack1.test
java.lang.IllegalAccessError: tried to access field pack2.base.i from class pack1.test$delayedInit$body
        at pack1.test$delayedInit$body.apply(test.scala:5)
        at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
        at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
        at scala.App$$anonfun$main$1.apply(App.scala:61)
        at scala.App$$anonfun$main$1.apply(App.scala:61)
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
        at scala.collection.immutable.List.foreach(List.scala:77)
        at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:31)
        at scala.App$class.main(App.scala:61)
        at pack1.test$.main(test.scala:4)
        at pack1.test.main(test.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:90)
        at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:38)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:159)
        at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:90)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:159)
        at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45)
        at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:70)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:92)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

@scabug
Copy link
Author

scabug commented Mar 11, 2012

@paulp said:
OK, that's much more useful. This is actually a bug in DelayedInit. If you skip App and write a main method, it works.

object test extends base {
  def main(args: Array[String]): Unit = {
    println(base.i)
  }
}

@scabug
Copy link
Author

scabug commented Mar 11, 2012

@OlegYch said:
Thanks, Paul.

@scabug
Copy link
Author

scabug commented Mar 12, 2012

@paulp said:
Note to self: also, we need to in general enforce that a protected static is being accessed from a class which extends the java class containing the static. Right now it will let you access from an unrelated class, and then the jvm will fail. The complication is that in the scala model, the static method being accessed is in an object and therefore is never in anyone's superclass.

@scabug
Copy link
Author

scabug commented Sep 26, 2012

@dwijnand said:
I'm not sure if this is the same issue (seems to be for me), if it is, here's another test case:

object illegal_access_error_test_case extends App {
  implicit def doubleWithApproxEquals(d: Double) = new {
    def ~==(v: Double, margin: Double = 0.001): Boolean =
      math.abs(d - v) < margin
  }

  assert(math.abs(-4.0) ~== (4.0, 0.001))
  assert(math.abs(-4.0) ~== 4.0)
}
Exception in thread "main" java.lang.IllegalAccessError: tried to access field illegal_access_error_test_case$.reflParams$Cache2 from class illegal_access_error_test_case$delayedInit$body
	at illegal_access_error_test_case$delayedInit$body.<clinit>(illegal_access_error_test_case.scala:8)
	at illegal_access_error_test_case$.<init>(illegal_access_error_test_case.scala:1)
	at illegal_access_error_test_case$.<clinit>(illegal_access_error_test_case.scala)
	at illegal_access_error_test_case.main(illegal_access_error_test_case.scala)

@scabug
Copy link
Author

scabug commented Mar 15, 2013

@adriaanm said:
Un-assigning to foster work stealing, as announced in https://groups.google.com/forum/?fromgroups=#!topic/scala-internals/o8WG4plpNkw

@scabug
Copy link
Author

scabug commented Jun 9, 2013

@magarciaEPFL said:
Can't reproduce in master. I guess (but haven't confirmed) the latest improvement to DelayedInit was beneficial:
scala/scala#2445

in particular scala/scala@b4fbb7b

@scabug
Copy link
Author

scabug commented Jul 10, 2013

@adriaanm said:
Unassigning and rescheduling to M6 as previous deadline was missed.

@scabug scabug closed this as completed Dec 11, 2013
@scabug
Copy link
Author

scabug commented Feb 18, 2014

@retronym said:
Test case submitted as scala/scala#3547

Fixed in b4fbb7be0e by @magarcia

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

1 participant