Skip to content

Invalidate dataflow cache for lambdas inside loops #1381

@coderabbitai

Description

@coderabbitai

Context

Follow-up from PR #1375 (comment: #1375 (comment))

In nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java around lines 1001-1002, there is a TODO noting that dataflow results for lambdas inside loops should be invalidated to avoid caching incomplete results from earlier loop iterations.

Problem

Without this invalidation, the analysis could cache dataflow facts from an incomplete loop iteration, leading to incorrect nullness conclusions on subsequent iterations. This is a correctness concern.

Proposed Solution

Implement invalidation by:

  1. Detecting when a lambda AST node is located within a loop construct (for/while/do/foreach)
  2. When performing dataflow analysis for that lambda, either:
    • (a) Mark the lambda's dataflow result as non-cacheable, OR
    • (b) Explicitly remove/clear its entry from the analysis cache immediately after analysis completes
  3. Add unit tests that exercise a lambda within a loop to ensure subsequent iterations do not reuse stale cached facts
  4. Add a brief comment referencing this issue for future reasoning

Related Code

The TODO is in the refineArgumentTypeWithDataflow method:

// TODO if we did the dataflow analysis for a lambda, always remove the analysis result from the
//  cache for safety

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions