From 8624d97f2908ee0b2b18d08fbeac3fd4476d063c Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 19 Jan 2022 18:17:33 +0300 Subject: [PATCH] [FIR] Extract adding of constraints from expected type to separate method --- .../fir/resolve/inference/FirCallCompleter.kt | 72 +++++++++++-------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt index 795c1182dea..bd292282fd4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt @@ -99,34 +99,15 @@ class FirCallCompleter( session.lookupTracker?.recordTypeResolveAsLookup(resolvedTypeRef, call.source, null) } - val expectedTypeConstraintPosition = ConeExpectedTypeConstraintPosition(expectedTypeMismatchIsReportedInChecker) - - when { - expectedTypeRef !is FirResolvedTypeRef -> { - // nothing - } - !shouldEnforceExpectedType -> { - candidate.system.addSubtypeConstraintIfCompatible( - initialType, expectedTypeRef.type, expectedTypeConstraintPosition - ) - } - isFromCast -> when { - candidate.isFunctionForExpectTypeFromCastFeature() -> { - candidate.system.addSubtypeConstraint( - initialType, expectedTypeRef.type, - ConeExpectedTypeConstraintPosition(expectedTypeMismatchIsReportedInChecker = false), - ) - } - } - !expectedTypeRef.coneType.isUnitOrFlexibleUnit || !mayBeCoercionToUnitApplied -> { - candidate.system.addSubtypeConstraint(initialType, expectedTypeRef.type, expectedTypeConstraintPosition) - } - candidate.system.notFixedTypeVariables.isNotEmpty() -> { - candidate.system.addSubtypeConstraintIfCompatible( - initialType, expectedTypeRef.type, expectedTypeConstraintPosition - ) - } - } + addConstraintFromExpectedType( + expectedTypeMismatchIsReportedInChecker, + expectedTypeRef, + shouldEnforceExpectedType, + candidate, + initialType, + isFromCast, + mayBeCoercionToUnitApplied + ) val completionMode = candidate.computeCompletionMode(session.inferenceComponents, expectedTypeRef, initialType) @@ -165,6 +146,41 @@ class FirCallCompleter( } } + private fun addConstraintFromExpectedType( + expectedTypeMismatchIsReportedInChecker: Boolean, + expectedTypeRef: FirTypeRef?, + shouldEnforceExpectedType: Boolean, + candidate: Candidate, + initialType: ConeKotlinType, + isFromCast: Boolean, + mayBeCoercionToUnitApplied: Boolean + ) { + val expectedType = expectedTypeRef?.coneTypeSafe() ?: return + val expectedTypeConstraintPosition = ConeExpectedTypeConstraintPosition(expectedTypeMismatchIsReportedInChecker) + + val system = candidate.system + when { + !shouldEnforceExpectedType -> { + system.addSubtypeConstraintIfCompatible(initialType, expectedType, expectedTypeConstraintPosition) + } + isFromCast -> { + if (candidate.isFunctionForExpectTypeFromCastFeature()) { + system.addSubtypeConstraint( + initialType, expectedType, + ConeExpectedTypeConstraintPosition(expectedTypeMismatchIsReportedInChecker = false), + ) + } + } + !expectedType.isUnitOrFlexibleUnit || !mayBeCoercionToUnitApplied -> { + system.addSubtypeConstraint(initialType, expectedType, expectedTypeConstraintPosition) + } + system.notFixedTypeVariables.isEmpty() -> return + else -> { + system.addSubtypeConstraintIfCompatible(initialType, expectedType, expectedTypeConstraintPosition) + } + } + } + fun runCompletionForCall( candidate: Candidate, completionMode: ConstraintSystemCompletionMode,