From d6c74a95fdaba4ed373f80baa725dcc53b27e402 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 +- lib/std/zig/llvm/Builder.zig | 7 +++-- src/InternPool.zig | 38 ++++++++++++++---------- src/Sema.zig | 2 +- src/codegen/aarch64/Assemble.zig | 17 ++++++----- test/behavior/tuple.zig | 6 ++-- test/behavior/type.zig | 2 +- test/cases/compile_errors/align_zero.zig | 20 +------------ 9 files changed, 45 insertions(+), 51 deletions(-) diff --git a/lib/compiler/aro/aro/Attribute.zig b/lib/compiler/aro/aro/Attribute.zig index a5b78b8463..4db287b65c 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 0cee23cfa8..65b7d60c18 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -939,7 +939,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/lib/std/zig/llvm/Builder.zig b/lib/std/zig/llvm/Builder.zig index f3ff63ec33..ba6faaec2c 100644 --- a/lib/std/zig/llvm/Builder.zig +++ b/lib/std/zig/llvm/Builder.zig @@ -8533,18 +8533,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 ++ "]f}"; + const T = std.fmt.Formatter(FormatData, format); field.* = .{ .name = name, - .type = std.fmt.Formatter(FormatData, format), + .type = T, .default_value_ptr = null, .is_comptime = false, - .alignment = 0, + .alignment = @alignOf(T), }; } fmt_str = fmt_str ++ ")\n"; diff --git a/src/InternPool.zig b/src/InternPool.zig index 15d895aed0..ed922db742 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -1137,13 +1137,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, @@ -1158,22 +1161,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/Sema.zig b/src/Sema.zig index b855e4cc9d..3bee1130af 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -20332,7 +20332,7 @@ fn zirReify( ).?); if (!try sema.intFitsInType(alignment_val, align_ty, null)) { - return sema.fail(block, src, "alignment must fit in '{}'", .{align_ty.fmt(pt)}); + return sema.fail(block, src, "alignment must fit in '{f}'", .{align_ty.fmt(pt)}); } const alignment_val_int = try alignment_val.toUnsignedIntSema(pt); const abi_align = try sema.validateAlign(block, src, alignment_val_int); diff --git a/src/codegen/aarch64/Assemble.zig b/src/codegen/aarch64/Assemble.zig index 494e012d80..098065bb16 100644 --- a/src/codegen/aarch64/Assemble.zig +++ b/src/codegen/aarch64/Assemble.zig @@ -33,13 +33,16 @@ pub fn nextInstruction(as: *Assemble) !?Instruction { var symbols: Symbols: { const symbols = @typeInfo(@TypeOf(instruction.symbols)).@"struct".fields; var symbol_fields: [symbols.len]std.builtin.Type.StructField = undefined; - for (&symbol_fields, symbols) |*symbol_field, symbol| symbol_field.* = .{ - .name = symbol.name, - .type = zonCast(SymbolSpec, @field(instruction.symbols, symbol.name), .{}).Storage(), - .default_value_ptr = null, - .is_comptime = false, - .alignment = 0, - }; + for (&symbol_fields, symbols) |*symbol_field, symbol| { + const Storage = zonCast(SymbolSpec, @field(instruction.symbols, symbol.name), .{}).Storage(); + symbol_field.* = .{ + .name = symbol.name, + .type = Storage, + .default_value_ptr = null, + .is_comptime = false, + .alignment = @alignOf(Storage), + }; + } break :Symbols @Type(.{ .@"struct" = .{ .layout = .auto, .fields = &symbol_fields, diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index e760455e09..9e17d61856 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -318,6 +318,8 @@ test "tuple type with void field" { test "zero sized struct in tuple handled correctly" { const State = struct { const Self = @This(); + const Inner = struct {}; + data: @Type(.{ .@"struct" = .{ .is_tuple = true, @@ -325,10 +327,10 @@ test "zero sized struct in tuple handled correctly" { .decls = &.{}, .fields = &.{.{ .name = "0", - .type = struct {}, + .type = Inner, .default_value_ptr = null, .is_comptime = false, - .alignment = 0, + .alignment = @alignOf(Inner), }}, }, }), diff --git a/test/behavior/type.zig b/test/behavior/type.zig index 58e049d896..b5ac2d95f7 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, diff --git a/test/cases/compile_errors/align_zero.zig b/test/cases/compile_errors/align_zero.zig index e6d1a993d4..e54a32ce31 100644 --- a/test/cases/compile_errors/align_zero.zig +++ b/test/cases/compile_errors/align_zero.zig @@ -65,24 +65,7 @@ export fn k() void { } }); } -export fn l() void { - _ = @Type(.{ .@"struct" = .{ - .layout = .@"packed", - .fields = &.{.{ - .name = "test", - .type = u32, - .default_value_ptr = null, - .is_comptime = false, - .alignment = 8, - }}, - .decls = &.{}, - .is_tuple = false, - } }); -} - // error -// backend=stage2 -// target=native // // :1:27: error: alignment must be >= 1 // :7:34: error: alignment must be >= 1 @@ -93,6 +76,5 @@ export fn l() void { // :29:17: error: alignment must be >= 1 // :33:35: error: alignment must be >= 1 // :37:34: error: alignment must be >= 1 -// :41:9: error: alignment can only be 0 on packed struct fields +// :41:9: error: alignment must be >= 1 // :56:9: error: alignment must be >= 1 -// :69:9: error: alignment in a packed struct field must be set to 0