authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-03-26 19:34:55+01:00
committergravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-04-03 13:49:34+02:00
logfd1ce329b3f978a4ac2ae272afee7f07b5841990
tree510f613addfea6dc329e42d633adae74ed277529
parente4d427f12e2052e9bcd6af40e7ddbc4e544451e6

stage2: add union compile error tests


4 files changed, 75 insertions(+), 0 deletions(-)

test/compile_errors/stage2/union_access_of_inactive_field.zig created+14
...@@ -0,0 +1,14 @@
1const U = union {
2 a: void,
3 b: u64,
4};
5comptime {
6 var u: U = .{.a = {}};
7 const v = u.b;
8 _ = v;
9}
10
11// access of inactive union field
12//
13// :7:16: error: access of union field 'b' while field 'a' is active
14// :1:11: note: union declared here
test/compile_errors/stage2/union_enum_field_missing.zig created+20
...@@ -0,0 +1,20 @@
1const E = enum {
2 a,
3 b,
4 c,
5};
6
7const U = union(E) {
8 a: i32,
9 b: f64,
10};
11
12export fn entry() usize {
13 return @sizeOf(U);
14}
15
16// enum field missing in union
17//
18// :7:1: error: enum field(s) missing in union
19// :4:5: note: field 'c' missing, declared here
20// :1:11: note: enum declared here
test/compile_errors/stage2/union_extra_field.zig created+19
...@@ -0,0 +1,19 @@
1const E = enum {
2 a,
3 b,
4 c,
5};
6const U = union(E) {
7 a: i32,
8 b: f64,
9 c: f64,
10 d: f64,
11};
12export fn entry() usize {
13 return @sizeOf(U);
14}
15
16// union extra field
17//
18// :6:1: error: enum 'tmp.E' hs no field named 'd'
19// :1:11: note: enum declared here
test/compile_errors/stage2/union_runtime_coercion_from_enum.zig created+22
...@@ -0,0 +1,22 @@
1const E = enum {
2 a,
3 b,
4};
5const U = union(E) {
6 a: u32,
7 b: u64,
8};
9fn foo() E {
10 return E.b;
11}
12export fn doTheTest() u64 {
13 var u: U = foo();
14 return u.b;
15}
16
17// runtime coercion from enum to union
18//
19// :13:19: error: runtime coercion from enum 'tmp.E' to union 'tmp.U' which has non-void fields
20// :6:5: note: field 'a' has type 'u32'
21// :7:5: note: field 'b' has type 'u64'
22// :5:11: note: union declared here