From e60297085dbac0de285bebdebbe2ff6ce3e15b3f Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 26 Jun 2026 23:16:19 +0900 Subject: [PATCH] wwstage: accept module-level const/let slice-from-arrlit (#28) wwstage rejected a module-level `const/let []T = [arrlit]` global with "let: not assignable"; cstage accepts (textbook Hare, ref/hare/path/stack.ha:30). The arrlit->slice admission in checkletassign was gated local-only; lift it to module scope too, aligning wwstage UP to cstage's arrlit_init_fits (check.c:3406-3409, slice arm 519-520). cstage unchanged. Two guards the un-gating requires: the n.rhs.lhs=arr stash stays local-only (a module decl keeps its raw N_ARRLIT for DATA emit, so stashing would leave an untyped count node for the pass-3 asserttyped walker); and tuple-element slice globals are excluded at module scope, because the synthesis delegates element checks to isassignable which lacks a strict tuple arm (#38) -- a [](str,*fn) table would over-accept a sig-mismatched &fn that cstage's strict type_assignable rejects (#124) -- so they stay on the existing typeeqast path. Closes two divergences 944_alias_emit_b7 pinned: Group A (cstage-runs/ww-rejects) migrates to test/lang/slice_global_arg_test.ww (promoted from _runonly, now cs==ww byte-id); Group B converges to a shared emit_slice_data reject with the identical diagnostic. --- selfhost/cmd/wcc/check.ww | 43 ++++++-- test/lang/slice_global_arg_runonly_test.ww | 52 --------- test/lang/slice_global_arg_test.ww | 116 +++++++++++++++++++++ test/wcc/944_alias_emit_b7_run.c | 53 ++++------ 4 files changed, 172 insertions(+), 92 deletions(-) delete mode 100644 test/lang/slice_global_arg_runonly_test.ww create mode 100644 test/lang/slice_global_arg_test.ww diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index e58ac82a..916e97d0 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -5975,16 +5975,39 @@ fn checkletassign(c: *checker, n: *syntax.node) void = { && n.rhs.kind == syntax.nkind.N_TUPLE) { checktuplearrfits(c, llhs, n.rhs); }; - // #25/#31: an array literal initialising a SLICE local. Re-stamp the + // #25/#31: an array literal initialising a SLICE binding. Re-stamp the // literal as [count]T (the slice element) so the #258 borrow's exact- // element typeeq holds and the cgen N_SLICE-over-N_ARRLIT arm reads the // declared element width. Run the same per-element coercion + range- // check the array path runs (checkarrlitfits against a synthesized - // [count]T), then drive isassignable + the borrow off [count]T. Twin of - // cstage arrlit_init_fits' slice arm. Local-only (c.cur != c.top): the - // borrow runs at runtime; module-level slice-from-arrlit stays #32. - if (c.cur != c.top && n.lhs.kind == syntax.nkind.N_TSLICE - && n.rhs.kind == syntax.nkind.N_ARRLIT) { + // [count]T), then drive isassignable off [count]T. Twin of cstage + // arrlit_init_fits' slice arm (cmd/wcc/check.c:519-520). #28/#32: this + // admission runs at BOTH scopes. The matching DATA-vs-borrow split is + // the desugar at the foot of this fn (gated c.cur != c.top): a LOCAL + // `let []u8 = [...]` lowers to a runtime arr[0:len] borrow, while a + // MODULE-level `const/let []u8 = [...]` keeps its raw N_ARRLIT for cgen + // to materialize as DATA (#18). cstage admits both — module-level via + // arrlit_init_fits in check_file pass-2 (check.c:3406-3409), which never + // desugars — so gating the admission local-only made wwstage REJECT + // valid Hare (`const dotdot: []u8 = ['.', '.'];` ref/hare/path/stack.ha:30). + // #28: a TUPLE-element slice global ([](str,*fn) tables) is EXCLUDED at + // module scope — the synthesis delegates per-element validation to + // checkarrlitfits, whose element check is isassignable, which has NO + // strict tuple arm (#38). cstage's arrlit_init_fits uses type_assignable + // (strict on tuples), so routing a tuple-element slice through the + // synthesis would over-accept a sig-mismatched `&fn` element that cstage + // rejects (#124 wrong_sig_table). Tuple-element slices instead stay on + // the natural whole-element typeeqast path below (isassignable's #258 + // array→slice arm), which IS strict and reaches cstage's same decision. + let elemtup: bool = false; + if (c.cur == c.top && n.lhs.lhs != nil) { + let etn: *syntax.node = resolvealias(c, unwrapbang(n.lhs.lhs)); + if (etn != nil && etn.kind == syntax.nkind.N_TTUPLE) { + elemtup = true; + }; + }; + if (n.lhs.kind == syntax.nkind.N_TSLICE + && n.rhs.kind == syntax.nkind.N_ARRLIT && !elemtup) { let cnt: u64 = 0u64; let e0: *syntax.node = n.rhs.list; for (e0 != nil) { @@ -6007,7 +6030,13 @@ fn checkletassign(c: *checker, n: *syntax.node) void = { // can size the backing NODE-wise via elemsizeofc(base.lhs). wwstage // narrow-primitive tinfos are unsized (i32/u8 .size==0, #8), so the // element width must come from the type NODE, not the tinfo. - n.rhs.lhs = arr; + // #28: LOCAL ONLY. The local desugar replaces n.rhs with the N_SLICE + // borrow, so this stash is consumed and then unreachable. A MODULE- + // level decl keeps its raw N_ARRLIT (DATA emit), and its emitslicedata + // sizes off the DECLARED slice tnode (d.lhs), never the stash — so the + // stash would only leave the synthesized, untyped count node (cn) on + // the live tree for the pass-3 asserttyped walker to trip on. + if (c.cur != c.top) { n.rhs.lhs = arr; }; src = arr; }; // #29: an un-suffixed rune literal narrowing into an integer let target diff --git a/test/lang/slice_global_arg_runonly_test.ww b/test/lang/slice_global_arg_runonly_test.ww deleted file mode 100644 index 86c94c35..00000000 --- a/test/lang/slice_global_arg_runonly_test.ww +++ /dev/null @@ -1,52 +0,0 @@ -// slice_global_arg_runonly_test — #148 (D2): a module-global slice passed BY -// VALUE as a slice arg must arrive with a real header, not garbage. Migrated -// from test/wcc/953_globalslice_arg_run.c. -// -// _runonly: CSTAGE-ONLY. wwstage's checker rejects a module-level `const []T` -// global ("let: not assignable", filed task #28), so this file does not compile -// under w6c_ww and is excluded from the T2 byte-id corpus. test-lang runs it -// through the cstage `ww test` only. -// -// Pre-fix the slice-ident call-arg fast path emitted BP-relative pushes for a -// global (localfind→0), reading saved-BP/RIP garbage instead of the global's -// header at name(SB). The fix mirrors the N_SLICE arm's isglobal dispatch. The -// callee reads .len AND a byte, so a wrong header is observable; u32_global -// pins the path is element-width-agnostic; direct_read + local_arg lock the -// in-place read and the off!=0 local arm against regression. - -package slice_global_arg_runonly_test; - -const dotdot: []u8 = ['.', '.']; -const dot: []u8 = ['.']; -const g: []u32 = [7u32, 8u32, 9u32]; -const d: []u32 = [11u32, 22u32]; - -fn seen_u8(bs: []u8) i32 = { - return bs.len: i32 * 1000 + bs[0]: i32; -}; - -fn seen_u32(xs: []u32) i32 = { - return xs.len: i32 * 100 + xs[0]: i32 + xs[2]: i32; -}; - -@test fn u8_dotdot() void = { - assert(seen_u8(dotdot) == 2046); -}; - -@test fn u8_dot() void = { - assert(seen_u8(dot) == 1046); -}; - -@test fn u32_global() void = { - assert(seen_u32(g) == 316); -}; - -@test fn direct_read() void = { - assert(d.len: i32 * 100 + d[0]: i32 + d[1]: i32 == 233); -}; - -@test fn u8_local_arg() void = { - let a: [2]u8 = ['.', '.']; - let s: []u8 = a[0:2]; - assert(seen_u8(s) == 2046); -}; diff --git a/test/lang/slice_global_arg_test.ww b/test/lang/slice_global_arg_test.ww new file mode 100644 index 00000000..03b233e1 --- /dev/null +++ b/test/lang/slice_global_arg_test.ww @@ -0,0 +1,116 @@ +// slice_global_arg_test — module-global slice/array constants, two folded +// concerns that share these globals: +// +// #148 (D2): a module-global slice passed BY VALUE as a slice arg must arrive +// with a real header, not garbage. Migrated from test/wcc/953_globalslice_arg_run.c. +// Pre-fix the slice-ident call-arg fast path emitted BP-relative pushes for a +// global (localfind→0), reading saved-BP/RIP garbage instead of the global's +// header at name(SB). The fix mirrors the N_SLICE arm's isglobal dispatch. +// +// #28: wwstage's checker REJECTED a module-level `const/let []T = [..]` +// ("let: not assignable") while cstage accepted — the slice-from-arrlit +// admission was gated local-only. const dotdot/dot/g/d below are the exact +// shape (ref/hare/path/stack.ha:30 `const dotdot: []u8 = ['.', '.'];`). This +// file was a `_runonly` (cstage-only) carrier until that fix; promoting it +// drops the suffix and re-arms the cs==ww byte-id net over the slice globals. +// +// The element values discriminate a real header from garbage; u32_global pins +// the path element-width-agnostic; the const/let/[_] table-driven readbacks pin +// the #28 admission across const, mutable let, and the inferred-array form. + +package slice_global_arg_test; + +type my64 = i64; +type my64b = my64; + +const dotdot: []u8 = ['.', '.']; +const dot: []u8 = ['.']; +const g: []u32 = [7u32, 8u32, 9u32]; +const d: []u32 = [11u32, 22u32]; + +// #28: a wider-element slice global, and a 2-level-alias element type — both +// converged from cstage-only (was test/wcc/944 Group A slc_plain_ctl / slc_2lvl, +// where wwstage rejected at the let checker) to cs==ww byte-id once the +// module-scope slice-from-arrlit admission landed. +let gi64: []i64 = [5, 6, 7]; +let galias: []my64b = [5, 6, 7]; + +// #28: a MUTABLE `let` slice global must be accepted identically to `const` +// (shared decl path; the pre-fix error said "let:" even for const). +let lu8: []u8 = [10u8, 20u8, 30u8]; + +// #28 subtlety 2: rune-lit elements coerce into u8 via the SAME per-element +// path; `[_]u8` is the inferred-length ARRAY form, not a slice. +const carr: [_]u8 = ['a', 'b']; + +fn seen_u8(bs: []u8) i32 = { + return bs.len: i32 * 1000 + bs[0]: i32; +}; + +fn seen_u32(xs: []u32) i32 = { + return xs.len: i32 * 100 + xs[0]: i32 + xs[2]: i32; +}; + +@test fn u8_dotdot() void = { + assert(seen_u8(dotdot) == 2046); +}; + +@test fn u8_dot() void = { + assert(seen_u8(dot) == 1046); +}; + +@test fn u32_global() void = { + assert(seen_u32(g) == 316); +}; + +@test fn direct_read() void = { + assert(d.len: i32 * 100 + d[0]: i32 + d[1]: i32 == 233); +}; + +@test fn u8_local_arg() void = { + let a: [2]u8 = ['.', '.']; + let s: []u8 = a[0:2]; + assert(seen_u8(s) == 2046); +}; + +// #28: const []u8 element readback, table-driven over the declared elements. +@test fn const_slice_elems() void = { + let want: [2]u8 = [46u8, 46u8]; + assert(dotdot.len == 2); + let i: i32 = 0; + for (i < dotdot.len: i32) { + assert(dotdot[i] == want[i]); + i += 1; + }; +}; + +// #28: mutable `let []u8` global — accept + element readback. +@test fn let_slice_elems() void = { + let want: [3]u8 = [10u8, 20u8, 30u8]; + assert(lu8.len == 3); + let i: i32 = 0; + for (i < lu8.len: i32) { + assert(lu8[i] == want[i]); + i += 1; + }; +}; + +// #28: const [_]u8 rune-lit array form — accept + element readback. +@test fn const_array_elems() void = { + let want: [2]u8 = [97u8, 98u8]; + assert(carr.len == 2); + let i: i32 = 0; + for (i < carr.len: i32) { + assert(carr[i] == want[i]); + i += 1; + }; +}; + +// #28: wider-element ([]i64) and 2-level-alias-element ([]my64b) slice globals +// (ex test/wcc/944 Group A) — accept + element readback. +@test fn wide_and_alias_slice() void = { + assert(gi64.len == 3); + assert(gi64[0] + gi64[1] + gi64[2] == 18); + assert(galias.len == 3); + assert(galias[0] + galias[1] + galias[2] == 18); +}; diff --git a/test/wcc/944_alias_emit_b7_run.c b/test/wcc/944_alias_emit_b7_run.c index 4aa17801..6900f625 100644 --- a/test/wcc/944_alias_emit_b7_run.c +++ b/test/wcc/944_alias_emit_b7_run.c @@ -4,19 +4,24 @@ * symmetric K_BUILDERR rejects (slcstr_plain_ctl, slcslc_2lvl) to * test/wcc/data/alias_{slcstr_plain,slcslc}/case.ww runww //ww:error. * - * What survives here are the 4 IRREDUCIBLE per-stage rows neither in-language - * surface can host (ken's emit_slice_data observation cells — load-bearing - * codegen pins, MUST NOT move): - * • slc_2lvl / slc_plain_ctl (Group A): cstage builds+RUNS exit 0; wwstage - * LOUD-REJECTS at the let checker ("let: not assignable"). cstage emits the - * []my64b / []i64 slice-literal global DATA and runs; the ww checker - * rejects every slice-literal global (the #120/#29-kin acceptance - * divergence, ken-d2-oracle; pre-slim #66-R2/#29). @test needs ww to - * build, //ww:error needs cs to fail — neither fits. - * • slcstr_2lvl / slctag_2lvl (Group B): BOTH reject but with DIFFERENT - * diagnostics (cs at emit_slice_data 3-way "static-init unsupported"; ww at - * the let checker "not assignable") — a held cs!=ww divergence; the two - * messages ARE the pin, so each stage's own substring is asserted. + * What survives here are the 2 IRREDUCIBLE both-stage emit-reject rows neither + * in-language surface can host (ken's emit_slice_data observation cells — + * load-bearing codegen pins, MUST NOT move): + * • slcstr_2lvl / slctag_2lvl: a slice-of-{str,tagged} literal GLOBAL is + * admitted by BOTH checkers and then LOUD-REJECTED at emit_slice_data + * ("slice-of-{str,slice,tagged} literal static-init unsupported", a rule-7 + * deferred #10 follow-up) in BOTH stages with the IDENTICAL message. @test + * needs a build, runww //ww:error drives one path — a dual-stage emit + * reject fits neither, so each stage's reject is asserted directly here. + * + * HISTORY (#28): Group A (slc_2lvl []my64b / slc_plain_ctl []i64, cstage-runs / + * wwstage-checker-rejects) and Group B's old wwstage HALF (the ww checker + * rejected EVERY slice-literal global at "let: not assignable") were both that + * pre-#28 divergence. Once the module-scope slice-from-arrlit admission landed + * (selfhost check.ww checkletassign), wwstage admits these globals at the + * checker exactly as cstage does: Group A converged to cs==ww build+run and + * migrated to test/lang/slice_global_arg_test.ww; Group B converged to the + * shared emit_slice_data reject below. */ #include #include @@ -105,24 +110,6 @@ run_row(const char *bin, const struct row *r, int i) } static const struct row rows[] = { - { "slc_2lvl", - "package main;\n" - "type my64 = i64;\n" - "type my64b = my64;\n" - "let G: []my64b = [5, 6, 7];\n" - "export fn main() i32 = {\n" - " if (G[0] + G[1] + G[2] != 18) { return 1; };\n" - " if (len(G) != 3) { return 2; };\n" - " return 0;\n" - "};\n", M_CSRUN_WWERR, NULL, "let: not assignable" }, - { "slc_plain_ctl", - "package main;\n" - "let G: []i64 = [5, 6, 7];\n" - "export fn main() i32 = {\n" - " if (G[0] + G[1] + G[2] != 18) { return 1; };\n" - " if (len(G) != 3) { return 2; };\n" - " return 0;\n" - "};\n", M_CSRUN_WWERR, NULL, "let: not assignable" }, { "slcstr_2lvl", "package main;\n" "type ms0 = str;\n" @@ -133,7 +120,7 @@ static const struct row rows[] = { " return 0;\n" "};\n", M_BOTHERR, "slice-of-{str,slice,tagged} literal static-init unsupported", - "let: not assignable" }, + "slice-of-{str,slice,tagged} literal static-init unsupported" }, { "slctag_2lvl", "package main;\n" "type u0 = (void | i64);\n" @@ -144,7 +131,7 @@ static const struct row rows[] = { " return 0;\n" "};\n", M_BOTHERR, "slice-of-{str,slice,tagged} literal static-init unsupported", - "let: not assignable" }, + "slice-of-{str,slice,tagged} literal static-init unsupported" }, }; int