Skip to content

Commit

Permalink
fixedInitialStore may be null
Browse files Browse the repository at this point in the history
  • Loading branch information
smillst authored Jul 19, 2023
1 parent 355bf05 commit c88e096
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import org.checkerframework.checker.interning.qual.InternedDistinct;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.PolyNull;
import org.checkerframework.dataflow.analysis.ConditionalTransferResult;
Expand Down Expand Up @@ -215,10 +214,14 @@ protected V getValueFromFactory(Tree tree, Node node) {
}

/** The fixed initial store. */
private @MonotonicNonNull S fixedInitialStore = null;
private @Nullable S fixedInitialStore = null;

/** Set a fixed initial Store. */
public void setFixedInitialStore(S s) {
/**
* Set a fixed initial Store.
*
* @param s initial store; possible null
*/
public void setFixedInitialStore(@Nullable S s) {
fixedInitialStore = s;
}

Expand Down Expand Up @@ -284,15 +287,19 @@ public S initialStore(UnderlyingAST underlyingAST, List<LocalVariableNode> param
}

} else if (underlyingAST.getKind() == UnderlyingAST.Kind.LAMBDA) {
// Create a copy and keep only the field values (nothing else applies).
store = analysis.createCopiedStore(fixedInitialStore);
// Allow that local variables are retained; they are effectively final,
// otherwise Java wouldn't allow access from within the lambda.
// TODO: what about the other information? Can code further down be simplified?
// store.localVariableValues.clear();
store.classValues.clear();
store.arrayValues.clear();
store.methodValues.clear();
if (fixedInitialStore != null) {
// Create a copy and keep only the field values (nothing else applies).
store = analysis.createCopiedStore(fixedInitialStore);
// Allow that local variables are retained; they are effectively final,
// otherwise Java wouldn't allow access from within the lambda.
// TODO: what about the other information? Can code further down be simplified?
// store.localVariableValues.clear();
store.classValues.clear();
store.arrayValues.clear();
store.methodValues.clear();
} else {
store = analysis.createEmptyStore(sequentialSemantics);
}

for (LocalVariableNode p : parameters) {
AnnotatedTypeMirror anno = atypeFactory.getAnnotatedType(p.getElement());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,8 @@ protected void performFlowAnalysis(ClassTree classTree) {
initializationStaticStore = capturedStore;
initializationStore = capturedStore;

Queue<IPair<LambdaExpressionTree, Store>> lambdaQueue = new ArrayDeque<>();
// The store is null if the lambda is unreachable.
Queue<IPair<LambdaExpressionTree, @Nullable Store>> lambdaQueue = new ArrayDeque<>();

// Queue up classes (for top-level `while` loop) and methods (for within this `try`
// construct); analyze top-level blocks and variable initializers as they are
Expand Down Expand Up @@ -1455,7 +1456,7 @@ protected void performFlowAnalysis(ClassTree classTree) {
}

while (!lambdaQueue.isEmpty()) {
IPair<LambdaExpressionTree, Store> lambdaPair = lambdaQueue.poll();
IPair<LambdaExpressionTree, @Nullable Store> lambdaPair = lambdaQueue.poll();
MethodTree mt =
(MethodTree)
TreePathUtil.enclosingOfKind(getPath(lambdaPair.first), Tree.Kind.METHOD);
Expand Down Expand Up @@ -1526,7 +1527,7 @@ protected void analyze(
boolean isInitializationCode,
boolean updateInitializationStore,
boolean isStatic,
Store capturedStore) {
@Nullable Store capturedStore) {
ControlFlowGraph cfg = CFCFGBuilder.build(root, ast, checker, this, processingEnv);

if (isInitializationCode) {
Expand Down
9 changes: 9 additions & 0 deletions framework/tests/all-systems/Issue6104.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Issue6104 {
public void m() {
try {
} catch (Exception e) {
// Because the try block is empty, this lambda is unreachable.
Runnable r = () -> {};
}
}
}

0 comments on commit c88e096

Please sign in to comment.