Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
torque opened this issue Sep 18, 2024 · 0 comments
Open
Labels
backend-c The C backend (CBE) outputs C source code. bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@torque
Copy link
Contributor

torque commented Sep 18, 2024

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.

@torque torque added the bug Observed behavior contradicts documented or intended behavior label Sep 18, 2024
@torque torque changed the title C backend: pointer to array of value does not get emitted properly C backend: pointer to array of non-simple value does not get emitted properly Sep 18, 2024
@Vexu Vexu added the backend-c The C backend (CBE) outputs C source code. label Sep 18, 2024
@Vexu Vexu added this to the 0.14.0 milestone Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-c The C backend (CBE) outputs C source code. bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants