Files
ww/test/lang/opaque_decl_test.ww
Hojun-Cho 47c7dbc2c8 test: migrate Fam12 opaque value tests to @test row-tables (#5-C1)
fold-2 chunk C1 (drew's Fam8-13 plan): 960_opaque_decl_run.c and
962_opaque_assign_cast_run.c were value-row C drivers. Migrate their 3+3
cases to test/lang/opaque_decl_test.ww and opaque_assign_cast_test.ww as
@test row-tables. The test-lang byte-id (LANGBYTEID) gate gives cs==ww
automatically -- 960 was cstage-only-run before, so this strengthens it.
Both byte-id clean, no cs!=ww carve. Retire the 2 C drivers (LANGBYTEID floor
57->59) and repoint a dead comment ref in lib/sort/sort.ww. 961_opaque_guards
(reject/guards) stays in C -- fold-3 territory, not in the Fam12 fold-2
worklist.
2026-06-24 01:00:47 +09:00

59 lines
2.4 KiB
Plaintext

// opaque_decl_test — runtime proof that the abstract `opaque` type (#108
// sub-fold a) EXISTS and is usable behind indirection: `*opaque` is a usable
// 8-byte pointer and `[]opaque` is a usable 24-byte slice header (.len/.ptr).
// Migrated from test/wcc/960_opaque_decl_run.c. The name resolves via the
// type-name resolver (lookup_builtin / tinfofornode's N_TNAME chain),
// mirroring uintptr/size; `opaque` itself is unsized (size=align=
// SIZE_UNDEFINED), so it is only ever materialised through a pointer or slice,
// never as a bare value. A green @test IS the proof the name binds: without the
// resolver arm every `*opaque` / `[]opaque` use fails to compile ("unknown type
// 'opaque'").
//
// This fold (a) deliberately does NOT test bare `let x: opaque`, `size(opaque)`,
// an `opaque` (bare) struct field, `[N]opaque`, or `[]opaque` INDEXING: those
// are the use-restriction GUARDS of sub-fold (b) (test/wcc/961_opaque_guards.c,
// reject rows — out of this fold-2 chunk). Every row here stays behind
// indirection.
package opaque_decl_test;
type handle = struct { p: *opaque, tag: i32 };
@test fn opaque_ptr_roundtrip() void = {
// `*opaque` is a usable 8-byte pointer: round-trip a real *i32 through
// *opaque and back, then deref. Proves the type binds and carries pointer
// width.
let n: i32 = 42;
let pn: *i32 = &n;
let po: *opaque = pn: *opaque;
let back: *i32 = po: *i32;
assert(*back == 42);
};
@test fn opaque_slice_header() void = {
// `[]opaque` is a usable 24-byte slice header: reinterpret a real []i32 as
// []opaque, read .len, and round-trip .ptr (a *opaque) back to *i32 + deref.
// The []opaque is never indexed (guard b). The .ptr deref is bound to a var
// before the assert (test/lang idiom; the inline `*so.ptr`-in-comparison
// form the C original avoided now compiles correct + cs==ww on HEAD).
let a: [4]i32;
a[0] = 11; a[1] = 22; a[2] = 33; a[3] = 44;
let s: []i32 = a[0:4];
let so: []opaque = s: []opaque;
let n: i32 = so.len: i32;
assert(n == 4);
let pp: *opaque = so.ptr;
let pi: *i32 = pp: *i32;
let v: i32 = *pi;
assert(v == 11);
};
@test fn opaque_struct_field() void = {
// `*opaque` as a struct field (behind indirection): set from a cast pointer,
// read back, deref + add a sibling scalar field.
let n: i32 = 99;
let h: handle = handle{ p = (&n): *opaque, tag = 7 };
let pi: *i32 = h.p: *i32;
assert(*pi + h.tag == 106);
};