Open and answer this issue on GitHub.
compiler, ready · opened 2026-11-15 · updated 2026-11-15 · 0 comments
Part of #1626. Step 1 of 5 of ADR-shared-handle-cycles-1. Steps 2-5 rest on it.
A value whose type reaches no SharedMut[T] and no Locked[T] can never be part of a cycle of handles. Neither can an immutable value. The checker computes the other types, the self-reaching ones, so that the cycle rule (step 2) only looks at them and every other type pays nothing.
SharedMut or a Locked. Edges go through fields, elements, variants and captures.immutable value is never part of a cycle.NK1352 entry under Decided, not built yet. This issue builds the set; it changes no text.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 probe.nika prints Checked.nikaia run probe.nika prints built. The two nodes keep each other alive and leak.In this probe Node is self-reaching through next. A struct Point { x: Int, y: Int }, or a struct Tree { kids: Vec[Tree] } without a handle, is not.
nikaia check --cycles FILE lists each self-reaching type with the field path that closes its cycle. The tests below show it. Measurement 3 of #1626 §9 is posted on this issue.
crates/nikaia-std/src/tools/check_cycles.nika. One node per struct, enum and lambda capture type. An edge from a type to each type it names in a field, element, variant or capture, marked when it passes through SharedMut[_] or Locked[_]. Keep the strongly connected components that contain at least one marked edge. Generic types are taken at their instantiations, the ones check_program_instantiations_beside (check_program.nika) already walks. A type parameter with no instantiation in view counts as able to reach any handle (conservative, D4).
Tests in tests/compiler/tests/, each with a .stdout:
a_type_that_reaches_itself_through_a_shared_handle_is_self_reaching (the probe’s Node);a_type_with_no_shared_handle_is_not_self_reaching (Point, and Tree with Vec[Tree]);a_cycle_through_two_types_and_a_locked_field_is_self_reaching (A holds Locked[B], B holds A?);a_generic_wrapper_is_self_reaching_only_where_its_argument_is (Box2[T] at Node and at Int).nikaia check --cycles prints one line per self-reaching type with its closing path (Node: next -> SharedMut[Node]), in the style of check --bounds. The flag goes beside --bounds where the check subcommand reads its options. The tests of item 1 run through it.immutable is free: a value of a self-reaching type bound immutable (ADR-389) is marked, so step 2 skips stores into it. A store into an immutable value is already refused, so this is a fact step 2 reads, not a new refusal. Test: an_immutable_value_of_a_self_reaching_type_needs_no_cycle_check (the --cycles line names the type and says immutable values: free).--cycles over crates/nikaia-std/src/tools/, packages/ and examples/. Post on this issue how many types are self-reaching and how many of their values are already immutable. Code is not changed for it.check_doors.nika computes reach over the call graph (reach_index_of), not over types. No type-graph component pass was found by grepping for SharedMut and Locked in crates/nikaia-std/src/tools/*.nika. Reuse the component code if one turns up.
crates/nikaia-std/src/tools/check_cycles.nika and its lowered .rs (output of lower-std). The flag wiring goes where check reads --bounds.NK1352 is step 2. Every existing test output stays byte-identical.check. Before it, measure nikaia check crates/nikaia-std/src/tools/check_program.nika user time on the day’s binary. Afterwards it may grow by at most 3 %. The graph is linear in the number of type uses, with one Tarjan pass.SharedMut as self-reaching. Only a cycle back to the type counts, and that list is what makes the rule cheap.