diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceCompletion.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt similarity index 96% rename from compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceCompletion.kt rename to compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt index f6aac35b3e5..8b68d845247 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceCompletion.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt @@ -27,9 +27,6 @@ import org.jetbrains.kotlin.types.model.typeConstructor import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addToStdlib.safeAs -val Candidate.csBuilder: NewConstraintSystemImpl get() = system.getBuilder() - - class ConstraintSystemCompleter(private val components: BodyResolveComponents) { val variableFixationFinder = VariableFixationFinder(components.inferenceComponents.trivialConstraintTypeInferenceOracle) @@ -76,10 +73,6 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) { if (completionMode == ConstraintSystemCompletionMode.FULL) { // force resolution for all not-analyzed argument's getOrderedNotAnalyzedPostponedArguments(topLevelAtoms).forEach(analyze) -// -// if (c.notFixedTypeVariables.isNotEmpty() && c.postponedTypeVariables.isEmpty()) { -// runCompletion(c, completionMode, topLevelAtoms, topLevelType, analyze) -// } } } @@ -211,14 +204,6 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) { postponedResolveKtPrimitives: List ) { val direction = TypeVariableDirectionCalculator(c, postponedResolveKtPrimitives, topLevelType).getDirection(variableWithConstraints) - fixVariable(c, variableWithConstraints, direction) - } - - fun fixVariable( - c: KotlinConstraintSystemCompleter.Context, - variableWithConstraints: VariableWithConstraints, - direction: TypeVariableDirectionCalculator.ResolveDirection - ) { val resultType = components.inferenceComponents.resultTypeResolver.findResultType(c, variableWithConstraints, direction) c.fixVariable(variableWithConstraints.typeVariable, resultType, atom = null) // TODO: obtain atom for diagnostics } @@ -325,3 +310,5 @@ private fun FirResolvable.processCandidateIfApplicable( } } } + +val Candidate.csBuilder: NewConstraintSystemImpl get() = system.getBuilder() 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 c9479a4f586..22d80cd8887 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 @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.fir.expressions.FirStatement import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents import org.jetbrains.kotlin.fir.resolve.calls.Candidate import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor -import org.jetbrains.kotlin.fir.resolve.transformers.FirCallCompletionResultsWriterTransformer import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeStubType import org.jetbrains.kotlin.fir.types.ConeTypeVariable @@ -87,7 +86,7 @@ class FirBuilderInferenceSession( ): Map? { val (commonSystem, effectivelyEmptyConstraintSystem) = buildCommonSystem(initialStorage) if (effectivelyEmptyConstraintSystem) { - updateCalls(commonSystem, lambda) + updateCalls(commonSystem) return null } @@ -103,7 +102,7 @@ class FirBuilderInferenceSession( error("Shouldn't be called in complete constraint system mode") } - updateCalls(commonSystem, lambda) + updateCalls(commonSystem) @Suppress("UNCHECKED_CAST") return commonSystem.fixedTypeVariables as Map @@ -199,7 +198,7 @@ class FirBuilderInferenceSession( return introducedConstraint } - private fun updateCalls(commonSystem: NewConstraintSystemImpl, lambda: ResolvedLambdaAtom) { + private fun updateCalls(commonSystem: NewConstraintSystemImpl) { val nonFixedToVariablesSubstitutor = createNonFixedTypeToVariableSubstitutor() val commonSystemSubstitutor = commonSystem.buildCurrentSubstitutor() as ConeSubstitutor val nonFixedTypesToResultSubstitutor = ConeComposedSubstitutor(commonSystemSubstitutor, nonFixedToVariablesSubstitutor) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt index d804f18373e..dfad22ba1b8 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt @@ -115,7 +115,6 @@ class FirDelegatedPropertyInferenceSession( addConstraintForThis(commonSystem) } - private fun Candidate.addConstraintsForSetValueMethod(commonSystem: ConstraintSystemBuilder) { if (expectedType != null) { val accessor = symbol.fir as? FirSimpleFunction ?: return diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceComponents.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceComponents.kt index 92a18b9a0f5..3f57aef502c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceComponents.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceComponents.kt @@ -31,9 +31,7 @@ class InferenceComponents( return ConeClassErrorType(message) } } - val trivialConstraintTypeInferenceOracle = - TrivialConstraintTypeInferenceOracle - .create(ctx) + val trivialConstraintTypeInferenceOracle = TrivialConstraintTypeInferenceOracle.create(ctx) private val incorporator = ConstraintIncorporator(approximator, trivialConstraintTypeInferenceOracle) private val injector = ConstraintInjector(incorporator, approximator, KotlinTypeRefiner.Default) val resultTypeResolver = ResultTypeResolver(approximator, trivialConstraintTypeInferenceOracle) @@ -55,5 +53,4 @@ class InferenceComponents( fun createConstraintSystem(): NewConstraintSystemImpl { return NewConstraintSystemImpl(injector, ctx) } - -} \ No newline at end of file +} diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceUtils.kt index 85918520d29..04d767e0497 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/InferenceUtils.kt @@ -13,41 +13,41 @@ import org.jetbrains.kotlin.fir.resolve.calls.Candidate import org.jetbrains.kotlin.fir.resolve.calls.isExtensionFunctionType import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.types.* +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract +@OptIn(ExperimentalContracts::class) fun ConeKotlinType.isBuiltinFunctionalType(session: FirSession): Boolean { - return when (this) { - is ConeClassLikeType -> { - val classId = fullyExpandedType(session).lookupTag.classId - val kind = - FunctionClassDescriptor.Kind.byClassNamePrefix(classId.packageFqName, classId.relativeClassName.asString()) ?: return false - kind == FunctionClassDescriptor.Kind.Function || kind == FunctionClassDescriptor.Kind.SuspendFunction - } - else -> false + contract { + returns(true) implies (this@isBuiltinFunctionalType is ConeClassLikeType) } + if (this !is ConeClassLikeType) return false + val classId = fullyExpandedType(session).lookupTag.classId + val kind = FunctionClassDescriptor.Kind.byClassNamePrefix(classId.packageFqName, classId.relativeClassName.asString()) ?: return false + return kind == FunctionClassDescriptor.Kind.Function || kind == FunctionClassDescriptor.Kind.SuspendFunction } +@OptIn(ExperimentalContracts::class) fun ConeKotlinType.isSuspendFunctionType(session: FirSession): Boolean { - return when (val type = this) { - is ConeClassLikeType -> { - val classId = type.fullyExpandedType(session).lookupTag.classId - val kind = - FunctionClassDescriptor.Kind.byClassNamePrefix(classId.packageFqName, classId.relativeClassName.asString()) ?: return false - kind == FunctionClassDescriptor.Kind.SuspendFunction - } - else -> false + contract { + returns(true) implies (this@isSuspendFunctionType is ConeClassLikeType) } + if (this !is ConeClassLikeType) return false + val classId = this.fullyExpandedType(session).lookupTag.classId + val kind = FunctionClassDescriptor.Kind.byClassNamePrefix(classId.packageFqName, classId.relativeClassName.asString()) ?: return false + return kind == FunctionClassDescriptor.Kind.SuspendFunction } fun ConeKotlinType.receiverType(expectedTypeRef: FirTypeRef?, session: FirSession): ConeKotlinType? { if (isBuiltinFunctionalType(session) && expectedTypeRef?.isExtensionFunctionType(session) == true) { - return ((this as ConeClassLikeType).fullyExpandedType(session).typeArguments.first() as ConeKotlinTypeProjection).type + return (this.fullyExpandedType(session).typeArguments.first() as ConeKotlinTypeProjection).type } return null } fun ConeKotlinType.receiverType(session: FirSession): ConeKotlinType? { if (isBuiltinFunctionalType(session)) { - return ((this as ConeClassLikeType).fullyExpandedType(session).typeArguments.first() as ConeKotlinTypeProjection).type + return (this.fullyExpandedType(session).typeArguments.first() as ConeKotlinTypeProjection).type } return null } @@ -65,8 +65,8 @@ fun ConeKotlinType.valueParameterTypesIncludingReceiver(session: FirSession): Li } } -val FirAnonymousFunction.returnType get() = returnTypeRef.coneTypeSafe() -val FirAnonymousFunction.receiverType get() = receiverTypeRef?.coneTypeSafe() +val FirAnonymousFunction.returnType: ConeKotlinType? get() = returnTypeRef.coneTypeSafe() +val FirAnonymousFunction.receiverType: ConeKotlinType? get() = receiverTypeRef?.coneTypeSafe() fun extractLambdaInfoFromFunctionalType( expectedType: ConeKotlinType?, @@ -79,7 +79,14 @@ fun extractLambdaInfoFromFunctionalType( val session = components.session if (expectedType == null) return null if (expectedType is ConeFlexibleType) { - return extractLambdaInfoFromFunctionalType(expectedType.lowerBound, expectedTypeRef, argument, returnTypeVariable, components, candidate) + return extractLambdaInfoFromFunctionalType( + expectedType.lowerBound, + expectedTypeRef, + argument, + returnTypeVariable, + components, + candidate + ) } if (!expectedType.isBuiltinFunctionalType(session)) return null diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt index 095f90b5a43..5c1cae3be6f 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt @@ -41,7 +41,6 @@ interface LambdaAnalyzer { ): ReturnArgumentsAnalysisResult } - class PostponedArgumentsAnalyzer( private val lambdaAnalyzer: LambdaAnalyzer, private val components: InferenceComponents, @@ -74,24 +73,24 @@ class PostponedArgumentsAnalyzer( val callableReferenceAccess = atom.reference atom.analyzed = true - val (candidate, applicability) = atom.resultingCandidate + val (resultingCandidate, applicability) = atom.resultingCandidate ?: Pair(null, CandidateApplicability.INAPPLICABLE) val namedReference = when { - candidate == null || applicability < CandidateApplicability.SYNTHETIC_RESOLVED -> + resultingCandidate == null || applicability < CandidateApplicability.SYNTHETIC_RESOLVED -> buildErrorNamedReference { source = callableReferenceAccess.source diagnostic = ConeUnresolvedReferenceError(callableReferenceAccess.calleeReference.name) } - else -> FirNamedReferenceWithCandidate(callableReferenceAccess.source, callableReferenceAccess.calleeReference.name, candidate) + else -> FirNamedReferenceWithCandidate(callableReferenceAccess.source, callableReferenceAccess.calleeReference.name, resultingCandidate) } - val transformedCalleeReference = callableReferenceAccess.transformCalleeReference( + callableReferenceAccess.transformCalleeReference( StoreNameReference, namedReference ).apply { - if (candidate != null) { - replaceTypeRef(buildResolvedTypeRef { type = candidate.resultingTypeForCallableReference!! }) + if (resultingCandidate != null) { + replaceTypeRef(buildResolvedTypeRef { type = resultingCandidate.resultingTypeForCallableReference!! }) } } } @@ -102,7 +101,7 @@ class PostponedArgumentsAnalyzer( candidate: Candidate //diagnosticHolder: KotlinDiagnosticsHolder ) { - val unitType = components.session.builtinTypes.unitType.type//Unit(components.session.firSymbolProvider).constructType(emptyArray(), false) + val unitType = components.session.builtinTypes.unitType.type val stubsForPostponedVariables = c.bindingStubsForPostponedVariables() val currentSubstitutor = c.buildCurrentSubstitutor(stubsForPostponedVariables.mapKeys { it.key.freshTypeConstructor(c) }) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedAtoms.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedAtoms.kt index b61062b3713..f1239df0c05 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedAtoms.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedAtoms.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.resolve.calls.model.PostponedResolvedAtomMarker // --------------------------- Variables --------------------------- - class ConeTypeVariableForLambdaReturnType(val argument: FirAnonymousFunction, name: String) : ConeTypeVariable(name) // -------------------------- Atoms -------------------------- @@ -44,7 +43,7 @@ class ResolvedLambdaAtom( val parameters: List, val returnType: ConeKotlinType, val typeVariableForLambdaReturnType: ConeTypeVariableForLambdaReturnType?, - val candidateOfOuterCall: Candidate? + candidateOfOuterCall: Candidate? ) : PostponedResolvedAtom() { init { candidateOfOuterCall?.let {