selfhost: aliaslookup mod-filter for pkg.-qualified type refs

This commit is contained in:
2026-05-15 10:54:58 +09:00
parent 66d6408cbe
commit 35b32c1304
3 changed files with 48 additions and 18 deletions

View File

@@ -15342,6 +15342,7 @@ use cgendecl;
type aliasent = struct { type aliasent = struct {
aname: str, aname: str,
amod: str, // originating module (`// MODULE: foo`), or empty
target: *node, // the rhs type expr target: *node, // the rhs type expr
aanext: *aliasent, aanext: *aliasent,
}; };
@@ -15354,8 +15355,9 @@ fn collectaliases(c: *cgen, file: *node) void = {
let body: *node = d.lhs; let body: *node = d.lhs;
if (body != nil) { if (body != nil) {
if (body.kind != nkind.N_TSTRUCT) { if (body.kind != nkind.N_TSTRUCT) {
let a: *aliasent = amalloc(c.a, 32u64): *aliasent; let a: *aliasent = amalloc(c.a, 64u64): *aliasent;
a.aname = d.str; a.aname = d.str;
a.amod = d.module;
a.target = body; a.target = body;
a.aanext = c.aliases; a.aanext = c.aliases;
c.aliases = a; c.aliases = a;
@@ -15373,19 +15375,27 @@ fn aliaslookup(c: *cgen, name: str) *node = {
if (streq(an, name)) { return a.target; }; if (streq(an, name)) { return a.target; };
a = a.aanext; a = a.aanext;
}; };
// Module-qualified form: `pkg.alias` → try the bare leaf so a // Module-qualified form: `pkg.alias` → match the leaf name
// cross-module reference resolves the same way bare access does // scoped to its originating module. Mirrors check.c's module-
// after driver concatenation. Mirrors the check.c module- // qualified type resolution; requiring `amod == pkg` is what
// qualified type resolution. // prevents two modules with same-leaf-name aliases from
// collapsing into whichever entry appears first in the chain.
let i: i32 = name.len - 1; let i: i32 = name.len - 1;
for (i >= 0) { for (i >= 0) {
if (name[i] == 46u8) { // '.' if (name[i] == 46u8) { // '.'
let pkg: str;
pkg.ptr = name.ptr;
pkg.len = i;
let leaf: str; let leaf: str;
leaf.ptr = name.ptr + ((i + 1): u64); leaf.ptr = name.ptr + ((i + 1): u64);
leaf.len = name.len - (i + 1); leaf.len = name.len - (i + 1);
let b: *aliasent = c.aliases; let b: *aliasent = c.aliases;
for (b != nil) { for (b != nil) {
if (streq(b.aname, leaf)) { return b.target; }; if (streq(b.aname, leaf)) {
if (streq(b.amod, pkg)) {
return b.target;
};
};
b = b.aanext; b = b.aanext;
}; };
i = -1; i = -1;

View File

@@ -46,6 +46,7 @@ use cgendecl;
type aliasent = struct { type aliasent = struct {
aname: str, aname: str,
amod: str, // originating module (`// MODULE: foo`), or empty
target: *node, // the rhs type expr target: *node, // the rhs type expr
aanext: *aliasent, aanext: *aliasent,
}; };
@@ -58,8 +59,9 @@ fn collectaliases(c: *cgen, file: *node) void = {
let body: *node = d.lhs; let body: *node = d.lhs;
if (body != nil) { if (body != nil) {
if (body.kind != nkind.N_TSTRUCT) { if (body.kind != nkind.N_TSTRUCT) {
let a: *aliasent = amalloc(c.a, 32u64): *aliasent; let a: *aliasent = amalloc(c.a, 64u64): *aliasent;
a.aname = d.str; a.aname = d.str;
a.amod = d.module;
a.target = body; a.target = body;
a.aanext = c.aliases; a.aanext = c.aliases;
c.aliases = a; c.aliases = a;
@@ -77,19 +79,27 @@ fn aliaslookup(c: *cgen, name: str) *node = {
if (streq(an, name)) { return a.target; }; if (streq(an, name)) { return a.target; };
a = a.aanext; a = a.aanext;
}; };
// Module-qualified form: `pkg.alias` → try the bare leaf so a // Module-qualified form: `pkg.alias` → match the leaf name
// cross-module reference resolves the same way bare access does // scoped to its originating module. Mirrors check.c's module-
// after driver concatenation. Mirrors the check.c module- // qualified type resolution; requiring `amod == pkg` is what
// qualified type resolution. // prevents two modules with same-leaf-name aliases from
// collapsing into whichever entry appears first in the chain.
let i: i32 = name.len - 1; let i: i32 = name.len - 1;
for (i >= 0) { for (i >= 0) {
if (name[i] == 46u8) { // '.' if (name[i] == 46u8) { // '.'
let pkg: str;
pkg.ptr = name.ptr;
pkg.len = i;
let leaf: str; let leaf: str;
leaf.ptr = name.ptr + ((i + 1): u64); leaf.ptr = name.ptr + ((i + 1): u64);
leaf.len = name.len - (i + 1); leaf.len = name.len - (i + 1);
let b: *aliasent = c.aliases; let b: *aliasent = c.aliases;
for (b != nil) { for (b != nil) {
if (streq(b.aname, leaf)) { return b.target; }; if (streq(b.aname, leaf)) {
if (streq(b.amod, pkg)) {
return b.target;
};
};
b = b.aanext; b = b.aanext;
}; };
i = -1; i = -1;

View File

@@ -15342,6 +15342,7 @@ use cgendecl;
type aliasent = struct { type aliasent = struct {
aname: str, aname: str,
amod: str, // originating module (`// MODULE: foo`), or empty
target: *node, // the rhs type expr target: *node, // the rhs type expr
aanext: *aliasent, aanext: *aliasent,
}; };
@@ -15354,8 +15355,9 @@ fn collectaliases(c: *cgen, file: *node) void = {
let body: *node = d.lhs; let body: *node = d.lhs;
if (body != nil) { if (body != nil) {
if (body.kind != nkind.N_TSTRUCT) { if (body.kind != nkind.N_TSTRUCT) {
let a: *aliasent = amalloc(c.a, 32u64): *aliasent; let a: *aliasent = amalloc(c.a, 64u64): *aliasent;
a.aname = d.str; a.aname = d.str;
a.amod = d.module;
a.target = body; a.target = body;
a.aanext = c.aliases; a.aanext = c.aliases;
c.aliases = a; c.aliases = a;
@@ -15373,19 +15375,27 @@ fn aliaslookup(c: *cgen, name: str) *node = {
if (streq(an, name)) { return a.target; }; if (streq(an, name)) { return a.target; };
a = a.aanext; a = a.aanext;
}; };
// Module-qualified form: `pkg.alias` → try the bare leaf so a // Module-qualified form: `pkg.alias` → match the leaf name
// cross-module reference resolves the same way bare access does // scoped to its originating module. Mirrors check.c's module-
// after driver concatenation. Mirrors the check.c module- // qualified type resolution; requiring `amod == pkg` is what
// qualified type resolution. // prevents two modules with same-leaf-name aliases from
// collapsing into whichever entry appears first in the chain.
let i: i32 = name.len - 1; let i: i32 = name.len - 1;
for (i >= 0) { for (i >= 0) {
if (name[i] == 46u8) { // '.' if (name[i] == 46u8) { // '.'
let pkg: str;
pkg.ptr = name.ptr;
pkg.len = i;
let leaf: str; let leaf: str;
leaf.ptr = name.ptr + ((i + 1): u64); leaf.ptr = name.ptr + ((i + 1): u64);
leaf.len = name.len - (i + 1); leaf.len = name.len - (i + 1);
let b: *aliasent = c.aliases; let b: *aliasent = c.aliases;
for (b != nil) { for (b != nil) {
if (streq(b.aname, leaf)) { return b.target; }; if (streq(b.aname, leaf)) {
if (streq(b.amod, pkg)) {
return b.target;
};
};
b = b.aanext; b = b.aanext;
}; };
i = -1; i = -1;