Skip to content

Commit a03ed6f

Browse files
author
Mikhael Bogdanov
committed
Fix for KT-16581: VerifyError when calling default value parameter with jvm-target 1.8
#KT-16581 Fixed
1 parent ffe3453 commit a03ed6f

File tree

5 files changed

+73
-26
lines changed

5 files changed

+73
-26
lines changed

compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -281,33 +281,24 @@ public static StackValue expression(Type type, KtExpression expression, Expressi
281281
}
282282

283283
private static void box(Type type, Type toType, InstructionAdapter v) {
284-
if (type == Type.BYTE_TYPE || toType.getInternalName().equals(NULLABLE_BYTE_TYPE_NAME) && type == Type.INT_TYPE) {
285-
v.cast(type, Type.BYTE_TYPE);
286-
v.invokestatic(NULLABLE_BYTE_TYPE_NAME, "valueOf", "(B)L" + NULLABLE_BYTE_TYPE_NAME + ";", false);
287-
}
288-
else if (type == Type.SHORT_TYPE || toType.getInternalName().equals(NULLABLE_SHORT_TYPE_NAME) && type == Type.INT_TYPE) {
289-
v.cast(type, Type.SHORT_TYPE);
290-
v.invokestatic(NULLABLE_SHORT_TYPE_NAME, "valueOf", "(S)L" + NULLABLE_SHORT_TYPE_NAME + ";", false);
291-
}
292-
else if (type == Type.LONG_TYPE || toType.getInternalName().equals(NULLABLE_LONG_TYPE_NAME) && type == Type.INT_TYPE) {
293-
v.cast(type, Type.LONG_TYPE);
294-
v.invokestatic(NULLABLE_LONG_TYPE_NAME, "valueOf", "(J)L" + NULLABLE_LONG_TYPE_NAME + ";", false);
295-
}
296-
else if (type == Type.INT_TYPE) {
297-
v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
298-
}
299-
else if (type == Type.BOOLEAN_TYPE) {
300-
v.invokestatic("java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
301-
}
302-
else if (type == Type.CHAR_TYPE) {
303-
v.invokestatic("java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false);
304-
}
305-
else if (type == Type.FLOAT_TYPE) {
306-
v.invokestatic("java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
307-
}
308-
else if (type == Type.DOUBLE_TYPE) {
309-
v.invokestatic("java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
284+
if (type == Type.INT_TYPE) {
285+
if (toType.getInternalName().equals(NULLABLE_BYTE_TYPE_NAME)) {
286+
type = Type.BYTE_TYPE;
287+
}
288+
else if (toType.getInternalName().equals(NULLABLE_SHORT_TYPE_NAME)) {
289+
type = Type.SHORT_TYPE;
290+
}
291+
else if (toType.getInternalName().equals(NULLABLE_LONG_TYPE_NAME)) {
292+
type = Type.LONG_TYPE;
293+
}
294+
v.cast(Type.INT_TYPE, type);
310295
}
296+
297+
Type boxedType = AsmUtil.boxType(type);
298+
if (boxedType == type) return;
299+
300+
v.invokestatic(boxedType.getInternalName(), "valueOf", Type.getMethodDescriptor(boxedType, type), false);
301+
coerce(boxedType, toType, v);
311302
}
312303

313304
private static void unbox(Type type, InstructionAdapter v) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// JVM_TARGET: 1.8
2+
fun failAtRuntime(numberArg: Number = 0.0): Number {
3+
return numberArg
4+
}
5+
6+
fun box(): String {
7+
failAtRuntime()
8+
return "OK"
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// JVM_TARGET: 1.8
2+
3+
fun test(o: Number) {}
4+
5+
fun test2(o: Number) {
6+
val p: Int = 1
7+
val o = if (z < 1) p else o
8+
test(o)
9+
}
10+
11+
var z = 1
12+
13+
fun box(): String {
14+
val x: Number = 1
15+
test2(x)
16+
return "OK"
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// JVM_TARGET: 1.8
2+
3+
fun number(doLong: Boolean): Number = when {
4+
doLong -> 1.toLong()
5+
else -> 0
6+
}
7+
8+
fun box(): String {
9+
number(true)
10+
return "OK"
11+
}
12+

compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ public void testKt14243_prop() throws Exception {
269269
doTest(fileName);
270270
}
271271

272+
@TestMetadata("kt16581.kt")
273+
public void testKt16581() throws Exception {
274+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/kt16581.kt");
275+
doTest(fileName);
276+
}
277+
278+
@TestMetadata("kt16581_2.kt")
279+
public void testKt16581_2() throws Exception {
280+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/kt16581_2.kt");
281+
doTest(fileName);
282+
}
283+
284+
@TestMetadata("kt16588.kt")
285+
public void testKt16588() throws Exception {
286+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/kt16588.kt");
287+
doTest(fileName);
288+
}
289+
272290
@TestMetadata("oneImplementation.kt")
273291
public void testOneImplementation() throws Exception {
274292
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/oneImplementation.kt");

0 commit comments

Comments
 (0)