59 lines
2.4 KiB
Plaintext
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/tool/rejects_test.ww
|
|
// opaque_guards 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);
|
|
};
|