conditional expression: use target type for overload cases when assignable

GitOrigin-RevId: 2be5da156f1726f8536e6fc6447ae847629b0001
This commit is contained in:
Anna Kozlova
2019-05-06 07:48:02 +02:00
committed by intellij-monorepo-bot
parent c8cf98f173
commit 0374b5f49c
3 changed files with 27 additions and 3 deletions

View File

@@ -67,11 +67,16 @@ public class PsiConditionalExpressionImpl extends ExpressionPsiElement implement
if (type1.equals(type2)) return type1;
if (PsiUtil.isLanguageLevel8OrHigher(this) &&
PsiPolyExpressionUtil.isPolyExpression(this) &&
!MethodCandidateInfo.isOverloadCheck(PsiUtil.skipParenthesizedExprUp(this.getParent()))) {
PsiPolyExpressionUtil.isPolyExpression(this)) {
//15.25.3 Reference Conditional Expressions
// The type of a poly reference conditional expression is the same as its target type.
return InferenceSession.getTargetType(this);
PsiType targetType = InferenceSession.getTargetType(this);
if (!MethodCandidateInfo.isOverloadCheck(PsiUtil.skipParenthesizedExprUp(this.getParent()))) {
return targetType;
}
return targetType != null &&
targetType.isAssignableFrom(type1) &&
targetType.isAssignableFrom(type2) ? targetType : null;
}
final int typeRank1 = TypeConversionUtil.getTypeRank(type1);

View File

@@ -0,0 +1,18 @@
interface I {
void f();
}
class B implements I {
@Override
public void f() {
}
}
class MyTest {
void m(I i) {}
void n(int ik) {
m(ik > 0 ? () -> {} : new B());
}
}

View File

@@ -202,6 +202,7 @@ public class GraphInferenceHighlightingTest extends LightDaemonAnalyzerTestCase
}
public void testNotInferredVarShouldNotBeUsedForInferenceJava10() { doTest(); }
public void testJavac8UnCaptureBug() { doTest(); }
public void testLambdaInConditional() { doTest(); }
public void testTopLevelParentNoParameters() {
doTest();
}