aboutsummaryrefslogtreecommitdiff
path: root/test/compile_errors
diff options
context:
space:
mode:
authorJohn Schmidt <john.schmidt.h@gmail.com>2022-03-26 19:34:55 +0100
committerJohn Schmidt <john.schmidt.h@gmail.com>2022-04-03 13:49:34 +0200
commitfd1ce329b3f978a4ac2ae272afee7f07b5841990 (patch)
tree510f613addfea6dc329e42d633adae74ed277529 /test/compile_errors
parente4d427f12e2052e9bcd6af40e7ddbc4e544451e6 (diff)
downloadzig-fd1ce329b3f978a4ac2ae272afee7f07b5841990.tar.gz
zig-fd1ce329b3f978a4ac2ae272afee7f07b5841990.zip
stage2: add union compile error tests
Diffstat (limited to 'test/compile_errors')
-rw-r--r--test/compile_errors/stage2/union_access_of_inactive_field.zig14
-rw-r--r--test/compile_errors/stage2/union_enum_field_missing.zig20
-rw-r--r--test/compile_errors/stage2/union_extra_field.zig19
-rw-r--r--test/compile_errors/stage2/union_runtime_coercion_from_enum.zig22
4 files changed, 75 insertions, 0 deletions
diff --git a/test/compile_errors/stage2/union_access_of_inactive_field.zig b/test/compile_errors/stage2/union_access_of_inactive_field.zig
new file mode 100644
index 0000000000..34fa661d79
--- /dev/null
+++ b/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
diff --git a/test/compile_errors/stage2/union_enum_field_missing.zig b/test/compile_errors/stage2/union_enum_field_missing.zig
new file mode 100644
index 0000000000..b29ca83d3a
--- /dev/null
+++ b/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
diff --git a/test/compile_errors/stage2/union_extra_field.zig b/test/compile_errors/stage2/union_extra_field.zig
new file mode 100644
index 0000000000..4a0ab41936
--- /dev/null
+++ b/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
diff --git a/test/compile_errors/stage2/union_runtime_coercion_from_enum.zig b/test/compile_errors/stage2/union_runtime_coercion_from_enum.zig
new file mode 100644
index 0000000000..f7e96834fd
--- /dev/null
+++ b/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