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

@@ -0,0 +1,167 @@
/*
* 989_slttypepref_run (#58/#50, c1) — scopelookuptype must prefer the
* current module's SK_TYPE when a value binding shadows a type leaf that
* two modules both export.
*
* THE BUG (wwstage only, cat-A silent wrong-binary under rc=0): the
* aliassym fallback (check.ww ~908) composes scopelookupprefer-then-
* scopelookuptype; lib/ww/sym.ww scopelookuptype had NO mod parameter and
* returned the first (newest-installed) SK_TYPE in the bucket chain.
* scopedefineinmodule PREPENDS, so chain-first = last-registered. When a
* param/local shadows the type leaf (scopelookupprefer lands on the value,
* not the SK_TYPE) and two modules export the same type leaf, wwstage
* silently resolved whichever module installed last — install-order
* dependent — while cstage passes c->cur_mod to scope_lookup_type
* (sym.c:131) and resolves the current module's type deterministically.
* THE FIX: give scopelookuptype a `mod` param and prefer the curmod-
* matching SK_TYPE (Pass-1 mod-match, Pass-2 chain-first fallback);
* the sole caller passes c.curmod. Mirror of cstage scope_lookup_type.
*
* row | shape | exit (cs==ww)
* ---------+-----------------------------------------------+--------------
* shadow | xb.f param `invalid` shadows type `invalid`; | 8 (was ww 1)
* | xb !i64, xa !i8; size(invalid) in xb.f |
* noshadow | param renamed (no shadow), aliassym not hit | 8 (control)
*
* The shadow row was RED pre-c1 (ww resolved xa's !i8 -> exit 1, the wrong
* module's type under rc=0). The noshadow control pins the non-shadow path
* (scopelookupprefer lands on the SK_TYPE directly) so the fix can't
* perturb it. Single-file multi-package source is the sanctioned shape
* (cmd/ww/main.c).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
struct row { const char *label; const char *src; int want_exit; };
static const struct row rows[] = {
{ "shadow",
"package xb;\n"
"export type invalid = !i64;\n"
"export fn f(invalid: i32) i64 = { return size(invalid): i64; };\n"
"package xa;\n"
"export type invalid = !i8;\n"
"package main;\n"
"import xb;\n"
"import xa;\n"
"fn main() int = { return xb.f(0): int; };\n",
8 },
{ "noshadow",
"package xb;\n"
"export type invalid = !i64;\n"
"export fn f(x: i32) i64 = { return size(invalid): i64; };\n"
"package xa;\n"
"export type invalid = !i8;\n"
"package main;\n"
"import xb;\n"
"import xa;\n"
"fn main() int = { return xb.f(0): int; };\n",
8 },
};
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/sltp_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/sltp_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int have_ww = (access(wdrv, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
int gc = run_build(cdrv, &rows[i], i);
if (gc < 0) {
fprintf(stderr, "slttypepref[cstage][%s]: build/run failed "
"(got %d)\n", rows[i].label, gc);
fail++;
continue;
}
if (gc != rows[i].want_exit) {
fprintf(stderr, "slttypepref[cstage][%s]: exit=%d want=%d\n",
rows[i].label, gc, rows[i].want_exit);
fail++;
}
if (!have_ww) {
fprintf(stderr, "slttypepref: skip wwstage (no %s)\n", wdrv);
continue;
}
int gw = run_build(wdrv, &rows[i], i);
if (gw != gc) {
fprintf(stderr, "slttypepref[%s]: cs=%d != ww=%d "
"(scopelookuptype mod-preference divergence — #58/#50 c1)\n",
rows[i].label, gc, gw);
fail++;
}
if (gw != rows[i].want_exit) {
fprintf(stderr, "slttypepref[wwstage][%s]: exit=%d want=%d\n",
rows[i].label, gw, rows[i].want_exit);
fail++;
}
}
if (fail) {
fprintf(stderr, "slttypepref_run: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("slttypepref_run: %d/%d ok\n", total, total);
return 0;
}