Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

zig translate-c fails to translate compound literals like &(float){0.5} #75

Open

Description

@Yeaseen

Zig Version

0.14.0-dev.2064+b5cafe223

Steps to Reproduce and Observed Behavior

void main() {
    float * p = &(float){0.5};
}

Expected Behavior

translate-c fails to process the compound literal &(float){0.5}, which is a standard C feature allowing in-line temporary variables. Zig translate-c does not generate an equivalent expression in Zig.

The translated Zig code should ideally create a temporary variable for the literal and then assign its address to p.

pub export fn main() void {
    var temp: f32 = @as(f32, @floatCast(0.5));
    _ = &temp;
    var x: [*c]f32 = &temp;
    _ = &x;
}

Activity

  1. mlugg commented on Oct 30, 2024

    @mlugg
    Member

    translate-c fails to translate and demotes the function to extern:

    // test.c:2:25: warning: unsupported initlist type: 'Builtin'
    
    // test.c:1:6: warning: unable to translate function, demoted to extern
    pub extern fn foo() void;
  2. rohlem commented on Oct 30, 2024

    @rohlem

    See https://en.cppreference.com/w/c/language/compound_literal for details - note that afaiu the value is not bound the current sub-expression / statement (the closest meaning of "temporary"), but instead to the current scope/block.
    Even if used in an expression (f.e. a function call), it has to translate to a stack location with lifetime until the end of the current block.

  3. Yeaseen commented on Oct 30, 2024

    @Yeaseen
    Author

    Thank you for the explanation! @rohlem That makes sense regarding the scope and the lifetime of the temporary variable. Just to clarify my main concern, translate-c doesn’t directly translate the compound literal &(float){0.5} in a way that maintains its temporary nature within the current scope or block.

    To be more specific, C compound literals like &(float){0.5} create a temporary value with an addressable location that exists until the end of the current block, and it would be ideal if translate-c could mimic this behavior by using a stack-allocated temporary variable. For example, generating something like var temp: f32 = 0.5; in Zig and then assigning p to &temp would produce the expected result and ensure that the temporary exists for the correct scope.

  4. transferred this issue fromziglang/zigon Sep 26, 2025
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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions