Skip to content

Commit 3b882c2

Browse files
committed
ClassfileParser: correct types for annotation arguments
As specified in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.16.1, an annotation argument of type boolean, byte, char or short will be represented as a CONSTANT_Integer in the classfile, so when we parse it we get an Int, but before this commit we did not convert this Int to the correct type for the annotation argument, thus creating ill-typed trees.
1 parent 1293e2f commit 3b882c2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,9 @@ class ClassfileParser(
430430
tag match {
431431
case STRING_TAG =>
432432
if (skip) None else Some(Literal(Constant(pool.getName(index).toString)))
433-
case BOOL_TAG | BYTE_TAG | CHAR_TAG | SHORT_TAG | INT_TAG |
434-
LONG_TAG | FLOAT_TAG | DOUBLE_TAG =>
433+
case BOOL_TAG | BYTE_TAG | CHAR_TAG | SHORT_TAG =>
434+
if (skip) None else Some(Literal(pool.getConstant(index, tag)))
435+
case INT_TAG | LONG_TAG | FLOAT_TAG | DOUBLE_TAG =>
435436
if (skip) None else Some(Literal(pool.getConstant(index)))
436437
case CLASS_TAG =>
437438
if (skip) None else Some(Literal(Constant(pool.getType(index))))
@@ -829,7 +830,7 @@ class ClassfileParser(
829830
if (sym == classRoot.symbol) staticScope.lookup(name)
830831
else {
831832
var module = sym.companionModule
832-
if (module == NoSymbol && sym.isAbsent)
833+
if (!module.exists && sym.isAbsent)
833834
module = sym.scalacLinkedClass
834835
module.info.member(name).symbol
835836
}
@@ -1045,14 +1046,28 @@ class ClassfileParser(
10451046
getClassSymbol(index)
10461047
}
10471048

1048-
def getConstant(index: Int)(implicit ctx: Context): Constant = {
1049+
def getConstant(index: Int, tag: Int = -1)(implicit ctx: Context): Constant = {
10491050
if (index <= 0 || len <= index) errorBadIndex(index)
10501051
var value = values(index)
10511052
if (value eq null) {
10521053
val start = starts(index)
10531054
value = (in.buf(start).toInt: @switch) match {
10541055
case CONSTANT_STRING =>
10551056
Constant(getName(in.getChar(start + 1).toInt).toString)
1057+
case CONSTANT_INTEGER if tag != -1 =>
1058+
val value = in.getInt(start + 1)
1059+
(tag: @switch) match {
1060+
case BOOL_TAG =>
1061+
Constant(value != 0)
1062+
case BYTE_TAG =>
1063+
Constant(value.toByte)
1064+
case CHAR_TAG =>
1065+
Constant(value.toChar)
1066+
case SHORT_TAG =>
1067+
Constant(value.toShort)
1068+
case _ =>
1069+
errorBadTag(tag)
1070+
}
10561071
case CONSTANT_INTEGER =>
10571072
Constant(in.getInt(start + 1))
10581073
case CONSTANT_FLOAT =>

0 commit comments

Comments
 (0)