Skip to content

Commit

Permalink
Merge pull request #10675 from SethTisue/issue-12936
Browse files Browse the repository at this point in the history
Classfile reader: allow `CONSTANT_Dynamic` in constant pool
  • Loading branch information
lrytz committed Jan 26, 2024
2 parents 6c0a9bc + 63a1976 commit afbad90
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
case CONSTANT_METHODHANDLE => in skip 3
case CONSTANT_FIELDREF | CONSTANT_METHODREF | CONSTANT_INTFMETHODREF => in skip 4
case CONSTANT_NAMEANDTYPE | CONSTANT_INTEGER | CONSTANT_FLOAT => in skip 4
case CONSTANT_INVOKEDYNAMIC => in skip 4
case CONSTANT_DYNAMIC | CONSTANT_INVOKEDYNAMIC => in skip 4
case CONSTANT_LONG | CONSTANT_DOUBLE => in skip 8 ; i += 1
case _ => errorBadTag(in.bp - 1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ object ClassfileConstants {
final val CONSTANT_NAMEANDTYPE = 12
final val CONSTANT_METHODHANDLE = 15
final val CONSTANT_METHODTYPE = 16
final val CONSTANT_DYNAMIC = 17
final val CONSTANT_INVOKEDYNAMIC = 18
final val CONSTANT_MODULE = 19
final val CONSTANT_PACKAGE = 20
Expand Down
17 changes: 17 additions & 0 deletions test/files/pos/t12396/A_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// javaVersion: 21+

public class A_1 {
public int f(Object s) {
switch(s) {
case Res.R -> {
return 1;
}
default -> {
return 3;
}
}
}
static enum Res {
R
}
}
5 changes: 5 additions & 0 deletions test/files/pos/t12396/B_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// javaVersion: 21+

class B {
def bar = (new A_1).f(null)
}
31 changes: 31 additions & 0 deletions test/junit/scala/tools/nsc/backend/jvm/ClassfileParserTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package scala.tools.nsc.backend.jvm

import org.junit.Assert.assertEquals
import org.junit.Test

import java.lang.reflect.Member

class ClassfileParserTest {
@Test
def noConstantPoolLag(): Unit = {
def constNames(ms: List[Member]) = ms.collect {
case f if f.getName.startsWith("CONSTANT_") => f.getName
}.sorted

val toScalac = Map(
"CONSTANT_INTERFACE_METHODREF" -> "CONSTANT_INTFMETHODREF",
"CONSTANT_INVOKE_DYNAMIC" -> "CONSTANT_INVOKEDYNAMIC",
"CONSTANT_METHOD_HANDLE" -> "CONSTANT_METHODHANDLE",
"CONSTANT_METHOD_TYPE" -> "CONSTANT_METHODTYPE",
"CONSTANT_NAME_AND_TYPE" -> "CONSTANT_NAMEANDTYPE",
).withDefault(x => x)

val asmConsts = constNames(Class.forName("scala.tools.asm.Symbol").getDeclaredFields.toList)
.map(_.stripSuffix("_TAG"))
.map(toScalac)
.::("CONSTANT_UNICODE")
.sorted
val scalacConsts = constNames(scala.reflect.internal.ClassfileConstants.getClass.getDeclaredMethods.toList)
assertEquals(scalacConsts, asmConsts)
}
}

0 comments on commit afbad90

Please sign in to comment.