diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt index b7709b2d5a9..382b2ddff8d 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt @@ -86,12 +86,13 @@ object NewCommonSuperTypeCalculator { } // i.e. result type also should be marked nullable - val notAllNotNull = - types.any { !isTypeVariable(it) && !AbstractNullabilityChecker.isSubtypeOfAny(stateStubTypesEqualToAnything, it) } - val notNullTypes = if (notAllNotNull) types.map { it.withNullability(false) } else types + val allNotNull = types.all { + isTypeVariable(it) || isNotNullStubTypeForBuilderInference(it) || AbstractNullabilityChecker.isSubtypeOfAny(stateStubTypesEqualToAnything, it) + } + val notNullTypes = if (!allNotNull) types.map { it.withNullability(false) } else types val commonSuperType = commonSuperTypeForNotNullTypes(notNullTypes, depth, stateStubTypesEqualToAnything, stateStubTypesNotEqual) - return if (notAllNotNull) + return if (!allNotNull) refineNullabilityForUndefinedNullability(types, commonSuperType) ?: commonSuperType.withNullability(true) else commonSuperType @@ -207,6 +208,10 @@ object NewCommonSuperTypeCalculator { return type.isStubTypeForVariableInSubtyping() || isCapturedTypeVariable(type) } + private fun TypeSystemCommonSuperTypesContext.isNotNullStubTypeForBuilderInference(type: SimpleTypeMarker): Boolean { + return type.isStubTypeForBuilderInference() && !type.isMarkedNullable() + } + private fun TypeSystemCommonSuperTypesContext.isCapturedTypeVariable(type: SimpleTypeMarker): Boolean { val projectedType = type.asCapturedType()?.typeConstructor()?.projection()?.takeUnless { it.isStarProjection() }?.getType() ?: return false