mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
lambda: forbid inference method params types from return value
This commit is contained in:
@@ -594,12 +594,14 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
|
||||
|
||||
final PsiParameter[] methodParameters = method.getParameterList().getParameters();
|
||||
final PsiSubstitutor subst = resolveResult.getSubstitutor();
|
||||
final boolean methodParamsDependOnTypeParams = methodParamsDependOnTypeParams(lambdaExpression, methodParameters, subst);
|
||||
final PsiType returnType = subst.substitute(method.getReturnType());
|
||||
if (returnType != null && returnType != PsiType.VOID) {
|
||||
Pair<PsiType, ConstraintType> constraint = null;
|
||||
final List<PsiExpression> expressions = lambdaExpression.getReturnExpressions();
|
||||
for (final PsiExpression expression : expressions) {
|
||||
final boolean independent = LambdaUtil.isFreeFromTypeInferenceArgs(methodParameters, lambdaExpression, expression);
|
||||
if (independent && methodParamsDependOnTypeParams) return FAILED_INFERENCE;
|
||||
if (!independent && lowerBound != PsiType.NULL) {
|
||||
return null;
|
||||
}
|
||||
@@ -647,6 +649,17 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean methodParamsDependOnTypeParams(PsiLambdaExpression lambdaExpression,
|
||||
PsiParameter[] methodParameters,
|
||||
PsiSubstitutor subst) {
|
||||
for (PsiParameter parameter : methodParameters) {
|
||||
if (LambdaUtil.dependsOnTypeParams(subst.substitute(parameter.getType()), lambdaExpression)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Pair<PsiType, ConstraintType> inferConstraintFromLambdaFormalParams(PsiTypeParameter typeParam,
|
||||
PsiClassType.ClassResolveResult resolveResult,
|
||||
|
||||
+2
@@ -30,6 +30,8 @@ class ReturnTypeCompatibility {
|
||||
|
||||
public static void main(String[] args) {
|
||||
call((String i)->{ return i;});
|
||||
call(<error descr="Cyclic inference">i->{ return i;}</error>);
|
||||
call(<error descr="Cyclic inference">i->""</error>);
|
||||
call<error descr="'call(ReturnTypeCompatibility.I1<java.lang.Integer>)' in 'ReturnTypeCompatibility' cannot be applied to '(<lambda expression>)'">((int i)->{ return i;})</error>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user