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

Casting bool to pointer type results in invalid zig #22964

Open
Yeaseen opened this issue Feb 21, 2025 · 6 comments
Open

Casting bool to pointer type results in invalid zig #22964

Yeaseen opened this issue Feb 21, 2025 · 6 comments
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)

Comments

@Yeaseen
Copy link

Yeaseen commented Feb 21, 2025

Zig Version

0.14.0-dev.3271+bd237bced

Steps to Reproduce and Observed Behavior

Source C code:

#include <stdbool.h>
void func(bool b) {
    void* x = (void*)b;
}

translate-c fails to translate casting of bool to pointer

Translated Zig Code:

pub export fn func(arg_b: bool) void {
    var b = arg_b;
    _ = &b;
    var x: ?*anyopaque = @as(?*anyopaque, @intFromBool(b));
    _ = &x;
}

Zig build error:

runner.zig:60:43: error: expected type '?*anyopaque', found 'u1'
    var x: ?*anyopaque = @as(?*anyopaque, @intFromBool(b));

Expected Behavior

zig translate-c should correctly handle the cast from bool (u1) to void* (?*anyopaque) in a safe and Zig-compliant way.

@Yeaseen Yeaseen added the bug Observed behavior contradicts documented or intended behavior label Feb 21, 2025
@Vexu Vexu added the translate-c C to Zig source translation feature (@cImport) label Feb 21, 2025
@chrboesch
Copy link
Contributor

I think your cast is not correct, because @intFromBool gives you an integer representation of the value, not the address. Try this:

pub export fn func(arg_b: bool) void {
    var b = arg_b;
    var x = @as(?*anyopaque, @ptrCast(&b));
    _ = &x;
}

@Yeaseen
Copy link
Author

Yeaseen commented Feb 21, 2025

The translated incorrect zig code I provided here was produced by zig translate-c

@leecannon
Copy link
Contributor

The original c code is casting the value of the bool to a pointer so the equivalent zig code would be const x: *anyopaque = @ptrFromInt(@intFromBool(b));

@chrboesch
Copy link
Contributor

Ok, got it. Even if the C example doesn't make sense. ;-)

@chrboesch
Copy link
Contributor

So, this is then the correct translation of the C example:

pub export fn func(arg_b: bool) void {
    var b = arg_b;
    _ = &b;
    var x: ?*anyopaque = @as(?*anyopaque, @ptrFromInt(@intFromBool(b)));
    _ = &x;
}

There is a missing @ptrFromInt, then we get address 00000000 or 00000001.

@Yeaseen
Copy link
Author

Yeaseen commented Feb 21, 2025

Ok, got it. Even if the C example doesn't make sense. ;-)

True. However, edge cases are lovely to explore.

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

No branches or pull requests

4 participants