Skip to content

Bug: Copy *mut-state handle field in Sema breaks generic lowering through the stage chain #576

Closed

Description

@ehartford

Problem

Converting DiagnosticList (a field of Sema, Parser, Zcu, Compilation) from { items: Vec[Diagnostic] } to the AstPool-style handle — { state: *mut DiagnosticListState } + impl Copy — makes the resulting compiler emit spurious unknown type 'T' errors when MIR-lowering generic blanket-impl bodies in the embedded std (impl[T: Clone] Clone for Vec[T], std/collections.w:21). Padding the handle back to the original struct size does NOT fix it, so it is not raw layout sensitivity; the trigger is the Copy/*mut-handle classification of a hot Sema-adjacent field.

Repro

At db8b0b2d, apply only the DiagnosticList handle conversion (Diagnostic.w + the Zcu/Lsp .items accessor follow-ups), build stage2, then:

fn main:
    let m = HashMap[str, i32].new()
    m.insert("a", 1)
    print(int_to_string(m.len()))

with-stage2 check repro.w → unknown type 'T' in std/collections.w. lldb backtrace lands in MirBuilder.binding_type → resolve_type_expr under lower_module — a generic template body being lowered with no substitution installed.

Why it matters

The handle pattern is the blessed §3.8 share idiom (AstPool, InternPool, CiPools, SuspendBits, CfgGraph all use it, all fine). DiagnosticList was the first conversion of a field threaded through Sema/Parser themselves, and it broke two stages downstream. #564 shipped without the handle (the consuming call sites route through field places instead — see Zcu.init in 1f9ec56), but the underlying miscompile family (#568's cousins) remains.

Related

#568 (auto-ref of pointer-ABI receiver miscompile, fixed), #574 (comptime field-iteration segv). Same family: latent codegen assumptions about struct classification changing underfoot.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Root cause found — and it reframes both this issue and #577: there is no miscompile. The "unknown type 'T'" diagnostics are genuine, have been emitted on every compile by every green compiler all along, and were invisible only because they were pushed into a doomed by-value copy of Sema.

    The actual defect: lower_module's skip predicate (src/MirLower.w ~9850) only checks fn_meta_tp_count(meta) > 0 — but blanket-impl methods (impl[T: Clone] Clone for Vec[T]) carry their type params on the impl, so their fn-level tp_count is 0. Sema knows this (SemaDecl.w:1134-1141 puts them in generic_fn_nodes with no sig); codegen's decl loops use the complete predicate (tp_count > 0 OR generic_fn_nodes.contains, CodegenDispatch.w:36-46); lower_module has only half. It lowers the raw templates with no substitutions installed and resolve_type_expr correctly fails on T — at exactly std/collections.w:21/47/48/52.

    Evidence: lldb on the current green stage1: emit_unknown_type_error hit count = 4 with the identical backtrace, exit 0. Bad binary: hit count = 4, exit 1. All other counters identical (lower_fn 117/117, lookup_generic_subst 717/717). Nothing is corrupt — the diagnostics sink differs. The handle conversion (this issue) and the Sema-return (#577) are just two ways of making the sink actually shared, surfacing the latent errors. That's why padding the handle never helped.

    Fix (validated end-to-end in a worktree at 2c8ba1a8): mirror codegen's predicate in lower_module —

    if sema.generic_fn_nodes.contains(fn_sym): continue
    let lm_sema_sym = sema.pool_lookup_symbol(pool.resolve_symbol(fn_sym))
    if lm_sema_sym != 0 and sema.generic_fn_nodes.contains(lm_sema_sym): continue
    

    With the fix plus the #577 trigger applied, the seed-built stage1 AND stage2 pass the HashMap repro, stage2 self-hosts, and the emit count is 0. The template MIR bodies were pure dead weight (codegen monomorphizes blanket impls from AST).

    After this lands: the DiagnosticList handle conversion and lower_module move-through should both be re-appliable.

    Meta-finding worth its own issue: any diagnostics emitted into a moved/doomed Sema copy are silently lost today — lower_module was one instance; other consume-then-check-diags flows deserve an audit.

  2. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Closing as fixed on HEAD (verified by probe): the user-level shape — HashMap[i32,i64] behind a *mut Copy handle field passed through a generic fn — checks, runs, and prints correct results. The prescribed root-cause fix is present: mir_fn_is_generic_template_at (src/MirLower.w:12429) now carries the full generic-fn predicate. Original stage-chain repro no longer applies.

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