Nullability of intersection type should be determined by supertypes

Intersection types cannot be nullable as is because of special rules for subtyping
This commit is contained in:
Mikhail Zarechenskiy
2018-03-29 16:24:18 +03:00
parent aebcb61402
commit 0632bbbed0
6 changed files with 58 additions and 0 deletions
@@ -0,0 +1,26 @@
// !LANGUAGE: +NewInference
class Recursive<T : Recursive<T>> : Generic<PlaceHolder<T>>, MainSupertype
open class Simple<T> : Generic<T>, MainSupertype
interface Generic<T>
interface PlaceHolder<T : MainSupertype> : Stub<T>
interface Stub<T : MainSupertype>
interface MainSupertype
interface SpecificStub<T : SpecificSimple> : Stub<T>
abstract class SpecificSimple : Simple<SpecificStub<*>>()
fun takeElement(recursive: Recursive<*>?, simpleWithSpecific: Simple<SpecificStub<*>>) {
select(recursive, simpleWithSpecific) // T -> intersection type
}
fun <T> select(x: T?, y: T): T = y
fun box(): String {
takeElement(null, Simple())
return "OK"
}
@@ -19432,6 +19432,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt")
public void testNoAssertionsWhenNullableTypeParameterReplacedWithIntersectionType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt");
doTest(fileName);
}
@TestMetadata("noCapturingForTypesWithTypeVariables.kt")
public void testNoCapturingForTypesWithTypeVariables() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt");
@@ -19432,6 +19432,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt")
public void testNoAssertionsWhenNullableTypeParameterReplacedWithIntersectionType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt");
doTest(fileName);
}
@TestMetadata("noCapturingForTypesWithTypeVariables.kt")
public void testNoCapturingForTypesWithTypeVariables() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt");
@@ -19432,6 +19432,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt")
public void testNoAssertionsWhenNullableTypeParameterReplacedWithIntersectionType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt");
doTest(fileName);
}
@TestMetadata("noCapturingForTypesWithTypeVariables.kt")
public void testNoCapturingForTypesWithTypeVariables() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt");
@@ -265,6 +265,14 @@ public class TypeUtils {
if (isTypeParameter(type)) {
return hasNullableSuperType(type);
}
TypeConstructor constructor = type.getConstructor();
if (constructor instanceof IntersectionTypeConstructor) {
for (KotlinType supertype : constructor.getSupertypes()) {
if (isNullableType(supertype)) return true;
}
}
return false;
}
@@ -26423,6 +26423,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive or add it to whitelist for that.");
}
@TestMetadata("noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt")
public void testNoAssertionsWhenNullableTypeParameterReplacedWithIntersectionType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noAssertionsWhenNullableTypeParameterReplacedWithIntersectionType.kt");
doTest(fileName);
}
@TestMetadata("noCapturingForTypesWithTypeVariables.kt")
public void testNoCapturingForTypesWithTypeVariables() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt");