zig/test/behavior/empty_union.zig
mlugg 1b672e41c5 InternPool,Sema,type,llvm: alignment fixes
This changeset fixes the handling of alignment in several places. The
new rules are:
* `@alignOf(T)` where `T` is a runtime zero-bit type is at least 1,
  maybe greater.
* Zero-bit fields in `extern` structs *do* force alignment, potentially
  offsetting following fields.
* Zero-bit fields *do* have addresses within structs which can be
  observed and are consistent with `@offsetOf`.

These are not necessarily all implemented correctly yet (see disabled
test), but this commit fixes all regressions compared to master, and
makes one new test pass.
2023-09-21 14:48:41 -07:00

70 lines
1.6 KiB
Zig

const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;
test "switch on empty enum" {
const E = enum {};
var e: E = undefined;
switch (e) {}
}
test "switch on empty enum with a specified tag type" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const E = enum(u8) {};
var e: E = undefined;
switch (e) {}
}
test "switch on empty auto numbered tagged union" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const U = union(enum(u8)) {};
var u: U = undefined;
switch (u) {}
}
test "switch on empty tagged union" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
const E = enum {};
const U = union(E) {};
var u: U = undefined;
switch (u) {}
}
test "empty union" {
const U = union {};
try expect(@sizeOf(U) == 0);
try expect(@alignOf(U) == 1);
}
test "empty extern union" {
const U = extern union {};
try expect(@sizeOf(U) == 0);
try expect(@alignOf(U) == 1);
}
test "empty union passed as argument" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const U = union(enum) {
fn f(u: @This()) void {
switch (u) {}
}
};
U.f(@as(U, undefined));
}
test "empty enum passed as argument" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const E = enum {
fn f(e: @This()) void {
switch (e) {}
}
};
E.f(@as(E, undefined));
}