diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-08-24 20:27:11 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-08-24 22:20:31 -0700 |
| commit | 7453f56e678c80928ababa2868c69cfe41647fed (patch) | |
| tree | 689bfb73cab6314cb91898b174fcf12d50196009 /test | |
| parent | af19909b9cde3d009f0306ac825f39912644bca6 (diff) | |
| download | zig-7453f56e678c80928ababa2868c69cfe41647fed.tar.gz zig-7453f56e678c80928ababa2868c69cfe41647fed.zip | |
stage2: explicitly tagged enums no longer have one possible value
Previously, Zig had inconsistent semantics for an enum like this:
`enum(u8){zero = 0}`
Although in theory this can only hold one possible value, the tag
`zero`, Zig no longer will treat the type this way. It will do loads and
stores, as if the type has runtime bits.
Closes #12619
Tests passed locally:
* test-behavior
* test-cases
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior.zig | 1 | ||||
| -rw-r--r-- | test/behavior/bugs/1111.zig | 11 | ||||
| -rw-r--r-- | test/behavior/enum.zig | 42 | ||||
| -rw-r--r-- | test/behavior/union.zig | 9 |
4 files changed, 46 insertions, 17 deletions
diff --git a/test/behavior.zig b/test/behavior.zig index ba8379cd72..12edd6f9a3 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -26,7 +26,6 @@ test { _ = @import("behavior/bugs/920.zig"); _ = @import("behavior/bugs/1025.zig"); _ = @import("behavior/bugs/1076.zig"); - _ = @import("behavior/bugs/1111.zig"); _ = @import("behavior/bugs/1277.zig"); _ = @import("behavior/bugs/1310.zig"); _ = @import("behavior/bugs/1381.zig"); diff --git a/test/behavior/bugs/1111.zig b/test/behavior/bugs/1111.zig deleted file mode 100644 index d274befaf3..0000000000 --- a/test/behavior/bugs/1111.zig +++ /dev/null @@ -1,11 +0,0 @@ -const Foo = enum(c_int) { - Bar = -1, -}; - -test "issue 1111 fixed" { - const v = Foo.Bar; - - switch (v) { - Foo.Bar => return, - } -} diff --git a/test/behavior/enum.zig b/test/behavior/enum.zig index 709d30af33..9e96163cc0 100644 --- a/test/behavior/enum.zig +++ b/test/behavior/enum.zig @@ -1,6 +1,7 @@ const builtin = @import("builtin"); const std = @import("std"); const expect = std.testing.expect; +const assert = std.debug.assert; const mem = std.mem; const Tag = std.meta.Tag; @@ -1128,3 +1129,44 @@ test "tag name functions are unique" { _ = a; } } + +test "size of enum with only one tag which has explicit integer tag type" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + const E = enum(u8) { nope = 10 }; + const S0 = struct { e: E }; + const S1 = extern struct { e: E }; + //const U = union(E) { nope: void }; + comptime assert(@sizeOf(E) == 1); + comptime assert(@sizeOf(S0) == 1); + comptime assert(@sizeOf(S1) == 1); + //comptime assert(@sizeOf(U) == 1); + + var s1: S1 = undefined; + s1.e = .nope; + try expect(s1.e == .nope); + const ptr = @ptrCast(*u8, &s1); + try expect(ptr.* == 10); + + var s0: S0 = undefined; + s0.e = .nope; + try expect(s0.e == .nope); +} + +test "switch on an extern enum with negative value" { + // TODO x86, wasm backends fail because they assume that enum tag types are unsigned + if (@import("builtin").zig_backend == .stage2_x86_64) return error.SkipZigTest; + if (@import("builtin").zig_backend == .stage2_wasm) return error.SkipZigTest; + + const Foo = enum(c_int) { + Bar = -1, + }; + + const v = Foo.Bar; + + switch (v) { + Foo.Bar => return, + } +} diff --git a/test/behavior/union.zig b/test/behavior/union.zig index 92f277b946..e2078c66df 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -1,6 +1,7 @@ const builtin = @import("builtin"); const std = @import("std"); const expect = std.testing.expect; +const assert = std.debug.assert; const expectEqual = std.testing.expectEqual; const Tag = std.meta.Tag; @@ -1065,6 +1066,8 @@ test "@unionInit on union with tag but no fields" { if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO const S = struct { const Type = enum(u8) { no_op = 105 }; @@ -1079,11 +1082,7 @@ test "@unionInit on union with tag but no fields" { }; comptime { - if (builtin.zig_backend == .stage1) { - // stage1 gets the wrong answer here - } else { - std.debug.assert(@sizeOf(Data) == 0); - } + assert(@sizeOf(Data) == 1); } fn doTheTest() !void { |
