mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
lambda: simple inference in case of conditional expressions (IDEA-90812)
This commit is contained in:
@@ -129,6 +129,9 @@ public class LambdaUtil {
|
||||
public static boolean isAcceptable(PsiLambdaExpression lambdaExpression, final PsiType leftType) {
|
||||
final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(leftType);
|
||||
final PsiClass psiClass = resolveResult.getElement();
|
||||
if (psiClass instanceof PsiAnonymousClass) {
|
||||
return isAcceptable(lambdaExpression, ((PsiAnonymousClass)psiClass).getBaseClassType());
|
||||
}
|
||||
final MethodSignature methodSignature = getFunction(psiClass);
|
||||
if (methodSignature == null) return false;
|
||||
final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
|
||||
@@ -331,7 +334,12 @@ public class LambdaUtil {
|
||||
@Nullable
|
||||
public static PsiType getFunctionalInterfaceType(PsiElement expression, final boolean tryToSubstitute) {
|
||||
PsiElement parent = expression.getParent();
|
||||
while (parent instanceof PsiParenthesizedExpression) {
|
||||
PsiElement element = expression;
|
||||
while (parent instanceof PsiParenthesizedExpression || parent instanceof PsiConditionalExpression) {
|
||||
if (parent instanceof PsiConditionalExpression &&
|
||||
((PsiConditionalExpression)parent).getThenExpression() != element &&
|
||||
((PsiConditionalExpression)parent).getElseExpression() != element) break;
|
||||
element = parent;
|
||||
parent = parent.getParent();
|
||||
}
|
||||
if (parent instanceof PsiArrayInitializerExpression) {
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.intellij.psi.util;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ProjectRootModificationTracker;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
@@ -644,8 +645,17 @@ public class TypeConversionUtil {
|
||||
|
||||
// todo[r.sh] implement
|
||||
if (right instanceof PsiMethodReferenceType && left instanceof PsiClassType) return true;
|
||||
if (right instanceof PsiLambdaExpressionType && left instanceof PsiClassType) {
|
||||
return LambdaUtil.isAcceptable(((PsiLambdaExpressionType)right).getExpression(), left);
|
||||
if (right instanceof PsiLambdaExpressionType) {
|
||||
final PsiLambdaExpression rLambdaExpression = ((PsiLambdaExpressionType)right).getExpression();
|
||||
if (left instanceof PsiClassType) {
|
||||
return LambdaUtil.isAcceptable(rLambdaExpression, left);
|
||||
}
|
||||
if (left instanceof PsiLambdaExpressionType) {
|
||||
final PsiLambdaExpression lLambdaExpression = ((PsiLambdaExpressionType)left).getExpression();
|
||||
final PsiType rType = rLambdaExpression.getFunctionalInterfaceType();
|
||||
final PsiType lType = lLambdaExpression.getFunctionalInterfaceType();
|
||||
return Comparing.equal(rType, lType);
|
||||
}
|
||||
}
|
||||
|
||||
if (left instanceof PsiIntersectionType) {
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
class Test {
|
||||
public interface I {
|
||||
int m();
|
||||
}
|
||||
|
||||
public interface I1 {
|
||||
int m(int y);
|
||||
}
|
||||
|
||||
{
|
||||
boolean flag = true;
|
||||
I i = flag ? (() -> 123) : (() -> 222);
|
||||
I i1 = flag ? (<error descr="Missing return value">() -> {}</error>) : (() -> 222);
|
||||
Object i2 = flag ? (<error descr="Target type of a lambda conversion must be an interface">() -> 42</error>) : (<error descr="Target type of a lambda conversion must be an interface">() -> 222</error>);
|
||||
<error descr="Incompatible types. Found: '<lambda expression>', required: 'Test.I'">I i3 = flag ? ((x) -> 42) : (() -> 222);</error>
|
||||
I i4 = flag ? (() -> 42) : new I() {
|
||||
@Override
|
||||
public int m() {
|
||||
return 0; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+4
@@ -120,6 +120,10 @@ public class LambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
|
||||
public void testUnhandledException() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testConditionalExpr() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false);
|
||||
|
||||
Reference in New Issue
Block a user