aboutsummaryrefslogtreecommitdiff
path: root/test/compile_errors.zig
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2020-08-18 23:55:44 +0300
committerGitHub <noreply@github.com>2020-08-18 23:55:44 +0300
commitd139e44cfae68ace32458ff4d804dc6331b1ec8e (patch)
treed18d1f3ece51b6408172157d5f023e7a6c076afc /test/compile_errors.zig
parente2c741f1e7dbddabdbfc18a2520f5efa376899bc (diff)
parent2948f2d262926725b4b8cb5beeb4fdba00b48336 (diff)
downloadzig-d139e44cfae68ace32458ff4d804dc6331b1ec8e.tar.gz
zig-d139e44cfae68ace32458ff4d804dc6331b1ec8e.zip
Merge pull request #5495 from xackus/fix_5314
stage1: fix non-exhaustive enums with one field
Diffstat (limited to 'test/compile_errors.zig')
-rw-r--r--test/compile_errors.zig55
1 files changed, 53 insertions, 2 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index fc5b16fd9d..fd74385d9b 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -51,6 +51,46 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:17:23: error: cannot adjust alignment of zero sized type 'fn(u32) anytype'",
});
+ cases.addTest("invalid non-exhaustive enum to union",
+ \\const E = enum(u8) {
+ \\ a,
+ \\ b,
+ \\ _,
+ \\};
+ \\const U = union(E) {
+ \\ a,
+ \\ b,
+ \\};
+ \\export fn foo() void {
+ \\ var e = @intToEnum(E, 15);
+ \\ var u: U = e;
+ \\}
+ \\export fn bar() void {
+ \\ const e = @intToEnum(E, 15);
+ \\ var u: U = e;
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:12:16: error: runtime cast to union 'U' from non-exhustive enum",
+ "tmp.zig:16:16: error: no tag by value 15",
+ });
+
+ cases.addTest("switching with exhaustive enum has '_' prong ",
+ \\const E = enum{
+ \\ a,
+ \\ b,
+ \\};
+ \\pub export fn entry() void {
+ \\ var e: E = .b;
+ \\ switch (e) {
+ \\ .a => {},
+ \\ .b => {},
+ \\ _ => {},
+ \\ }
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:7:5: error: switch on exhaustive enum has `_` prong",
+ });
+
cases.addTest("invalid pointer with @Type",
\\export fn entry() void {
\\ _ = @Type(.{ .Pointer = .{
@@ -564,6 +604,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ b,
\\ _,
\\};
+ \\const U = union(E) {
+ \\ a: i32,
+ \\ b: u32,
+ \\};
\\pub export fn entry() void {
\\ var e: E = .b;
\\ switch (e) { // error: switch not handling the tag `b`
@@ -574,10 +618,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ .a => {},
\\ .b => {},
\\ }
+ \\ var u = U{.a = 2};
+ \\ switch (u) { // error: `_` prong not allowed when switching on tagged union
+ \\ .a => {},
+ \\ .b => {},
+ \\ _ => {},
+ \\ }
\\}
, &[_][]const u8{
- "tmp.zig:8:5: error: enumeration value 'E.b' not handled in switch",
- "tmp.zig:12:5: error: switch on non-exhaustive enum must include `else` or `_` prong",
+ "tmp.zig:12:5: error: enumeration value 'E.b' not handled in switch",
+ "tmp.zig:16:5: error: switch on non-exhaustive enum must include `else` or `_` prong",
+ "tmp.zig:21:5: error: `_` prong not allowed when switching on tagged union",
});
cases.add("switch expression - unreachable else prong (bool)",