Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,29 @@ private static boolean isCatchVariable(VariableDeclarationNode node) {
return variableElement != null && variableElement.getKind() == EXCEPTION_PARAMETER;
}

/**
* In JSpecify mode, this method invokes {@link
* GenericsChecks#registerVarLocalDeclaration(VariableTree)} to register {@code var}-declared
* locals. This is required since sometimes during dataflow analysis, we require the full generic
* type of such a variable at a use, which may require running generic method inference on the
* right-hand side of the declaration. javac does not provide an efficient way to retrieve a local
* variable declaration given its {@code Symbol}. So, as an efficient alternative, we cache all
* such declarations when they are first visited by dataflow analysis, in case inference needs to
* be performed later at a use.
*
* <p>Note that the above assumes dataflow analysis will always see the declaration of a local
* before any use. This depends on the worklist ordering used by the dataflow solver, which is a
* fragile dependence. But, any alternative would require running a separate visitor over the
* whole method body (or a traversal of the full CFG) to find the declarations, which could be
* expensive. So, we go with this approach, and our tests should catch if there is some unexpected
* change in worklist orderings.
*/
@Override
public TransferResult<Nullness, NullnessStore> visitVariableDeclaration(
VariableDeclarationNode node, TransferInput<Nullness, NullnessStore> input) {
if (config.isJSpecifyMode()) {
genericsChecks.registerVarLocalDeclaration(node.getTree());
}
ReadableUpdates updates = new ReadableUpdates();
if (isCatchVariable(node)) {
updates.set(node, NONNULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.checkerframework.nullaway.dataflow.analysis.ForwardTransferFunction;
import org.checkerframework.nullaway.dataflow.analysis.Store;
import org.checkerframework.nullaway.dataflow.cfg.ControlFlowGraph;
import org.checkerframework.nullaway.dataflow.cfg.node.Node;
import org.jspecify.annotations.Nullable;

/**
* A ForwardAnalysis implementation that overrides {@link #performAnalysis(ControlFlowGraph)} to
Expand Down Expand Up @@ -32,4 +34,27 @@ public void performAnalysis(ControlFlowGraph cfg) {
analysisPerformed = true;
}
}

/**
* Override as a workaround for <a
* href="https://github.com/typetools/checker-framework/issues/7726">Checker Framework issue
* 7726</a>. This version returns the current value for {@code n} even if we have a running
* analysis and {@code n} is not a (transitive) operand of the current node of the analysis.
* Otherwise, its implementation is identical to that of {@code
* org.checkerframework.nullaway.dataflow.analysis.AbstractAnalysis#getValue(Node).}
*
* <p>We should remove this method if / when CF issue 7726 is fixed in a suitable manner.
*/
@Override
public @Nullable V getValue(Node n) {
if (isRunning) {
if (currentNode == null
|| currentNode == n
|| (currentTree != null && currentTree == n.getTree())) {
return null;
}
assert !n.isLValue() : "Did not expect an lvalue, but got " + n;
}
return nodeValues.get(n);
}
}
Loading
Loading