ziglang / zig

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

stb_image.h does not compile #17495

Open teoet6 opened 9 months ago

teoet6 commented 9 months ago

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

const c = @cImport({
    @cDefine("STB_IMAGE_IMPLEMENTATION", {});
    @cInclude("stb_image.h");
});

pub fn main() !void {
    var x = @as(i32, undefined);
    var y = @as(i32, undefined);
    const data = c.stbi_load("test.png", &x, &y, null, 3);
    defer c.stbi_image_free(data);
}
error: expected type '[4]u8', found 'u8'
            }).*[@as(c_uint, @intCast(@as(c_int, 2)))] = stbi__get8(s);
                                                         ~~~~~~~~~~^~~

error: cast increases pointer alignment
            var y_bytes: __m128i = _mm_loadl_epi64(@as([*c]__m128i, @ptrCast(@volatileCast(@constCast(y + @as(usize, @bitCast(@as(isize, @intCast(i)))))))));
                                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

note: '[*c]const u8' has alignment '1'
            var y_bytes: __m128i = _mm_loadl_epi64(@as([*c]__m128i, @ptrCast(@volatileCast(@constCast(y + @as(usize, @bitCast(@as(isize, @intCast(i)))))))));
                                                                                                      ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

The code to compile.

Vexu commented 9 months ago

See https://github.com/ziglang/zig/issues/17302#issuecomment-1737417445 for a workaround.