From 02052421e658c91bfe3cc84d63a8330ca662d143 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Mon, 4 Dec 2023 17:47:50 +0100 Subject: [PATCH] K2 Inference. Fix naming of locals to match its purpose There were a number of locals where components of InitialConstraint were named lower and upper in the FirBuilderInferenceSession. That is not true for equality constraints, so such naming should be avoided to avoid misconception about the nature of type relation encoded in constraint. ^KT-64031 Fixed --- .../inference/FirBuilderInferenceSession.kt | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt index 2a6b5d959ab..99ea27a8a4e 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt @@ -280,39 +280,37 @@ class FirBuilderInferenceSession( val substitutedConstraintWith = initialConstraint.substitute(callSubstitutor).substituteNonFixedToVariables(nonFixedToVariablesSubstitutor) - // TODO(KT-64031): Invalid naming, it doesn't make sense in case of equality constraints - val lower = substitutedConstraintWith.a - val upper = substitutedConstraintWith.b + val substitutedA = substitutedConstraintWith.a + val substitutedB = substitutedConstraintWith.b - if (commonSystem.isProperType(lower) && (lower == upper || commonSystem.isProperType(upper))) return false + if (commonSystem.isProperType(substitutedA) && (substitutedA == substitutedB || commonSystem.isProperType(substitutedB))) return false val position = substitutedConstraintWith.position when (initialConstraint.constraintKind) { ConstraintKind.LOWER -> error("LOWER constraint shouldn't be used, please use UPPER") - ConstraintKind.UPPER -> commonSystem.addSubtypeConstraint(lower, upper, position) + ConstraintKind.UPPER -> commonSystem.addSubtypeConstraint(substitutedA, substitutedB, position) ConstraintKind.EQUALITY -> with(commonSystem) { - addSubtypeConstraint(lower, upper, position) - addSubtypeConstraint(upper, lower, position) + addSubtypeConstraint(substitutedA, substitutedB, position) + addSubtypeConstraint(substitutedB, substitutedA, position) } } return true } private fun InitialConstraint.substitute(substitutor: TypeSubstitutorMarker): InitialConstraint { - // TODO(KT-64031): Invalid naming, it doesn't make sense in case of equality constraints - val lowerSubstituted = substitutor.safeSubstitute(resolutionContext.typeContext, this.a) - val upperSubstituted = substitutor.safeSubstitute(resolutionContext.typeContext, this.b) + val substitutedA = substitutor.safeSubstitute(resolutionContext.typeContext, this.a) + val substitutedB = substitutor.safeSubstitute(resolutionContext.typeContext, this.b) - if (lowerSubstituted == a && upperSubstituted == b) return this + if (substitutedA == a && substitutedB == b) return this // TODO(KT-64033): Missing check for ForbidInferringPostponedTypeVariableIntoDeclaredUpperBound language feature return InitialConstraint( - lowerSubstituted, - upperSubstituted, + substitutedA, + substitutedB, this.constraintKind, ConeBuilderInferenceSubstitutionConstraintPosition(this) ) @@ -348,8 +346,8 @@ class FirBuilderInferenceSession( // a = C, b = D // commonCapTypes = [CapturedType(out B)_0] // capTypesSubstitutor = { CapturedTypeConstructor_0 => CapturedType(out W)_1 } - // substitutedLowerType = C - // substitutedUpperType = D + // substitutedA = C + // substitutedB = D val substitutedCommonCapType = commonCapTypes.associate { it.constructor to nonFixedToVariablesSubstitutor.substituteOrSelf(it) } @@ -361,19 +359,18 @@ class FirBuilderInferenceSession( } } - // TODO(KT-64031): invalid naming. It doesn't make sense in case of equality constraints - val substitutedLowerType = nonFixedToVariablesSubstitutor.safeSubstitute( + val substitutedA = nonFixedToVariablesSubstitutor.safeSubstitute( resolutionContext.typeContext, capTypesSubstitutor.safeSubstitute(resolutionContext.typeContext, this.a) ) - val substitutedUpperType = nonFixedToVariablesSubstitutor.safeSubstitute( + val substitutedB = nonFixedToVariablesSubstitutor.safeSubstitute( resolutionContext.typeContext, capTypesSubstitutor.safeSubstitute(resolutionContext.typeContext, this.b) ) return InitialConstraint( - substitutedLowerType, - substitutedUpperType, + substitutedA, + substitutedB, constraintKind, position )