Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
14ad323
Do not treat an operand's conditional stores as those of an enclosing…
mernst Oct 23, 2026
1f1d2f7
Merge branch 'master' into conditional-stores-of-operands
mernst Oct 23, 2026
60653f5
Merge conditional stores by default; refine them for boolean operators
mernst Oct 23, 2026
862e58b
createTransferResult: keep two (equal) stores when the input has two
mernst Oct 23, 2026
45810a0
Merge branch 'master' into conditional-stores-of-operands
mernst Oct 24, 2026
704c420
JointJavacJavaParserVisitor: assert that an Optional is present befor…
mernst Oct 27, 2026
229f6a7
Address review findings: tree-based operand check, single lambda-resu…
mernst Oct 27, 2026
3919420
Merge branch 'master' into ajava-optional-asserts
mernst Oct 27, 2026
49abdc9
Merge branch 'ajava-optional-asserts' into conditional-stores-of-oper…
mernst Oct 27, 2026
380fe28
Suppress interning warning for reference equality of trees
mernst Oct 27, 2026
a9bb8e8
Merge branch 'master' of /home/mernst/research/types/checker-framewor…
mernst Oct 28, 2026
4e24e89
Merge branch 'master' into conditional-stores-of-operands
mernst Oct 29, 2026
7bba3e0
Merge remote-tracking branch 'upstream/master' into conditional-store…
mernst Oct 30, 2026
bc75694
Improve comments
mernst Oct 30, 2026
86e3b0d
Improve documentation of createTransferResult and related methods; ad…
mernst Oct 30, 2026
1264a0e
Precision
mernst Oct 30, 2026
bc95d4c
Merge branch 'master' into conditional-stores-of-operands
mernst Oct 31, 2026
a90cc0a
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 1, 2026
b833d67
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 7, 2026
7e5be39
Rename the parameter of visitConditionalNot to `in`
mernst Nov 8, 2026
d967709
Clarify the comment about skipping the least upper bound of equal stores
mernst Nov 8, 2026
c1d64f5
createTransferResult: keep the result's stores distinct when the inpu…
mernst Nov 8, 2026
e466218
Do not refine == or != with two boxed operands, which Java compares b…
mernst Nov 8, 2026
db2f512
Suppress the interning warning for the reference-equality test of the…
mernst Nov 10, 2026
3588b43
Always merge the stores; boolean operators compute their result direc…
mernst Nov 10, 2026
7d73e3e
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 11, 2026
ea2bd87
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 13, 2026
8f5017c
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 16, 2026
d7aff19
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 16, 2026
428b10e
Merge github.com:typetools/checker-framework into conditional-stores-…
mernst Nov 18, 2026
a79e282
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 19, 2026
9eb47a7
Add NodeUtils.constantBooleanValue; narrow an interning suppression t…
mernst Nov 21, 2026
d24c4e0
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 21, 2026
ea51dae
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 23, 2026
5f89b80
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 23, 2026
bec1bae
Clarify the Javadoc of CFAbstractTransfer.visitValueLiteral
smillst Nov 23, 2026
e0941b5
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 23, 2026
637663f
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 24, 2026
31eeca0
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 26, 2026
44182a4
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 26, 2026
2bbe488
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 27, 2026
5d9fd32
Move the CFAbstractTransfer changelog entries to the unreleased section
mernst Nov 29, 2026
da17a0e
Merge branch 'master' into conditional-stores-of-operands
mernst Nov 30, 2026
3375b3b
Merge branch 'master' into conditional-stores-of-operands
mernst Dec 1, 2026
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
285 changes: 285 additions & 0 deletions checker/tests/nullness/ConditionalStoresOfOperands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
// Test that an operator does not treat the facts that hold when one operand is true or false as
// facts that hold when the operator's result is true or false.

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

