Skip to content
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

(2.13.4 -target:11) java.lang.IllegalAccessError: Update to non-static final field ... attempted from a different method than the initializer method <init> #12340

Closed
unkarjedy opened this issue Feb 12, 2021 · 9 comments · Fixed by scala/scala#9915
Assignees
Labels
Milestone

Comments

@unkarjedy
Copy link

unkarjedy commented Feb 12, 2021

reproduction steps

using Scala 2.13.4
with JDK 11 (Oracle Corporation Java 11.0.3)

Create simple sbt project with target jvm bytecode level set to 11

name := "bug-update-to-non-static-field"
version := "0.1"
scalaVersion := "2.13.4"
scalacOptions := Seq(
  "-target:11"
)

Create simple file:

object Test {
  def main(args: Array[String]): Unit =
    foo("hello")

  def foo(param: String): Unit = {
    trait T {
      def bar(): Unit =
        println(param)
    }

    println(new T {})
  }
}

problem

run it e.g. via sbt runMain Test
see the error:

[error] (run-main-12) java.lang.IllegalAccessError: Error running public static void Test.main(java.lang.String[]).
[error] java.lang.IllegalAccessError: Update to non-static final field Test$$anon$1.param$1 attempted from a different method (Test$T$1$_setter_$param$1_$eq) than the initializer method <init>
[error] If using a layered classloader, this can occur if jvm package private classes are accessed across layers. This can be fixed by changing to the Flat or ScalaInstance class loader layering strategies.
[error] java.lang.IllegalAccessError: Error running public static void Test.main(java.lang.String[]).

sbt last command doesn't show the stack trace for some reason,
so run e.g. with IntelliJ to see the stack trace:

Exception in thread "main" java.lang.IllegalAccessError: Update to non-static final field Test$$anon$1.param$1 attempted from a different method (Test$T$1$_setter_$param$1_$eq) than the initializer method <init> 
	at Test$$anon$1.Test$T$1$_setter_$param$1_$eq(Test.scala:11)
	at Test$T$1.$init$(Test.scala:5)
	at Test$$anon$1.<init>(Test.scala:11)
	at Test$.foo(Test.scala:11)
	at Test$.main(Test.scala:3)
	at Test.main(Test.scala)

Note that with -target:8 (default value) no exception occurs

@unkarjedy
Copy link
Author

similar exceptions occurred in
#11855
#11781

@unkarjedy unkarjedy changed the title java.lang.IllegalAccessError: Update to non-static final field ... attempted from a different method than the initializer method <init> (2.13.4 -target:11) java.lang.IllegalAccessError: Update to non-static final field ... attempted from a different method than the initializer method <init> Feb 12, 2021
@unkarjedy
Copy link
Author

The bug was originally caught in
https://github.com/JetBrains/intellij-scala
when trying to update the target bytecode level to -target:11

@lrytz lrytz added this to the 2.13.6 milestone Feb 15, 2021
@dwijnand
Copy link
Member

I have this suspicion that this is fixed with scala/scala@d8840a6 that shipped in 2.13.5. Could you check if this is still an issue, please?

@dwijnand dwijnand modified the milestones: 2.13.6, 2.13.7 May 10, 2021
@unkarjedy
Copy link
Author

Same with 2.13.5

name := "untitled"
version := "0.1"
scalaVersion := "2.13.5"
scalacOptions := Seq(
  "-target:11"
)

@unkarjedy
Copy link
Author

JFTR same for 2.13.8

@lrytz lrytz self-assigned this Jan 31, 2022
@lrytz
Copy link
Member

lrytz commented Jan 31, 2022

This seems to be a case that was missed in scala/scala#7028 (discussion on scala/scala-dev#408). The anonymous class T {} has a final field private final Ljava/lang/String; param$1, and a setter method Test$T$1$_setter_$param$1_$eq that assigns to that field. I'll take a look.

@mernst-github
Copy link

Is this/are these things that can be backported to 2.12?

@andyglow
Copy link

+1 to backport to 2.12

@som-snytt
Copy link

Meanwhile you can only -target 8 on 2.12 per scala/scala#10109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants