From 97bec59bc931d805b4544777495ebc8c7edbaafc Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 14 Mar 2023 11:40:54 +0100 Subject: [PATCH] K1: add forgotten INFERRED_INTO_DECLARED_UPPER_BOUNDS for incorporation case #KT-56169 Fixed --- .../KotlinConstraintSystemCompleter.kt | 26 ++++++++++++------- .../builderInference/buildListToUpperBound.kt | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt index fcc3fd7e673..9a9b4f5ff36 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt @@ -14,10 +14,7 @@ import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.error.ErrorTypeKind import org.jetbrains.kotlin.types.error.ErrorUtils -import org.jetbrains.kotlin.types.model.TypeConstructorMarker -import org.jetbrains.kotlin.types.model.TypeVariableMarker -import org.jetbrains.kotlin.types.model.TypeVariableTypeConstructorMarker -import org.jetbrains.kotlin.types.model.safeSubstitute +import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.types.typeUtil.asTypeProjection import org.jetbrains.kotlin.utils.addIfNotNull @@ -453,14 +450,23 @@ class KotlinConstraintSystemCompleter( private fun reportWarningIfFixedIntoDeclaredUpperBounds( diagnosticsHolder: KotlinDiagnosticsHolder, - variableWithConstraints: VariableWithConstraints + variableWithConstraints: VariableWithConstraints, + resultType: KotlinTypeMarker ) { - val areAllConstraintFromDeclaredUpperBounds = variableWithConstraints.constraints.all { - val position = it.position.from - position is BuilderInferenceSubstitutionConstraintPosition<*, *> && position.isFromNotSubstitutedDeclaredUpperBound + var upperBoundType: KotlinTypeMarker? = null + val constraintFromDeclaredUpperBoundExists = variableWithConstraints.constraints.any { constraint -> + val position = constraint.position.from.let { basicPosition -> + if (basicPosition is IncorporationConstraintPosition) basicPosition.from else basicPosition + } + if (position is BuilderInferenceSubstitutionConstraintPosition<*, *> && position.isFromNotSubstitutedDeclaredUpperBound) { + upperBoundType = constraint.type + true + } else { + false + } } - if (areAllConstraintFromDeclaredUpperBounds) { + if (constraintFromDeclaredUpperBoundExists && upperBoundType == resultType) { diagnosticsHolder.addDiagnostic( KotlinConstraintSystemDiagnostic(InferredIntoDeclaredUpperBounds(variableWithConstraints.typeVariable)) ) @@ -478,7 +484,7 @@ class KotlinConstraintSystemCompleter( val variable = variableWithConstraints.typeVariable val resolvedAtom = findResolvedAtomBy(variable, topLevelAtoms) ?: topLevelAtoms.firstOrNull() - reportWarningIfFixedIntoDeclaredUpperBounds(diagnosticsHolder, variableWithConstraints) + reportWarningIfFixedIntoDeclaredUpperBounds(diagnosticsHolder, variableWithConstraints, resultType) c.fixVariable(variable, resultType, FixVariableConstraintPositionImpl(variable, resolvedAtom)) } diff --git a/compiler/testData/diagnostics/testsWithStdLib/builderInference/buildListToUpperBound.kt b/compiler/testData/diagnostics/testsWithStdLib/builderInference/buildListToUpperBound.kt index 909030f40d5..ff647131a90 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/builderInference/buildListToUpperBound.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/builderInference/buildListToUpperBound.kt @@ -3,7 +3,7 @@ // ISSUE: KT-50520 fun box(): String { - buildList { + buildList { val foo = { first() } add(0, foo) }