Skip to content

View-liveness checker matches same-named identifiers across files by byte-span window (phantom 'cannot mutate while live view' errors) #753

Closed

Description

@ehartford

SemaCheck.find_symbol_use_after_in_span scans the ENTIRE merged node pool for NK_IDENT nodes with the target symbol, filtering only by byte-span containment in the root's window. Spans are per-file byte offsets and nodes carry no file identity, so a same-named identifier from a different file whose offsets land inside the window counts as a "future use" of a view, keeping its borrow record alive across a mutation.

Observed during #750 work: inserting 4 inert let lines into Sema.w's resolve_type_expr produced 1 phantom cannot mutate \self` while `sym` is a live view into iterror atSema.w:6984 (expire_borrows_in_scope, 3200 lines downstream); inserting a 7-line diagnostic branch at the same spot produced 7; no insertion produced 0. The victim code's own view (let sym = self.bind_names.get(i)) has no genuine use after the mutation — the "last use" came from sym`-named identifiers in other files whose offsets happened to fall in the tail's window. Verdicts shift with ANY upstream edit in the file (offset displacement), which is how it appeared as a heisenbug.

Mitigations landed with the #750 scaffolding batch:

  • find_symbol_use_after_in_span now requires expr_uses_symbol(root, sym) before the pool scan — prunes only matches the subtree provably lacks, killing the no-real-use phantom class.
  • The expire_borrows_in_scope victim annotates the binding (let sym: i32 = ...) so it materializes a copy instead of a view (also needed so the current seed, which predates the guard, keeps accepting src).

Remaining defect (this issue): the window scan still cannot distinguish files when the subtree DOES use the symbol somewhere — a cross-file candidate numerically after the mutation can extend liveness that actually ended. Complete fix wants either per-node file identity in the AST pool or a true position-filtered subtree walk (an expr_uses_symbol variant that respects after), replacing the whole-pool scan.

Activity

  1. ehartford commented on Sep 1, 2026

    @ehartford
    ContributorAuthor

    Sizing notes from the #756 session (same defect family — #756's two span-containment sites are now guarded by decls_share_source_file; this issue's window scan is the remaining member):

    Shortcut evaluated and rejected: filtering pool-scan candidates by node-id range ownership (child-first allocation ⇒ a decl's subtree ≈ (prev_decl_root, decl_root]). Two problems: (1) the comptime transform appends synthesized nodes at the END of the pool, so a transformed decl's idents fall outside its range and would be excluded — which errs in the UNSOUND direction (borrow expires early, mutation allowed while a view lives). A liveness filter must err toward keeping candidates. (2) fixup nodes allocated after the decl root have the same failure shape.

    The right fix remains the issue's own prescription, one of:

    • a position-filtered SUBTREE walk: port expr_uses_symbol (~220 lines, ~60 per-kind branches, SemaCheck.w:21999) into a node-returning variant taking after, and make expr_uses_symbol a thin wrapper so there is exactly one traversal (single-source doctrine); or
    • per-node file identity in the AST pool (one i32/i16 column; memory cost on multi-M-node pools; touches Ast layout — fixpoint-relevant).

    The walker port is the cheaper of the two and mechanical, but 60 branches of drift risk wants a fresh-context session with the battery run immediately after — not a tail-of-batch edit. The #750-era mitigations (expr_uses_symbol pre-check + the annotated victim binding) keep the no-real-use phantom class dead meanwhile.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): done. find_symbol_use_after_in_span now limits candidates to the root's own file (let root_file = self.ast.file(root as NodeId) and the comment above it in src/SemaCheck.w), landed in 376af1d (2026-10-03).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions