Use mem.zeroes for empty union initializer list

This commit is contained in:
danielsan901998 2024-01-30 16:24:41 +01:00 committed by Veikka Tuominen
parent fc48bbdf90
commit d7a27bf803
3 changed files with 26 additions and 1 deletions

View File

@ -2505,6 +2505,11 @@ fn transInitListExprRecord(
var field_inits = std.ArrayList(ast.Payload.ContainerInit.Initializer).init(c.gpa);
defer field_inits.deinit();
if (init_count == 0) {
const source_loc = @as(*const clang.Expr, @ptrCast(expr)).getBeginLoc();
return transZeroInitExpr(c, scope, source_loc, ty);
}
var init_i: c_uint = 0;
var it = record_def.field_begin();
const end_it = record_def.field_end();

View File

@ -0,0 +1,20 @@
union U {
int x;
long y;
};
void foo(void) {
union U u = {};
}
// translate-c
// target=x86_64-linux
// c_frontend=clang
//
// pub const union_U = extern union {
// x: c_int,
// y: c_long,
// };
// pub export fn foo() void {
// var u: union_U = @import("std").mem.zeroes(union_U);
// _ = &u;
// }

View File

@ -11,7 +11,7 @@ static inline void foo() {
// pub const struct_empty_struct = extern struct {};
// pub fn foo() callconv(.C) void {
// const bar = struct {
// var static: struct_empty_struct = struct_empty_struct{};
// var static: struct_empty_struct = @import("std").mem.zeroes(struct_empty_struct);
// };
// _ = &bar;
// }