Skip to content

Commit

Permalink
compiler: Disallow align(0) everywhere in the language.
Browse files Browse the repository at this point in the history
Thus leaving the design space for this alignment value open, e.g. for packing.
  • Loading branch information
alexrp authored and andrewrk committed Nov 23, 2024
1 parent 2742ab4 commit aef5c75
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
19 changes: 4 additions & 15 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand All @@ -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: {
Expand Down
52 changes: 52 additions & 0 deletions test/cases/compile_errors/align_zero.zig
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export fn entry() align(0) void {}
export fn entry() align(64) void {}

// error
// backend=stage2
Expand Down

0 comments on commit aef5c75

Please sign in to comment.