zig icon indicating copy to clipboard operation
zig copied to clipboard

Translate-c generates invalid Zig (pointless discard of local constant)

Open sigod opened this issue 1 year ago • 3 comments

Zig Version

0.10.0-dev.4437+1e963053d

Steps to Reproduce

Try to compile stb_image v2.27 library:

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

pub fn main() void {
    const png = [_]u8 {};
    const png_len = @intCast(c_int, png.len);
    var c_width: c_int = undefined;
    var c_height: c_int = undefined;
    _ = c.stbi_info_from_memory(png.ptr, png_len, &c_width, &c_height, null);
}

Expected Behavior

Compile successfully.

Actual Behavior

Compilation fails with

[snip]\cimport.zig:3691:66: error: pointless discard of local constant
                                                    break :blk_4 tmp;
                                                                 ^~~
[snip]\cimport.zig:3690:60: note: used here
                                                    }).* = tmp;
                                                           ^~~
[6 more errors]

[snip]\src\main.zig:1:11: error: C import failed: AnalysisFail
pub usingnamespace @cImport({
                   ^~~~~~~~

sigod avatar Oct 18 '22 01:10 sigod

what was the rest of your test code?

nektro avatar Oct 18 '22 03:10 nektro

I've updated the usage example.

sigod avatar Oct 18 '22 15:10 sigod

Reduction:

test {
    var b: u32 = 1;
    _ = blk: {
        const a = 1;
        b = a;
        break :blk a;
    };
}

Vexu avatar Oct 18 '22 15:10 Vexu

ohhhh same error happened but it works on stage1

ryoppippi avatar Nov 09 '22 16:11 ryoppippi