public class ConditionalStoresOfOperands {

@EnsuresNonNullIf(expression = "#1", result = true)
static boolean isNN(@Nullable Object o) {
return o != null;
}

@EnsuresNonNullIf(expression = "#1", result = false)
static boolean isNull(@Nullable Object o) {
return o == null;
}

void orLiteral(@Nullable Object o) {
if (isNN(o) | true) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void orRight(@Nullable Object o, boolean b) {
if (b | isNN(o)) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void orRightElse(@Nullable Object o, boolean b) {
if (b | isNull(o)) {
} else {
o.hashCode();
}
}

void andRight(@Nullable Object o, boolean b) {
if (b & isNN(o)) {
o.hashCode();
} else {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void xorLiteral(@Nullable Object o) {
if (isNN(o) ^ true) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void equalToVariable(@Nullable Object o, boolean b) {
if (b == isNN(o)) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void notEqualToVariable(@Nullable Object o, boolean b) {
if (b != isNN(o)) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void equalToComparison(@Nullable Object o) {
if (isNN(o) == (2 < 1)) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void equalToString(@Nullable Object o) {
if ("x" == (isNN(o) + "")) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void equalToNegatedTrue(@Nullable Object o) {
if (isNN(o) == !true) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void equalToTrue(@Nullable Object o) {
if (isNN(o) == true) {
o.hashCode();
}
}

void equalToFalse(@Nullable Object o) {
if (isNN(o) == false) {
return;
}
o.hashCode();
}

void equalToNegatedFalse(@Nullable Object o) {
if (isNN(o) == !false) {
o.hashCode();
}
}

void notEqualToNegatedTrue(@Nullable Object o) {
if (isNN(o) != !true) {
o.hashCode();
}
}

void trueEqualTo(@Nullable Object o) {
if (true == isNN(o)) {
o.hashCode();
}
}

void andIsNull(@Nullable Object o, boolean b) {
if (b & isNull(o)) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void trueAnd(@Nullable Object o) {
if (true & isNN(o)) {
o.hashCode();
}
}

void andTrue(@Nullable Object o) {
if (isNN(o) & true) {
o.hashCode();
}
}

void orFalse(@Nullable Object o) {
if (isNN(o) | false) {
o.hashCode();
}
}

void xorFalse(@Nullable Object o) {
if (isNN(o) ^ false) {
o.hashCode();
}
}

void xorTrueElse(@Nullable Object o) {
if (isNN(o) ^ true) {
} else {
o.hashCode();
}
}

// The then-branch is unreachable, so any store is sound there.
void andFalseUnreachable(@Nullable Object o) {
if (isNN(o) & false) {
o.hashCode();
}
}

void falseAndUnreachable(@Nullable Object o) {
if (false & isNN(o)) {
o.hashCode();
}
}

void equalToCastTrue(@Nullable Object o) {
if (isNN(o) == (boolean) true) {
o.hashCode();
}
}

void falseNotEqualTo(@Nullable Object o) {
if (false != isNN(o)) {
o.hashCode();
}
}

void notEqualToFalse(@Nullable Object o) {
if (isNN(o) != false) {
o.hashCode();
}
}

void instanceOfLambda(@Nullable Object o) {
if (isNN(o) | !(((Runnable) () -> {}) instanceof Runnable)) {
} else {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void instanceOfArrayCreation(@Nullable Object o) {
if (isNN(o) | !(new Object[] {} instanceof Object[])) {
} else {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void instanceOfStringConcatenation(@Nullable Object o, @Nullable Object p) {
if (isNN(o) | !(("" + isNN(p)) instanceof String)) {
} else {
// :: error: [dereference.of.nullable]
p.hashCode();
}
}

void instanceOfInConditionalAnd(@Nullable Object o) {
// :: error: [dereference.of.nullable]
if (("" + isNN(o)) instanceof String && o.hashCode() == 0) {}
}

void conditionalAnd(@Nullable Object o, boolean b) {
if (b && isNN(o)) {
o.hashCode();
}
}

void conditionalOrElse(@Nullable Object o, boolean b) {
if (b || isNull(o)) {
} else {
o.hashCode();
}
}

void castToBoolean(@Nullable Object o) {
if ((boolean) isNN(o)) {
o.hashCode();
}
}

void ternary(@Nullable Object o, boolean b) {
if (b ? isNN(o) : isNN(o)) {
o.hashCode();
}
}

@EnsuresNonNullIf(expression = "#1", result = true)
static Boolean isNNBoxed(@Nullable Object o) {
return o != null;
}

// Java compares two boxed operands of == and != by reference, so their values might be equal
// even when the comparison is false. isNNBoxed might return a Boolean that is not Boolean.TRUE.

void equalToBothBoxed(@Nullable Object o) {
if (isNNBoxed(o) == (Boolean) false) {
// :: error: [dereference.of.nullable]
o.hashCode();
} else {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

void notEqualBothBoxed(@Nullable Object o) {
if (isNNBoxed(o) != (Boolean) false) {
// :: error: [dereference.of.nullable]
o.hashCode();
}
}

@EnsuresNonNullIf(expression = "#1", result = true)
static boolean returnsIsNN(@Nullable Object o) {
return isNN(o);
}

@EnsuresNonNullIf(expression = "#1", result = true)
static boolean returnsEqualToTrue(@Nullable Object o) {
return isNN(o) == true;
}

@EnsuresNonNullIf(expression = "#1", result = true)
static boolean returnsOrTrue(@Nullable Object o) {
// :: error: [contracts.conditional.postcondition]
return isNN(o) | true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.cfg.node.BooleanLiteralNode;
import org.checkerframework.dataflow.cfg.node.ConditionalNotNode;
import org.checkerframework.dataflow.cfg.node.ConditionalOrNode;
Expand Down Expand Up @@ -110,12 +111,29 @@ public static boolean isMethodInvocation(
* @return true if the node is equivalent to a literal with value {@code value}
*/
public static boolean isConstantBoolean(Node n, boolean value) {
Boolean constant = constantBooleanValue(n);
return constant != null && constant == value;
}

/**
* Returns the value of the given node if it statically evaluates to a boolean value and has no
* side effects, as {@code !(boolean) true} does. Otherwise, returns null.
*
* @param n a node
* @return the boolean value that the node is equivalent to, or null if the node is not equivalent
* to a boolean literal
*/
public static @Nullable Boolean constantBooleanValue(Node n) {
boolean negated = false;
n = removeCasts(n);
while (n instanceof ConditionalNotNode cnn) {
negated = !negated;
n = removeCasts(cnn.getOperand());
}
if (n instanceof BooleanLiteralNode bln) {
return bln.getValue() == value;
} else if (n instanceof ConditionalNotNode cnn) {
return isConstantBoolean(cnn.getOperand(), !value);
return bln.getValue() != negated;
} else {
return false;
return null;
}
}

Expand Down
21 changes: 21 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ constant to `byte`, `short`, `char`, `Byte`, `Short`, or `Character`, as
assignment conversion does. New method `TreeUtils.isCompileTimeConstant()`
returns true if a tree is a constant expression.

`CFAbstractTransfer.createTransferResult()` now merges the input's then-store
and else-store: the result's then-store and else-store are both their least
upper bound. Use it when the input's then-store and else-store describe an
earlier node, such as an operand. New method
`CFAbstractTransfer.createTransferResultKeepingStores()` keeps the input's
then-store and else-store; use it when they describe the current node, as for a
cast.

These methods of `CFAbstractTransfer` no longer call `visitNode()`:
`visitConditionalAnd()`, `visitConditionalNot()`, `visitConditionalOr()`,
`visitMarker()`, `visitReturn()`, `visitTernaryExpression()`,
`visitTypeCast()`; for boolean operands, `visitBitwiseAnd()`,
`visitBitwiseOr()`, `visitBitwiseXor()`, `visitEqualTo()`, and
`visitNotEqual()`; and, for a boolean literal that is the right operand of a
boolean operator, `visitValueLiteral()`. A subclass that overrides
`visitNode()` may need to override them too.

`CFAbstractTransfer.visitLambdaResultExpression()` no longer calls the transfer
function of the lambda's result node; it uses the value that the result node
already computed.

`BaseTypeVisitor.OverrideChecker` is now a top-level class,
`org.checkerframework.common.basetype.OverrideChecker`. Its constructor takes a
new first argument, the `BaseTypeVisitor` that uses it. A subclass that is an
Expand Down
Loading