diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/jvm/RuntimeAssertions.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/jvm/RuntimeAssertions.kt index 83ba29f96bc..8dcee1a3756 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/jvm/RuntimeAssertions.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/jvm/RuntimeAssertions.kt @@ -27,10 +27,8 @@ import org.jetbrains.kotlin.resolve.calls.context.CallResolutionContext import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.TypeUtils +import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.KotlinTypeChecker -import org.jetbrains.kotlin.types.upperIfFlexible public class RuntimeAssertionInfo(public val needNotNullAssertion: Boolean, public val message: String) { public interface DataFlowExtras { @@ -53,7 +51,7 @@ public class RuntimeAssertionInfo(public val needNotNullAssertion: Boolean, publ dataFlowExtras: DataFlowExtras ): RuntimeAssertionInfo? { fun assertNotNull(): Boolean { - if (expectedType.isError() || expressionType.isError()) return false + if (expectedType.isError || expressionType.isError) return false // T : Any, T! = T..T? // Let T$ will be copy of T! with enhanced nullability. diff --git a/compiler/testData/codegen/notNullAssertions/assertionForNotNullCaptured.kt b/compiler/testData/codegen/notNullAssertions/assertionForNotNullCaptured.kt new file mode 100644 index 00000000000..bd2cda22762 --- /dev/null +++ b/compiler/testData/codegen/notNullAssertions/assertionForNotNullCaptured.kt @@ -0,0 +1,7 @@ +class A { + fun add(element: T) {} +} + +public fun foo(x: MutableCollection, block: java.util.AbstractList) { + x.add(block.get(0)) +} diff --git a/compiler/testData/codegen/notNullAssertions/noAssertionForNullableCaptured.kt b/compiler/testData/codegen/notNullAssertions/noAssertionForNullableCaptured.kt new file mode 100644 index 00000000000..5cede7badaa --- /dev/null +++ b/compiler/testData/codegen/notNullAssertions/noAssertionForNullableCaptured.kt @@ -0,0 +1,7 @@ +class A { + fun add(element: T) {} +} + +public fun foo(x: MutableCollection, block: () -> R) { + x.add(block()) +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/GenerateNotNullAssertionsTest.java b/compiler/tests/org/jetbrains/kotlin/codegen/GenerateNotNullAssertionsTest.java index 77f244bbe4f..d41499d74f4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/GenerateNotNullAssertionsTest.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/GenerateNotNullAssertionsTest.java @@ -157,6 +157,22 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase { assertNoIntrinsicsMethodIsCalledInMyClasses(true); } + public void testNoAssertionForNullableCaptured() { + setUpEnvironment(false, true); + + loadFile("notNullAssertions/noAssertionForNullableCaptured.kt"); + + assertNoIntrinsicsMethodIsCalledInMyClasses(true); + } + + public void testAssertionForNotNullCaptured() { + setUpEnvironment(false, true); + + loadFile("notNullAssertions/assertionForNotNullCaptured.kt"); + + assertTrue(generateToText().contains("checkExpressionValueIsNotNull")); + } + public void testNoAssertionForNullableGenericMethodCall() { setUpEnvironment(false, true);