Skip to content

Local variables naming collision when using translate-c with __builtin_convertvector #23999

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
Rokannon opened this issue May 27, 2025 · 0 comments · May be fixed by #24029
Open

Local variables naming collision when using translate-c with __builtin_convertvector #23999

Rokannon opened this issue May 27, 2025 · 0 comments · May be fixed by #24029
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@Rokannon
Copy link

Zig Version

master

Steps to Reproduce and Observed Behavior

Create a lib.c file with following content:

typedef int i32x2 __attribute__((__vector_size__(8)));
typedef float f32x2 __attribute__((__vector_size__(8)));

f32x2 cast_function(i32x2 x) {
  return (f32x2) __builtin_convertvector((i32x2) { x[0], x[1] }, f32x2);
}

Run zig translate-c lib.c. The function will transform into the following zig code:

pub export fn cast_function(arg_x: i32x2) f32x2 {
    var x = arg_x;
    _ = &x;
    return blk: {
        const tmp = @as(f32, @floatFromInt((blk_1: {
            const tmp = x[@as(c_uint, @intCast(@as(c_int, 0)))];
            const tmp_2 = x[@as(c_uint, @intCast(@as(c_int, 1)))];
            break :blk_1 i32x2{
                tmp,
                tmp_2,
            };
        })[0]));
        const tmp_1 = @as(f32, @floatFromInt((blk_1: {
            const tmp = x[@as(c_uint, @intCast(@as(c_int, 0)))];
            const tmp_2 = x[@as(c_uint, @intCast(@as(c_int, 1)))];
            break :blk_1 i32x2{
                tmp,
                tmp_2,
            };
        })[1]));
        break :blk f32x2{
            tmp,
            tmp_1,
        };
    };
}

When trying to compile this code (either directly, or by just c-importing it into zig code) we get an error:

/.../cimport.zig:309:19: error: local constant 'tmp' shadows local constant from outer scope
            const tmp = x[@as(c_uint, @intCast(@as(c_int, 0)))];
                  ^~~
/.../cimport.zig:300:15: note: previous declaration here
        const tmp = @as(f32, @floatFromInt((blk_1: {

Expected Behavior

The C code should be translated without naming collisions. Apparently something is not fully working with how compiler generates unique temporary names.

For reference: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-convertvector

I am also a little bit surprised by another thing. Part const tmp = is mentioned three times in generated code. But the error is produce for collision between first and third mentions. Why then the second mention is not shadowing the first one.

Question: I wrote a simple test in test/cases/translate_c/ to reproduce the issue (with faulty zig code output). Should I create a pull request so once the issue is fixed we can alter the test and ensure that the output is good now?

Submitting a bug here to ensure this is not a known issue.

@Rokannon Rokannon added the bug Observed behavior contradicts documented or intended behavior label May 27, 2025
@Rokannon Rokannon changed the title Local variables naming collision when using traslate-c with __builtin_convertvector Local variables naming collision when using translate-c with __builtin_convertvector May 27, 2025
@alexrp alexrp added the translate-c C to Zig source translation feature (@cImport) label May 27, 2025
@alexrp alexrp added this to the 0.15.0 milestone May 27, 2025
Rokannon added a commit to Rokannon/zig that referenced this issue May 30, 2025
Rokannon added a commit to Rokannon/zig that referenced this issue May 30, 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
2 participants