wcc: name-bind the size type in type position (#85 fold-2)

Resolve `size` -> TY_SIZE at the type-name resolver (C lookup_builtin /
ww tinfofornode's N_TNAME chain), mirroring uintptr, both stages. This
makes `size` writable as a type (`let x: size`, struct field, etc.),
the prerequisite for lib/types SIZE_MAX.

Twins every NAME-keyed uintptr arm in the wwstage so it behaves like
the cstage's kind-keyed Type switches (already TY_SIZE-aware from
fold-1): primtypesize + astalign (8B/8-align), primsize + letscalarprim
(8B scalar slot), isinttypeast + isnumerictname (int/numeric). rule-10
symmetric; dead on the size-free selfhost corpus so 990-997 stay byte-id.

Coexists with the size(T) size-of operator (separate c.top SK_FN seed +
N_CALL fold, NOT a type path) and `.size` field access (N_DOT); neither
touched. No c.top SK_TYPE "size" seed (would collide with the operator
seed at check.ww:96). Regenerates w6c/wwdump combined.ww (checker
embedded). New probe 957_size_type_run exercises type-position `size`
and the operator in one scope.
This commit is contained in:
2026-05-26 01:55:54 +09:00
parent bd7181ae1f
commit 9a265a31f5
8 changed files with 164 additions and 6 deletions

View File

@@ -0,0 +1,137 @@
/*
* 957_size_type_run — runtime proof that the `size` type name is usable
* in TYPE position (#85 fold-2) and that it coexists with the `size(T)`
* size-of OPERATOR. The two are different paths — TYPE-position `size`
* resolves via the type-name resolver (lookup_builtin / tinfofornode's
* N_TNAME chain), while `size(T)` is a c.top SK_FN seed folded to an
* N_INTLIT — so a program may use BOTH in the same scope with no clash.
* Each row below exercises that coexistence; without the fold-2 resolver
* arm the `let _: size` rows fail to compile ("unknown type 'size'"), so
* a green row IS the proof the name binds.
*
* `size` mirrors uintptr: platform-width (8B amd64) unsigned int. The
* rows assert it is usable in arithmetic, comparison, and an i32 cast.
*
* Table-driven like 952_floats_run: each row is a self-contained ww
* program; the C-side cstage `ww build -I lib` compiles it, we run the
* binary and assert the exit code. cstage-only by design (mirrors 951/
* 952/969): `ww_ww run` is broken (#95) and per-program wwstage byte-id
* is the 990-997 gates' job; the wwstage resolver arm is twinned for
* rule-10 symmetry and stays dead on the size-free selfhost corpus.
*/
#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 *src; int want_exit; };
static const struct row rows[] = {
/* type-position `size` + the size(T) operator in one scope. x is a
* size-typed local; size(int)/size(i32) are folded operator results.
* Both must resolve and agree (8/4 on amd64). */
{ "package main;\n"
"export fn main() i32 = {\n"
" let x: size = 0;\n"
" if (x != 0) { return 1; };\n"
" let y: size = 42;\n"
" if (y != 42) { return 2; };\n"
" if (size(int) != 8) { return 3; };\n"
" if (size(i32) != 4) { return 4; };\n"
" return 0;\n"
"};\n", 0 },
/* size arithmetic + i32 cast value propagation: a size-typed sum
* truncates to i32 and propagates (40 + 2 == 42). */
{ "package main;\n"
"export fn main() i32 = {\n"
" let x: size = 40;\n"
" let y: size = 2;\n"
" return (x + y): i32;\n"
"};\n", 42 },
/* size as a struct field type: 8-byte slot, written and read back. */
{ "package main;\n"
"type box = struct { n: size };\n"
"export fn main() i32 = {\n"
" let b: box = box{ n = 7 };\n"
" return b.n: i32;\n"
"};\n", 7 },
/* the size(T) operator value-propagated through an i32 cast (operator
* path, must keep working alongside the new type-name arm). */
{ "package main;\n"
"export fn main() i32 = {\n"
" return size(int): i32;\n"
"};\n", 8 },
{ NULL, 0 }
};
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char absbin[1024];
if (bin[0] != '/') {
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwsz_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwsz_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
char cmd[2048];
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build -I %s/lib %s",
tmpdir, bin, cwd, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row %d: build failed\n src: %s\n",
i, rows[i].src);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char outbin[128];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row %d: exit %d, want %d\n src: %s\n",
i, got, rows[i].want_exit, rows[i].src);
fail++;
}
unlink(src); unlink(outbin); rmdir(tmpdir);
}
if (fail) {
fprintf(stderr, "%d/%d size_type tests failed\n", fail, n);
return 1;
}
printf("size_type: %d/%d ok\n", n, n);
return 0;
}