Use underlying type when computing type mapping mode for inline classes

This commit is contained in:
Dmitry Petrov
2018-08-14 12:46:28 +03:00
parent 948e72f653
commit 8e95ecb821
9 changed files with 117 additions and 2 deletions
+45
View File
@@ -0,0 +1,45 @@
// !LANGUAGE: +InlineClasses
// WITH_RUNTIME
// IGNORE_BACKEND: JVM_IR, JS_IR
inline class Bar(val y: Int)
inline class Foo<T>(val x: Int)
inline class Foo2<T>(val x: Foo<T>)
inline class Foo3<T>(val x: Bar)
fun testValueParameter(z: Foo<Any>) = z.x
fun testValueParameter2(z: Foo2<Any>) = z.x.x
fun testValueParameter3(z: Foo3<Any>) = z.x.y
fun testReturnType(x: Int) = Foo<Any>(x)
fun testReturnType2(x: Int) = Foo2<Any>(Foo<Any>(x))
fun testReturnType3(x: Int) = Foo3<Any>(Bar(x))
fun testGenericTypeArgumentInValueParameter(zs: List<Foo<Any>>) = zs[0].x
fun testGenericTypeArgumentInValueParameter2(zs: List<Foo2<Any>>) = zs[0].x.x
fun testGenericTypeArgumentInValueParameter3(zs: List<Foo3<Any>>) = zs[0].x.y
fun testGenericTypeArgumentInReturnType(x: Int) = listOf(Foo<Any>(x))
fun testGenericTypeArgumentInReturnType2(x: Int) = listOf(Foo2(Foo<Any>(x)))
fun testGenericTypeArgumentInReturnType3(x: Int) = listOf(Foo3<Any>(Bar(x)))
fun box(): String {
if (testValueParameter(Foo(42)) != 42) throw AssertionError()
if (testValueParameter2(Foo2(Foo<Any>(42))) != 42) throw AssertionError()
if (testValueParameter3(Foo3(Bar(42))) != 42) throw AssertionError()
if (testReturnType(42).x != 42) throw AssertionError()
if (testReturnType2(42).x.x != 42) throw AssertionError()
if (testReturnType3(42).x.y != 42) throw AssertionError()
if (testGenericTypeArgumentInValueParameter(listOf(Foo<Any>(42))) != 42) throw AssertionError()
if (testGenericTypeArgumentInValueParameter2(listOf(Foo2(Foo<Any>(42)))) != 42) throw AssertionError()
if (testGenericTypeArgumentInValueParameter3(listOf(Foo3<Any>(Bar(42)))) != 42) throw AssertionError()
if (testGenericTypeArgumentInReturnType(42)[0].x != 42) throw AssertionError()
if (testGenericTypeArgumentInReturnType2(42)[0].x.x != 42) throw AssertionError()
if (testGenericTypeArgumentInReturnType3(42)[0].x.y != 42) throw AssertionError()
return "OK"
}
@@ -0,0 +1,13 @@
// !LANGUAGE: +InlineClasses
// WITH_RUNTIME
// IGNORE_BACKEND: JVM_IR
inline class Foo<T>(val x: Int)
class Bar(val y: Foo<Any>)
fun box(): String {
if (Bar(Foo<Any>(42)).y.x != 42) throw AssertionError()
return "OK"
}
@@ -11548,6 +11548,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt");
}
@TestMetadata("kt26103.kt")
public void testKt26103() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt");
}
@TestMetadata("kt26103_original.kt")
public void testKt26103_original() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt");
}
@TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt")
public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt");
@@ -11548,6 +11548,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt");
}
@TestMetadata("kt26103.kt")
public void testKt26103() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt");
}
@TestMetadata("kt26103_original.kt")
public void testKt26103_original() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt");
}
@TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt")
public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt");
@@ -11548,6 +11548,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt");
}
@TestMetadata("kt26103.kt")
public void testKt26103() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt");
}
@TestMetadata("kt26103_original.kt")
public void testKt26103_original() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt");
}
@TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt")
public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt");
@@ -103,6 +103,13 @@ class TypeMappingMode private constructor(
): TypeMappingMode {
if (type.arguments.isEmpty()) return DEFAULT
if (type.isInlineClassType() && shouldUseUnderlyingType(type)) {
val underlyingType = computeUnderlyingType(type)
if (underlyingType != null) {
return getOptimalModeForSignaturePart(underlyingType, isForAnnotationParameter, canBeUsedInSupertypePosition)
}
}
val contravariantArgumentMode =
if (!canBeUsedInSupertypePosition)
TypeMappingMode(
@@ -240,7 +240,7 @@ private fun <T : Any> mapBuiltInType(
return null
}
private fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? {
internal fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? {
if (!shouldUseUnderlyingType(inlineClassType)) return null
val descriptor = inlineClassType.unsubstitutedUnderlyingType()?.constructor?.declarationDescriptor ?: return null
@@ -250,7 +250,7 @@ private fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? {
inlineClassType.substitutedUnderlyingType()
}
private fun shouldUseUnderlyingType(inlineClassType: KotlinType): Boolean {
internal fun shouldUseUnderlyingType(inlineClassType: KotlinType): Boolean {
val underlyingType = inlineClassType.unsubstitutedUnderlyingType() ?: return false
return !inlineClassType.isMarkedNullable ||
@@ -10118,6 +10118,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt");
}
@TestMetadata("kt26103.kt")
public void testKt26103() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt");
}
@TestMetadata("kt26103_original.kt")
public void testKt26103_original() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt");
}
@TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt")
public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt");
@@ -11173,6 +11173,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt");
}
@TestMetadata("kt26103.kt")
public void testKt26103() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt");
}
@TestMetadata("kt26103_original.kt")
public void testKt26103_original() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt");
}
@TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt")
public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt");