-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 interop: expected type bool
found c_int
#19950
Comments
Thanks for the recatogrization, I wasn't sure if it was a bug or proposal. I don't know how the tags work, so maybe this is correct anyways, but I didn't invoke |
I've uncovered even weirder behaviour. In
To the top of the It doesn't work. Still defines Totally unsure what's going on now. Is mapping C |
Okay, it seems indeed that this is a general thing. any time you have const c = @cImport({
@cDefine("foo", "1");
@cUndef("foo");
@CDefine("foo", "2");
}); It's going to correctly apply the second I can't tell how much of this is intentional design vs. bugs. |
More minimal #define bool _Bool
#define true 1
#define false 0 The last 3 lines of pub const @"bool" = bool;
pub const @"true" = @as(c_int, 1);
pub const @"false" = @as(c_int, 0); |
I have a pretty janky workaround: @cImport({
@cDefine("true", "(_Bool)1");
@cDefine("false", "(_Bool)0");
@cInclude("stdbool.h");
@cUndef("true");
@cUndef("false");
@cDefine("true", "(_Bool)1");
@cDefine("false", "(_Bool)0");
// other @cInclude()s that use stdbool.h...
}); A couple of points of explanation:
|
It generates
Which is kind of goofy, but does work. |
Zig Version
0.13.0-dev.46+3648d7df1
Steps to Reproduce and Observed Behavior
Explanation below, but it's easier to see than explain. I've created a minimal reproduction so you can
The error I get
The issue is with
#include <stdbool.h>
from c-code, and then referencing values from zig.bool
gets mapped to zigbool
, buttrue
andfalse
get mapped toc_int
s, so a bool defined in c code can't be used as a bool when interfacing with that c code.In particular, the generated zig code looks like
Expected Behavior
It's tricky. I understand why
bool
would get mapped tobool
, and I understand why#define false 0
instdbool.h
would get translated to ac_int
. There's nothing "buggy" about either of those individually. But together they make interfacing with C code that makes use of bools kind of unworkable.Assigning a c-defined bool to a c-defined bool-type variable seems like it should just work.
The text was updated successfully, but these errors were encountered: