scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

"bad constant pool tag 17" when using a library compiled using Java 21 #12936

Closed SethTisue closed 5 months ago

SethTisue commented 5 months ago

this is the Scala 2 equivalent of https://github.com/lampepfl/dotty/issues/19527, reported against Scala 3 by @adamw yesterday

the original report has some information about what kind of bytecode is involved — it’s switch on certain types, e.g. Java enums

Scala 2 reproducer:

//> using scala 2.nightly
//> using dep com.softwaremill.jox:core:0.0.5

import com.softwaremill.jox.Channel

object Test {
  def main(argv: Array[String]): Unit =
    new Channel(0)
}

result:

Error: 
  bad constant pool tag 17 at byte 10172
     while compiling: /Users/tisue/tmp/dotty-19527/S.scala
        during phase: globalPhase=typer, enteringPhase=namer
    ...
    at scala.tools.nsc.symtab.classfile.ClassfileParser$ConstantPool.errorBadTag(ClassfileParser.scala:411)
    at scala.tools.nsc.symtab.classfile.ClassfileParser$ConstantPool.<init>(ClassfileParser.scala:230)
    at scala.tools.nsc.symtab.classfile.ClassfileParser.$anonfun$parse$2(ClassfileParser.scala:173)
    at scala.tools.nsc.symtab.classfile.ClassfileParser.$anonfun$parse$1(ClassfileParser.scala:159)
    at scala.tools.nsc.symtab.classfile.ClassfileParser.parse(ClassfileParser.scala:142)
    at scala.tools.nsc.symtab.SymbolLoaders$ClassfileLoader.doComplete(SymbolLoaders.scala:342)
    at scala.tools.nsc.symtab.SymbolLoaders$SymbolLoader.$anonfun$complete$2(SymbolLoaders.scala:249)
    ...
lrytz commented 5 months ago

Reproducer

public class A {
  int f(Object s) {
    switch(s) {
      case Res.R -> {
        return 1;
      }
      default -> {
        return 3;
      }
    }
  }
  static enum Res {
    R
  }
}
lrytz commented 5 months ago
➜ sandbox javap -v A.class
...
  #30 = Dynamic            #1:#31         // #1:invoke:Ljava/lang/Enum$EnumDesc;
...

Dynamic?

https://github.com/scala/scala/blob/v2.13.12/src/reflect/scala/reflect/internal/ClassfileConstants.scala#L83-L84

Where's 17?

Oh it was added in Java 11. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.4-210.

Why did nobody tell us? 🙃

He-Pin commented 5 months ago

Seems like Scala missed the tag 17 and has a tag 2. Added in https://openjdk.org/jeps/309

lrytz commented 5 months ago

That's entertaining; trivia on https://stackoverflow.com/a/63314177

som-snytt commented 5 months ago

It would be nice if ASM told us.

https://gitlab.ow2.org/asm/asm/-/blame/master/asm/src/main/java/org/objectweb/asm/Symbol.java?ref_type=heads#L84