Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip stores when comparing to a constant boolean. #3721

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions checker/tests/nullness/flow/Issue3275.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Test case for issue #3275:
// https://github.com/typetools/checker-framework/issues/3275

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

class Issue3275 {
public @NonNull Object f = new Object();
public boolean b = false;

void return_n(@Nullable Object obj) {
if (obj != null) {
obj.toString();
}
}

void return_np(@Nullable Object obj) {
if ((obj != null)) {
obj.toString();
}
}

void return_en(@Nullable Object obj) {
if (!(obj == null)) {
obj.toString();
}
}

void return_eet(@Nullable Object obj) {
if ((obj == null) == true) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void return_eef(@Nullable Object obj) {
if ((obj == null) == false) {
obj.toString();
}
}

void return_eeb(@Nullable Object obj) {
if ((obj == null) == b) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void return_ent(@Nullable Object obj) {
if ((obj == null) != true) {
obj.toString();
}
}

void return_enf(@Nullable Object obj) {
if ((obj == null) != false) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void return_enb(@Nullable Object obj) {
if ((obj == null) != b) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void return_net(@Nullable Object obj) {
if ((obj != null) == true) {
obj.toString();
}
}

void return_nef(@Nullable Object obj) {
if ((obj != null) == false) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void return_neb(@Nullable Object obj) {
if ((obj != null) == b) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void return_nnt(@Nullable Object obj) {
if ((obj != null) != true) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void return_nnf(@Nullable Object obj) {
if ((obj != null) != false) {
obj.toString();
}
}

void return_nnb(@Nullable Object obj) {
if ((obj != null) != b) {
// :: error: (dereference.of.nullable)
obj.toString();
}
}

void assign_n(@Nullable Object obj) {
if (obj != null) {
f = obj;
}
}

void assign_np(@Nullable Object obj) {
if ((obj != null)) {
f = obj;
}
}

void assign_en(@Nullable Object obj) {
if (!(obj == null)) {
f = obj;
}
}

void assign_eet(@Nullable Object obj) {
if ((obj == null) == true) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}

void assign_eef(@Nullable Object obj) {
if ((obj == null) == false) {
f = obj;
}
}

void assign_eeb(@Nullable Object obj) {
if ((obj == null) == b) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}

void assign_ent(@Nullable Object obj) {
if ((obj == null) != true) {
f = obj;
}
}

void assign_enf(@Nullable Object obj) {
if ((obj == null) != false) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}

void assign_enb(@Nullable Object obj) {
if ((obj == null) != b) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}

void assign_net(@Nullable Object obj) {
if ((obj != null) == true) {
f = obj;
}
}

void assign_nef(@Nullable Object obj) {
if ((obj != null) == false) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}

void assign_neb(@Nullable Object obj) {
if ((obj != null) == b) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}

void assign_nnt(@Nullable Object obj) {
if ((obj != null) != true) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}

void assign_nnf(@Nullable Object obj) {
if ((obj != null) != false) {
f = obj;
}
}

void assign_nnb(@Nullable Object obj) {
if ((obj != null) != b) {
// :: error: (assignment.type.incompatible)
f = obj;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
import org.checkerframework.dataflow.cfg.node.BooleanLiteralNode;
import org.checkerframework.dataflow.cfg.node.ConditionalNotNode;
import org.checkerframework.dataflow.cfg.node.ConditionalOrNode;
import org.checkerframework.dataflow.cfg.node.FieldAccessNode;
import org.checkerframework.dataflow.cfg.node.MethodInvocationNode;
Expand Down Expand Up @@ -69,4 +71,21 @@ public static boolean isMethodInvocation(
ExecutableElement invoked = ((MethodInvocationNode) node).getTarget().getMethod();
return ElementUtils.isMethod(invoked, method, env);
}

/**
* Returns true if the given node statically evaluates to {@code value} and has no side effects.
*
* @param n a node
* @param value the boolean value that the node is tested against
* @return true if the node is equivalent to a literal with value {@code value}
*/
public static boolean isConstantBoolean(Node n, boolean value) {
if (n instanceof BooleanLiteralNode) {
return ((BooleanLiteralNode) n).getValue() == value;
} else if (n instanceof ConditionalNotNode) {
return isConstantBoolean(((ConditionalNotNode) n).getOperand(), !value);
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.checkerframework.dataflow.cfg.node.ThisLiteralNode;
import org.checkerframework.dataflow.cfg.node.VariableDeclarationNode;
import org.checkerframework.dataflow.cfg.node.WideningConversionNode;
import org.checkerframework.dataflow.util.NodeUtils;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType;
Expand Down Expand Up @@ -717,6 +718,14 @@ public TransferResult<V, S> visitEqualTo(EqualToNode n, TransferInput<V, S> p) {
V leftV = p.getValueOfSubNode(leftN);
V rightV = p.getValueOfSubNode(rightN);

if (res.containsTwoStores()
&& (NodeUtils.isConstantBoolean(leftN, false)
|| NodeUtils.isConstantBoolean(rightN, false))) {
S thenStore = res.getElseStore();
S elseStore = res.getThenStore();
res = new ConditionalTransferResult<>(res.getResultValue(), thenStore, elseStore);
}

// if annotations differ, use the one that is more precise for both
// sides (and add it to the store if possible)
res = strengthenAnnotationOfEqualTo(res, leftN, rightN, leftV, rightV, false);
Expand All @@ -733,6 +742,14 @@ public TransferResult<V, S> visitNotEqual(NotEqualNode n, TransferInput<V, S> p)
V leftV = p.getValueOfSubNode(leftN);
V rightV = p.getValueOfSubNode(rightN);

if (res.containsTwoStores()
&& (NodeUtils.isConstantBoolean(leftN, true)
|| NodeUtils.isConstantBoolean(rightN, true))) {
S thenStore = res.getElseStore();
S elseStore = res.getThenStore();
res = new ConditionalTransferResult<>(res.getResultValue(), thenStore, elseStore);
}

// if annotations differ, use the one that is more precise for both
// sides (and add it to the store if possible)
res = strengthenAnnotationOfEqualTo(res, leftN, rightN, leftV, rightV, true);
Expand Down