Fix unboxing values of inline class type from type parameters

This commit is contained in:
Mikhail Zarechenskiy
2018-02-19 19:43:55 +03:00
parent 99cea07bf4
commit 530dd01ca6
9 changed files with 88 additions and 1 deletions
@@ -36,7 +36,8 @@ interface Callable {
fun isStaticCall(): Boolean
fun invokeMethodWithArguments(resolvedCall: ResolvedCall<*>, receiver: StackValue, codegen: ExpressionCodegen): StackValue {
return StackValue.functionCall(returnType, resolvedCall.resultingDescriptor.returnType) {
// it's important to use unsubstituted return type here to unbox value if it comes from type variable
return StackValue.functionCall(returnType, resolvedCall.resultingDescriptor.original.returnType) {
codegen.invokeMethodWithArguments(this, resolvedCall, receiver)
}
}
@@ -0,0 +1,23 @@
// !LANGUAGE: +InlineClasses
inline class Result<T>(val a: Any?) {
fun typed(): T = a as T
}
fun box(): String {
val asInt = Result<Int>(19)
val asString = Result<String>("sample")
val asResult = Result<Result<Int>>(asInt)
val unboxedInt = asInt.typed()
val unboxedString = asString.typed()
val unboxedResult = asResult.typed()
if (unboxedInt != 19) return "fail"
if (unboxedString != "sample") return "fail"
if (unboxedResult.typed() != 19) return "fail"
if (asResult.typed().typed() != 19) return "fail"
return "OK"
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +InlineClasses
inline class Result<T>(val a: Any?) {
fun typed(): T = a as T
}
fun <K> materialize(): K = TODO()
fun test(asInt: Result<Int>, asString: Result<String>, asResult: Result<Result<Int>>) {
val a1 = materialize<Result<Int>>() // unbox
val a2 = materialize<Result<Result<Int>>>() // unbox
val b1 = asInt.typed() // intValue
val b2 = asString.typed()
val c1 = asResult.typed() // unbox
materialize<Result<Int>>()
asInt.typed()
asString.typed()
asResult.typed()
}
// 0 INVOKESTATIC Result\$Erased.box
// 3 INVOKEVIRTUAL Result.unbox
// 0 valueOf
// 1 intValue
@@ -0,0 +1,5 @@
package
public fun bar(/*0*/ i: kotlin.Int): kotlin.Unit
public fun foo(): kotlin.Int
public fun test(): kotlin.Unit
@@ -10623,6 +10623,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("checkUnboxingResultFromTypeVariable.kt")
public void testCheckUnboxingResultFromTypeVariable() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");
@@ -10623,6 +10623,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("checkUnboxingResultFromTypeVariable.kt")
public void testCheckUnboxingResultFromTypeVariable() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");
@@ -2079,6 +2079,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("unboxInlineClassFromParameterizedType.kt")
public void testUnboxInlineClassFromParameterizedType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassFromParameterizedType.kt");
doTest(fileName);
}
@TestMetadata("unboxInlineClassesAfterSmartCasts.kt")
public void testUnboxInlineClassesAfterSmartCasts() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassesAfterSmartCasts.kt");
@@ -10623,6 +10623,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("checkUnboxingResultFromTypeVariable.kt")
public void testCheckUnboxingResultFromTypeVariable() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");
@@ -11685,6 +11685,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("checkUnboxingResultFromTypeVariable.kt")
public void testCheckUnboxingResultFromTypeVariable() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");