ExpressionTypeMemoryState:isSuperStateOf to check additional field

Fixes IDEA-183267 Completion based on instanceof is gone
This commit is contained in:
Tagir Valeev
2017-12-06 17:17:52 +07:00
parent e92d1c1daf
commit af2f419e5f
4 changed files with 54 additions and 0 deletions

View File

@@ -27,6 +27,9 @@ import com.intellij.util.containers.MultiMap;
import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Map;
/**
* @author peter
*/
@@ -68,6 +71,22 @@ public class ExpressionTypeMemoryState extends DfaMemoryStateImpl {
return copy;
}
@Override
public boolean isSuperStateOf(DfaMemoryStateImpl that) {
if (!super.isSuperStateOf(that)) {
return false;
}
MultiMap<PsiExpression, PsiType> thatStates = ((ExpressionTypeMemoryState)that).myStates;
for (Map.Entry<PsiExpression, Collection<PsiType>> entry : myStates.entrySet()) {
Collection<PsiType> thisTypes = entry.getValue();
Collection<PsiType> thatTypes = thatStates.get(entry.getKey());
if (!thatTypes.containsAll(thisTypes)) {
return false;
}
}
return true;
}
@Override
public boolean applyCondition(DfaValue dfaCond) {
if (dfaCond instanceof DfaInstanceofValue) {

View File

@@ -0,0 +1,17 @@
import java.math.BigInteger;
import java.util.function.Function;
class Test {
private static boolean instanceOfAfterFunction(Integer lambda,
Function<Integer, Number> replacer, Object type) {
Number function = replacer.apply(lambda);
if (function instanceof BigInteger && ((BigInteger)function).bitCount() > 0) {
return false;
}
if (type instanceof String && type.subst<caret>) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,17 @@
import java.math.BigInteger;
import java.util.function.Function;
class Test {
private static boolean instanceOfAfterFunction(Integer lambda,
Function<Integer, Number> replacer, Object type) {
Number function = replacer.apply(lambda);
if (function instanceof BigInteger && ((BigInteger)function).bitCount() > 0) {
return false;
}
if (type instanceof String && ((String) type).substring()) {
return false;
}
return true;
}
}

View File

@@ -50,6 +50,7 @@ class NormalCompletionDfaTest extends NormalCompletionTestCase {
void testCastWhenMethodComesFromDfaSuperType() { doTest() }
void testGenericTypeDfa() { doTest() }
void testNoUnnecessaryCastDfa() { doTest() }
void testInstanceOfAfterFunction() { doTest() }
void testCastTwice() {
configureByTestName()