lambda: check all return expressions for constraints

This commit is contained in:
Anna Kozlova
2012-08-17 22:45:24 +04:00
parent 7c519fe536
commit 321d3b9dd0
2 changed files with 37 additions and 3 deletions
@@ -595,6 +595,7 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
final PsiSubstitutor subst = resolveResult.getSubstitutor();
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);
@@ -621,12 +622,20 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
if (exprType == null){
return FAILED_INFERENCE;
}
Pair<PsiType, ConstraintType> constraint =
final Pair<PsiType, ConstraintType> returnExprConstraint =
getSubstitutionForTypeParameterConstraint(typeParam, returnType, exprType, false, PsiUtil.getLanguageLevel(method));
if (constraint != null) {
return constraint; //todo check that all return statements lead to the same inference
if (returnExprConstraint != null) {
if (returnExprConstraint == FAILED_INFERENCE) return returnExprConstraint;
if (constraint != null) {
final PsiType leastUpperBound = GenericsUtil.getLeastUpperBound(constraint.getFirst(), returnExprConstraint.getFirst(), typeParam.getManager());
constraint = new Pair<PsiType, ConstraintType>(leastUpperBound, ConstraintType.SUPERTYPE);
} else {
constraint = returnExprConstraint;
}
}
}
if (constraint != null) return constraint;
}
for (PsiParameter parameter : methodParameters) {
if (LambdaUtil.dependsOnTypeParams(parameter.getType(), lambdaExpression)) {
@@ -1,3 +1,18 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.List;
class Test1 {
@@ -23,6 +38,16 @@ class Test1 {
bar2("", x -> x);
bar3(x -> x, "");
int ixc = 42;
bar(x -> {
if (ixc == 2) return "aaa";
return x;
});
bar(x -> {
if (ixc == 2) return x;
return x;
});
}
}