Skip to content

Commit 2e578d6

Browse files
authored
Invoke generic method inference in more places (#1286)
Fixes #1262, fixes #1263 We now invoke inference for generic method calls in more places, including when it gets queried from dataflow analysis of a full method. Where possible, we provide a `TreePath` to the relevant call. With this `TreePath`, we can reconstruct the assignment context of the call and use that information when performing inference (see the new `getInvocationAndContextForInference` method). In some cases, we pass `null` as the `TreePath` since one is not readily available. I _believe_ that in most / all of these cases, inference should have already been performed for the call, so we should be able to use the cached result. If / when we find cases where that didn't happen, and it is impacting the inference result negatively, we can fix in follow ups. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Path-aware nullness inference for generic method calls that selects the correct invocation/assignment context, including nested calls and assignment sites. * **Bug Fixes** * More accurate generic return nullness determination to reduce incorrect diagnostics in complex call and assignment patterns. * **Performance** * Per-invocation caching to speed repeated inference and reuse results across related calls. * **Tests** * Re-enabled and expanded generics inference tests, adding dataflow-triggered inference scenarios and related assertions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent c187bf5 commit 2e578d6

File tree

4 files changed

+306
-61
lines changed

4 files changed

+306
-61
lines changed

nullaway/src/main/java/com/uber/nullaway/NullAway.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2711,9 +2711,12 @@ private boolean mayBeNullMethodCall(
27112711
if (Nullness.hasNullableAnnotation(exprSymbol, config)) {
27122712
return true;
27132713
}
2714+
// NOTE: we cannot rely on state.getPath() here to get a TreePath to the invocation, since
2715+
// sometimes the invocation is a sub-node of the leaf of the path. So, here if inference runs,
2716+
// it will do so without an assignment context. If this becomes a problem, we can revisit
27142717
if (config.isJSpecifyMode()
27152718
&& genericsChecks
2716-
.getGenericReturnNullnessAtInvocation(exprSymbol, invocationTree, state)
2719+
.getGenericReturnNullnessAtInvocation(exprSymbol, invocationTree, null, state)
27172720
.equals(Nullness.NULLABLE)) {
27182721
return true;
27192722
}

nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPathNullnessPropagation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ private boolean genericReturnIsNullable(MethodInvocationNode node) {
11551155
if (tree != null) {
11561156
Nullness nullness =
11571157
genericsChecks.getGenericReturnNullnessAtInvocation(
1158-
ASTHelpers.getSymbol(tree), tree, state);
1158+
ASTHelpers.getSymbol(tree), tree, node.getTreePath(), state);
11591159
return nullness.equals(NULLABLE);
11601160
}
11611161
}

0 commit comments

Comments
 (0)