Open and answer this issue on GitHub.
compiler, ready · opened 2026-11-15 · updated 2026-11-15 · 1 comment
Part of #1626. Step 2 of 5 of ADR-shared-handle-cycles-1. It rests on #1646 (the self-reaching types).
A program that builds a cycle of SharedMut or Locked handles today compiles and leaks. Within one function body, such a store is refused at compile time with NK1352.
SharedMut[T], or into a Locked[T] field, is refused when the stored handle’s target can reach the value written into, among self-reaching types, and no back edge on that cycle is proved. The check is sound: it may refuse a correct program, but it never lets a cycle through. Where reachability is unknown, the store is refused.NK1352 (decided, not built). This is text only so far; this issue builds it and moves the NK1352 entry from Decided, not built yet to the built list.struct Node { name: String, next: SharedMut[Node]? }
fn main() throws {
let a = SharedMut(Node { name: "a", next: null })
let b = SharedMut(Node { name: "b", next: a })
a.update(fn(mut v) { v.next = b })
print("built")
}
nikaia check prints Checked; the program runs and leaks both nodes.error[NK1352] at v.next = b. The message reads close to: “This store closes a cycle of handles: b reaches a through next, and a.next now holds b, so neither is ever freed.” The help points to an index into a list.What it needs to fail: b is built from a, so b reaches a, and the store writes b into a. Each of these variants still compiles and must keep compiling:
v.next = SharedMut(Node { name: "c", next: null }), a fresh handle that reaches nothing;v.next = null;b built with next: null.The probe is refused with NK1352, the three variants compile, the tests below pass, and measurement 5 of #1626 §9 is posted on this issue.
check_cycles.nika (from #1646), for each store into a self-reaching type, compute what the stored value can reach within the function. Use a may-reach set per local, filled by construction (SharedMut(Node { next: a }) reaches a and what a reaches), by assignment and by the stores already seen. Join at branches; iterate loops to a fixed point. A call result, or a parameter, of a self-reaching type is unknown, because summaries are step 3. Have check --cycles print each store it would refuse, with its reason: closes, or unknown: call / unknown: parameter.
Then run it over crates/nikaia-std/src/tools/, packages/ and examples/, and post measurement 5 on this issue: the count per reason, and how many are correct programs.
Stop point: if correct stores in our own code are refused for unknown, the refusal in item 2 is built behind those sites. Item 2 waits for step 3 (function summaries) and the issue says so. No site is rewritten around the rule (CLAUDE.md, Fix, do not work around).NK1352, error level, with line, cycle path and the index help. Move the code’s entry in docs/specification/30-nikaia-tooling.md to built. Tests in tests/compiler/tests/:
a_store_that_closes_a_cycle_of_shared_handles_is_refused (the probe, .stderr);a_store_of_a_fresh_handle_is_not_a_cycle (variant 1, .stdout);a_cycle_closed_through_a_locked_field_is_refused (.stderr);a_cycle_closed_in_one_branch_is_refused (the store sits in if, .stderr);a_cycle_closed_by_a_loop_is_refused (a ring built in a for, .stderr);a_store_into_a_type_that_is_not_self_reaching_is_not_checked (.stdout).tests/compiler/tests/ that the rule now refuses. Each one is either a test of a leak, and its expected output changes to the refusal, or a correct shape for step 4, which is filed there by name. A test is never deleted or skipped.Doors of SharedMut are also checked in check_doors.nika (NK22xx, a lock held) and locks.nika. The store sites this rule needs are the same places those passes visit, so reuse their walk of update and lock bodies; no second walk.
crates/nikaia-std/src/tools/check_cycles.nika and its lowered .rs; docs/specification/30-nikaia-tooling.md (the NK1352 line); the new tests.NK1352 is reserved for this, and no other new code is taken.check. Before it, measure user time of nikaia check crates/nikaia-std/src/tools/check_program.nika on the day’s binary. Afterwards it may grow by at most 5 %, against #1646’s figure. Reach sets are per function, over self-reaching locals only.For To build item 3: tests/compiler/tests/a_shared_mut_handle_links_two_nodes.nika (from #1212, pushed in e76bcd7d) builds exactly the probe’s cycle on purpose, and its comment calls the freeing open. Under this rule, its cycle part becomes the refusal: change the expected output to the NK1352 .stderr, and keep the duplication checks it also holds as a separate test without a cycle.
Generated by Claude Code