diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/compile_errors.zig | 23 | ||||
| -rw-r--r-- | test/run_translated_c.zig | 1 | ||||
| -rw-r--r-- | test/translate_c.zig | 87 |
3 files changed, 79 insertions, 32 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 6b231a323d..4adc538602 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,29 @@ const tests = @import("tests.zig"); const std = @import("std"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.addTest("duplicate/unused labels", + \\comptime { + \\ blk: { blk: while (false) {} } + \\ blk: while (false) { blk: for (@as([0]void, undefined)) |_| {} } + \\ blk: for (@as([0]void, undefined)) |_| { blk: {} } + \\} + \\comptime { + \\ blk: {} + \\ blk: while(false) {} + \\ blk: for(@as([0]void, undefined)) |_| {} + \\} + , &[_][]const u8{ + "tmp.zig:2:17: error: redeclaration of label 'blk'", + "tmp.zig:2:10: note: previous declaration is here", + "tmp.zig:3:31: error: redeclaration of label 'blk'", + "tmp.zig:3:10: note: previous declaration is here", + "tmp.zig:4:51: error: redeclaration of label 'blk'", + "tmp.zig:4:10: note: previous declaration is here", + "tmp.zig:7:10: error: unused block label", + "tmp.zig:8:10: error: unused while label", + "tmp.zig:9:10: error: unused for label", + }); + cases.addTest("@alignCast of zero sized types", \\export fn foo() void { \\ const a: *void = undefined; diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index efdc9702a4..3fa183ce3b 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -15,7 +15,6 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ } \\ if (s0 != 1) abort(); \\ if (s1 != 10) abort(); - \\ return 0; \\} , ""); diff --git a/test/translate_c.zig b/test/translate_c.zig index 38faffe747..f7e983276e 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -3,12 +3,33 @@ const std = @import("std"); const CrossTarget = std.zig.CrossTarget; pub fn addCases(cases: *tests.TranslateCContext) void { + cases.add("missing return stmt", + \\int foo() {} + \\int bar() { + \\ int a = 2; + \\} + \\int baz() { + \\ return 0; + \\} + , &[_][]const u8{ + \\pub export fn foo() c_int { + \\ return 0; + \\} + \\pub export fn bar() c_int { + \\ var a: c_int = 2; + \\ return 0; + \\} + \\pub export fn baz() c_int { + \\ return 0; + \\} + }); + cases.add("alignof", - \\int main() { + \\void main() { \\ int a = _Alignof(int); \\} , &[_][]const u8{ - \\pub export fn main() c_int { + \\pub export fn main() void { \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int))); \\} }); @@ -539,6 +560,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ c = (a * b); \\ c = @divTrunc(a, b); \\ c = @rem(a, b); + \\ return 0; \\} \\pub export fn u() c_uint { \\ var a: c_uint = undefined; @@ -549,6 +571,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ c = (a *% b); \\ c = (a / b); \\ c = (a % b); + \\ return 0; \\} }); @@ -1596,13 +1619,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { }); cases.add("worst-case assign", - \\int foo() { + \\void foo() { \\ int a; \\ int b; \\ a = b = 2; \\} , &[_][]const u8{ - \\pub export fn foo() c_int { + \\pub export fn foo() void { \\ var a: c_int = undefined; \\ var b: c_int = undefined; \\ a = blk: { @@ -1650,11 +1673,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ a = 7; \\ if (!true) break; \\ } + \\ return 0; \\} }); cases.add("for loops", - \\int foo() { + \\void foo() { \\ for (int i = 2, b = 4; i + 2; i = 2) { \\ int a = 2; \\ a = 6, 5, 7; @@ -1662,7 +1686,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ char i = 2; \\} , &[_][]const u8{ - \\pub export fn foo() c_int { + \\pub export fn foo() void { \\ { \\ var i: c_int = 2; \\ var b: c_int = 4; @@ -1712,7 +1736,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { }); cases.add("switch on int", - \\int switch_fn(int i) { + \\void switch_fn(int i) { \\ int res = 0; \\ switch (i) { \\ case 0: @@ -1727,19 +1751,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ } \\} , &[_][]const u8{ - \\pub export fn switch_fn(arg_i: c_int) c_int { + \\pub export fn switch_fn(arg_i: c_int) void { \\ var i = arg_i; \\ var res: c_int = 0; - \\ __switch: { - \\ __case_2: { - \\ __default: { - \\ __case_1: { - \\ __case_0: { + \\ @"switch": { + \\ case_2: { + \\ default: { + \\ case_1: { + \\ case: { \\ switch (i) { - \\ @as(c_int, 0) => break :__case_0, - \\ @as(c_int, 1)...@as(c_int, 3) => break :__case_1, - \\ else => break :__default, - \\ @as(c_int, 4) => break :__case_2, + \\ @as(c_int, 0) => break :case, + \\ @as(c_int, 1)...@as(c_int, 3) => break :case_1, + \\ else => break :default, + \\ @as(c_int, 4) => break :case_2, \\ } \\ } \\ res = 1; @@ -1747,7 +1771,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ res = 2; \\ } \\ res = (@as(c_int, 3) * i); - \\ break :__switch; + \\ break :@"switch"; \\ } \\ res = 5; \\ } @@ -1787,13 +1811,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { }); cases.add("assign", - \\int max(int a) { + \\void max(int a) { \\ int tmp; \\ tmp = a; \\ a = tmp; \\} , &[_][]const u8{ - \\pub export fn max(arg_a: c_int) c_int { + \\pub export fn max(arg_a: c_int) void { \\ var a = arg_a; \\ var tmp: c_int = undefined; \\ tmp = a; @@ -2082,7 +2106,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ int b; \\}a; \\float b = 2.0f; - \\int foo(void) { + \\void foo(void) { \\ struct Foo *c; \\ a.b; \\ c->b; @@ -2093,7 +2117,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\}; \\pub extern var a: struct_Foo; \\pub export var b: f32 = 2; - \\pub export fn foo() c_int { + \\pub export fn foo() void { \\ var c: [*c]struct_Foo = undefined; \\ _ = a.b; \\ _ = c.*.b; @@ -2204,11 +2228,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ if (a < b) return b; \\ if (a < b) return b else return a; \\ if (a < b) {} else {} + \\ return 0; \\} }); cases.add("if statements", - \\int foo() { + \\void foo() { \\ if (2) { \\ int a = 2; \\ } @@ -2217,7 +2242,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ } \\} , &[_][]const u8{ - \\pub export fn foo() c_int { + \\pub export fn foo() void { \\ if (true) { \\ var a: c_int = 2; \\ } @@ -2782,11 +2807,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ var x = arg_x; \\ return blk: { \\ const tmp = x; - \\ (blk: { + \\ (blk_1: { \\ const ref = &p; - \\ const tmp_1 = ref.*; + \\ const tmp_2 = ref.*; \\ ref.* += 1; - \\ break :blk tmp_1; + \\ break :blk_1 tmp_2; \\ }).?.* = tmp; \\ break :blk tmp; \\ }; @@ -2811,12 +2836,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { }); cases.add("arg name aliasing decl which comes after", - \\int foo(int bar) { + \\void foo(int bar) { \\ bar = 2; \\} \\int bar = 4; , &[_][]const u8{ - \\pub export fn foo(arg_bar_1: c_int) c_int { + \\pub export fn foo(arg_bar_1: c_int) void { \\ var bar_1 = arg_bar_1; \\ bar_1 = 2; \\} @@ -2824,12 +2849,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { }); cases.add("arg name aliasing macro which comes after", - \\int foo(int bar) { + \\void foo(int bar) { \\ bar = 2; \\} \\#define bar 4 , &[_][]const u8{ - \\pub export fn foo(arg_bar_1: c_int) c_int { + \\pub export fn foo(arg_bar_1: c_int) void { \\ var bar_1 = arg_bar_1; \\ bar_1 = 2; \\} |
