From 91f3edc6a625363b4bf41a4f15204c3b775645b1 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 27 Jul 2022 13:09:39 +0200 Subject: [PATCH] K1: add protection against exceptions possible in IDE partial resolve IDE partial resolve allows to drop some statements from resolve, in particular it can drop some lambda. However, this can lead to the situation when we have no descriptor for lambda or to incorrect type checks for its containing block. This commit protects about this situation --- .../org/jetbrains/kotlin/diagnostics/diagnosticUtils.kt | 7 ++++++- .../kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/diagnosticUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/diagnosticUtils.kt index d58a8c0baed..ee31a59e133 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/diagnosticUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/diagnosticUtils.kt @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeConstructorSubstitution import org.jetbrains.kotlin.types.TypeUtils +import org.jetbrains.kotlin.types.TypeUtils.noExpectedType import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny import org.jetbrains.kotlin.types.typeUtil.isNothing import org.jetbrains.kotlin.types.typeUtil.isNullableNothing @@ -48,7 +49,11 @@ fun ResolutionContext<*>.reportTypeMismatchDueToTypeProjection( expectedType: KotlinType, expressionType: KotlinType? ): Boolean { - if (!TypeUtils.contains(expectedType) { it.isAnyOrNullableAny() || it.isNothing() || it.isNullableNothing() }) return false + if (!TypeUtils.contains(expectedType) { + // We have to check expected type is available otherwise we'll get an exception + !noExpectedType(it) && (it.isAnyOrNullableAny() || it.isNothing() || it.isNullableNothing()) + } + ) return false val (resolvedCall, correspondingNotApproximatedTypeByDescriptor: (CallableDescriptor) -> KotlinType?) = when (callPosition) { is CallPosition.ValueArgumentPosition -> diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt index 231c6ee469b..964b85320f5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt @@ -330,7 +330,10 @@ class ResolvedAtomCompleter( } val descriptor = topLevelTrace.bindingContext.get(BindingContext.FUNCTION, ktFunction) as? SimpleFunctionDescriptorImpl - ?: throw AssertionError("No function descriptor for resolved lambda argument") + ?: + // Normally we should not be here, but in IDE partial resolve mode lambda analysis can be dropped, + // and it's possible we don't have any descriptor + return val substitutedLambdaTypes = substituteFunctionLiteralDescriptor(lambda, descriptor, resultSubstitutor)