Skip to content

Codegen: each type declaration gets its own LLVM type (#1446) - #1462

Merged
ehartford merged 1 commit into
fix-1443-ref-box-receiverfrom
fix-1446-module-type-identity
Sep 23, 2026
Merged

ehartford merged 1 commit into
fix-1443-ref-box-receiverfrom
fix-1446-module-type-identity

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1446. Top of stack #1463, above #1461.

Bug. Two modules each declaring pub type Item shared one LLVM struct, and module a's values were read through b's layout (a 1724130190).

Evidence. with ir: one %Item = type { %str, i32 }, used by both make_a and make_b. Sema has two TypeIds with the same name symbol (775). lldb on the base compiler shows Codegen.declare_struct_type called twice with name symbol 0x307 (775), for declaration nodes 0x1dcd and 0x1de1.

Root cause. Pass 0a/0b register a named LLVM type under the declaration's bare name symbol. Every TypeId-to-LLVM lookup goes back through that name.

Fix. collect_nominal_aliases gives every later declaration of a name that more than one TypeId declares its own symbol, name$<tid>. The first declaration keeps the plain name. Every lookup that goes from a TypeId, a type node or Self now reaches that declaration's symbol. When no name is split, every hook returns its input unchanged.

Matrix. Two modules each declare Item, a Pair holding Item, a payload enum and a disc enum with different reprs. Checked: fields, the nested struct, enum matches, Vec[Item], Option[Item], methods with distinct names, -> Self with Item { } literals, and str drops (leak count=0). Base prints wrong values, or fails LLVM verification once methods are involved. A stage2 from the fixed stage1 validates src/main.w.

Fixtures. behav_same_named_type_layout (the issue's repro) and behav_same_named_types_two_modules, with modules in test/behavior/lib/issue1446/.

Filed. #1457: Sema types a Self { .. } literal in one module's impl as the other module's Item, and same-named methods collide. Both are wrong on base too.

Stack verification (top, all four fixes).

  • Stage2 built by the fixed stage1; check src/main.w --validate-all: ok.
  • 412 behavior/spec/codegen/debug_alloc tests (module, enum, match, box, generic and receiver sets) pass.
  • behav_fn_abi_async and async_tuple_await pass (they failed on an earlier cut of MIR/codegen: a discriminant enum's discriminant is its repr type; a local's slot is its declared type (#1444) #1460; see that PR).
  • tools/drop_audit.w with this stage2 as candidate: 176 cells, 0 non-PASS, 0 regressions.
  • tools/move_audit.w: 0 vs-expected FAIL. Its output is byte-identical to the same audit run with base stage2 as candidate.

Draft until the batch battery is green.

@ehartford
ehartford added this pull request to stack #1463 September 23, 2026 08:41
@ehartford ehartford changed the title fix 1446 module type identity Codegen: each type declaration gets its own LLVM type (#1446) Sep 23, 2026
…ules declare no longer shares one struct (#1446)

Two modules each declaring `pub type Item` printed `a  1724130190`:
module a's `{ i64, i64 }` values were read through b's `{ str, i32 }`.
Route: codegen of a compilable input — IR, then lldb on the base compiler.

Evidence. `with ir` of the repro: one `%Item = type { %str, i32 }`, used
by both `make_a` and `make_b`. analyze: Sema has two TypeIds (108, 109),
both `kind=6 d0=775` with different field tables. lldb on base stage2
(`ir`, breakpoint on Codegen.declare_struct_type printing w1/w2): 23
struct declarations, and name symbol 0x307 (775) twice, for declaration
nodes 0x1dcd and 0x1de1 — the second call sets the body of the struct the
first one defined.

Root cause. Codegen.w Pass 0a/0b register a named LLVM type under the
declaration's bare name symbol (`pool.get_data0(decl)` via
shadow_reg_sym), and every TypeId-to-LLVM lookup goes back through that
name (sema_type_to_llvm, mir_sema_type_to_llvm, place projection,
enum payload lookup). The D29 `$std` alias already split a std-tier
declaration from a user one; nothing split two user modules.

Fix. Before registration, collect_nominal_aliases finds names more than
one non-generic TypeId declares: the first declaration in decl order keeps
the plain symbol, each later one registers as `name$<tid>`. The
TypeId-driven lookups map a TypeId to its declaration's symbol; a type
node naming a split name asks Sema which declaration it means from the
declaring module (sync_decl_context), and `Self` in a split name's method
is the declaration visible from the method's module. Names declared once
are untouched: every hook is the identity when no name is split.

Matrix (two modules, each declaring Item, Pair { first: Item }, a payload
enum Shape and a disc enum Kind with different reprs): field reads, a
struct holding the split type, payload enum match, u8 vs i64 disc enum
match, Vec[Item] iteration, Option[Item], methods with distinct names,
`-> Self` returning an `Item { }` literal, str fields dropped (debug
allocator leak count=0). Base: the plain repro prints wrong values, the
full program fails LLVM verification. A stage2 built by the fixed stage1
validates src/main.w.

Filed, not fixed here: Sema types a `Self { .. }` literal in one module's
impl as the other module's `Item`, and same-named methods on the two
types collide as one symbol (#1457).
@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1466 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 1c76062 + #1418 → #1419 → #1425 → #1428 → #1449 → #1377 → #1459 → #1460 → #1461 → #1462 (head cf36211, including the #1449/#1462 Codegen resolution and the fixed #1460): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commits on the layer below. Merge the whole stack: gh stack merge 1462 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 09:58
@ehartford
ehartford merged commit 75ff8f2 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1446-module-type-identity branch September 24, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Silent wrong layout: two modules declaring a same-named type share one LLVM struct in codegen

1 participant