ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.73k stars 2.48k forks source link

error: @bitCast size mismatch: destination type 'usize' has 64 bits but source type 'c_long' has 32 bits #17427

Open leap0x7b opened 11 months ago

leap0x7b commented 11 months ago

Zig Version

0.12.0-dev.3+9c05810be

Steps to Reproduce and Observed Behavior

  1. git clone https://github.com/kora-org/headstart
  2. zig build -Darch=riscv64
  3. This error should pop up:

    zig build-exe headstart Debug riscv64-uefi-msvc: error: the following command failed with 1 compilation errors:
    C:\Users\User\AppData\Local\Microsoft\WinGet\Packages\zig.zig.nightly_Microsoft.Winget.Source_8wekyb3d8bbwe\zig-windows-x86_64-0.12.0-dev.3+9c05810be\zig.exe build-exe D:\Azzam\kora\headstart\src\main.zig -cflags -ffreestanding -nostdlib -mno-red-zone -- D:\Azzam\kora\headstart\external\flanterm\flanterm.c D:\Azzam\kora\headstart\external\flanterm\backends\fb.c --cache-dir D:\Azzam\kora\headstart\zig-cache --global-cache-dir C:\Users\User\AppData\Local\zig --name headstart -mcmodel medium -target riscv64-uefi-msvc -mcpu baseline_rv64 --mod build_options::D:\Azzam\kora\headstart\zig-cache\c\617d0a7ef579f0c5c8f720583a81a2fe\options.zig --deps build_options -I D:\Azzam\kora\headstart\external --listen=- 
    Build Summary: 1/6 steps succeeded; 1 failed (disable with --summary none)
    run transitive failure
    └─ run sh transitive failure
    └─ install transitive failure
      └─ install headstart transitive failure
         └─ zig build-exe headstart Debug riscv64-uefi-msvc 1 errors
    D:\Azzam\kora\headstart\zig-cache\o\ba881674b31c5b5c615cc912c8122d92\cimport.zig:207:175: error: @bitCast size mismatch: destination type 'usize' has 64 bits but source type 'c_long' has 32 bits
    return flanterm_fb_init(null, null, framebuffer, width, height, pitch, null, null, null, null, null, null, null, @as(?*anyopaque, @ptrFromInt(@as(c_int, 0))), @as(usize, @bitCast(@as(c_long, @as(c_int, 0)))), @as(usize, @bitCast(@as(c_long, @as(c_int, 0)))), @as(usize, @bitCast(@as(c_long, @as(c_int, 1)))), @as(usize, @bitCast(@as(c_long, @as(c_int, 1)))), @as(usize, @bitCast(@as(c_long, @as(c_int, 1)))), @as(usize, @bitCast(@as(c_long, @as(c_int, 0)))));
    
                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    referenced by:
    init: src\console\flanterm.zig:11:16
    main: src\main.zig:25:12
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Expected Behavior

It should compiles fine like in x86_64 and aarch64.

f-cozzocrea commented 8 months ago

I wasn't able to build the project. Could you provide a minimal reproduction of the issue?

nektro commented 8 months ago
test {
    @compileLog(@bitSizeOf(usize));
    @compileLog(@bitSizeOf(c_long));
}
❯ zig test test.zig -target riscv64-linux-gnu
test.zig:22:5: error: found compile log statement

Compile Log Output:
@as(comptime_int, 64)
@as(comptime_int, 64)
nektro commented 8 months ago
❯ zig test test.zig -target riscv64-uefi-msvc

Compile Log Output:
@as(comptime_int, 64)
@as(comptime_int, 32)
f-cozzocrea commented 8 months ago

Here's a test case reproduction. I used a short to reproduce the issue on more platforms, but long also works for me on x86_64-linux:

int main() {
    short s = 42;
    short* s_ptr = &s;
    s_ptr = (short*)s;
}

// run-translated-c