From 8217aa96331f6aefd4a4f05186b3019a0e260f27 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 9 Feb 2024 09:04:08 +0100 Subject: [PATCH] K2: introduce fun interface PostponedAtomAnalyzer to make code cleaner --- .../inference/ConstraintSystemCompleter.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt index 79a4693f36b..03ae915afd3 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt @@ -38,6 +38,10 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { private val postponedArgumentsInputTypesResolver = inferenceComponents.postponedArgumentInputTypesResolver private val languageVersionSettings = components.session.languageVersionSettings + fun interface PostponedAtomAnalyzer { + fun analyze(postponedResolvedAtom: PostponedResolvedAtom, withPCLASession: Boolean) + } + fun complete( c: ConstraintSystemCompletionContext, completionMode: ConstraintSystemCompletionMode, @@ -45,8 +49,8 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { candidateReturnType: ConeKotlinType, context: ResolutionContext, collectVariablesFromContext: Boolean = false, - analyze: (PostponedResolvedAtom, Boolean) -> Unit, - ) = c.runCompletion(completionMode, topLevelAtoms, candidateReturnType, context, collectVariablesFromContext, analyze) + analyzer: PostponedAtomAnalyzer, + ) = c.runCompletion(completionMode, topLevelAtoms, candidateReturnType, context, collectVariablesFromContext, analyzer) private fun ConstraintSystemCompletionContext.runCompletion( completionMode: ConstraintSystemCompletionMode, @@ -54,7 +58,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { topLevelType: ConeKotlinType, context: ResolutionContext, collectVariablesFromContext: Boolean = false, - analyze: (PostponedResolvedAtom, Boolean) -> Unit, + analyzer: PostponedAtomAnalyzer, ) { val topLevelTypeVariables = topLevelType.extractTypeVariables() @@ -74,7 +78,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { // Stage 1: analyze postponed arguments with fixed parameter types if (analyzeArgumentWithFixedParameterTypes(languageVersionSettings, postponedArguments) { - analyze(it, /* withPCLASession = */ false) + analyzer.analyze(it, withPCLASession = false) } ) continue @@ -142,7 +146,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { // Stage 5: analyze the next ready postponed argument if (analyzeNextReadyPostponedArgument(languageVersionSettings, postponedArguments, completionMode) { - analyze(it, /* withPCLASession = */ false) + analyzer.analyze(it, withPCLASession = false) } ) continue @@ -152,7 +156,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { // Stage 7: try to complete call with the builder inference if there are uninferred type variables val areThereAppearedProperConstraintsForSomeVariable = tryToCompleteWithPCLA( - completionMode, postponedArguments, analyze, + completionMode, postponedArguments, analyzer, ) if (areThereAppearedProperConstraintsForSomeVariable) @@ -162,7 +166,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { // Complete all lambdas, maybe with fixing type variables used as top-level input types. // It's necessary because we need to process all data-flow info before going to the next statement. if (analyzeRemainingNotAnalyzedPostponedArgument(postponedArguments) { - analyze(it, /* withPCLASession = */ false) + analyzer.analyze(it, withPCLASession = false) } ) continue reportNotEnoughInformationForTypeVariablesRequiredForInputTypesOfLambdas( @@ -178,7 +182,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { // Stage 9: force analysis of remaining not analyzed postponed arguments and rerun stages if there are if (completionMode.allLambdasShouldBeAnalyzed) { if (analyzeRemainingNotAnalyzedPostponedArgument(postponedArguments) { - analyze(it, /* withPCLASession = */ false) + analyzer.analyze(it, withPCLASession = false) } ) continue } @@ -239,7 +243,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { private fun ConstraintSystemCompletionContext.tryToCompleteWithPCLA( completionMode: ConstraintSystemCompletionMode, postponedArguments: List, - analyze: (PostponedResolvedAtom, Boolean) -> Unit, + analyzer: PostponedAtomAnalyzer, ): Boolean { if (!completionMode.allLambdasShouldBeAnalyzed) return false @@ -253,7 +257,7 @@ class ConstraintSystemCompleter(components: BodyResolveComponents) { val notFixedInputTypeVariables = argument.inputTypes.flatMap { it.extractTypeVariables() }.filter { it !in fixedTypeVariables } if (notFixedInputTypeVariables.isEmpty()) continue - analyze(argument, /* withPCLASession = */ true) + analyzer.analyze(argument, withPCLASession = true) anyAnalyzed = true }