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