Skip to content
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

translate-c generates invalid code when C input is coerce-negating bool expressions #10175

Closed
cryptocode opened this issue Nov 19, 2021 · 0 comments · Fixed by #10191
Closed
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@cryptocode
Copy link
Sponsor Contributor

cryptocode commented Nov 19, 2021

Zig Version

0.9.0-dev.1675+3d528161c

Steps to Reproduce

To reproduce, feed this to zig translate-c:

#include <stdio.h>
#include <limits.h>

int sign(int v) {
    return -(v < 0); 
}

void main() {
    int res = sign(-5);
    printf("Sign: %d\n", res);
}

Expected Behavior

I expected working C code for the sign function, maybe something like this:

pub export fn sign(arg_v: c_int) c_int {
    var v = arg_v;
    return -(@intCast(c_int, @boolToInt(v < @as(c_int, 0))));
}

Actual Behavior

zig translate-c currently generates this:

pub export fn sign(arg_v: c_int) c_int {
    var v = arg_v;
    return -(v < @as(c_int, 0));
}

which causes

`error: negation of type 'bool'`

when trying to compile the code.

@cryptocode cryptocode added the bug Observed behavior contradicts documented or intended behavior label Nov 19, 2021
@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Nov 20, 2021
@andrewrk andrewrk added this to the 0.10.0 milestone Nov 20, 2021
ehaas added a commit to ehaas/zig that referenced this issue Nov 21, 2021
@andrewrk andrewrk modified the milestones: 0.11.0, 0.9.0 Nov 21, 2021
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

Successfully merging a pull request may close this issue.

2 participants