Skip to content

Same-named types in two modules: Self in a struct literal resolves to the other module's type; same-named methods collide (Item.total) #1457

Closed

Description

@ehartford

Spec: §18.1 modules (each module's declarations are its own; an impl names its module's type). Related to #1446 (codegen layout), which the #1446 branch fixes; this is the Sema layer of the same situation.

Two modules each declare pub type Item. Inside module ca, Self in a struct literal resolves to the other module's Item:

lib/ca.w

pub type Item { x: i64, y: i64 }
impl Item:
    pub fn twin_a() -> Self: Self { x: self.y, y: self.x }
pub fn Item.make_a() -> Self: Self { x: 5, y: 6 }
pub fn show_a(i: &Item) -> Unit: print(f"a {i.x} {i.y}")
pub fn new_a() -> Item: Item.make_a()

lib/cb.w

pub type Item { s: str, k: i32 }
impl Item:
    pub fn twin_b() -> Self: Self { s: self.s ++ "!", k: self.k + 1 }
pub fn Item.make_b() -> Self: Self { s: "bee".clone(), k: 7 }
pub fn show_b(i: &Item) -> Unit: print(f"b {i.s} {i.k}")
pub fn new_b() -> Item: Item.make_b()

root:

use ca
use cb

fn main:
    let a = new_a()
    show_a(&a)
    show_a(&a.twin_a())

Expected a 5 6 / a 6 5. On the #1446 branch: a 5 0 / a 0 0; on origin/main 1c76062: a 6 / a 5 (plus the #1446 layout collision). --dump-mir of Item.make_a: _0: ty235 (ca's Item) but the literal's temp is _1: ty236 (cb's Item) with fields const 5ty3, const 6ty3 — Sema recorded the struct literal Self { ... } as cb's type (the flat name map's newest Item), and the fields were typed against a struct that has no x/y. Spelling the literal Item { x: 5, y: 6 } gives the right answer.

Second symptom, same layer: a method name both impls declare collides. Give both Items a pub fn total() -> i64 and call each from its own module:

error: invalid MIR before codegen: body index map mismatch for fn symbol 778

Method symbols are Item.total for both declarations.

Expected: Self and method symbols are those of the impl's own declaration.

Activity

  1. ehartford commented on Oct 2, 2026

    @ehartford
    ContributorAuthor

    Verified on main (30f89aa, installed with = 1688728), with the issue's exact two-module program:

    $ with run main.w
    a 5 6
    a 6 5
    

    Symptom 1 (Self { .. } in a method body built the other module's Item) is fixed by f3005a6 (#1581): check_fn_body_with_sig_at binds Self to the method owner's declaration for the body.

    Symptom 2 (same-named method on both Items, total) is no longer invalid MIR, but it is still refused, by design of that commit, with a located error:

    error: method 'total' is declared for two different types named 'Item' (the other is in lib/ca.w); a method name shared by same-named types in two modules is not supported yet (#1457)
     --> lib/cb.w:4:5
    

    The remaining work is the method half of #1446's name$<tid> split: a method symbol must carry its owning declaration through Sema's owner-keyed method tables (lookup_method_sig, method_impl_nodes, method_owner_symbol_for_type) and codegen's function naming (two Item.total LLVM functions). Leaving open for that; the diagnostic cites this issue.

  2. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 0f0d76b (#2012, battery green on 6644771). audit:all over src/main.w: 13 violations → 0.

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