You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 26, 2025. It is now read-only.
Repository navigation
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
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.
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.
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.
Activity
mlugg commentedon Oct 30, 2024
translate-c fails to translate and demotes the function to extern:
rohlem commentedon Oct 30, 2024
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.
Yeaseen commentedon Oct 30, 2024
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-cdoesn’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 iftranslate-ccould mimic this behavior by using a stack-allocated temporary variable. For example, generating something likevar temp: f32 = 0.5;in Zig and then assigningpto&tempwould produce the expected result and ensure that the temporary exists for the correct scope.