w6c+selfhost: same-module preference for bare-leaf lookup

This commit is contained in:
2026-05-15 11:24:33 +09:00
parent 35b32c1304
commit e6045f3ada
13 changed files with 382 additions and 12 deletions

View File

@@ -31,6 +31,10 @@ type checker = struct {
errs: i32,
verbose: i32, // when non-zero, log each unresolved name
fnret: *node, // enclosing fn's return type AST (for `?`)
curmod: str, // importing-module bareword for the decl
// currently being walked; "" for primary
// compilation unit. Drives same-module
// preference in bare-leaf lookups.
};
// seedprimitives — install the built-in type names so `i32`, `str`,
@@ -129,7 +133,7 @@ fn resolvewalk(c: *checker, n: *node) void = {
if (k == nkind.N_IDENT) {
let nm: str = n.str;
if (nm.len > 0) {
let s: *sym = scopelookup(c.cur, nm);
let s: *sym = scopelookupprefer(c.cur, c.curmod, nm);
if (s == nil) {
c.nunresolved += 1;
if (c.verbose != 0) {
@@ -144,7 +148,7 @@ fn resolvewalk(c: *checker, n: *node) void = {
if (k == nkind.N_TNAME) {
let nm: str = n.str;
if (nm.len > 0) {
let s: *sym = scopelookup(c.cur, nm);
let s: *sym = scopelookupprefer(c.cur, c.curmod, nm);
// `pkg.Type` — strip the last dot prefix and look up
// the leaf with a mod filter so same-leaf-name types
// from different imports (`bufio.stream` vs
@@ -943,6 +947,8 @@ export fn checkinit(c: *checker, a: *arena, tc: *tctx) void = {
c.errs = 0;
c.verbose = 0;
c.fnret = nil;
let empty: str;
c.curmod = empty;
seedprimitives(c);
};
@@ -958,8 +964,12 @@ export fn checkfile(c: *checker, file: *node) void = {
};
// Pass 2: walk decl bodies/types and resolve identifiers.
// Track the per-decl module bareword so bare-leaf lookups inside
// the body prefer same-module entries over alphabetically-earlier
// same-leaf imports.
d = file.list;
for (d != nil) {
c.curmod = declmod(file, d);
let k: nkind = d.kind;
if (k == nkind.N_FNDECL) {
if (d.lhs != nil) { resolvewalk(c, d.lhs); }; // return type
@@ -975,5 +985,7 @@ export fn checkfile(c: *checker, file: *node) void = {
};};};};
d = d.next;
};
let empty: str;
c.curmod = empty;
};