Issue #1647: A store that closes a cycle of shared handles within one function is refused

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).

Goal

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.

Governing decision

Probe (run 2026-11-15 on develop 211b549b)

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")
}

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:

Done when

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.

To build

  1. Count first, refuse nothing. In 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).
  2. The refusal 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).
  3. Existing tests that build a cycle: list any test in 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.

The rule that repeats

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.

Files and limits

Ruled out

Comments

keywan-ghadami, 2026-11-15

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

Back to Open Issues