Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@cryptocode

Description

@cryptocode

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviortranslate-cC to Zig source translation feature (@cImport)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions