From ebf81fb70d25dcf13c3d1162766191a28f798de0 Mon Sep 17 00:00:00 2001 From: David Rubin Date: Mon, 24 Feb 2025 04:02:06 -0800 Subject: [PATCH] remove usages of `.alignment = 0` --- lib/compiler/aro/aro/Attribute.zig | 2 +- lib/std/meta.zig | 2 +- src/InternPool.zig | 38 +++++++++++++++++------------- src/codegen/llvm/Builder.zig | 7 +++--- test/behavior/tuple.zig | 2 +- test/behavior/type.zig | 2 +- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/compiler/aro/aro/Attribute.zig b/lib/compiler/aro/aro/Attribute.zig index a5b78b8463a4..4db287b65cc9 100644 --- a/lib/compiler/aro/aro/Attribute.zig +++ b/lib/compiler/aro/aro/Attribute.zig @@ -708,7 +708,7 @@ pub const Arguments = blk: { field.* = .{ .name = decl.name, .type = @field(attributes, decl.name), - .alignment = 0, + .alignment = @alignOf(@field(attributes, decl.name)), }; } diff --git a/lib/std/meta.zig b/lib/std/meta.zig index c4e3774da559..413470961330 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1007,7 +1007,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type { .type = T, .default_value_ptr = null, .is_comptime = false, - .alignment = 0, + .alignment = @alignOf(T), }; } diff --git a/src/InternPool.zig b/src/InternPool.zig index 391d583004e1..ed8acb6aef26 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -1135,13 +1135,16 @@ const Local = struct { const elem_info = @typeInfo(Elem).@"struct"; const elem_fields = elem_info.fields; var new_fields: [elem_fields.len]std.builtin.Type.StructField = undefined; - for (&new_fields, elem_fields) |*new_field, elem_field| new_field.* = .{ - .name = elem_field.name, - .type = *[len]elem_field.type, - .default_value_ptr = null, - .is_comptime = false, - .alignment = 0, - }; + for (&new_fields, elem_fields) |*new_field, elem_field| { + const T = *[len]elem_field.type; + new_field.* = .{ + .name = elem_field.name, + .type = T, + .default_value_ptr = null, + .is_comptime = false, + .alignment = @alignOf(T), + }; + } return @Type(.{ .@"struct" = .{ .layout = .auto, .fields = &new_fields, @@ -1156,22 +1159,25 @@ const Local = struct { const elem_info = @typeInfo(Elem).@"struct"; const elem_fields = elem_info.fields; var new_fields: [elem_fields.len]std.builtin.Type.StructField = undefined; - for (&new_fields, elem_fields) |*new_field, elem_field| new_field.* = .{ - .name = elem_field.name, - .type = @Type(.{ .pointer = .{ + for (&new_fields, elem_fields) |*new_field, elem_field| { + const T = @Type(.{ .pointer = .{ .size = opts.size, .is_const = opts.is_const, .is_volatile = false, - .alignment = 0, + .alignment = @alignOf(elem_field.type), .address_space = .generic, .child = elem_field.type, .is_allowzero = false, .sentinel_ptr = null, - } }), - .default_value_ptr = null, - .is_comptime = false, - .alignment = 0, - }; + } }); + new_field.* = .{ + .name = elem_field.name, + .type = T, + .default_value_ptr = null, + .is_comptime = false, + .alignment = @alignOf(T), + }; + } return @Type(.{ .@"struct" = .{ .layout = .auto, .fields = &new_fields, diff --git a/src/codegen/llvm/Builder.zig b/src/codegen/llvm/Builder.zig index f79b5700a8f8..81274de66c6d 100644 --- a/src/codegen/llvm/Builder.zig +++ b/src/codegen/llvm/Builder.zig @@ -8465,18 +8465,19 @@ pub const Metadata = enum(u32) { .type = []const u8, .default_value_ptr = null, .is_comptime = false, - .alignment = 0, + .alignment = @alignOf([]const u8), }; } fmt_str = fmt_str ++ "("; inline for (fields[2..], names) |*field, name| { fmt_str = fmt_str ++ "{[" ++ name ++ "]S}"; + const T = std.fmt.Formatter(format); field.* = .{ .name = name, - .type = std.fmt.Formatter(format), + .type = T, .default_value_ptr = null, .is_comptime = false, - .alignment = 0, + .alignment = @alignOf(T), }; } fmt_str = fmt_str ++ ")\n"; diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index 816b9e6d46d3..f0961415effa 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -332,7 +332,7 @@ test "zero sized struct in tuple handled correctly" { .type = struct {}, .default_value_ptr = null, .is_comptime = false, - .alignment = 0, + .alignment = @alignOf(struct {}), }}, }, }), diff --git a/test/behavior/type.zig b/test/behavior/type.zig index 13e2873e280a..df1809c8d5e0 100644 --- a/test/behavior/type.zig +++ b/test/behavior/type.zig @@ -735,7 +735,7 @@ test "struct field names sliced at comptime from larger string" { var it = std.mem.tokenizeScalar(u8, text, '\n'); while (it.next()) |name| { fields = fields ++ &[_]Type.StructField{.{ - .alignment = 0, + .alignment = @alignOf(usize), .name = name ++ "", .type = usize, .default_value_ptr = null,