authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-02 20:13:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-02 20:13:10-07:00
log258bc73eee557ada944c9d25ed85d9baf8035ad7
treed34d6cca4d4f91a6938ff27fb5eb55c9b7ce1915
parent187d00ca835d2c923cbc0a3ab9e861e82888d403

fix implicit cast after unreachable bad code gen


3 files changed, 50 insertions(+), 40 deletions(-)

src/codegen.cpp+3-15
......@@ -203,21 +203,6 @@ static LLVMValueRef gen_array_ptr(CodeGen *g, AstNode *node) {
203203 AstNode *array_expr_node = node->data.array_access_expr.array_ref_expr;
204204
205205 LLVMValueRef array_ptr = gen_expr(g, array_expr_node);
206 /*
207 if (array_expr_node->type == NodeTypeSymbol) {
208 VariableTableEntry *var = find_variable(array_expr_node->codegen_node->expr_node.block_context,
209 &array_expr_node->data.symbol);
210 assert(var);
211
212 array_ptr = var->value_ref;
213 } else if (array_expr_node->type == NodeTypeFieldAccessExpr) {
214 zig_panic("TODO gen array ptr field access expr");
215 } else if (array_expr_node->type == NodeTypeArrayAccessExpr) {
216 zig_panic("TODO gen array ptr array access expr");
217 } else {
218 array_ptr = gen_expr(g, array_expr_node);
219 }
220 */
221206
222207 LLVMValueRef subscript_value = gen_expr(g, node->data.array_access_expr.subscript);
223208
......@@ -1363,6 +1348,9 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
13631348
13641349 {
13651350 TypeTableEntry *before_type = node->codegen_node->expr_node.type_entry;
1351 if (before_type && before_type->id == TypeTableEntryIdUnreachable) {
1352 return val;
1353 }
13661354 val = gen_cast_node(g, node, val, before_type, &node->codegen_node->expr_node.implicit_cast);
13671355 }
13681356
std/std.zig+26-25
......@@ -19,8 +19,8 @@ fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
1919 : "rcx", "r11")
2020}
2121
22pub fn getrandom(buf: &u8, count: usize, flags: u32) -> isize {
23 return syscall3(SYS_getrandom, buf as usize, count, flags as usize) as isize;
22pub fn getrandom(buf: &u8, count: usize, flags: u32) -> i32 {
23 return syscall3(SYS_getrandom, buf as usize, count, flags as usize) as i32;
2424}
2525
2626pub fn write(fd: isize, buf: &const u8, count: usize) -> isize {
......@@ -32,6 +32,30 @@ pub fn exit(status: i32) -> unreachable {
3232 unreachable;
3333}
3434
35// TODO error handling
36pub fn os_get_random_bytes(buf: &u8, count: usize) -> i32 {
37 return getrandom(buf, count, 0);
38}
39
40// TODO error handling
41// TODO handle buffering and flushing (mutex protected)
42pub fn print_str(str: string) -> isize { fprint_str(stdout_fileno, str) }
43
44// TODO error handling
45// TODO handle buffering and flushing (mutex protected)
46pub fn fprint_str(fd: isize, str: string) -> isize {
47 return write(fd, str.ptr, str.len);
48}
49
50// TODO handle buffering and flushing (mutex protected)
51// TODO error handling
52pub fn print_u64(x: u64) -> isize {
53 // TODO use max_u64_base10_digits instead of hardcoding 20
54 var buf: [u8; 20];
55 const len = buf_print_u64(buf.ptr, x);
56 return write(stdout_fileno, buf.ptr, len);
57}
58
3559fn digit_to_char(digit: u64) -> u8 { '0' + (digit as u8) }
3660
3761const max_u64_base10_digits: usize = 20;
......@@ -63,26 +87,3 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
6387 return len;
6488}
6589
66// TODO error handling
67// TODO handle buffering and flushing (mutex protected)
68pub fn print_str(str: string) -> isize { fprint_str(stdout_fileno, str) }
69
70// TODO error handling
71// TODO handle buffering and flushing (mutex protected)
72pub fn fprint_str(fd: isize, str: string) -> isize {
73 return write(fd, str.ptr, str.len);
74}
75
76// TODO handle buffering and flushing (mutex protected)
77// TODO error handling
78pub fn print_u64(x: u64) -> isize {
79 // TODO use max_u64_base10_digits instead of hardcoding 20
80 var buf: [u8; 20];
81 const len = buf_print_u64(buf.ptr, x);
82 return write(stdout_fileno, buf.ptr, len);
83}
84
85// TODO error handling
86pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize {
87 return getrandom(buf, count, 0);
88}
test/run_tests.cpp+21
......@@ -682,6 +682,21 @@ export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
682682 return 0;
683683}
684684 )SOURCE", "x is true\n");
685
686 add_simple_case("implicit cast after unreachable", R"SOURCE(
687use "std.zig";
688export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
689 const x = outer();
690 if (x == 1234) {
691 print_str("OK\n");
692 }
693 return 0;
694}
695fn inner() -> i32 { 1234 }
696fn outer() -> isize {
697 return inner();
698}
699 )SOURCE", "OK\n");
685700}
686701
687702////////////////////////////////////////////////////////////////////////////////////
......@@ -978,6 +993,12 @@ fn f() {
978993 if (const x ?= true) { }
979994}
980995 )SOURCE", 1, ".tmp_source.zig:3:20: error: expected maybe type");
996
997 add_compile_fail_case("cast unreachable", R"SOURCE(
998fn f() -> i32 {
999 (return 1) as i32
1000}
1001 )SOURCE", 1, ".tmp_source.zig:3:16: error: invalid cast from type 'unreachable' to 'i32'");
9811002}
9821003
9831004static void print_compiler_invocation(TestCase *test_case) {