Skip to content

translate-c generates invalid code if for loop increment contains parentheses around variable #7899

@vvoidu

Description

@vvoidu

I ran across this while trying to use the Nuklear library. The nk_utf_decode_byte function is incorrectly translated and creates a syntax error.

Here is a minimal reproducible example:

void foo(void) {
    int i = 0;
    int *x = &i;

    for (i = 0; i < 3; (i) += 1) {}
    for (*x = 0; *x < 3; ++(*x)) {}
    for (*x = 0; *x < 3; (*x) += 1) {}
}

which translate-c translates to

pub export fn foo() void {
    var i: c_int = 0;
    var x: [*c]c_int = &i;
    {
        i = 0;
        while (i < @as(c_int, 3)) ( i(i) += @as(c_int, 1)) {}
    }
    {
        x.?.* = 0;
        while (x.?.* < @as(c_int, 3)) ? .*(x.?.*) += 1) {}
    }
    {
        x.?.* = 0;
        while (x.?.* < @as(c_int, 3)) ? .*(x.?.*) += @as(c_int, 1)) {}
    }
    return 0;
}

I tested using versions 0.7.1 and 0.8.0-dev.1060+1ed8c54cd.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.translate-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