[NI] Preserve nullability of resulting type from CST if it's possible
Consider common supertype of `S` and `Nothing`, where `S` has nullable upper bound or it's flexible. Before the fix, result was `S?`, which is correct but too conservative. Now, we'll preserve nullability of resulting type if it's already nullable. This happened because we were failing to find path of not-nullable types from `Nothing` to `S`, which should obviously exists by semantics of Nothing
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
// !LANGUAGE: +NewInference
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
interface Base<out T>
|
||||
|
||||
class ParameterizedChild<out R> : Base<R>
|
||||
class Child : Base<Nothing>
|
||||
|
||||
fun <K> elvis(x: K?, y: K): K = TODO()
|
||||
fun <K> select(x: K, y: K): K = TODO()
|
||||
|
||||
fun <V> myRun(f: () -> V): V = f()
|
||||
|
||||
fun <S> test1(a: ParameterizedChild<S>?, b: Child): Base<S> = myRun {
|
||||
elvis(a, b)
|
||||
}
|
||||
|
||||
fun <S> test2(a: S?, b: S): S = <!TYPE_MISMATCH!>myRun {
|
||||
<!TYPE_MISMATCH!>select(a, b)<!>
|
||||
}<!>
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ K> elvis(/*0*/ x: K?, /*1*/ y: K): K
|
||||
public fun </*0*/ V> myRun(/*0*/ f: () -> V): V
|
||||
public fun </*0*/ K> select(/*0*/ x: K, /*1*/ y: K): K
|
||||
public fun </*0*/ S> test1(/*0*/ a: ParameterizedChild<S>?, /*1*/ b: Child): Base<S>
|
||||
public fun </*0*/ S> test2(/*0*/ a: S?, /*1*/ b: S): S
|
||||
|
||||
public interface Base</*0*/ out T> {
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final class Child : Base<kotlin.Nothing> {
|
||||
public constructor Child()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final class ParameterizedChild</*0*/ out R> : Base<R> {
|
||||
public constructor ParameterizedChild</*0*/ out R>()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
@@ -9823,6 +9823,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("cstFromNullableChildAndNonParameterizedType.kt")
|
||||
public void testCstFromNullableChildAndNonParameterizedType() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/commonSystem/cstFromNullableChildAndNonParameterizedType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("dontCaptureTypeVariable.kt")
|
||||
public void testDontCaptureTypeVariable() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.kt");
|
||||
|
||||
Generated
+5
@@ -9818,6 +9818,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
|
||||
runTest("compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("cstFromNullableChildAndNonParameterizedType.kt")
|
||||
public void testCstFromNullableChildAndNonParameterizedType() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/commonSystem/cstFromNullableChildAndNonParameterizedType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("dontCaptureTypeVariable.kt")
|
||||
public void testDontCaptureTypeVariable() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.kt");
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.types.checker.TypeCheckerContext.SeveralSupertypesWi
|
||||
import org.jetbrains.kotlin.types.checker.TypeCheckerContext.SupertypesPolicy
|
||||
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
|
||||
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
|
||||
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
import org.jetbrains.kotlin.utils.SmartList
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
@@ -452,7 +453,7 @@ object NullabilityChecker {
|
||||
}
|
||||
|
||||
private fun TypeCheckerContext.hasPathByNotMarkedNullableNodes(start: SimpleType, end: TypeConstructor) =
|
||||
anySupertype(start, { !it.isMarkedNullable && it.constructor == end }) {
|
||||
anySupertype(start, { it.isNothing() || (!it.isMarkedNullable && it.constructor == end) }) {
|
||||
if (it.isMarkedNullable) SupertypesPolicy.None else SupertypesPolicy.LowerIfFlexible
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user