zig icon indicating copy to clipboard operation
zig copied to clipboard

cImport does not normalize early return

Open shyim opened this issue 2 months ago • 0 comments

Zig Version

0.13.0-dev.3+dddddcffd

Steps to Reproduce and Observed Behavior

Import using cImport this PHP header file: https://github.com/php/php-src/blob/b3700e29ccea783707bd269e659c9777b4717cf7/Zend/zend_call_stack.h#L71-L100

The generated code will be:

pub fn zend_call_stack_default_size() callconv(.C) usize {
    return @as(usize, @bitCast(@as(c_long, (@as(c_int, 8) * @as(c_int, 1024)) * @as(c_int, 1024))));
    return @as(usize, @bitCast(@as(c_long, (@as(c_int, 2) * @as(c_int, 1024)) * @as(c_int, 1024))));
}

So it uses the Linux line, and the fallback if nothing matches and then produces this error:

/home/shyim/Developer/zig-play/zig-cache/o/cbbd911349adaf63af234b82d73aebae/cimport.zig:29125:5: error: unreachable code
    return @as(usize, @bitCast(@as(c_long, (@as(c_int, 2) * @as(c_int, 1024)) * @as(c_int, 1024))));
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/shyim/Developer/zig-play/zig-cache/o/cbbd911349adaf63af234b82d73aebae/cimport.zig:29124:5: note: control flow is diverted here
    return @as(usize, @bitCast(@as(c_long, (@as(c_int, 8) * @as(c_int, 1024)) * @as(c_int, 1024))));
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

Use the first early return and ignore the other one.

shyim avatar Apr 20 '24 22:04 shyim