wcc/ww: scopelookuptype prefers the current module's symbol

A type lookup in a bundled build resolved to the newest-installed
same-leaf symbol from ANY module; prefer the current module first
(mirror cstage sym.c:131; the prior attempt's failure was its own
u64-vs-i32 guard bug, not a deeper layer — probe-proven). Also adds
the rule-7 #58 notes at the latent varianterr/scruttype pair and
rewrites the stale deferral block to closing cites. Report item [5].
This commit is contained in:
2026-06-13 02:58:09 +09:00
parent ffe37deaee
commit 63e837e820
6 changed files with 331 additions and 45 deletions

View File

@@ -10208,7 +10208,7 @@ export fn scopelookup(s: *scope, name: str) *sym = {
return nil;
};
// scopelookuptype — find an SK_TYPE entry by name regardless of mod.
// scopelookuptype — find an SK_TYPE entry by name, same-module preferred.
//
// Same FNV bucket + hashnext chain + parent walk as scopelookup, with
// an `skind == SK_TYPE` filter. Used to disambiguate the bare-TNAME
@@ -10216,23 +10216,43 @@ export fn scopelookup(s: *scope, name: str) *sym = {
// SK_USE sym for a leaf that ALSO names a type (e.g. `tok` struct
// declared in lib/ww/lex/tok.ww with `package lex;` while
// `import tok;` registers a same-name SK_USE), the resolver needs
// the type entry regardless of its declared package — the struct's
// mod may differ from the leaf (lex/tok pair) so
// scopelookupinmodule(c, leaf, leaf) won't find it.
// the type entry — the struct's mod may differ from the leaf (lex/tok
// pair) so scopelookupinmodule(c, leaf, leaf) won't find it.
//
// Mirrors the bare-vs-qualified disambiguation pattern from task #57.
export fn scopelookuptype(s: *scope, name: str) *sym = {
for (s != nil) {
// #58/#50: within each scope, Pass-1 prefers an SK_TYPE whose `sym.mod`
// matches `mod`; Pass-2 falls back to the first SK_TYPE regardless of
// mod (chain-first, the prior behavior). scopedefineinmodule PREPENDS,
// so chain-first = last-registered — when two modules export the same
// type leaf the bare walk silently picked the newest-installed one,
// install-order-dependent, while cstage is deterministic on cur_mod.
// Mirrors cstage cmd/wcc/sym.c scope_lookup_type(s, mod, name) (the
// kind-filtered + mod-preferring single walk); sole caller passes
// c.curmod. i32-correct: streq throughout, only `.len > 0` guards (the
// reverted attempt compared str.len as u64 — str.len is i32).
export fn scopelookuptype(s: *scope, mod: str, name: str) *sym = {
let p: *scope = s;
for (p != nil) {
let h: u64 = hashstr(name);
let bi: i32 = (h % (s.nbuckets: u64)): i32;
let b: *sym = s.buckets[bi];
let bi: i32 = (h % (p.nbuckets: u64)): i32;
let b: *sym = p.buckets[bi];
let fallback: *sym = nil;
for (b != nil) {
if (streq(b.name, name)) {
if (b.skind == skind.SK_TYPE) { return b; };
if (b.skind == skind.SK_TYPE) {
if (mod.len > 0) {
if (b.mod.len > 0) {
if (streq(b.mod, mod)) {
return b;
};
};
};
if (fallback == nil) { fallback = b; };
};
};
b = b.hashnext;
};
s = s.parent;
if (fallback != nil) { return fallback; };
p = p.parent;
};
return nil;
};
@@ -11305,9 +11325,11 @@ fn aliassym(c: *checker, n: *node) *sym = {
// on the head-first bucket walk: e.g. utf8.invalid `!void`
// vs strconv.invalid `!i32` resolves to whichever
// registered first, driving localloadop MOVSXD/MOVQ
// divergence at 994/995. Other bare-leaf callers in this
// file (L1597 exprtype N_IDENT, L1795/L2720 N_DOT-callee
// leaf, L600 varianterr, L647 scruttype) tracked as #55.
// divergence at 994/995. The exprtype N_IDENT / N_DOT-callee
// + exprtypeoftry / &fn-synth bare-leaf callers now all use
// scopelookupprefer (the #56/#4/#11a wave). The only bare-leaf
// type lookups left — varianterr (:1051) + scruttype (:1098) —
// stay mod-blind but have no reproducible divergence; #58.
s = scopelookupprefer(c.cur, c.curmod, nm);
// #61 A.5: bare TNAME that collides with an imported
// module bareword. Two shapes hit this:
@@ -11325,7 +11347,13 @@ fn aliassym(c: *checker, n: *node) *sym = {
// bare-vs-qualified pattern from task #57.
if (s != nil) {
if (s.skind != skind.SK_TYPE) {
let sm: *sym = scopelookuptype(c.cur, nm);
// #58/#50: prefer the curmod-matching SK_TYPE.
// Two modules exporting the same type leaf (e.g.
// utf8.invalid !void vs strconv.invalid !i32)
// otherwise resolve install-order-dependent here
// when a value binding shadows the leaf; cstage
// passes c->cur_mod to scope_lookup_type (sym.c).
let sm: *sym = scopelookuptype(c.cur, c.curmod, nm);
if (sm != nil) { s = sm; };
};
};
@@ -11462,6 +11490,10 @@ fn varianterr(c: *checker, v: *node) bool = {
if (v == nil) { return false; };
if (v.kind == nkind.N_TBANG) { return true; };
if (v.kind == nkind.N_TNAME) {
// #58: mod-blind by leaf — latent. A same-leaf error-vs-plain
// type pair across two modules could in principle flip iserror,
// but the union's variants resolve at its declaration before
// varianterr runs, so no repro exists. Tracked: #58.
let s: *sym = scopelookup(c.cur, v.str);
if (s != nil) {
if (s.skind == skind.SK_TYPE) {
@@ -11509,6 +11541,10 @@ fn iserrvariant(c: *checker, tagged: *node, v: *node) bool = {
fn scruttype(c: *checker, e: *node) *node = {
if (e == nil) { return nil; };
if (e.kind == nkind.N_IDENT) {
// #58: mod-blind by leaf — lenient-only. Feeds match
// exhaustiveness; codegen reads the stamped n.type_, so a
// mis-resolution cannot drive a wrong binary (no repro).
// Tracked: #58.
let s: *sym = scopelookup(c.cur, e.str);
if (s == nil) { return nil; };
if (s.decl == nil) { return nil; };