compare primitive types with equals as type annotations could spoil them (IDEA-147399)

This commit is contained in:
Anna Kozlova
2015-11-05 17:30:30 +01:00
parent 7a2ae3a051
commit b52bf6b5c0
9 changed files with 31 additions and 21 deletions
@@ -15,12 +15,12 @@
*/
package com.intellij.psi.impl.source.javadoc;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiType;
import com.intellij.psi.javadoc.JavadocTagInfo;
import com.intellij.psi.javadoc.PsiDocTagValue;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType;
import com.intellij.util.ArrayUtil;
/**
@@ -53,7 +53,7 @@ class ReturnDocTagInfo implements JavadocTagInfo {
PsiMethod method = (PsiMethod)element;
final PsiType type = method.getReturnType();
if (type == null) return false;
return type != PsiType.VOID;
return !PsiType.VOID.equals(type);
}
@Override
@@ -1491,7 +1491,7 @@ public class InferenceSession {
final PsiType sReturnType = sSubstitutor.substitute(sInterfaceMethod.getReturnType());
final PsiType tReturnType = tSubstitutor.substitute(tInterfaceMethod.getReturnType());
if (tReturnType == PsiType.VOID) {
if (PsiType.VOID.equals(tReturnType)) {
return true;
}
@@ -1507,8 +1507,8 @@ public class InferenceSession {
return false;
}
} else {
final boolean sPrimitive = sReturnType instanceof PsiPrimitiveType && sReturnType != PsiType.VOID;
final boolean tPrimitive = tReturnType instanceof PsiPrimitiveType && tReturnType != PsiType.VOID;
final boolean sPrimitive = sReturnType instanceof PsiPrimitiveType && !PsiType.VOID.equals(sReturnType);
final boolean tPrimitive = tReturnType instanceof PsiPrimitiveType && !PsiType.VOID.equals(tReturnType);
if (sPrimitive ^ tPrimitive) {
for (PsiExpression returnExpression : returnExpressions) {
if (!PsiPolyExpressionUtil.isPolyExpression(returnExpression)) {
@@ -1557,19 +1557,19 @@ public class InferenceSession {
}
final PsiType sReturnType = sSubstitutor.substitute(sInterfaceMethod.getReturnType());
final PsiType tReturnType = tSubstitutor.substitute(tInterfaceMethod.getReturnType());
if (tReturnType == PsiType.VOID) {
if (PsiType.VOID.equals(tReturnType)) {
return true;
}
final boolean sPrimitive = sReturnType instanceof PsiPrimitiveType && sReturnType != PsiType.VOID;
final boolean tPrimitive = tReturnType instanceof PsiPrimitiveType && tReturnType != PsiType.VOID;
final boolean sPrimitive = sReturnType instanceof PsiPrimitiveType && !PsiType.VOID.equals(sReturnType);
final boolean tPrimitive = tReturnType instanceof PsiPrimitiveType && !PsiType.VOID.equals(tReturnType);
if (sPrimitive ^ tPrimitive) {
final PsiMember member = ((PsiMethodReferenceExpression)arg).getPotentiallyApplicableMember();
LOG.assertTrue(member != null, arg);
if (member instanceof PsiMethod) {
final PsiType methodReturnType = ((PsiMethod)member).getReturnType();
if (sPrimitive && methodReturnType instanceof PsiPrimitiveType && methodReturnType != PsiType.VOID ||
if (sPrimitive && methodReturnType instanceof PsiPrimitiveType && !PsiType.VOID.equals(methodReturnType) ||
tPrimitive && methodReturnType instanceof PsiClassType) {
return true;
}
@@ -85,7 +85,7 @@ public class PsiGraphInferenceHelper implements PsiInferenceHelper {
PsiType arg,
boolean isContraVariantPosition,
LanguageLevel languageLevel) {
if (arg == PsiType.VOID || param == PsiType.VOID) return PsiType.NULL;
if (PsiType.VOID.equals(arg) || PsiType.VOID.equals(param)) return PsiType.NULL;
if (param instanceof PsiArrayType && arg instanceof PsiArrayType) {
return getSubstitutionForTypeParameter(typeParam, ((PsiArrayType)param).getComponentType(), ((PsiArrayType)arg).getComponentType(), isContraVariantPosition, languageLevel);
}
@@ -194,7 +194,7 @@ public class ExpressionCompatibilityConstraint extends InputOutputConstraintForm
PsiType returnType,
Set<InferenceVariable> result) {
if (psiExpression instanceof PsiLambdaExpression) {
if (returnType != PsiType.VOID) {
if (!PsiType.VOID.equals(returnType)) {
final List<PsiExpression> returnExpressions = LambdaUtil.getReturnExpressions((PsiLambdaExpression)psiExpression);
for (PsiExpression expression : returnExpressions) {
final Set<InferenceVariable> resultInputVars = createSelfConstraint(returnType, expression).getInputVariables(session);
@@ -104,8 +104,8 @@ public class PsiMethodReferenceCompatibilityConstraint implements ConstraintForm
} else {
return false;
}
if (returnType != PsiType.VOID && returnType != null) {
if (applicableMethodReturnType == PsiType.VOID) {
if (!PsiType.VOID.equals(returnType) && returnType != null) {
if (PsiType.VOID.equals(applicableMethodReturnType)) {
return false;
}
@@ -125,7 +125,7 @@ public class MethodReferenceResolver implements ResolveCache.PolyVariantContextR
return substitutor;
}
if (includeReturnConstraint && interfaceMethodReturnType != PsiType.VOID && interfaceMethodReturnType != null) {
if (includeReturnConstraint && !PsiType.VOID.equals(interfaceMethodReturnType) && interfaceMethodReturnType != null) {
final PsiType returnType = method.isConstructor() ? composeReturnType(containingClass, substitutor) : method.getReturnType();
if (returnType != null) {
session.registerReturnTypeConstraints(returnType, interfaceMethodReturnType);
@@ -224,7 +224,7 @@ public class PsiLambdaExpressionImpl extends ExpressionPsiElement implements Psi
}
PsiType methodReturnType = interfaceMethod.getReturnType();
if (methodReturnType != null && methodReturnType != PsiType.VOID) {
if (methodReturnType != null && !PsiType.VOID.equals(methodReturnType)) {
Map<PsiElement, PsiType> map = LambdaUtil.getFunctionalTypeMap();
try {
if (map.put(this, leftType) != null) {
@@ -249,7 +249,7 @@ public class PsiLambdaExpressionImpl extends ExpressionPsiElement implements Psi
}
final PsiType methodReturnType = interfaceMethod.getReturnType();
final PsiElement body = getBody();
if (methodReturnType == PsiType.VOID) {
if (PsiType.VOID.equals(methodReturnType)) {
if (body instanceof PsiCodeBlock) {
return isVoidCompatible();
} else {
@@ -460,7 +460,7 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
if (interfaceMethod != null) {
final PsiType interfaceReturnType = LambdaUtil.getFunctionalInterfaceReturnType(left);
if (interfaceReturnType == PsiType.VOID || interfaceReturnType == null) {
if (PsiType.VOID.equals(interfaceReturnType) || interfaceReturnType == null) {
return true;
}
@@ -477,7 +477,7 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
returnType = ((PsiMethod)resolve).getReturnType();
}
if (returnType == PsiType.VOID) {
if (PsiType.VOID.equals(returnType)) {
return false;
}
@@ -75,6 +75,16 @@ public class InferredTypeTest extends LightCodeInsightFixtureTestCase {
assertTrue(ensureNotCached.getCanonicalText(), ensureNotCached.equalsToText("java.util.List<java.lang.Integer>"));
}
public void testAnnotatedVoidReturnType() throws Exception {
myFixture.addClass("@java.lang.annotation.Target(value={java.lang.annotation.ElementType.TYPE_USE}) @interface D {}");
final PsiJavaFile file = (PsiJavaFile)myFixture.addFileToProject("R.java", "public interface R {@D void run();}");
final PsiClass psiClass = file.getClasses()[0];
final PsiMethod method = psiClass.getMethods()[0];
assertFalse(PsiType.VOID == method.getReturnType());
myFixture.configureByText("a.java", "class A {{R r = () -> {};}} ");
myFixture.checkHighlighting(false, false, false);
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {