diff options
| author | Veikka Tuominen <git@vexu.eu> | 2020-08-18 23:55:44 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-18 23:55:44 +0300 |
| commit | d139e44cfae68ace32458ff4d804dc6331b1ec8e (patch) | |
| tree | d18d1f3ece51b6408172157d5f023e7a6c076afc /test/compile_errors.zig | |
| parent | e2c741f1e7dbddabdbfc18a2520f5efa376899bc (diff) | |
| parent | 2948f2d262926725b4b8cb5beeb4fdba00b48336 (diff) | |
| download | zig-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.zig | 55 |
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)", |
