diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowInfoImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowInfoImpl.kt index b79dc942be4..f955ec541bf 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowInfoImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowInfoImpl.kt @@ -142,7 +142,7 @@ internal class DataFlowInfoImpl private constructor( private fun KotlinType.canBeDefinitelyNotNullOrNotNull(settings: LanguageVersionSettings): Boolean { return if (settings.supportsFeature(LanguageFeature.NewInference)) - this.isMarkedNullable || DefinitelyNotNullType.makesSenseToBeDefinitelyNotNull(this.unwrap()) + TypeUtils.isNullableType(this) else this.isMarkedNullable } diff --git a/compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.kt b/compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.kt new file mode 100644 index 00000000000..e4501bca208 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.kt @@ -0,0 +1,18 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER + +interface A +interface B { + fun test() {} +} + +fun select(a: K, b: K): K = a + +fun test(a: A?, b: B?) { + b as A? + a as B? + val c = select(a, b) + if (c != null) { + c.test() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.txt b/compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.txt new file mode 100644 index 00000000000..b71675d80cf --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.txt @@ -0,0 +1,17 @@ +package + +public fun select(/*0*/ a: K, /*1*/ b: K): K +public fun test(/*0*/ a: A?, /*1*/ b: B?): kotlin.Unit + +public interface A { + 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 interface B { + 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 fun test(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 6ee71c5e2f7..7c7aa194ceb 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -8435,6 +8435,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/generics/nullability/kt25182.kt"); } + @TestMetadata("notNullSmartcastOnIntersectionOfNullables.kt") + public void testNotNullSmartcastOnIntersectionOfNullables() throws Exception { + runTest("compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.kt"); + } + @TestMetadata("nullToGeneric.kt") public void testNullToGeneric() throws Exception { runTest("compiler/testData/diagnostics/tests/generics/nullability/nullToGeneric.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 0a95764675d..1e8c0a07846 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -8435,6 +8435,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/generics/nullability/kt25182.kt"); } + @TestMetadata("notNullSmartcastOnIntersectionOfNullables.kt") + public void testNotNullSmartcastOnIntersectionOfNullables() throws Exception { + runTest("compiler/testData/diagnostics/tests/generics/nullability/notNullSmartcastOnIntersectionOfNullables.kt"); + } + @TestMetadata("nullToGeneric.kt") public void testNullToGeneric() throws Exception { runTest("compiler/testData/diagnostics/tests/generics/nullability/nullToGeneric.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt index 900137eae0d..d5efe59d538 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt @@ -81,8 +81,8 @@ class DefinitelyNotNullType private constructor(val original: SimpleType) : Dele } } - fun makesSenseToBeDefinitelyNotNull(type: UnwrappedType): Boolean = - type.canHaveUndefinedNullability() && !NullabilityChecker.isSubtypeOfAny(type) + private fun makesSenseToBeDefinitelyNotNull(type: UnwrappedType): Boolean = + type.canHaveUndefinedNullability() && !NullabilityChecker.isSubtypeOfAny(type) } override val delegate: SimpleType