Skip to content

Implement null-safety checker as DataFlow analysis#74

Draft
Copilot wants to merge 4 commits intosimn-developmentfrom
copilot/investigate-null-safety-checker
Draft

Implement null-safety checker as DataFlow analysis#74
Copilot wants to merge 4 commits intosimn-developmentfrom
copilot/investigate-null-safety-checker

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 2, 2026

Investigates using the existing SSA-based DataFlow module for null-safety checking. The framework is adequate with one extension: a narrow hook on DataFlowApi for refining phi incoming values based on edge conditions.

DataFlow framework extension

  • Added narrow : analyzer_context -> opt_ctx -> cfg_edge -> texpr -> t -> t to DataFlowApi signature
  • Called during phi node evaluation, allows implementations to refine incoming values based on the CFG edge (e.g., which branch of a null check we came from)
  • Existing implementations (ConstPropagationImpl, CopyPropagationImpl) use identity narrowing

NullAnalysisImpl

  • Lattice: Top | NotNull | IsNull | Bottom — tracks whether variables are provably non-null, provably null, or potentially null
  • Transfer: Type-based — Null<T> return types → Bottom, non-nullable → NotNull, TConst TNullIsNull
  • Narrow: Resolves if (x == null) / if (x != null) through temp var lookups via get_var_value, narrowing phi inputs on each branch
  • Commit: Walks the dominator tree with a narrowing stack for intra-block narrowing (early return/throw/break guards), reports WNullSafety warnings for field access, method calls, and array access on potentially-null Null<T> values

Activated by -D analyzer-check-null, runs after const/copy propagation and before local DCE.

What works

var ns = getNullString(); // Null<String>
trace(ns.length);         // ⚠ warns: field access on potentially null value

if (ns == null) { ns = getString(); }
trace(ns.length);         // no warning — phi narrowing proves non-null

var ns2 = getNullString();
if (ns2 == null) return;
trace(ns2.length);        // no warning — intra-block narrowing via dominator tree

Config / plumbing

  • FlagNullAnalysis added to cfg_edge_Flag
  • null_safety field on AnalyzerConfig.t, reads analyzer_check_null define
  • Updated TestNullChecker.hx: removed legacy @:analyzer(testIsNull) annotations, replaced with test methods exercising null-flow patterns

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 3 commits March 2, 2026 17:19
- Add FlagNullAnalysis to cfg_edge_Flag
- Extend DataFlowApi with 'narrow' for conditional narrowing at phi nodes
- Implement NullAnalysisImpl with Top/NotNull/IsNull/Bottom lattice
- Transfer function uses type-based nullability evaluation
- Narrow function handles if(x==null)/if(x!=null) at phi merge points
- Commit walks dominator tree with narrowing context for intra-block narrowing
- Flags unsafe field access, method calls, array access on Null<T> values
- Wire into analyzer pipeline conditional on -D analyzer-check-null
- All existing optimization and unit tests pass

Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Remove legacy @:analyzer(testIsNull/testIsNotNull) annotations.
Replace with meaningful test methods that exercise null-check patterns:
assignment, reassignment, null-check-then, null-check-else, nested checks,
early return guards, break/continue in loops, throw guards.

Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Copilot AI changed the title [WIP] Investigate implementation of null-safety checker Implement null-safety checker as DataFlow analysis Mar 2, 2026
Copilot finished work on behalf of Simn March 2, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants