Skip to content

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

Open
@Rokannon

Description

@Rokannon

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.

Activity

added
bugObserved behavior contradicts documented or intended behavior
on May 27, 2025
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[/+] on May 27, 2025
added
translate-cC to Zig source translation feature (@cImport)
on May 27, 2025
added this to the 0.15.0 milestone on May 27, 2025
added a commit that references this issue on May 30, 2025
cef8d7b
added a commit that references this issue on May 30, 2025
3787124
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Relationships

    None yet

      Participants

      @alexrp@Rokannon

      Issue actions

        Local variables naming collision when using translate-c with __builtin_convertvector · Issue #23999 · ziglang/zig