From 321d3b9dd064f4205eef50cfd6f5f95689406b8c Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 17 Aug 2012 21:43:26 +0400 Subject: [PATCH] lambda: check all return expressions for constraints --- .../source/resolve/PsiResolveHelperImpl.java | 15 ++++++++--- .../TypeArgsConsistencyMisc1.java | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java index 2ad9cbc2964e..5bb8634d7044 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java @@ -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 constraint = null; final List 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 constraint = + + final Pair 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(leastUpperBound, ConstraintType.SUPERTYPE); + } else { + constraint = returnExprConstraint; + } } } + if (constraint != null) return constraint; } for (PsiParameter parameter : methodParameters) { if (LambdaUtil.dependsOnTypeParams(parameter.getType(), lambdaExpression)) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/TypeArgsConsistencyMisc1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/TypeArgsConsistencyMisc1.java index 8c901fa4fe80..19d7d09b152c 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/TypeArgsConsistencyMisc1.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/TypeArgsConsistencyMisc1.java @@ -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; + }); } }