diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-05-07 12:07:35 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-05-07 12:07:35 -0400 |
| commit | 818a0a26291cf456cfaa955401b1aa8219737d6c (patch) | |
| tree | 9eac0aacdcc41c718a18c98f3513beffaaeac2d3 /test/compile_errors.zig | |
| parent | 29beb603b752928184f5f5e9f7479412de1e1951 (diff) | |
| download | zig-818a0a26291cf456cfaa955401b1aa8219737d6c.tar.gz zig-818a0a26291cf456cfaa955401b1aa8219737d6c.zip | |
switch expression - add compile errors
* for duplicate integer value
* for missing integer values
* for missing else prong
see #43
Diffstat (limited to 'test/compile_errors.zig')
| -rw-r--r-- | test/compile_errors.zig | 94 |
1 files changed, 75 insertions, 19 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 4d6877267d..54ab8444b8 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -542,7 +542,46 @@ pub fn addCases(cases: &tests.CompileErrorContext) { ".tmp_source.zig:5:5: error: redefinition of 'Bar'", ".tmp_source.zig:2:1: note: previous definition is here"); - cases.add("multiple else prongs in a switch", + cases.add("switch expression - missing enumeration prong", + \\const Number = enum { + \\ One, + \\ Two, + \\ Three, + \\ Four, + \\}; + \\fn f(n: Number) -> i32 { + \\ switch (n) { + \\ Number.One => 1, + \\ Number.Two => 2, + \\ Number.Three => i32(3), + \\ } + \\} + \\ + \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } + , ".tmp_source.zig:8:5: error: enumeration value 'Number.Four' not handled in switch"); + + cases.add("switch expression - duplicate enumeration prong", + \\const Number = enum { + \\ One, + \\ Two, + \\ Three, + \\ Four, + \\}; + \\fn f(n: Number) -> i32 { + \\ switch (n) { + \\ Number.One => 1, + \\ Number.Two => 2, + \\ Number.Three => i32(3), + \\ Number.Four => 4, + \\ Number.Two => 2, + \\ } + \\} + \\ + \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } + , ".tmp_source.zig:13:15: error: duplicate switch value", + ".tmp_source.zig:10:15: note: other value is here"); + + cases.add("switch expression - multiple else prongs", \\fn f(x: u32) { \\ const value: bool = switch (x) { \\ 1234 => false, @@ -555,6 +594,41 @@ pub fn addCases(cases: &tests.CompileErrorContext) { \\} , ".tmp_source.zig:5:9: error: multiple else prongs in switch expression"); + cases.add("switch expression - non exhaustive integer prongs", + \\fn foo(x: u8) { + \\ switch (x) { + \\ 0 => {}, + \\ } + \\} + \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } + , + ".tmp_source.zig:2:5: error: switch must handle all possibilities"); + + cases.add("switch expression - duplicate or overlapping integer value", + \\fn foo(x: u8) -> u8 { + \\ switch (x) { + \\ 0 ... 100 => u8(0), + \\ 101 ... 200 => 1, + \\ 201, 203 ... 207 => 2, + \\ 206 ... 255 => 3, + \\ } + \\} + \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } + , + ".tmp_source.zig:6:9: error: duplicate switch value", + ".tmp_source.zig:5:14: note: previous value is here"); + + cases.add("switch expression - switch on pointer type with no else", + \\fn foo(x: &u8) { + \\ switch (x) { + \\ &y => {}, + \\ } + \\} + \\const y: u8 = 100; + \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } + , + ".tmp_source.zig:2:5: error: else prong required when switching on type '&u8'"); + cases.add("global variable initializer must be constant expression", \\extern fn foo() -> i32; \\const x = foo(); @@ -716,24 +790,6 @@ pub fn addCases(cases: &tests.CompileErrorContext) { ".tmp_source.zig:4:26: error: division by zero is undefined"); - cases.add("missing switch prong", - \\const Number = enum { - \\ One, - \\ Two, - \\ Three, - \\ Four, - \\}; - \\fn f(n: Number) -> i32 { - \\ switch (n) { - \\ Number.One => 1, - \\ Number.Two => 2, - \\ Number.Three => i32(3), - \\ } - \\} - \\ - \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } - , ".tmp_source.zig:8:5: error: enumeration value 'Number.Four' not handled in switch"); - cases.add("normal string with newline", \\const foo = "a \\b"; |
