Skip to content

translate-c incorrectly lowers (expr1, bool_expr) as bool, causing assignment to int to fail #23975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Yeaseen opened this issue May 24, 2025 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@Yeaseen
Copy link

Yeaseen commented May 24, 2025

Zig Version

0.14.0-dev.3271+bd237bced

Steps to Reproduce and Observed Behavior

C code to reproduce

int main() {
    int a = 1, b = 0, c;
    c = ((a = b), (1 && 1));
    return 0;
}

Translated Zig code:

pub export fn main() c_int {
    var a: c_int = 1;
    _ = &a;
    var b: c_int = 0;
    _ = &b;
    var c: c_int = undefined;
    _ = &c;
    var p: [*c]c_int = &a;
    _ = &p;
    var q: [*c]c_int = null;
    _ = &q;
    c = blk: {
        _ = blk_1: {
            const tmp = b;
            a = tmp;
            break :blk_1 tmp;
        };
        break :blk true and true;
    };
    return 0;
}

Zig build failure:

runner.zig:74:25: error: expected type 'c_int', found 'bool'
        break :blk true and true;
                   ~~~~~^~~~~~~~

Zig's translate-c incorrectly lowers comma expressions like (a = b, 1 && 1) into a boolean result, even when assigned to an int. This leads to a type mismatch error:

Expected Behavior

Expected Behavior

The translated Zig code should preserve C's semantics, where logical expressions like 1 && 1 evaluate to 1 (of type int). When used in a comma expression and assigned to an int, the result should be treated as an int, not a bool.

I will close if this is a duplicate.

@Yeaseen Yeaseen added the bug Observed behavior contradicts documented or intended behavior label May 24, 2025
@Vexu Vexu added the translate-c C to Zig source translation feature (@cImport) label May 24, 2025
@alexrp alexrp added this to the 0.15.0 milestone May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

No branches or pull requests

3 participants