authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-08 23:44:21-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-08 23:44:21-05:00
log916d24cd2111559b0694d9579872832f4764a391
tree2811c7bdf35394b7aab45a31b2c4230c961a5054
parent4b16874f04b7bae48a32da582af39667a3ef731d

add compile error tests for error sets


1 files changed, 151 insertions(+), 0 deletions(-)

test/compile_errors.zig+151
...@@ -1,6 +1,157 @@...@@ -1,6 +1,157 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: &tests.CompileErrorContext) void {3pub fn addCases(cases: &tests.CompileErrorContext) void {
4 cases.add("inferred error set with no returned error",
5 \\export fn entry() void {
6 \\ foo() catch unreachable;
7 \\}
8 \\fn foo() !void {
9 \\}
10 ,
11 ".tmp_source.zig:4:11: error: function with inferred error set must return at least one possible error");
12
13 cases.add("error not handled in switch",
14 \\export fn entry() void {
15 \\ foo(452) catch |err| switch (err) {
16 \\ error.Foo => {},
17 \\ };
18 \\}
19 \\fn foo(x: i32) !void {
20 \\ switch (x) {
21 \\ 0 ... 10 => return error.Foo,
22 \\ 11 ... 20 => return error.Bar,
23 \\ 21 ... 30 => return error.Baz,
24 \\ else => {},
25 \\ }
26 \\}
27 ,
28 ".tmp_source.zig:2:26: error: error.Baz not handled in switch",
29 ".tmp_source.zig:2:26: error: error.Bar not handled in switch");
30
31 cases.add("duplicate error in switch",
32 \\export fn entry() void {
33 \\ foo(452) catch |err| switch (err) {
34 \\ error.Foo => {},
35 \\ error.Bar => {},
36 \\ error.Foo => {},
37 \\ else => {},
38 \\ };
39 \\}
40 \\fn foo(x: i32) !void {
41 \\ switch (x) {
42 \\ 0 ... 10 => return error.Foo,
43 \\ 11 ... 20 => return error.Bar,
44 \\ else => {},
45 \\ }
46 \\}
47 ,
48 ".tmp_source.zig:5:14: error: duplicate switch value: '@typeOf(foo).ReturnType.ErrorSet.Foo'",
49 ".tmp_source.zig:3:14: note: other value is here");
50
51 cases.add("range operator in switch used on error set",
52 \\export fn entry() void {
53 \\ try foo(452) catch |err| switch (err) {
54 \\ error.A ... error.B => {},
55 \\ else => {},
56 \\ };
57 \\}
58 \\fn foo(x: i32) !void {
59 \\ switch (x) {
60 \\ 0 ... 10 => return error.Foo,
61 \\ 11 ... 20 => return error.Bar,
62 \\ else => {},
63 \\ }
64 \\}
65 ,
66 ".tmp_source.zig:3:17: error: operator not allowed for errors");
67
68 cases.add("inferring error set of function pointer",
69 \\comptime {
70 \\ const z: ?fn()!void = null;
71 \\}
72 ,
73 ".tmp_source.zig:2:15: error: inferring error set of return type valid only for function definitions");
74
75 cases.add("access non-existent member of error set",
76 \\const Foo = error{A};
77 \\comptime {
78 \\ const z = Foo.Bar;
79 \\}
80 ,
81 ".tmp_source.zig:3:18: error: no error named 'Bar' in 'Foo'");
82
83 cases.add("error union operator with non error set LHS",
84 \\comptime {
85 \\ const z = i32!i32;
86 \\}
87 ,
88 ".tmp_source.zig:2:15: error: expected error set type, found type 'i32'");
89
90 cases.add("error equality but sets have no common members",
91 \\const Set1 = error{A, C};
92 \\const Set2 = error{B, D};
93 \\export fn entry() void {
94 \\ foo(Set1.A);
95 \\}
96 \\fn foo(x: Set1) void {
97 \\ if (x == Set2.B) {
98 \\
99 \\ }
100 \\}
101 ,
102 ".tmp_source.zig:7:11: error: error sets 'Set1' and 'Set2' have no common errors");
103
104 cases.add("only equality binary operator allowed for error sets",
105 \\comptime {
106 \\ const z = error.A > error.B;
107 \\}
108 ,
109 ".tmp_source.zig:2:23: error: operator not allowed for errors");
110
111 cases.add("explicit error set cast known at comptime violates error sets",
112 \\const Set1 = error {A, B};
113 \\const Set2 = error {A, C};
114 \\comptime {
115 \\ var x = Set1.B;
116 \\ var y = Set2(x);
117 \\}
118 ,
119 ".tmp_source.zig:5:17: error: error.B not a member of error set 'Set2'");
120
121 cases.add("cast error union of global error set to error union of smaller error set",
122 \\const SmallErrorSet = error{A};
123 \\export fn entry() void {
124 \\ var x: SmallErrorSet!i32 = foo();
125 \\}
126 \\fn foo() error!i32 {
127 \\ return error.B;
128 \\}
129 ,
130 ".tmp_source.zig:3:35: error: expected 'SmallErrorSet!i32', found 'error!i32'",
131 ".tmp_source.zig:3:35: note: unable to cast global error set into smaller set");
132
133 cases.add("cast global error set to error set",
134 \\const SmallErrorSet = error{A};
135 \\export fn entry() void {
136 \\ var x: SmallErrorSet = foo();
137 \\}
138 \\fn foo() error {
139 \\ return error.B;
140 \\}
141 ,
142 ".tmp_source.zig:3:31: error: expected 'SmallErrorSet', found 'error'",
143 ".tmp_source.zig:3:31: note: unable to cast global error set into smaller set");
144
145 cases.add("recursive inferred error set",
146 \\export fn entry() void {
147 \\ foo() catch unreachable;
148 \\}
149 \\fn foo() !void {
150 \\ try foo();
151 \\}
152 ,
153 ".tmp_source.zig:5:5: error: cannot resolve inferred error set '@typeOf(foo).ReturnType.ErrorSet': function 'foo' not fully analyzed yet");
154
4 cases.add("implicit cast of error set not a subset",155 cases.add("implicit cast of error set not a subset",
5 \\const Set1 = error{A, B};156 \\const Set1 = error{A, B};
6 \\const Set2 = error{A, C};157 \\const Set2 = error{A, C};