diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index 53a58d44b8c..867d7730abd 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.types.TypeConstructor import org.jetbrains.kotlin.types.UnwrappedType import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.types.typeUtil.contains -import org.jetbrains.kotlin.types.typeUtil.isTypeParameter import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -45,6 +44,10 @@ class NewConstraintSystemImpl( KotlinConstraintSystemCompleter.Context, PostponedArgumentsAnalyzer.Context { + companion object { + private val TYPE_PARAMETER_FOR_EXCLXCL = "" // TODO: Get this variable from ResolveConstruct + } + private val storage = MutableConstraintStorage() private var state = State.BUILDING private val typeVariablesTransaction: MutableList = SmartList() @@ -205,7 +208,7 @@ class NewConstraintSystemImpl( override fun fixVariable(variable: NewTypeVariable, resultType: UnwrappedType) { checkState(State.BUILDING, State.COMPLETION) - val actualResultType = eliminateSpecialIntersectionType(resultType) ?: resultType + val actualResultType = eliminateSpecialIntersectionType(variable, resultType) ?: resultType constraintInjector.addInitialEqualityConstraint(this, variable.defaultType, actualResultType, FixVariableConstraintPosition(variable)) notFixedTypeVariables.remove(variable.freshTypeConstructor) @@ -218,7 +221,9 @@ class NewConstraintSystemImpl( storage.fixedTypeVariables[variable.freshTypeConstructor] = actualResultType } - private fun eliminateSpecialIntersectionType(type: UnwrappedType): UnwrappedType? { + private fun eliminateSpecialIntersectionType(variable: NewTypeVariable, type: UnwrappedType): UnwrappedType? { + if (variable.shouldBeDefinitelyNotNull()) return null + val constructor = type.constructor.safeAs() ?: return null if (constructor.supertypes.size != 2) return null @@ -227,6 +232,13 @@ class NewConstraintSystemImpl( return actualType?.unwrap() } + private fun NewTypeVariable.shouldBeDefinitelyNotNull(): Boolean { + return when (this) { + is TypeVariableFromCallableDescriptor -> originalTypeParameter.name.asString() == TYPE_PARAMETER_FOR_EXCLXCL + is TypeVariableForLambdaReturnType -> false + } + } + // KotlinConstraintSystemCompleter.Context, PostponedArgumentsAnalyzer.Context override fun canBeProper(type: UnwrappedType): Boolean { checkState(State.BUILDING, State.COMPLETION) diff --git a/compiler/testData/codegen/box/regressions/nullableAfterExclExcl.kt b/compiler/testData/codegen/box/regressions/nullableAfterExclExcl.kt new file mode 100644 index 00000000000..781ff87ee01 --- /dev/null +++ b/compiler/testData/codegen/box/regressions/nullableAfterExclExcl.kt @@ -0,0 +1,13 @@ +interface Sample { + val callMe: Int +} + +class Caller(val member: M) { + fun test() { + member!!.callMe + } +} + +fun box(): String { + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 48c82a48416..858e66e1264 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -17042,6 +17042,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("nullableAfterExclExcl.kt") + public void testNullableAfterExclExcl() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullableAfterExclExcl.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 14fe12d94f8..e787d0ad547 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -17042,6 +17042,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("nullableAfterExclExcl.kt") + public void testNullableAfterExclExcl() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullableAfterExclExcl.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 0157c49c079..8e6577da8dd 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -17042,6 +17042,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("nullableAfterExclExcl.kt") + public void testNullableAfterExclExcl() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullableAfterExclExcl.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 3aab74dfe83..65596a14544 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -20828,6 +20828,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("nullableAfterExclExcl.kt") + public void testNullableAfterExclExcl() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullableAfterExclExcl.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.kt");