mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
new inference: infer recursively for conditional expression
This commit is contained in:
+35
-27
@@ -28,7 +28,6 @@ import com.intellij.util.ArrayUtilRt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -274,32 +273,7 @@ public class InferenceSession {
|
||||
if (!PsiType.VOID.equals(returnType) && returnType != null) {
|
||||
PsiType targetType = PsiTypesUtil.getExpectedTypeByParent(context);
|
||||
if (targetType == null) {
|
||||
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(context.getParent());
|
||||
if (parent instanceof PsiExpressionList) {
|
||||
final PsiElement gParent = parent.getParent();
|
||||
if (gParent instanceof PsiCallExpression) {
|
||||
final PsiExpressionList argumentList = ((PsiCallExpression)gParent).getArgumentList();
|
||||
if (argumentList != null) {
|
||||
final Pair<PsiMethod, PsiSubstitutor> pair = MethodCandidateInfo.getCurrentMethod(argumentList);
|
||||
final JavaResolveResult resolveResult = pair == null ? ((PsiCallExpression)gParent).resolveMethodGenerics() : null;
|
||||
final PsiElement parentMethod = pair != null ? pair.first : resolveResult.getElement();
|
||||
if (parentMethod instanceof PsiMethod) {
|
||||
final PsiParameter[] parameters = ((PsiMethod)parentMethod).getParameterList().getParameters();
|
||||
PsiElement arg = context;
|
||||
while (arg.getParent() instanceof PsiParenthesizedExpression) {
|
||||
arg = parent.getParent();
|
||||
}
|
||||
final PsiExpression[] args = argumentList.getExpressions();
|
||||
targetType = getParameterType(parameters, args, ArrayUtilRt.find(args, arg), pair != null ? pair.second : resolveResult.getSubstitutor());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (parent instanceof PsiConditionalExpression) {
|
||||
targetType = PsiTypesUtil.getExpectedTypeByParent((PsiExpression)parent);
|
||||
}
|
||||
else if (parent instanceof PsiLambdaExpression) {
|
||||
targetType = LambdaUtil.getFunctionalInterfaceReturnType(((PsiLambdaExpression)parent).getFunctionalInterfaceType());
|
||||
}
|
||||
targetType = getTargetType(context);
|
||||
}
|
||||
if (targetType != null) {
|
||||
myConstraints.add(new TypeCompatibilityConstraint(GenericsUtil.eliminateWildcards(targetType, false), PsiImplUtil.normalizeWildcardTypeByPosition(returnType, context)));
|
||||
@@ -308,6 +282,40 @@ public class InferenceSession {
|
||||
}
|
||||
}
|
||||
|
||||
private static PsiType getTargetType(PsiExpression context) {
|
||||
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(context.getParent());
|
||||
if (parent instanceof PsiExpressionList) {
|
||||
final PsiElement gParent = parent.getParent();
|
||||
if (gParent instanceof PsiCallExpression) {
|
||||
final PsiExpressionList argumentList = ((PsiCallExpression)gParent).getArgumentList();
|
||||
if (argumentList != null) {
|
||||
final Pair<PsiMethod, PsiSubstitutor> pair = MethodCandidateInfo.getCurrentMethod(argumentList);
|
||||
final JavaResolveResult resolveResult = pair == null ? ((PsiCallExpression)gParent).resolveMethodGenerics() : null;
|
||||
final PsiElement parentMethod = pair != null ? pair.first : resolveResult.getElement();
|
||||
if (parentMethod instanceof PsiMethod) {
|
||||
final PsiParameter[] parameters = ((PsiMethod)parentMethod).getParameterList().getParameters();
|
||||
PsiElement arg = context;
|
||||
while (arg.getParent() instanceof PsiParenthesizedExpression) {
|
||||
arg = arg.getParent();
|
||||
}
|
||||
final PsiExpression[] args = argumentList.getExpressions();
|
||||
return getParameterType(parameters, args, ArrayUtilRt.find(args, arg), pair != null ? pair.second : resolveResult.getSubstitutor());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (parent instanceof PsiConditionalExpression) {
|
||||
PsiType targetType = PsiTypesUtil.getExpectedTypeByParent((PsiExpression)parent);
|
||||
if (targetType == null) {
|
||||
targetType = getTargetType((PsiExpression)parent);
|
||||
}
|
||||
return targetType;
|
||||
}
|
||||
else if (parent instanceof PsiLambdaExpression) {
|
||||
return LambdaUtil.getFunctionalInterfaceReturnType(((PsiLambdaExpression)parent).getFunctionalInterfaceType());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public InferenceVariable getInferenceVariable(PsiType psiType) {
|
||||
return getInferenceVariable(psiType, true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user