IR: Fix StackOverflowError caused by using DNN type in lambda

^KT-54140 Fixed
This commit is contained in:
Denis.Zharkov
2023-02-02 13:02:45 +01:00
committed by Space Team
parent c586d7a03a
commit 6d7096b5e9
14 changed files with 92 additions and 4 deletions
@@ -46581,6 +46581,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("referenceToSelfInLocal.kt")
public void testReferenceToSelfInLocal() throws Exception {
@@ -1150,7 +1150,9 @@ fun IrType.toIrBasedKotlinType(): KotlinType = when (this) {
is IrSimpleType ->
makeKotlinType(classifier, arguments, isMarkedNullable()).let {
if (classifier is IrTypeParameterSymbol && nullability == SimpleTypeNullability.DEFINITELY_NOT_NULL) {
DefinitelyNotNullType.makeDefinitelyNotNull(it.unwrap()) ?: it
// avoidCheckingActualTypeNullability = true is necessary because in recursive cases `makesSenseToBeDefinitelyNotNull` triggers
// supertype computation for a type parameter and for which bounds we need to call `toIrBasedKotlinType` again
DefinitelyNotNullType.makeDefinitelyNotNull(it.unwrap(), avoidCheckingActualTypeNullability = true) ?: it
} else {
it
}
@@ -0,0 +1,15 @@
fun <T : In<T & Any>?> foo(
value: T,
range: CR<T & Any>
) = value?.run { toString() } ?: "fail"
interface In<T>
interface CR<D>
class Impl : In<Impl> {
override fun toString(): String = "OK"
}
fun box(): String {
return foo(Impl(), object : CR<Impl> {})
}
@@ -44505,6 +44505,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("referenceToSelfInLocal.kt")
public void testReferenceToSelfInLocal() throws Exception {
@@ -46581,6 +46581,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("referenceToSelfInLocal.kt")
public void testReferenceToSelfInLocal() throws Exception {
@@ -35869,6 +35869,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@TestMetadata("referenceToSelfInLocal.kt")
public void testReferenceToSelfInLocal() throws Exception {
runTest("compiler/testData/codegen/box/regressions/referenceToSelfInLocal.kt");
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager
@@ -97,14 +96,22 @@ class DefinitelyNotNullType private constructor(
DefinitelyNotNullTypeMarker {
companion object {
// Having `@JvmOverloads` just to make sure we don't break ABI compatibility
@JvmOverloads
fun makeDefinitelyNotNull(
type: UnwrappedType,
useCorrectedNullabilityForTypeParameters: Boolean = false
useCorrectedNullabilityForTypeParameters: Boolean = false,
// Should be used when we are sure that original type is nullable, i.e. makesSenseToBeDefinitelyNotNull would return true,
// but we can't actually call it because otherwise we would fail with StackOverFlow because supertypes are being computed recursively
// and there's no easy way to prevent recursion.
// NB: makesSenseToBeDefinitelyNotNull is mostly needed as an optimization because nothing really bad would happen even if we
// create DNN for a type parameter with non-nullable bound.
avoidCheckingActualTypeNullability: Boolean = false,
): DefinitelyNotNullType? {
return when {
type is DefinitelyNotNullType -> type
makesSenseToBeDefinitelyNotNull(type, useCorrectedNullabilityForTypeParameters) -> {
avoidCheckingActualTypeNullability || makesSenseToBeDefinitelyNotNull(type, useCorrectedNullabilityForTypeParameters) -> {
if (type is FlexibleType) {
assert(type.lowerBound.constructor == type.upperBound.constructor) {
"DefinitelyNotNullType for flexible type ($type) can be created only from type variable with the same constructor for bounds"
@@ -33377,6 +33377,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("resolvedCallForGetOperator.kt")
public void testResolvedCallForGetOperator() throws Exception {
@@ -33557,6 +33557,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("resolvedCallForGetOperator.kt")
public void testResolvedCallForGetOperator() throws Exception {
@@ -33557,6 +33557,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("resolvedCallForGetOperator.kt")
public void testResolvedCallForGetOperator() throws Exception {
@@ -33557,6 +33557,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("resolvedCallForGetOperator.kt")
public void testResolvedCallForGetOperator() throws Exception {
@@ -37077,6 +37077,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("resolvedCallForGetOperator.kt")
public void testResolvedCallForGetOperator() throws Exception {
@@ -36620,6 +36620,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@Test
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@Test
@TestMetadata("resolvedCallForGetOperator.kt")
public void testResolvedCallForGetOperator() throws Exception {
@@ -30039,6 +30039,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt");
}
@TestMetadata("recursiveDnnTypeInLambda.kt")
public void testRecursiveDnnTypeInLambda() throws Exception {
runTest("compiler/testData/codegen/box/regressions/recursiveDnnTypeInLambda.kt");
}
@TestMetadata("resolvedCallForGetOperator.kt")
public void testResolvedCallForGetOperator() throws Exception {
runTest("compiler/testData/codegen/box/regressions/resolvedCallForGetOperator.kt");