Issue #1646: The checker knows which types can be part of a cycle of shared handles

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.

Goal

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.

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

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.

Done when

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.

To build

  1. The type graph and its components, as a new 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).
  2. The report: 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.
  3. 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).
  4. Measurement 3 (#1626 §9): run --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.

The rule that repeats

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.

Files and limits

Ruled out

Back to Open Issues