Commit 7f56e4ac16
Changed files (3)
src/codegen.cpp
@@ -1228,6 +1228,7 @@ static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) {
} else {
err_val = expr_val;
}
+ add_debug_source_node(g, node);
LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);
LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");
LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapErrError");
test/run_tests.cpp
@@ -390,168 +390,6 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
0o10700.00010e0: 0x1.1c0001p+12
)OUTPUT");
-
-
- add_simple_case("continue and break", R"SOURCE(
-const io = @import("std").io;
-pub fn main(args: [][]u8) -> %void {
- var i : i32 = 0;
- while (true) {
- %%io.stdout.printf("loop\n");
- i += 1;
- if (i < 4) {
- continue;
- }
- break;
- }
-}
- )SOURCE", "loop\nloop\nloop\nloop\n");
-
- add_simple_case("@sizeof() and @typeof()", R"SOURCE(
-const io = @import("std").io;
-const x: u16 = 13;
-const z: @typeof(x) = 19;
-pub fn main(args: [][]u8) -> %void {
- const y: @typeof(x) = 120;
- %%io.stdout.print_u64(@sizeof(@typeof(y)));
- %%io.stdout.printf("\n");
-}
- )SOURCE", "2\n");
-
- add_simple_case("pointer dereferencing", R"SOURCE(
-const io = @import("std").io;
-
-pub fn main(args: [][]u8) -> %void {
- var x = i32(3);
- const y = &x;
-
- *y += 1;
-
- if (x != 4) {
- %%io.stdout.printf("BAD\n");
- }
- if (*y != 4) {
- %%io.stdout.printf("BAD\n");
- }
- %%io.stdout.printf("OK\n");
-}
- )SOURCE", "OK\n");
-
- add_simple_case("constant expressions", R"SOURCE(
-const io = @import("std").io;
-
-const ARRAY_SIZE : i8 = 20;
-
-pub fn main(args: [][]u8) -> %void {
- var array : [ARRAY_SIZE]u8 = undefined;
- %%io.stdout.print_u64(@sizeof(@typeof(array)));
- %%io.stdout.printf("\n");
-}
- )SOURCE", "20\n");
-
- add_simple_case("@min_value() and @max_value()", R"SOURCE(
-const io = @import("std").io;
-pub fn main(args: [][]u8) -> %void {
- %%io.stdout.printf("max u8: ");
- %%io.stdout.print_u64(@max_value(u8));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("max u16: ");
- %%io.stdout.print_u64(@max_value(u16));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("max u32: ");
- %%io.stdout.print_u64(@max_value(u32));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("max u64: ");
- %%io.stdout.print_u64(@max_value(u64));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("max i8: ");
- %%io.stdout.print_i64(@max_value(i8));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("max i16: ");
- %%io.stdout.print_i64(@max_value(i16));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("max i32: ");
- %%io.stdout.print_i64(@max_value(i32));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("max i64: ");
- %%io.stdout.print_i64(@max_value(i64));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min u8: ");
- %%io.stdout.print_u64(@min_value(u8));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min u16: ");
- %%io.stdout.print_u64(@min_value(u16));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min u32: ");
- %%io.stdout.print_u64(@min_value(u32));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min u64: ");
- %%io.stdout.print_u64(@min_value(u64));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min i8: ");
- %%io.stdout.print_i64(@min_value(i8));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min i16: ");
- %%io.stdout.print_i64(@min_value(i16));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min i32: ");
- %%io.stdout.print_i64(@min_value(i32));
- %%io.stdout.printf("\n");
-
- %%io.stdout.printf("min i64: ");
- %%io.stdout.print_i64(@min_value(i64));
- %%io.stdout.printf("\n");
-}
- )SOURCE",
- "max u8: 255\n"
- "max u16: 65535\n"
- "max u32: 4294967295\n"
- "max u64: 18446744073709551615\n"
- "max i8: 127\n"
- "max i16: 32767\n"
- "max i32: 2147483647\n"
- "max i64: 9223372036854775807\n"
- "min u8: 0\n"
- "min u16: 0\n"
- "min u32: 0\n"
- "min u64: 0\n"
- "min i8: -128\n"
- "min i16: -32768\n"
- "min i32: -2147483648\n"
- "min i64: -9223372036854775808\n");
-
-
- add_simple_case("overflow intrinsics", R"SOURCE(
-const io = @import("std").io;
-pub fn main(args: [][]u8) -> %void {
- var result: u8 = undefined;
- if (!@add_with_overflow(u8, 250, 100, &result)) {
- %%io.stdout.printf("BAD\n");
- }
- if (@add_with_overflow(u8, 100, 150, &result)) {
- %%io.stdout.printf("BAD\n");
- }
- if (result != 250) {
- %%io.stdout.printf("BAD\n");
- }
- %%io.stdout.printf("OK\n");
-}
- )SOURCE", "OK\n");
-
add_simple_case("order-independent declarations", R"SOURCE(
const io = @import("std").io;
const z = io.stdin_fileno;
@@ -567,18 +405,6 @@ fn print_ok(val: @typeof(x)) -> @typeof(foo) {
const foo : i32 = 0;
)SOURCE", "OK\n");
- add_simple_case("nested arrays", R"SOURCE(
-const io = @import("std").io;
-
-pub fn main(args: [][]u8) -> %void {
- const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
- for (array_of_strings) |str| {
- %%io.stdout.printf(str);
- %%io.stdout.printf("\n");
- }
-}
- )SOURCE", "hello\nthis\nis\nmy\nthing\n");
-
add_simple_case("for loops", R"SOURCE(
const io = @import("std").io;
@@ -604,126 +430,6 @@ pub fn main(args: [][]u8) -> %void {
}
)SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");
- add_simple_case("function pointers", R"SOURCE(
-const io = @import("std").io;
-
-pub fn main(args: [][]u8) -> %void {
- const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
- for (fns) |f| {
- %%io.stdout.print_u64(f());
- %%io.stdout.printf("\n");
- }
-}
-
-fn fn1() -> u32 {5}
-fn fn2() -> u32 {6}
-fn fn3() -> u32 {7}
-fn fn4() -> u32 {8}
- )SOURCE", "5\n6\n7\n8\n");
-
- add_simple_case("statically initialized struct", R"SOURCE(
-const io = @import("std").io;
-struct Foo {
- x: i32,
- y: bool,
-}
-var foo = Foo { .x = 13, .y = true, };
-pub fn main(args: [][]u8) -> %void {
- foo.x += 1;
- if (foo.x != 14) {
- %%io.stdout.printf("BAD\n");
- }
-
- %%io.stdout.printf("OK\n");
-}
- )SOURCE", "OK\n");
-
- add_simple_case("statically initialized array literal", R"SOURCE(
-const io = @import("std").io;
-const x = []u8{1,2,3,4};
-pub fn main(args: [][]u8) -> %void {
- const y : [4]u8 = x;
- if (y[3] != 4) {
- %%io.stdout.printf("BAD\n");
- }
-
- %%io.stdout.printf("OK\n");
-}
- )SOURCE", "OK\n");
-
- add_simple_case("return with implicit cast from while loop", R"SOURCE(
-const io = @import("std").io;
-pub fn main(args: [][]u8) -> %void {
- while (true) {
- %%io.stdout.printf("OK\n");
- return;
- }
-}
- )SOURCE", "OK\n");
-
- add_simple_case("return struct byval from function", R"SOURCE(
-const io = @import("std").io;
-struct Foo {
- x: i32,
- y: i32,
-}
-fn make_foo(x: i32, y: i32) -> Foo {
- Foo {
- .x = x,
- .y = y,
- }
-}
-pub fn main(args: [][]u8) -> %void {
- const foo = make_foo(1234, 5678);
- if (foo.y != 5678) {
- %%io.stdout.printf("BAD\n");
- }
- %%io.stdout.printf("OK\n");
-}
- )SOURCE", "OK\n");
-
- add_simple_case("string concatenation", R"SOURCE(
-const io = @import("std").io;
-pub fn main(args: [][]u8) -> %void {
- %%io.stdout.printf("OK" ++ " IT " ++ "WORKED\n");
-}
- )SOURCE", "OK IT WORKED\n");
-
- add_simple_case("constant struct with negation", R"SOURCE(
-const io = @import("std").io;
-struct Vertex {
- x: f32,
- y: f32,
- r: f32,
- g: f32,
- b: f32,
-}
-const vertices = []Vertex {
- Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
- Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
- Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
-};
-pub fn main(args: [][]u8) -> %void {
- if (vertices[0].x != -0.6) {
- %%io.stdout.printf("BAD\n");
- }
- %%io.stdout.printf("OK\n");
-}
- )SOURCE", "OK\n");
-
- add_simple_case("int to ptr cast", R"SOURCE(
-const io = @import("std").io;
-pub fn main(args: [][]u8) -> %void {
- const x = isize(13);
- const y = (&u8)(x);
- const z = usize(y);
- if (z != 13) {
- %%io.stdout.printf("BAD\n");
- }
- %%io.stdout.printf("OK\n");
-}
- )SOURCE", "OK\n");
-
add_simple_case("pointer to void return type", R"SOURCE(
const io = @import("std").io;
const x = void{};
test/self_hosted.zig
@@ -986,3 +986,183 @@ fn get_byte(ptr: ?&u8) -> u8 {*??ptr}
fn get_first_byte(T: type)(mem: []T) -> u8 {
get_byte((&u8)(&mem[0]))
}
+
+#attribute("test")
+fn continue_and_break() {
+ run_continue_and_break_test();
+ assert(continue_and_break_counter == 8);
+}
+var continue_and_break_counter: i32 = 0;
+fn run_continue_and_break_test() {
+ var i : i32 = 0;
+ while (true) {
+ continue_and_break_counter += 2;
+ i += 1;
+ if (i < 4) {
+ continue;
+ }
+ break;
+ }
+ assert(i == 4);
+}
+
+#attribute("test")
+fn sizeof_and_typeof() {
+ const y: @typeof(sizeof_and_typeof_x) = 120;
+ assert(@sizeof(@typeof(y)) == 2);
+}
+const sizeof_and_typeof_x: u16 = 13;
+const sizeof_and_typeof_z: @typeof(sizeof_and_typeof_x) = 19;
+
+
+#attribute("test")
+fn pointer_dereferencing() {
+ var x = i32(3);
+ const y = &x;
+
+ *y += 1;
+
+ assert(x == 4);
+ assert(*y == 4);
+}
+
+#attribute("test")
+fn constant_expressions() {
+ var array : [ARRAY_SIZE]u8 = undefined;
+ assert(@sizeof(@typeof(array)) == 20);
+}
+const ARRAY_SIZE : i8 = 20;
+
+
+#attribute("test")
+fn min_value_and_max_value() {
+ assert(@max_value(u8) == 255);
+ assert(@max_value(u16) == 65535);
+ assert(@max_value(u32) == 4294967295);
+ assert(@max_value(u64) == 18446744073709551615);
+
+ assert(@max_value(i8) == 127);
+ assert(@max_value(i16) == 32767);
+ assert(@max_value(i32) == 2147483647);
+ assert(@max_value(i64) == 9223372036854775807);
+
+ assert(@min_value(u8) == 0);
+ assert(@min_value(u16) == 0);
+ assert(@min_value(u32) == 0);
+ assert(@min_value(u64) == 0);
+
+ assert(@min_value(i8) == -128);
+ assert(@min_value(i16) == -32768);
+ assert(@min_value(i32) == -2147483648);
+ assert(@min_value(i64) == -9223372036854775808);
+}
+
+#attribute("test")
+fn overflow_intrinsics() {
+ var result: u8 = undefined;
+ assert(@add_with_overflow(u8, 250, 100, &result));
+ assert(!@add_with_overflow(u8, 100, 150, &result));
+ assert(result == 250);
+}
+
+
+#attribute("test")
+fn nested_arrays() {
+ const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
+ for (array_of_strings) |str, i| {
+ if (i == 0) assert(str_eql(str, "hello"));
+ if (i == 1) assert(str_eql(str, "this"));
+ if (i == 2) assert(str_eql(str, "is"));
+ if (i == 3) assert(str_eql(str, "my"));
+ if (i == 4) assert(str_eql(str, "thing"));
+ }
+}
+
+#attribute("test")
+fn int_to_ptr_cast() {
+ const x = isize(13);
+ const y = (&u8)(x);
+ const z = usize(y);
+ assert(z == 13);
+}
+
+#attribute("test")
+fn string_concatenation() {
+ assert(str_eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
+}
+
+#attribute("test")
+fn constant_struct_with_negation() {
+ assert(vertices[0].x == -0.6);
+}
+struct Vertex {
+ x: f32,
+ y: f32,
+ r: f32,
+ g: f32,
+ b: f32,
+}
+const vertices = []Vertex {
+ Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
+ Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
+ Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
+};
+
+
+#attribute("test")
+fn return_with_implicit_cast_from_while_loop() {
+ %%return_with_implicit_cast_from_while_loop_test();
+}
+fn return_with_implicit_cast_from_while_loop_test() -> %void {
+ while (true) {
+ return;
+ }
+}
+
+#attribute("test")
+fn return_struct_byval_from_function() {
+ const bar = make_bar(1234, 5678);
+ assert(bar.y == 5678);
+}
+struct Bar {
+ x: i32,
+ y: i32,
+}
+fn make_bar(x: i32, y: i32) -> Bar {
+ Bar {
+ .x = x,
+ .y = y,
+ }
+}
+
+#attribute("test")
+fn function_pointers() {
+ const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
+ for (fns) |f, i| {
+ assert(f() == u32(i) + 5);
+ }
+}
+fn fn1() -> u32 {5}
+fn fn2() -> u32 {6}
+fn fn3() -> u32 {7}
+fn fn4() -> u32 {8}
+
+
+
+#attribute("test")
+fn statically_initalized_struct() {
+ st_init_str_foo.x += 1;
+ assert(st_init_str_foo.x == 14);
+}
+struct StInitStrFoo {
+ x: i32,
+ y: bool,
+}
+var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
+
+#attribute("test")
+fn statically_initialized_array_literal() {
+ const y : [4]u8 = st_init_arr_lit_x;
+ assert(y[3] == 4);
+}
+const st_init_arr_lit_x = []u8{1,2,3,4};