-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
NULL isn't coerced to optional function type in translated c header files #5596
Comments
This is quite tricky to fix since One reasonable solution would be to allow casts from |
Is it worth me having a go at fixing this as a first contribution or should I find something simpler first? |
I definitely recommend something simpler. Issues labeled contributor friendly are good choices. |
I think we could also get away with just translating |
So I've spent some time digging around the codebase and testing various stuff. Interestingly the error I get compiling with the master branch is different to with 0.6.0, although I think it still seems straightforward to fix. I've also found a pretty minimal example of the error:
Then compile with
I think the way to fix this is to check if the type is a pointer or an optional pointer instead of only checking if it is a pointer, does that sound ok? |
That's a recent regression from when we started translating Thinking about it now, we could simplify |
I can add a function |
Implementing the |
I implemented I thought this could be fixed by checking if the int being cast is comptime 0 and target type is
which works only when it's local scoped and I'm not sure why. It needs The only other option I can think of is to check the c AST specifically for |
I've done a bit more experimenting, for some reason
is fine if it's scoped but
as a global variable errors with Why does it make a difference whether the variable is global, I'd understand if it always worked or didn't, but a local variable can be null while a global can't? Maybe another option is to allow coercion from |
I've added type coercion from I've gone back to the cast function and I think it needs to be more complicated. |
Can we get some example C code added to this issue that the test case wanted to be translated? |
This also happens with Lua's Reduced test case: #include <stddef.h>
typedef void (*function)();
extern void foo(function);
#define bar() foo(NULL) After pub const NULL = @import("std").zig.c_translation.cast(?*c_void, @as(c_int, 0));
pub const function = ?fn (...) callconv(.C) void;
pub extern fn foo(function) void;
pub inline fn bar() @TypeOf(foo(NULL)) {
return foo(NULL);
} Error:
|
The problem might be more general than this, but when importing gtk and using g_signal_connect I get the error:
With the issue seeming to be that it can't coerce NULL to the optional function because it's already given NULL the type of
?*c_void
If this is a bug that needs a not crazy complicated fix, I can try making a pull request for it.
The text was updated successfully, but these errors were encountered: