selfhost: structlookup/enumlookup mod-filter (mirror aliaslookup)

Tags structinfo/enumtype with originating module; exact-match first,
then split pkg.X and filter by smod/emod. Without this, two modules
with same-leaf-name struct/enum types collapsed to whichever entry
appeared first in the chain.

Wired into 696_modtype_leaf_collision via a wwstage run_pos using
ww_ww (negative case omitted: w6c_ww has no checkfile pass). Updated
the test's Makefile deps to include the wwstage binaries.

Audited the rest of the lookup family — fnretlookup, fnparamslookup,
deflookup don't need the same treatment: the parser emits N_DOT.str
(call/field name) as the leaf only, and fnparamslookup is only
invoked with N_IDENT.str. Dotted module-qualified function calls go
through the module-mangling path instead.
This commit is contained in:
2026-05-15 13:58:56 +09:00
parent 0ef94eef04
commit 9d85aa4142
6 changed files with 202 additions and 46 deletions

View File

@@ -1149,12 +1149,41 @@ fn nodeprimwidth(c: *cgen, n: *node) i32 = {
// ---- type-driven slot sizing ----------------------------------------
fn structlookup(c: *cgen, name: str) *structinfo = {
// Exact match first: bare-from-source struct names and already-
// leafed lookups hit here directly.
let s: *structinfo = c.structs;
for (s != nil) {
let sn: str = s.sname;
if (streq(sn, name)) { return s; };
s = s.sinext;
};
// Module-qualified form: `pkg.S` → match the leaf scoped to its
// originating module. Mirrors aliaslookup's mod-filter; the
// `smod == pkg` guard is what prevents two modules with same-
// leaf-name structs from collapsing into whichever entry appears
// first in the chain.
let i: i32 = name.len - 1;
for (i >= 0) {
if (name[i] == 46u8) { // '.'
let pkg: str;
pkg.ptr = name.ptr;
pkg.len = i;
let leaf: str;
leaf.ptr = name.ptr + ((i + 1): u64);
leaf.len = name.len - (i + 1);
let b: *structinfo = c.structs;
for (b != nil) {
if (streq(b.sname, leaf)) {
if (streq(b.smod, pkg)) {
return b;
};
};
b = b.sinext;
};
return nil;
};
i -= 1;
};
return nil;
};
@@ -1500,9 +1529,10 @@ fn fieldsize(c: *cgen, tnode: *node) i32 = {
return 8;
};
fn registerstruct(c: *cgen, name: str, tstruct: *node) void = {
let si: *structinfo = amalloc(c.a, 64u64): *structinfo;
fn registerstruct(c: *cgen, name: str, module: str, tstruct: *node) void = {
let si: *structinfo = amalloc(c.a, 80u64): *structinfo;
si.sname = name;
si.smod = module;
si.fields = nil;
si.totsize = 0;
let head: *fieldinfo = nil;
@@ -1550,7 +1580,7 @@ fn collectstructs(c: *cgen, file: *node) void = {
let body: *node = d.lhs;
if (body != nil) {
if (body.kind == nkind.N_TSTRUCT) {
registerstruct(c, d.str, body);
registerstruct(c, d.str, d.module, body);
};
};
};