zig icon indicating copy to clipboard operation
zig copied to clipboard

translate-c: destination type 'c_uint' has size 4 but source type 'u1' has size 1

Open sigod opened this issue 3 years ago • 3 comments

Zig Version

0.9.1 0.10.0-dev.3978+4fd4c733d 0.10.0-dev.4560+828735ac0

Steps to Reproduce

This code is produced when trying to compile miniaudio v0.11.10 library:

return @bitCast(ma_bool32, @boolToInt(ma_device_get_state(pDevice) != @bitCast(c_uint, ma_device_state_uninitialized)));

Reduction:

    var a: c_uint = 0;
    var b: c_int = 0;
    _ = @bitCast(c_uint, @boolToInt(a != @bitCast(c_uint, b)));

Expected Behavior

Compile successfully.

Actual Behavior

On 0.9.1:

.\src\main.zig:4:18: error: destination type 'c_uint' has size 4 but source type 'u1' has size 1
    _ = @bitCast(c_uint, @boolToInt(a != @bitCast(c_uint, b)));
                 ^

On 0.10.0-dev:

main.zig:4:26: error: @bitCast size mismatch: destination type 'c_uint' has 32 bits but source type 'u1' has 1 bits
    _ = @bitCast(c_uint, @boolToInt(a != @bitCast(c_uint, b)));
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sigod avatar Oct 24 '22 02:10 sigod

the @bitCast error is correct, translate-c should be using @intCast or @as in this scenario

nektro avatar Oct 24 '22 07:10 nektro

Can you produce a reduced C sample case for this?

IridescentRose avatar Oct 31 '22 17:10 IridescentRose

Something like this:

extern int bar(void)
{
    return 0;
}

extern unsigned int foo(void)
{
    return bar() == 1;
}
const c = @cImport({
    @cInclude("test.h");
});

pub fn main() void {
    _ = c.foo();
}

This code:

extern unsigned int foo(void)
{
    return 0 == 1;
}

Gives a different error:

error: unable to @bitCast from type 'comptime_int'
    return @bitCast(c_uint, @boolToInt(@as(c_int, 0) == @as(c_int, 1)));
                            ^

sigod avatar Oct 31 '22 22:10 sigod