Coerce annotation array values to the appropriate element type
MOE_MIGRATED_REVID=138930155
diff --git a/java/com/google/turbine/binder/ConstEvaluator.java b/java/com/google/turbine/binder/ConstEvaluator.java
index 720586e..524cb5a 100644
--- a/java/com/google/turbine/binder/ConstEvaluator.java
+++ b/java/com/google/turbine/binder/ConstEvaluator.java
@@ -926,11 +926,16 @@
return value;
case ARRAY_TY:
{
- if (value.kind() == Const.Kind.ARRAY) {
- return value;
+ Type elementType = ((Type.ArrayTy) ty).elementType();
+ ImmutableList<Const> elements =
+ value.kind() == Const.Kind.ARRAY
+ ? ((Const.ArrayInitValue) value).elements()
+ : ImmutableList.of(value);
+ ImmutableList.Builder<Const> coerced = ImmutableList.builder();
+ for (Const element : elements) {
+ coerced.add(cast(elementType, element));
}
- Type.ArrayTy aty = (Type.ArrayTy) ty;
- return new Const.ArrayInitValue(ImmutableList.of(cast(aty.elementType(), value)));
+ return new Const.ArrayInitValue(coerced.build());
}
default:
throw new AssertionError(ty.tyKind());
diff --git a/javatests/com/google/turbine/lower/LowerIntegrationTest.java b/javatests/com/google/turbine/lower/LowerIntegrationTest.java
index 7799c07..fd332b3 100644
--- a/javatests/com/google/turbine/lower/LowerIntegrationTest.java
+++ b/javatests/com/google/turbine/lower/LowerIntegrationTest.java
@@ -265,6 +265,7 @@
"enum_abstract.test",
"deficient_types_classfile.test",
"ctor_anno.test",
+ "anno_const_coerce.test",
};
List<Object[]> tests =
ImmutableList.copyOf(testCases).stream().map(x -> new Object[] {x}).collect(toList());
diff --git a/javatests/com/google/turbine/lower/testdata/anno_const_coerce.test b/javatests/com/google/turbine/lower/testdata/anno_const_coerce.test
new file mode 100644
index 0000000..f1b7fb7
--- /dev/null
+++ b/javatests/com/google/turbine/lower/testdata/anno_const_coerce.test
@@ -0,0 +1,10 @@
+=== Anno.java ===
+@interface Anno {
+ long a() default 0;
+ long[] b() default {0};
+}
+=== Test.java ===
+class Test {
+ @Anno() int one;
+ @Anno(a=0, b={0, 1}) int two;
+}