ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.83k stars 2.47k forks source link

C backend: pointer to array of non-simple value does not get emitted properly #21439

Open torque opened 3 days ago

torque commented 3 days ago

Zig Version

0.14.0-dev.1583+812557bfd

Steps to Reproduce and Observed Behavior

pub fn main() void {
    var t: *[4]struct { something: u8 } = undefined;
    _ = &t;
}
zig build-exe -ofmt=c test.zig

The output C file has something like this:

struct test_main__struct_1841__1841; /* test.main__struct_1841 */

static void test_main__225(void) {
 struct test_main__struct_1841__1841 (*t0)[4];
 /* file:2:5 */
 memset(&t0, 0xaa, sizeof(struct test_main__struct_1841__1841 (*)[4]));
 /* dbg_var_ptr:t */
 /* file:3:5 */
 return;
}

which causes the various C compilers I've tried (clang and gcc) to emit this error when attempting to compile the output:

test.c:6695:43: error: array has incomplete element type 'struct test_main__struct_1841__1841'
 struct test_main__struct_1841__1841 (*t0)[4];
                                          ^
test.c:60:8: note: forward declaration of 'struct test_main__struct_1841__1841'
struct test_main__struct_1841__1841; /* test.main__struct_1841 */
       ^

Even though the value itself is a pointer, it seems the array declaration requires the element type to be fully-defined beforehand.

This behavior exists back to at least zig 0.11.0.

Expected Behavior

The emitted C code should contain the correctly resolved types.