aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-07-04 00:35:28 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-07-04 00:35:28 -0400
commit96fd1030730e2980fa852ae45a67a2a4008bb163 (patch)
tree35994567d3e9c618e05301131c581d85146d4e78 /test
parentbfe0bf695b8471a5553bf5cdf5fc527c42eda1e8 (diff)
downloadzig-96fd1030730e2980fa852ae45a67a2a4008bb163.tar.gz
zig-96fd1030730e2980fa852ae45a67a2a4008bb163.zip
improve the error message and test coverage
Diffstat (limited to 'test')
-rw-r--r--test/compile_errors.zig35
-rw-r--r--test/stage1/behavior/switch.zig28
2 files changed, 40 insertions, 23 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index c6852621e3..df4e38583c 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -3,6 +3,24 @@ const builtin = @import("builtin");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
+ "capture group on switch prong with incompatible payload types",
+ \\const Union = union(enum) {
+ \\ A: usize,
+ \\ B: isize,
+ \\};
+ \\comptime {
+ \\ var u = Union{ .A = 8 };
+ \\ switch (u) {
+ \\ .A, .B => |e| unreachable,
+ \\ }
+ \\}
+ ,
+ "tmp.zig:8:20: error: capture group with incompatible types",
+ "tmp.zig:8:9: note: type 'usize' here",
+ "tmp.zig:8:13: note: type 'isize' here",
+ );
+
+ cases.add(
"wrong type to @hasField",
\\export fn entry() bool {
\\ return @hasField(i32, "hi");
@@ -6073,21 +6091,4 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:5:30: error: expression value is ignored",
"tmp.zig:9:30: error: expression value is ignored",
);
-
- cases.add(
- "capture group on switch prong with different payloads",
- \\const Union = union(enum) {
- \\ A: usize,
- \\ B: isize,
- \\};
- \\comptime {
- \\ var u = Union{ .A = 8 };
- \\ switch (u) {
- \\ .A, .B => |e| unreachable,
- \\ }
- \\}
- ,
- "tmp.zig:8:20: error: switch prong contains cases with different payloads",
- "tmp.zig:8:20: note: payload types are usize and isize",
- );
}
diff --git a/test/stage1/behavior/switch.zig b/test/stage1/behavior/switch.zig
index 2b7422fa6d..806f51b28e 100644
--- a/test/stage1/behavior/switch.zig
+++ b/test/stage1/behavior/switch.zig
@@ -392,20 +392,36 @@ test "switch with null and T peer types and inferred result location type" {
comptime S.doTheTest(1);
}
-test "switch prongs with cases with identical payloads" {
+test "switch prongs with cases with identical payload types" {
const Union = union(enum) {
A: usize,
B: isize,
C: usize,
};
const S = struct {
- fn doTheTest(u: Union) void {
+ fn doTheTest() void {
+ doTheSwitch1(Union{ .A = 8 });
+ doTheSwitch2(Union{ .B = -8 });
+ }
+ fn doTheSwitch1(u: Union) void {
switch (u) {
- .A, .C => |e| expect(@typeOf(e) == usize),
- .B => |e| expect(@typeOf(e) == isize),
+ .A, .C => |e| {
+ expect(@typeOf(e) == usize);
+ expect(e == 8);
+ },
+ .B => |e| @panic("fail"),
+ }
+ }
+ fn doTheSwitch2(u: Union) void {
+ switch (u) {
+ .A, .C => |e| @panic("fail"),
+ .B => |e| {
+ expect(@typeOf(e) == isize);
+ expect(e == -8);
+ },
}
}
};
- S.doTheTest(Union{ .A = 8 });
- comptime S.doTheTest(Union{ .B = -8 });
+ S.doTheTest();
+ comptime S.doTheTest();
}