Skip to content

Commit fbb3513

Browse files
committed
feat(rust): port ownership analysis with exact (line,code) parity (#214 checkpoint 2b)
The ownership half of #214's analysis heart, on top of the generic solver. Exact port of ownlang/analysis.py, gated by the frozen diagnostics oracle. own-analysis::ownership: - {OWNED,MOVED,RELEASED,ESCAPED} u8-bit lattice State (per-RID bitset, loans, handle->RID aliasing, move/acquire provenance); join with the Python merge invariants (block-scoped loans equal; 1:1 handle->RID). RID = SymId (u32), the deterministic replacement for Python id(sym). - full step() transfer over every Instr; two-phase run (silent fixpoint via the generic solver, then emitting pass + end-of-function leak checks). Parity (tests/parity.rs, replaying tests/fixtures/diag_parity.json): - 66 covered cases asserted, all pass EXACTLY on the full ordered (line,code) list — no field weakened; - 3 deferred to checkpoint 3 by name (2x OWN014 escape, 1x OWN019 buffer policy). The partition is sound: a case is covered iff none of its codes come from an unported pass (check_lifetimes / validate_policies), so d1+d2 equals Python's full output for every covered case. Mandated tests: - ownership lattice laws EXHAUSTIVE over the finite per-RID state (idempotent/ commutative/associative join, x<=join, bottom identity, the read-OWNED vs join-empty default split); - metamorphic: rename-invariance, repeated-analysis determinism, unreachable code never removes a finding, serde round-trip. Preserved Python behaviors (matched, not fixed): OWN020-for-parse-errors; the OWN003 "twice"/"maybe" and OWN002 "consumed"/"released" splits are message-only (same code+line), merged since only (line,code) is compared at this step. own-cfg: expose BufferMode/BufferInfo::stack_backed (the ownership analysis needs it for the OWN015/OWN016 escaping-stack-buffer checks). Integration gate unchanged: final semantic-port merge stays gated on the separate post-batch OSS remeasure. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AZjB2ggU9qm8vmuTQRtZXR
1 parent f0ca1cf commit fbb3513

8 files changed

Lines changed: 1058 additions & 2 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# P-022 step 4 (#214) — checkpoint 2: generic solver + ownership parity
2+
3+
> Status: **checkpoint-2 deliverable** (generic worklist solver + the ownership
4+
> analysis, gated by the frozen diagnostics oracle). Second of #214's three
5+
> review checkpoints. Builds on
6+
> [checkpoint 1](p022-diag-parity-checkpoint1.md) (the frozen
7+
> `tests/fixtures/diag_parity.json` + comparison design). Lifetime/effect/DI and
8+
> evidence/SARIF are checkpoint 3 / later steps.
9+
10+
## What landed
11+
12+
- **`own-analysis::solver`** — a generic monotone-lattice forward worklist
13+
solver (the `rustc_mir_dataflow` shape the Python `_Analyzer.fixpoint` ports
14+
to): `Lattice` / `ControlFlowGraph` / `Analysis` traits, RPO scheduling, a
15+
schedule-independent fixpoint, and a convergence guard that fails loudly on a
16+
non-monotone join. Dependency-free.
17+
- **`own-analysis::ownership`** — an exact port of `ownlang/analysis.py`: the
18+
`{OWNED,MOVED,RELEASED,ESCAPED}` bit-lattice `State` (per-RID `u8` bitset,
19+
loans, handle→RID aliasing, move/acquire provenance), `join` with the Python
20+
invariants (block-scoped loans equal at a merge; 1:1 handle→RID), the full
21+
`step()` transfer over every `Instr`, the two-phase `run` (silent fixpoint,
22+
then emitting pass + end-of-function leak checks). RID = `SymId` (`u32`), the
23+
deterministic replacement for Python's `id(sym)`.
24+
25+
## Parity result
26+
27+
`tests/parity.rs` replays the frozen fixture through the ported `check` surface
28+
(own-syntax parse → own-cfg resolver `d1` → ownership `d2`, stable-sorted by
29+
`(line, code)`; a parse error is the preserved OWN020 quirk):
30+
31+
- **66 covered cases asserted, all pass exactly** (full ordered `(line, code)`
32+
list — no field weakened);
33+
- **3 deferred** to checkpoint 3, listed by name: two OWN014 escape cases
34+
(`corpus/wpf/systemevents-region-escape`, `corpus/wpf/viewmodel-escapes-to-app`)
35+
and one OWN019 buffer-policy case (`curated_buffer_policy`).
36+
37+
### The partition is sound, not a weakening
38+
39+
A case is **covered** iff none of its Python codes belong to a not-yet-ported
40+
pass. Ported at checkpoint 2: the ownership analysis (`analyze`, codes
41+
OWN001–OWN013, OWN015–OWN017, OWN025, OWN034, OWN041) and — already parity-frozen
42+
by #203 — the `own-cfg` resolver (`d1`). **Not** ported: `check_lifetimes`
43+
(OWN014/OWN036) and `validate_policies` (OWN019/OWN021/OWN023/OWN024), plus the
44+
resolver-name codes those passes can *also* emit (OWN030/OWN031) and the
45+
OwnIR-fact sidecar families (DI\*/EFF\*/OBL\*, own-bridge step 6). For a covered
46+
case those unported passes are provably silent, so Python's full output equals
47+
`d1 + d2` — the exact quantity Rust computes. Covered cases are therefore
48+
asserted on the **complete** ordered `(line, code)` list; deferred cases are
49+
skipped whole (never half-asserted).
50+
51+
## Mandated correctness battery
52+
53+
- **Lattice laws** (`ownership.rs` unit tests): idempotent / commutative /
54+
associative `join` and `x <= join(x, y)`, **exhaustive** over the finite
55+
per-RID state space (all 16 masks × triples); plus `bottom` identity, the
56+
growth-reporting contract, and the two different default rules (absent RID
57+
**reads** as OWNED, **joins** as ∅ — the Python `.get(rid, {OWNED})` vs
58+
`.get(k, set())` split, pinned by a test).
59+
- **Solver laws** (`solver.rs`, checkpoint 2a): CFG shapes (straight/diamond/
60+
self-loop/nested-loop/unreachable/multi-pred/abnormal-exit), exhaustive
61+
lattice laws on the test lattice, worklist-order independence across five
62+
schedules, and the convergence guard firing on a diverging join.
63+
- **Metamorphic** (`metamorphic.rs`): renaming a local preserves `(line, code)`;
64+
repeated analysis is identical (determinism); adding unreachable code never
65+
removes a finding; diagnostics survive a serde round-trip.
66+
- **Differential**: the frozen fixture *is* the differential oracle — Python
67+
authors the goldens (`python tests/test_diag_fixtures.py --write`), Rust
68+
replays them with zero Python at steady state. The 62-file corpus + 7 curated
69+
cases exercise acquire/release/use/move/branch/loop/return combinations.
70+
71+
## Deliberately preserved Python behaviors (matched, not "fixed")
72+
73+
- **OWN020 for syntax errors** (from checkpoint 1) — still matched.
74+
- **Two OWN003 shapes merged**: Python emits a distinct *message* for "released
75+
twice" vs "may already be released on some path", but both are code **OWN003**
76+
at the same line; the Rust port emits OWN003 for either (only `(line, code)`
77+
is compared at this step). Same for the "consumed" vs "released" OWN002 split.
78+
This is a message-text distinction, not a verdict distinction — it returns
79+
when message-text parity is added (step 5). No verdict is changed.
80+
81+
## Integration gate (unchanged, restated)
82+
83+
Per owner direction, checkpoint 1's provisional-approval gate stands: **final
84+
semantic-port merge is gated on the separate post-batch OSS remeasure** run by
85+
another agent; this checkpoint permits implementation to proceed but does not
86+
substitute for the broad oracle audit. If that sweep surfaces an unexplained
87+
disappearance, it is fixed **Python-first**, the golden fixture is regenerated
88+
explicitly, and Rust is re-brought to parity — never patched in Rust alone.
89+
90+
## Not done here (checkpoint 3)
91+
92+
`check_lifetimes` (OWN014/OWN036 escape/region) and `validate_policies`
93+
(OWN019/OWN021/OWN023/OWN024 buffer policy) — which close the 3 deferred cases —
94+
then evidence-slice and SARIF parity (step 5), and the effects/DI fact surface
95+
(own-bridge, step 6).

‎rust/Cargo.lock‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎rust/crates/own-analysis/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ version = "0.1.0"
1414
# (ownership next) add own-cfg / own-diagnostics. The DAG edge test
1515
# (own-diagnostics/tests/dag.rs) already allows that (reviewed) set.
1616
[dependencies]
17+
own-cfg = { path = "../own-cfg" }
18+
own-syntax = { path = "../own-syntax" }
19+
own-diagnostics = { path = "../own-diagnostics" }
1720

1821
[dev-dependencies]
22+
serde_json = { workspace = true }
1923

2024
[lints]
2125
workspace = true

‎rust/crates/own-analysis/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//! (`tests/parity.rs`, replaying `tests/fixtures/diag_parity.json`) pins the
1313
//! Rust output to it on `(path, line, code)`.
1414
15+
pub mod ownership;
1516
pub mod solver;
1617

18+
pub use ownership::analyze;
1719
pub use solver::{solve, solve_with, Analysis, ControlFlowGraph, Lattice, Schedule, Solution};

0 commit comments

Comments
 (0)