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
This commit is contained in:
Mikhail Glukhikh
2022-07-27 13:09:39 +02:00
committed by teamcity
parent 3e58c54da9
commit 91f3edc6a6
2 changed files with 10 additions and 2 deletions
@@ -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 ->
@@ -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)