Every inferred-type module-global -- let n = 5; ... return n, or
let g = pt{...}; g.a -- yielded <nil> downstream in cstage: the module
N_LET pass-2 stamped d->type from the initializer but never repointed the
Sym, so later references resolved the still-unstamped Sym. The wwstage
checker already repointed correctly, so this aligns cstage UP (cstage-only;
no combined.ww / check.ww change, w6c_ww unchanged).
Mirrors the #11 [_]-array repoint. The fix is shape-agnostic (keyed on the
unstamped Sym, not the use site) -- verified across scalar/field/arg/index/
str/match/nested inferred-global shapes. Commit B of #150 (Commit A 5c37648
fixed the by-value struct-arg cgen). A pure inferred ARRAY global is now
correct in cstage but trips wwstage asserttyped -- opposite-stage, filed
#125-class. byte-id 990-997 8/8. test/wcc/823 table-driven.
This commit is contained in:
7
Makefile
7
Makefile
@@ -251,6 +251,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_arr_zero_vs_infer \
|
||||
$(BIN)/test_def_str_index_reject \
|
||||
$(BIN)/test_struct_global_byval_arg \
|
||||
$(BIN)/test_inferred_global_let \
|
||||
$(BIN)/test_slice_str_global_zero \
|
||||
$(BIN)/test_slice_literal_global \
|
||||
$(BIN)/test_global_arr_elem_field \
|
||||
@@ -665,6 +666,12 @@ $(BIN)/test_struct_global_byval_arg: test/wcc/822_struct_global_byval_arg.c $(BI
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_inferred_global_let: test/wcc/823_inferred_global_let.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_def_arr_infer_len: test/wcc/814_def_arr_infer_len.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
|
||||
@@ -3015,7 +3015,19 @@ check_file(Checker *c, Node *file)
|
||||
err(c, d->pos, "[_]T needs an "
|
||||
"array-literal initialiser");
|
||||
}
|
||||
if (d->type == NULL) d->type = type_default(rt);
|
||||
if (d->type == NULL) {
|
||||
d->type = type_default(rt);
|
||||
/* #150 bug-1: pass-1 installed the Sym with
|
||||
* the annotation-less NULL type; without
|
||||
* repointing it, every downstream N_IDENT
|
||||
* read of this inferred module-global
|
||||
* resolves nil (`g.a`/`take(g)` → <nil>).
|
||||
* The general-inferred twin of the #11
|
||||
* [_]-array Sym repoint above. */
|
||||
Sym *s = scope_lookup_local(c->cur,
|
||||
d->str);
|
||||
if (s) s->type = d->type;
|
||||
}
|
||||
if (d->type && rt != ty_err && d->type != ty_err
|
||||
&& !type_assignable(d->type, rt)
|
||||
&& !arrlit_init_fits(c, d->type, d->rhs))
|
||||
|
||||
236
test/wcc/823_inferred_global_let.c
Normal file
236
test/wcc/823_inferred_global_let.c
Normal file
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* 823_inferred_global_let — an inferred-type (annotation-less) module-global
|
||||
* `let g = <init>;` must type-resolve at every downstream read. cstage's
|
||||
* module N_LET pass-2 (cmd/wcc/check.c) stamped `d->type` from the rhs but
|
||||
* never repointed the Sym that pass-1 installed with the annotation-less NULL
|
||||
* type — so every later N_IDENT read of the global resolved its type as nil:
|
||||
* `let n = 5; return n + 0` → `arithmetic on non-numeric type`
|
||||
* `let g = pt{..}; g.a + g.b` → `arithmetic on non-numeric type`
|
||||
* `let g = pt{..}; take(g)` → `argument type <nil> not assignable to pt`
|
||||
*
|
||||
* The fix (cmd/w6c/cgen.c sibling check.c) mirrors the #11 [_]-array Sym
|
||||
* repoint a few lines up: after `d->type = type_default(rt)`, repoint the
|
||||
* Sym at the stamped type. cstage-ONLY — wwstage's single-pass resolve
|
||||
* already stamps the inferred global (all three rows built clean pre-fix),
|
||||
* so this LIFTS cstage from loud-reject to accept, converging acceptance.
|
||||
* No asm moves for code that already built; the inferred form now reaches
|
||||
* the same cgen as the explicit-typed form, so cstage==wwstage byte-id.
|
||||
*
|
||||
* row | shape | want
|
||||
* --------------+---------------------------------------------+-----
|
||||
* scalar | let n = 5; return n + 0 (broadest case) | 5
|
||||
* struct_field | let g = pt{a=7,b=9}; g.a + g.b | 16
|
||||
* struct_arg | let g = pt{a=5,b=9}; take(g) (rides #150-A) | 14
|
||||
*
|
||||
* All three are mutation-sane: pre-fix cstage loud-rejects each (the build
|
||||
* fails), so a regression that drops the repoint re-louds and fails the row.
|
||||
* The byte-id rows pin cstage==wwstage `.s` for the now-accepted inferred
|
||||
* source.
|
||||
*/
|
||||
#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; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* scalar — the broadest case: an inferred scalar global read in
|
||||
* arithmetic. The read must flow through the repointed Sym's numeric
|
||||
* type; pre-fix cstage `arithmetic on non-numeric type` (a bare
|
||||
* `return n: i32` would NOT loud — the cast's target type masks the
|
||||
* nil Sym — so `+ 0` is the mutation-sane shape). */
|
||||
{ "scalar",
|
||||
"package main;\n"
|
||||
"let n = 5;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn (n + 0): i32;\n"
|
||||
"};\n",
|
||||
5 },
|
||||
|
||||
/* struct_field — inferred struct global, both fields read. pre-fix
|
||||
* cstage `arithmetic on non-numeric type`. */
|
||||
{ "struct_field",
|
||||
"package main;\n"
|
||||
"type pt = struct { a: int, b: int };\n"
|
||||
"let g = pt { a = 7, b = 9 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn (g.a + g.b): i32;\n"
|
||||
"};\n",
|
||||
16 },
|
||||
|
||||
/* struct_arg — inferred struct global passed BY VALUE; rides the
|
||||
* #150 Commit A cgen marshalling. pre-fix cstage `argument type
|
||||
* <nil> not assignable to pt`. */
|
||||
{ "struct_arg",
|
||||
"package main;\n"
|
||||
"type pt = struct { a: int, b: int };\n"
|
||||
"let g = pt { a = 5, b = 9 };\n"
|
||||
"fn take(x: pt) int = {\n"
|
||||
"\treturn x.a + x.b;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn take(g): i32;\n"
|
||||
"};\n",
|
||||
14 },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[64], tmpdir[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/igl_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/igl_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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 = runwait(outbin);
|
||||
|
||||
unlink(src); unlink(outbin); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */
|
||||
static int
|
||||
asm_byte_identical(const char *bin, const struct row *r, int i)
|
||||
{
|
||||
char src[64], cs[64], ws[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/igl_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/igl_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/igl_asm_%d_%d_w.s", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
|
||||
unlink(src);
|
||||
return -1;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
|
||||
bin, ws, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
|
||||
unlink(src); unlink(cs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *fc = fopen(cs, "rb");
|
||||
FILE *fw = fopen(ws, "rb");
|
||||
int rc = 0;
|
||||
if (!fc || !fw) {
|
||||
rc = -1;
|
||||
} else {
|
||||
for (;;) {
|
||||
int a = fgetc(fc);
|
||||
int b = fgetc(fw);
|
||||
if (a != b) { rc = -1; break; }
|
||||
if (a == EOF) break;
|
||||
}
|
||||
}
|
||||
if (fc) fclose(fc);
|
||||
if (fw) fclose(fw);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
|
||||
r->label);
|
||||
unlink(src); unlink(cs); unlink(ws);
|
||||
return rc;
|
||||
}
|
||||
|
||||
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];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[1024];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
drivers[] = {
|
||||
{ "cstage", cdrv, 0 },
|
||||
{ "wwstage", wdrv, 1 },
|
||||
{ NULL, NULL, 0 },
|
||||
};
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int d = 0; drivers[d].name; d++) {
|
||||
if (drivers[d].gated_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr, "inferred_global_let: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"inferred_global_let[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0)
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"inferred_global_let: %d/%d fixtures failed\n", fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("inferred_global_let: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user