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

Initialize implicit this. #6908

Merged
merged 4 commits into from
Dec 6, 2024
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
12 changes: 12 additions & 0 deletions checker/tests/nonempty/SizeInIsEmpty.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import java.util.AbstractSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.checkerframework.checker.nonempty.qual.EnsuresNonEmptyIf;
import org.checkerframework.checker.nonempty.qual.PolyNonEmpty;
import org.checkerframework.dataflow.qual.Pure;
Expand Down Expand Up @@ -31,9 +33,19 @@ public boolean isEmpty() {

@EnsuresNonEmptyIf(result = false, expression = "this")
public boolean isEmpty2() {
// :: error: contracts.conditional.postcondition
return size() == 0 ? true : false;
}

Set<Object> set = new HashSet<>();

@EnsuresNonEmptyIf(result = false, expression = "this.set")
public boolean isEmpty2B() {
// TODO: this is a false positive
// :: error: contracts.conditional.postcondition
return set.size() == 0 ? true : false;
}

@EnsuresNonEmptyIf(result = false, expression = "this")
public boolean isEmpty3() {
return size() == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,9 @@ public void clearValue(JavaExpression expr) {
} else if (expr instanceof ClassName) {
ClassName c = (ClassName) expr;
classValues.remove(c);
} else { // thisValue ...
} else if (expr instanceof ThisReference) {
thisValue = null;
} else {
// No other types of expressions are stored.
}
}
Expand Down Expand Up @@ -1190,9 +1192,10 @@ private S upperBound(S other, boolean shouldWiden) {
{
V otherVal = other.thisValue;
V myVal = thisValue;
V mergedVal = myVal == null ? null : upperBoundOfValues(otherVal, myVal, shouldWiden);
if (mergedVal != null) {
newStore.thisValue = mergedVal;
if (myVal == null || otherVal == null) {
newStore.thisValue = null;
} else {
newStore.thisValue = upperBoundOfValues(otherVal, myVal, shouldWiden);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ protected V getValueFromFactory(Tree tree, Node node) {
return analysis.createAbstractValue(at);
}

/** The fixed initial store. */
/**
* The fixed initial store which includes values from field initialization and, if analyzing a
* lambda, final local variables.
*/
private @Nullable S fixedInitialStore = null;

/**
Expand Down Expand Up @@ -268,6 +271,12 @@ public S initialStore(UnderlyingAST underlyingAST, List<LocalVariableNode> param
store.initializeMethodParameter(p, analysis.createAbstractValue(anno));
}

AnnotatedTypeMirror implicitThis = atypeFactory.getSelfType(underlyingAST.getCode());
if (implicitThis != null) {
V thisSelf = analysis.createAbstractValue(implicitThis);
store.thisValue = thisSelf.mostSpecific(store.thisValue, null);
}

// add properties known through precondition
CFGMethod method = (CFGMethod) underlyingAST;
MethodTree methodDeclTree = method.getMethod();
Expand Down