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 23a689cc904..b79dc942be4 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 @@ -250,7 +250,14 @@ internal class DataFlowInfoImpl private constructor( if (!value.type.isFlexible() && value.type.isSubtypeOf(type)) return this val nullabilityInfo = hashMapOf() - if (!type.isMarkedNullable) { + + val isTypeNotNull = + if (languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) + !TypeUtils.isNullableType(type) + else + !type.isMarkedNullable + + if (isTypeNotNull) { putNullabilityAndTypeInfo(nullabilityInfo, value, NOT_NULL, languageVersionSettings) } diff --git a/compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.kt b/compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.kt new file mode 100644 index 00000000000..e791f13517f --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.kt @@ -0,0 +1,6 @@ +// !LANGUAGE: +NewInference + +inline fun foo(x: T?, y: T): T { + if (x is S) return x + return y +} diff --git a/compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.txt b/compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.txt new file mode 100644 index 00000000000..127e4132f72 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.txt @@ -0,0 +1,3 @@ +package + +public inline fun foo(/*0*/ x: T?, /*1*/ y: T): T diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 6307a7dc10a..c130899d142 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -8385,6 +8385,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/generics/nullability/capturedTypeWithPlatformSupertype.kt"); } + @TestMetadata("considerTypeNotNullOnlyIfItHasNotNullBound.kt") + public void testConsiderTypeNotNullOnlyIfItHasNotNullBound() throws Exception { + runTest("compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.kt"); + } + @TestMetadata("correctSubstitutionForIncorporationConstraint.kt") public void testCorrectSubstitutionForIncorporationConstraint() throws Exception { runTest("compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 56e39edf150..26b2b7917c1 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -8385,6 +8385,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/generics/nullability/capturedTypeWithPlatformSupertype.kt"); } + @TestMetadata("considerTypeNotNullOnlyIfItHasNotNullBound.kt") + public void testConsiderTypeNotNullOnlyIfItHasNotNullBound() throws Exception { + runTest("compiler/testData/diagnostics/tests/generics/nullability/considerTypeNotNullOnlyIfItHasNotNullBound.kt"); + } + @TestMetadata("correctSubstitutionForIncorporationConstraint.kt") public void testCorrectSubstitutionForIncorporationConstraint() throws Exception { runTest("compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.kt");