NI: Fix resolution ambiguity for references returned from lambda

^KT-32267 Fixed
This commit is contained in:
Denis Zharkov
2019-12-26 17:23:14 +03:00
committed by Mikhail Zarechenskiy
parent 534718794c
commit caf02806d5
13 changed files with 290 additions and 18 deletions
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtPsiUtil
import org.jetbrains.kotlin.psi.KtReturnExpression
@@ -116,6 +117,16 @@ class KotlinResolutionCallbacksImpl(
return it
}
val deparenthesizedExpression = KtPsiUtil.deparenthesize(ktExpression) ?: ktExpression
if (deparenthesizedExpression is KtCallableReferenceExpression) {
return psiCallResolver.createCallableReferenceKotlinCallArgument(
newContext, deparenthesizedExpression, DataFlowInfo.EMPTY,
CallMaker.makeExternalValueArgument(deparenthesizedExpression),
argumentName = null,
outerCallContext
)
}
return createSimplePSICallArgument(
trace.bindingContext, outerCallContext.statementFilter, outerCallContext.scope.ownerDescriptor,
CallMaker.makeExternalValueArgument(ktExpression), DataFlowInfo.EMPTY, typeInfo, languageVersionSettings,
@@ -702,7 +702,7 @@ class PSICallResolver(
return createSimplePSICallArgument(context, valueArgument, typeInfo) ?: createParseErrorElement()
}
private fun createCallableReferenceKotlinCallArgument(
fun createCallableReferenceKotlinCallArgument(
context: BasicCallResolutionContext,
ktExpression: KtCallableReferenceExpression,
startDataFlowInfo: DataFlowInfo,
@@ -895,7 +895,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (returnedExpression != null) {
if (newInferenceLambdaInfo != null) {
LambdaContextInfo contextInfo;
if (returnedExpression instanceof KtLambdaExpression) {
if (returnedExpression instanceof KtLambdaExpression || returnedExpression instanceof KtCallableReferenceExpression) {
contextInfo = new LambdaContextInfo(
new KotlinTypeInfo(DONT_CARE, context.dataFlowInfo),
null,
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.types.expressions;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -317,10 +316,24 @@ public class ExpressionTypingServices {
@NotNull CoercionStrategy coercionStrategyForLastExpression,
@NotNull ExpressionTypingInternals blockLevelVisitor
) {
boolean isUnitExpectedType = context.expectedType != NO_EXPECTED_TYPE &&
(
context.expectedType == UNIT_EXPECTED_TYPE ||
//the first check is necessary to avoid invocation 'isUnit(UNIT_EXPECTED_TYPE)'
(
coercionStrategyForLastExpression == COERCION_TO_UNIT &&
KotlinBuiltIns.isUnit(context.expectedType)
)
);
if (!isUnitExpectedType && statementExpression instanceof KtCallableReferenceExpression) {
KotlinTypeInfo typeInfo = createDontCareTypeInfoForNILambda(statementExpression, context);
if (typeInfo != null) return typeInfo;
}
if (context.expectedType != NO_EXPECTED_TYPE) {
KotlinType expectedType;
if (context.expectedType == UNIT_EXPECTED_TYPE ||//the first check is necessary to avoid invocation 'isUnit(UNIT_EXPECTED_TYPE)'
(coercionStrategyForLastExpression == COERCION_TO_UNIT && KotlinBuiltIns.isUnit(context.expectedType))) {
if (isUnitExpectedType) {
expectedType = UNIT_EXPECTED_TYPE;
}
else {
@@ -329,20 +342,12 @@ public class ExpressionTypingServices {
return blockLevelVisitor.getTypeInfo(statementExpression, context.replaceExpectedType(expectedType), true);
}
if (context.languageVersionSettings.supportsFeature(LanguageFeature.NewInference) &&
statementExpression instanceof KtLambdaExpression) {
PsiElement parent = PsiUtilsKt.getNonStrictParentOfType(statementExpression, KtFunctionLiteral.class);
if (parent != null) {
KtFunctionLiteral functionLiteral = (KtFunctionLiteral) parent;
KotlinResolutionCallbacksImpl.LambdaInfo info =
context.trace.getBindingContext().get(BindingContext.NEW_INFERENCE_LAMBDA_INFO, functionLiteral);
if (info != null) {
info.getLastExpressionInfo().setLexicalScope(context.scope);
info.getLastExpressionInfo().setTrace(context.trace);
return new KotlinTypeInfo(DONT_CARE, context.dataFlowInfo);
}
}
if (statementExpression instanceof KtLambdaExpression) {
KotlinTypeInfo typeInfo = createDontCareTypeInfoForNILambda(statementExpression, context);
if (typeInfo != null) return typeInfo;
}
KotlinTypeInfo result = blockLevelVisitor.getTypeInfo(statementExpression, context, true);
if (coercionStrategyForLastExpression == COERCION_TO_UNIT) {
boolean mightBeUnit = false;
@@ -369,6 +374,25 @@ public class ExpressionTypingServices {
return result;
}
@Nullable
private static KotlinTypeInfo createDontCareTypeInfoForNILambda(
@NotNull KtExpression statementExpression,
@NotNull ExpressionTypingContext context
) {
if (!context.languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) return null;
KtFunctionLiteral functionLiteral = PsiUtilsKt.getNonStrictParentOfType(statementExpression, KtFunctionLiteral.class);
if (functionLiteral != null) {
KotlinResolutionCallbacksImpl.LambdaInfo info =
context.trace.getBindingContext().get(BindingContext.NEW_INFERENCE_LAMBDA_INFO, functionLiteral);
if (info != null) {
info.getLastExpressionInfo().setLexicalScope(context.scope);
info.getLastExpressionInfo().setTrace(context.trace);
return new KotlinTypeInfo(DONT_CARE, context.dataFlowInfo);
}
}
return null;
}
private static class EffectsFilteringTrace extends AbstractFilteringTrace {
public EffectsFilteringTrace(BindingTrace parentTrace) {
super(parentTrace, "Effects filtering trace");