From aef5c756027c11885a3ae7f55061cdbd3e3bc0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 4 Nov 2024 13:18:13 +0100 Subject: [PATCH] compiler: Disallow align(0) everywhere in the language. Thus leaving the design space for this alignment value open, e.g. for packing. --- src/Sema.zig | 19 ++----- test/cases/compile_errors/align_zero.zig | 52 +++++++++++++++++++ ...nction_alignment_on_unsupported_target.zig | 2 +- 3 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 test/cases/compile_errors/align_zero.zig diff --git a/src/Sema.zig b/src/Sema.zig index 0424970f18..25fb54dab8 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2584,18 +2584,7 @@ fn validateAlign( src: LazySrcLoc, alignment: u64, ) !Alignment { - const result = try validateAlignAllowZero(sema, block, src, alignment); - if (result == .none) return sema.fail(block, src, "alignment must be >= 1", .{}); - return result; -} - -fn validateAlignAllowZero( - sema: *Sema, - block: *Block, - src: LazySrcLoc, - alignment: u64, -) !Alignment { - if (alignment == 0) return .none; + if (alignment == 0) return sema.fail(block, src, "alignment must be >= 1", .{}); if (!std.math.isPowerOfTwo(alignment)) { return sema.fail(block, src, "alignment value '{d}' is not a power of two", .{ alignment, @@ -20496,7 +20485,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air else => {}, } const align_bytes = (try val.getUnsignedIntSema(pt)).?; - break :blk try sema.validateAlignAllowZero(block, align_src, align_bytes); + break :blk try sema.validateAlign(block, align_src, align_bytes); } else .none; const address_space: std.builtin.AddressSpace = if (inst_data.flags.has_addrspace) blk: { @@ -26858,7 +26847,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A if (val.isGenericPoison()) { break :blk null; } - break :blk try sema.validateAlignAllowZero(block, align_src, try val.toUnsignedIntSema(pt)); + break :blk try sema.validateAlign(block, align_src, try val.toUnsignedIntSema(pt)); } else if (extra.data.bits.has_align_ref) blk: { const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; @@ -26876,7 +26865,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A error.GenericPoison => break :blk null, else => |e| return e, }; - break :blk try sema.validateAlignAllowZero(block, align_src, try align_val.toUnsignedIntSema(pt)); + break :blk try sema.validateAlign(block, align_src, try align_val.toUnsignedIntSema(pt)); } else .none; const @"addrspace": ?std.builtin.AddressSpace = if (extra.data.bits.has_addrspace_body) blk: { diff --git a/test/cases/compile_errors/align_zero.zig b/test/cases/compile_errors/align_zero.zig new file mode 100644 index 0000000000..a63523b853 --- /dev/null +++ b/test/cases/compile_errors/align_zero.zig @@ -0,0 +1,52 @@ +pub var global_var: i32 align(0) = undefined; + +pub export fn a() void { + _ = &global_var; +} + +pub extern var extern_var: i32 align(0); + +pub export fn b() void { + _ = &extern_var; +} + +pub export fn c() align(0) void {} + +pub export fn d() void { + _ = *align(0) fn () i32; +} + +pub export fn e() void { + var local_var: i32 align(0) = undefined; + _ = &local_var; +} + +pub export fn f() void { + _ = *align(0) i32; +} + +pub export fn g() void { + _ = []align(0) i32; +} + +pub export fn h() void { + _ = struct { field: i32 align(0) }; +} + +pub export fn i() void { + _ = union { field: i32 align(0) }; +} + +// error +// backend=stage2 +// target=native +// +// :1:31: error: alignment must be >= 1 +// :7:38: error: alignment must be >= 1 +// :13:25: error: alignment must be >= 1 +// :16:16: error: alignment must be >= 1 +// :20:30: error: alignment must be >= 1 +// :25:16: error: alignment must be >= 1 +// :29:17: error: alignment must be >= 1 +// :33:35: error: alignment must be >= 1 +// :37:34: error: alignment must be >= 1 diff --git a/test/cases/compile_errors/function_alignment_on_unsupported_target.zig b/test/cases/compile_errors/function_alignment_on_unsupported_target.zig index 7b033e0f60..e3ea1dd068 100644 --- a/test/cases/compile_errors/function_alignment_on_unsupported_target.zig +++ b/test/cases/compile_errors/function_alignment_on_unsupported_target.zig @@ -1,4 +1,4 @@ -export fn entry() align(0) void {} +export fn entry() align(64) void {} // error // backend=stage2