Skip to content

(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
scala/scala
#9915
@unkarjedy

Description

@unkarjedy

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions