check: gate C-style ... to bodiless decls, both stages (#11)

A bodied fn with a bare C-style `...` was silently accepted by cstage
and SEGFAULTED wwstage (resolvefnbody walked a typeless `...` param).
Gate it: bare C-`...` is allowed only on bodiless decls (extern /
@symbol prototypes), the real FFI path; Hare-style `T...` is unaffected.

ww restricts C-`...` to bodiless decls pending vastart/vaarg/vaend
builtins (#16); harec permits bodied C-variadic fns (check.c:3656) -- a
documented divergence, reopened when #16 lands.

Test 852 runs both stages; its reject rows require the gate's diagnostic
(not merely a nonzero exit), so a crash can't pass them vacuously.
This commit is contained in:
2026-06-21 12:14:31 +09:00
parent c814856550
commit 1f1efb273a
4 changed files with 263 additions and 0 deletions

View File

@@ -0,0 +1,225 @@
/*
* 852_variadic_body_reject (C3) — cstage and wwstage LOUD-REJECT a
* BODIED function whose param list ends in a bare C-style `...`, while
* still ACCEPTING the bodiless extern/@symbol prototype (the real FFI
* path) and the bodied Hare-style `T...` typed variadic.
*
* DIVERGENCE (rule 7/8): harec places NO bodiless restriction on C
* variadism — a bodied C-variadic fn is legal in Hare because that is
* where vastart/vaarg/vaend live (ref/harec/src/check.c:3656). ww ships
* no va* builtins (task #16), so a bodied bare-`...` body could never
* read its varargs — the construct is inexpressible. Gating it to
* bodiless decls is therefore a sound ww-pragmatic restriction, NOT
* "what Hare does"; when #16's builtins land this gate reopens.
*
* Pre-fix: cstage silently ACCEPTED a bodied bare-`...` fn (cmd/wcc/
* check.c N_FNDECL pass-2 only guarded body==NULL); wwstage SEGFAULTED
* (resolvefnbody walked a `...` param with no type). The fix adds, at
* the N_FNDECL body-check site of each stage, a guard rejecting
* (body != NULL && C-style variadic). The bare C-style param is the one
* with str=="..."; Hare-style `T...` carries a name + op==TK_ELLIPSIS,
* so it is unaffected. Bodiless decls keep flowing the FFI path.
*
* row | shape | expect
* -------------+----------------------------------------+--------
* bodied_cvar | fn f(x: i64, ...) void = {...} | REJECT
* bodied_bare | fn f(...) void = {...} | REJECT
* extern_cvar | @symbol(..) fn ext(x: i64, ...) i64; | BUILD, run 7
* hare_var | fn sum(xs: i64...) i64 = {...} | BUILD, run 7
*/
#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;
}
static void
outbin(char *dst, size_t n, const char *tmpdir, const char *src)
{
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(dst, n, "%s/%s", tmpdir, base);
char *dot = strrchr(dst, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
}
/*
* The reject must be the gate's clean diagnostic, not any nonzero exit:
* the wwstage gate's absence reverts to a w6c SEGFAULT, which the driver
* also wraps as a nonzero "w6c failed". Matching this shared diagnostic
* substring distinguishes a loud reject from a crash, so the wwstage
* reject rows are non-vacuous.
*/
static const char rejmsg[] =
"C-style variadic '...' requires a bodiless declaration";
static int
filehas(const char *path, const char *needle)
{
char buf[8192];
FILE *f = fopen(path, "rb");
if (!f) return 0;
size_t n = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[n] = '\0';
return strstr(buf, needle) != NULL;
}
static int
build_should_fail(const char *driver, const char *label, const char *src,
int i)
{
char s[64], tmpdir[64], cmd[1024], bin[128], errf[64];
snprintf(s, sizeof s, "/tmp/vbr_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/vbr_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/vbr_%d_e_%d.txt", getpid(), i);
FILE *f = fopen(s, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s >/dev/null 2>%s",
tmpdir, driver, s, errf);
int rc = runwait(cmd);
int hasmsg = filehas(errf, rejmsg);
outbin(bin, sizeof bin, tmpdir, s);
unlink(s);
unlink(bin);
unlink(errf);
rmdir(tmpdir);
if (rc == 0) {
fprintf(stderr, "vbr[%s][%s]: built ok, expected a reject\n",
driver, label);
return -1;
}
if (!hasmsg) {
fprintf(stderr, "vbr[%s][%s]: nonzero exit but missing gate "
"diagnostic (a crash, not a clean reject)\n", driver, label);
return -1;
}
return 0;
}
static int
run_build(const char *driver, const char *label, const char *src, int i)
{
char s[64], tmpdir[64], cmd[1024], bin[128];
snprintf(s, sizeof s, "/tmp/vbr_ok_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/vbr_ok_%d_d_%d", getpid(), i);
FILE *f = fopen(s, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, s);
if (runwait(cmd) != 0) {
fprintf(stderr, "vbr[%s][%s]: build failed (valid variadic "
"must compile)\n", driver, label);
unlink(s); rmdir(tmpdir);
return -1;
}
outbin(bin, sizeof bin, tmpdir, s);
int got = runwait(bin);
unlink(s); unlink(bin); rmdir(tmpdir);
return got;
}
struct rejrow { const char *label; const char *src; };
static const struct rejrow reject_rows[] = {
{ "bodied_cvar",
"package main;\n"
"fn f(x: i64, ...) void = { return; };\n"
"fn main() i32 = { return 0; };\n" },
{ "bodied_bare",
"package main;\n"
"fn f(...) void = { return; };\n"
"fn main() i32 = { return 0; };\n" },
};
struct okrow { const char *label; const char *src; int want; };
static const struct okrow ok_rows[] = {
{ "extern_cvar",
"package main;\n"
"@symbol(\"extfix\") fn extfix(x: i64, ...) i64;\n"
"fn main() i32 = { return 7; };\n",
7 },
{ "hare_var",
"package main;\n"
"fn sum(xs: i64...) i64 = { return 0; };\n"
"fn main() i32 = { return 7; };\n",
7 },
};
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);
struct { const char *name; const char *path; int gated; } drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int nrej = (int)(sizeof reject_rows / sizeof reject_rows[0]);
int nok = (int)(sizeof ok_rows / sizeof ok_rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "vbr: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < nrej; i++) {
total++;
if (build_should_fail(drivers[d].path,
reject_rows[i].label, reject_rows[i].src,
d * 100 + i) != 0)
fail++;
}
for (int i = 0; i < nok; i++) {
total++;
int got = run_build(drivers[d].path, ok_rows[i].label,
ok_rows[i].src, d * 100 + i);
if (got != ok_rows[i].want) {
fprintf(stderr, "vbr[%s][%s]: exit=%d want=%d\n",
drivers[d].name, ok_rows[i].label,
got, ok_rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "vbr: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("vbr: %d/%d ok\n", total, total);
return 0;
}