I use Scala 3.1.0 (17.0.1, Java OpenJDK 64-Bit Server VM).
In this setting, I found a Verify Error when using mutable fields in parametric enumeration as follows:
enum A(c: Int):
case B(var k: Int) extends A(k)
with the following error message:
Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
A$B.<init>(I)V @7: invokevirtual
Reason:
Type uninitializedThis (current frame, stack[1]) is not assignable to 'A$B'
Current Frame:
bci: @7
flags: { flagThisUninit }
locals: { uninitializedThis, integer }
stack: { uninitializedThis, uninitializedThis }
Bytecode:
0000000: 2a1b b500 1f2a 2ab6 0022 b700 24b1
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402)
at java.base/java.lang.Class.getMethodsRecursive(Class.java:3543)
at java.base/java.lang.Class.getMethod0(Class.java:3529)
at java.base/java.lang.Class.getMethod(Class.java:2225)
at dotty.tools.scripting.ScriptingDriver.collectMainMethods$3(ScriptingDriver.scala:79)
at dotty.tools.scripting.ScriptingDriver.$anonfun$3(ScriptingDriver.scala:88)
at scala.collection.immutable.List.flatMap(List.scala:293)
at dotty.tools.scripting.ScriptingDriver.detectMainClassAndMethod(ScriptingDriver.scala:89)
at dotty.tools.scripting.ScriptingDriver.compileAndRun(ScriptingDriver.scala:34)
at dotty.tools.scripting.Main$.main(Main.scala:43)
at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:168)
at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:175)
at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)
However, the following code does not throw any error:
enum A(c: Int):
case B(k: Int) extends A(k)
I think that it happens because the compiled JVM bytecode tries to use the mutable field k
before its initialization in A(k)
.