Skip to content

Sema: Self { .. } in a method body is the method's own declaration; a same-named method on two same-named types is a located error, not invalid MIR (#1457, part) - #1581

Merged
ehartford merged 1 commit into
fix-1445-align-padded-field-readfrom
fix-1457-self-literal-module-type
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1445-align-padded-field-readfrom
fix-1457-self-literal-module-type

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Two modules each declare pub type Item. In module a's impl Item, the
literal Self { x: 5, y: 6 } was built as module b's Item (a 5 0
after #1446; a 6 before it). --dump-mir showed _0: ty235 (a's
Item) but the literal's temp _1: ty236 (b's).

Root cause. SemaCheck.check_struct_literal types a Self literal from
named_types[Self], which only collect_fn_decl binds, for the signature,
and unbinds before the bodies are checked (SemaDecl.w:1784).
check_fn_body_with_sig_at never bound it, so in a body the literal came
back untyped: analyze shows the node as type=<error>, and no error
was emitted (the pool_resolve(name) != "Self" guard skipped it). MirLower
then resolved the untyped literal BY NAME (MirLower.w:2386-2398), through
the flat named_types map, which holds the newest declaration of that
name in any module: cb's Item.

Fix. check_fn_body_with_sig_at binds Self to the method owner's type
visible from the body's module (method_decl_owner_symbol +
lookup_named_type_visible, after update_decl_source_context) for the
duration of the body and restores it after, for non-generic owners; a
generic owner's methods are checked per instantiation with Self in the
substitution (check_fn_body_concrete), and binding the template there
made Cell.wrap -> Self: Self { value } a return type mismatch
(issue61_query_method_cache_keys). A Self literal with no binding is
now an error, never a silent untyped node.

The method-symbol collision (both impls declare total, one symbol
Item.total, the later declaration shadowed the earlier and its body
was skipped, "invalid MIR before codegen: body index map mismatch for fn
symbol N") is reported at the second declaration with both modules named
("method 'total' is declared for two different types named 'Item' ...
not supported yet (#1457)") instead of the ICE. It is NOT fixed: a
method's symbol must carry its owning declaration (the method half of
#1446's name$<tid> split) through Sema's owner-keyed method tables
(lookup_method_sig, method_impl_nodes, method_owner_symbol_for_type) and
codegen's function naming, where two Item.total LLVM functions would
also collide. That is its own change.

Matrix (stage1 before -> after): a.twin_a() a 0 0 -> a 6 5; new_a()
a 5 0 -> a 5 6; b's twin_b/make_b unchanged and right; two types with
Self literals in one module unchanged; generic owner Self { value }
unchanged; same-named total on both: ICE -> located error. stage1 check src/main.w: ok. The impl/method/module/self/trait/extension test
list (84 files) passes except behav_migrate_module_paths, which fails the
same way on the layer below. examples/ and the UAT fixtures check the
same as on the base.

Fixtures: behav_1457_self_literal_own_module_type (lib/issue1457/{ca,cb}),
err_1457_same_named_method_two_types (lib/issue1457/{ma,mb}).

Stack #1582, layer 11/11. Draft until the batch battery is green (D56).

… a same-named method on two same-named types is a located error, not invalid MIR (#1457, part)

Two modules each declare `pub type Item`. In module a's `impl Item`, the
literal `Self { x: 5, y: 6 }` was built as module b's `Item` (`a 5 0`
after #1446; `a  6` before it). `--dump-mir` showed `_0: ty235` (a's
Item) but the literal's temp `_1: ty236` (b's).

Root cause. SemaCheck.check_struct_literal types a `Self` literal from
`named_types[Self]`, which only collect_fn_decl binds, for the signature,
and unbinds before the bodies are checked (SemaDecl.w:1784).
check_fn_body_with_sig_at never bound it, so in a body the literal came
back untyped: `analyze` shows the node as `type=<error>`, and no error
was emitted (the `pool_resolve(name) != "Self"` guard skipped it). MirLower
then resolved the untyped literal BY NAME (MirLower.w:2386-2398), through
the flat named_types map, which holds the newest declaration of that
name in any module: cb's Item.

Fix. check_fn_body_with_sig_at binds `Self` to the method owner's type
visible from the body's module (method_decl_owner_symbol +
lookup_named_type_visible, after update_decl_source_context) for the
duration of the body and restores it after, for non-generic owners; a
generic owner's methods are checked per instantiation with `Self` in the
substitution (check_fn_body_concrete), and binding the template there
made `Cell.wrap -> Self: Self { value }` a return type mismatch
(issue61_query_method_cache_keys). A `Self` literal with no binding is
now an error, never a silent untyped node.

The method-symbol collision (both impls declare `total`, one symbol
`Item.total`, the later declaration shadowed the earlier and its body
was skipped, "invalid MIR before codegen: body index map mismatch for fn
symbol N") is reported at the second declaration with both modules named
("method 'total' is declared for two different types named 'Item' ...
not supported yet (#1457)") instead of the ICE. It is NOT fixed: a
method's symbol must carry its owning declaration (the method half of
#1446's `name$<tid>` split) through Sema's owner-keyed method tables
(lookup_method_sig, method_impl_nodes, method_owner_symbol_for_type) and
codegen's function naming, where two `Item.total` LLVM functions would
also collide. That is its own change.

Matrix (stage1 before -> after): a.twin_a() `a 0 0` -> `a 6 5`; new_a()
`a 5 0` -> `a 5 6`; b's twin_b/make_b unchanged and right; two types with
Self literals in one module unchanged; generic owner `Self { value }`
unchanged; same-named `total` on both: ICE -> located error. `stage1
check src/main.w`: ok. The impl/method/module/self/trait/extension test
list (84 files) passes except behav_migrate_module_paths, which fails the
same way on the layer below. examples/ and the UAT fixtures check the
same as on the base.

Fixtures: behav_1457_self_literal_own_module_type (lib/issue1457/{ca,cb}),
err_1457_same_named_method_two_types (lib/issue1457/{ma,mb}).
@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1582 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 02301f8 + #1571 → … → #1581 (head dca1a8f; every PR head is a tested commit): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merges cleanly into main. Merge the stack: gh stack merge 1581 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 22:04
@ehartford
ehartford merged commit f3005a6 into main Sep 24, 2026
ehartford added a commit that referenced this pull request Sep 24, 2026
…uilt Vec[i32]; main red at stage2 after #1546 + #1581 (#1596)

* MIR: the three switch tables built from Vec[i32] take Vec[i64] — discriminants are i64 end to end (#1541) and the width check (#1368) now rejects the narrowed table; main was red at stage2

* drop-audit: the field_join_view match cell indents its arms under the match — the seed parsed the flush arms by accident; §29.13 (#1391) rejects them

* MirValidationTests: the last switch table built from Vec[i32] takes Vec[i64] — the internals tests compile this file, src/main.w does not

---------

Co-authored-by: Eric Hartford <[email protected]>
@ehartford
ehartford deleted the fix-1457-self-literal-module-type branch September 24, 2026 16:56
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.

1 participant