// A module-global aggregate (array of struct literals, a struct literal, and
// nested array/struct shapes) materializes its DECLARED field values into the
// global's static initializer, so reading the fields without any prior store
// returns the literal values — not zero.
// Regression (issue 0080): a global `[N]Struct` initialized with struct literals
// was emitted as `zeroinitializer`, silently dropping every field, because the
// constant-aggregate serializer had no struct-literal arm and collapsed the
// whole initializer to null. The fix threads the element/field type so struct
// and nested-array leaves serialize correctly; a genuinely non-constant
// initializer is now rejected loudly instead of silently zeroed.

#import "modules/std.sx";

Pair :: struct { a: i64; b: i64; }
WithArr :: struct { id: i64; xs: [3]i64; }

// global array of struct literals
pairs : [2]Pair = .[ .{ a = 1, b = 2 }, .{ a = 3, b = 4 } ];
// global struct literal
solo : Pair = .{ a = 7, b = 9 };
// global struct containing a fixed array (struct-with-array)
wa : WithArr = .{ id = 5, xs = .[ 11, 22, 33 ] };
// nested: global array of structs each containing an array
nested : [2]WithArr = .[ .{ id = 1, xs = .[ 1, 2, 3 ] }, .{ id = 2, xs = .[ 4, 5, 6 ] } ];

main :: () {
    // Read the declared initializer values back with NO prior store.
    print("pairs={},{} {},{}\n", pairs[0].a, pairs[0].b, pairs[1].a, pairs[1].b);
    print("solo={},{}\n", solo.a, solo.b);
    print("wa={} xs={},{},{}\n", wa.id, wa.xs[0], wa.xs[1], wa.xs[2]);
    print("nested0={} xs={},{},{}\n", nested[0].id, nested[0].xs[0], nested[0].xs[1], nested[0].xs[2]);
    print("nested1={} xs={},{},{}\n", nested[1].id, nested[1].xs[0], nested[1].xs[1], nested[1].xs[2]);

    // A store on top of the materialized initializer still works (live storage).
    pairs[0].a = 100;
    nested[1].xs[2] = 999;
    print("after-store={} {}\n", pairs[0].a, nested[1].xs[2]);

    if pairs[0].b == 2 and pairs[1].a == 3 and pairs[1].b == 4
        and solo.a == 7 and solo.b == 9
        and wa.id == 5 and wa.xs[0] == 11 and wa.xs[2] == 33
        and nested[0].id == 1 and nested[0].xs[0] == 1 and nested[0].xs[2] == 3
        and nested[1].id == 2 and nested[1].xs[0] == 4
        and pairs[0].a == 100 and nested[1].xs[2] == 999 {
        print("PASS\n");
    } else {
        print("FAIL: global aggregate literal initializer zeroed\n");
    }
}
