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 translator failure with void typedef #10356

Closed
presentfactory opened this issue Dec 18, 2021 · 9 comments · Fixed by #10564
Closed

C translator failure with void typedef #10356

presentfactory opened this issue Dec 18, 2021 · 9 comments · Fixed by #10564
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@presentfactory
Copy link

Zig Version

0.9.0-dev.1695+c6b4fe006

Steps to Reproduce

Add the following to two files:
main.zig:

const c = @cImport({
    @cInclude("test.h");
});

pub fn main() !void {
    c.foo(null);
}

test.h:

typedef void V;

V foo(V *) {}

Attempt to compile with zig build (just used the init-exe build script with link libc linked and a include directory set to get it to find the .h file).

Expected Behavior

The program should compile.

Actual Behavior

A compilation failure occurs with the error:

error: Opaque return type 'c_void' not allowed
pub extern fn foo(?*V) V;
                       ^
./main.zig:6:6: note: referenced here
    c.foo(null);

This is likely due to the fact that in C the singular typedef is being used for what was split into 2 things in Zig (void and c_void) so it cannot possibly compile correctly in this case if it uses the alias V directly. Note some code does actually do this, like this project for whatever reason.

@presentfactory presentfactory added the bug Observed behavior contradicts documented or intended behavior label Dec 18, 2021
@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Dec 19, 2021
@andrewrk andrewrk added this to the 0.11.0 milestone Dec 19, 2021
ehaas added a commit to ehaas/zig that referenced this issue Jan 10, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.9.1 Jan 11, 2022
@Juhlinus
Copy link

zig build test-run-translated-c test-translate-c on a fresh master pull fails with the following:

/Users/linus/.cache/zig/o/97a17fb16820da027a256a972d0fa328/source.zig:52:31: error: Opaque return type 'anyopaque' not allowed
pub extern fn foo(arg_f: ?*V) V;
                              ^
/Users/linus/.cache/zig/o/97a17fb16820da027a256a972d0fa328/source.zig:55:5: note: referenced here
    foo(@ptrCast(?*V, &x));
    ^
run-translated-c Typedef'ed void used as return type. Issue #10356 build-exe...The following command exited with error code 1:
/opt/homebrew/Cellar/zig/HEAD-85d4c86_1/bin/zig build-exe /Users/linus/.cache/zig/o/97a17fb16820da027a256a972d0fa328/source.zig -lc --cache-dir /Users/linus/Projects/zig/zig/zig-cache --global-cache-dir /Users/linus/.cache/zig --name translated_c --enable-cache 
error: the following build command failed with exit code 1:
/Users/linus/Projects/zig/zig/zig-cache/o/4126aa5dcb05a7a3ce213bcab7f6f3d8/build /opt/homebrew/Cellar/zig/HEAD-85d4c86_1/bin/zig /Users/linus/Projects/zig/zig /Users/linus/Projects/zig/zig/zig-cache /Users/linus/.cache/zig test-run-translated-c test-translate-c

@ehaas @andrewrk

@ehaas
Copy link
Sponsor Contributor

ehaas commented Jan 14, 2022

Just to double check - did you rebuild after pulling? zig build test-run-translated-c test-translate-c won't actually rebuild the compiler, so it's possible you ran the new test with an old build. I just did a clean build and don't get that error. Otherwise, maybe a problem with the Zig cache? Next step might be to delete that and re-run the tests.

@Juhlinus
Copy link

Just to double check - did you rebuild after pulling? zig build test-run-translated-c test-translate-c won't actually rebuild the compiler, so it's possible you ran the new test with an old build. I just did a clean build and don't get that error. Otherwise, maybe a problem with the Zig cache? Next step might be to delete that and re-run the tests.

This was a fresh clone of the repo today. So it shouldn’t have anything to do with cache or the compiler as far as I’m aware.

@ehaas
Copy link
Sponsor Contributor

ehaas commented Jan 14, 2022

What system are you building it on (OS, CPU, etc)?

@Juhlinus
Copy link

OSX, M1. The Air model if that’s relevant.

@ehaas
Copy link
Sponsor Contributor

ehaas commented Jan 14, 2022

Ok, unfortunately I don't have access to an M1 mac. The only other thing I can think of is that if Zig was previously installed in some other way, an old version might be in your path and it's using that instead of the freshly built one. If that's not the case I think we'd have to find someone with an M1 mac to see if it happens for them. I can't imagine why it would only happen there though.

@Juhlinus
Copy link

I’ll double check this and get back to you.

@andrewrk
Copy link
Member

I just ran zig build test-translate-c test-run-translated-c on an M1 and it passed. Also the CI system does that, and master branch is passing the CI.

@Juhlinus
Copy link

Yeah, sorry that’s my bad, my paths were messed up. Runs fine for me now.

scorphus pushed a commit to scorphus/zig that referenced this issue Jan 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants