authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-13 16:46:27-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-13 16:46:27-04:00
log06f2f4d64b63cf78a3ff77cc64dbc822123f454d
tree2444a286bc3a33c09fa769f692a4fc013316c7c3
parentea2f6594ce02187875ce052c1a7c43acf60884f9

change `unreachable{}` to `@unreachable()`

instead of a container init expression, it's a builtin function call.

13 files changed, 89 insertions(+), 81 deletions(-)

src/all_types.hpp+1
......@@ -1174,6 +1174,7 @@ enum BuiltinFnId {
11741174 BuiltinFnIdDivExact,
11751175 BuiltinFnIdTruncate,
11761176 BuiltinFnIdIntType,
1177 BuiltinFnIdUnreachable,
11771178};
11781179
11791180struct BuiltinFnEntry {
src/analyze.cpp+2-7
......@@ -2677,13 +2677,6 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
26772677 } else {
26782678 return resolve_expr_const_val_as_void(g, node);
26792679 }
2680 } else if (container_type->id == TypeTableEntryIdUnreachable) {
2681 if (container_init_expr->entries.length != 0) {
2682 add_node_error(g, node, buf_sprintf("unreachable expression expects no arguments"));
2683 return g->builtin_types.entry_invalid;
2684 } else {
2685 return container_type;
2686 }
26872680 } else {
26882681 add_node_error(g, node,
26892682 buf_sprintf("type '%s' does not support %s initialization syntax",
......@@ -5435,6 +5428,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
54355428 return analyze_compile_err(g, import, context, node);
54365429 case BuiltinFnIdIntType:
54375430 return analyze_int_type(g, import, context, node);
5431 case BuiltinFnIdUnreachable:
5432 return g->builtin_types.entry_unreachable;
54385433 }
54395434 zig_unreachable();
54405435}
src/codegen.cpp+17-9
......@@ -497,6 +497,20 @@ static LLVMValueRef gen_truncate(CodeGen *g, AstNode *node) {
497497 return LLVMBuildTrunc(g->builder, src_val, dest_type->type_ref, "");
498498}
499499
500static LLVMValueRef gen_unreachable(CodeGen *g, AstNode *node) {
501 assert(node->type == NodeTypeFnCallExpr);
502
503 set_debug_source_node(g, node);
504
505 if (want_debug_safety(g, node) || g->is_test_build) {
506 gen_debug_safety_crash(g);
507 } else {
508 LLVMBuildUnreachable(g->builder);
509 }
510
511 return nullptr;
512}
513
500514static LLVMValueRef gen_shl_with_overflow(CodeGen *g, AstNode *node) {
501515 assert(node->type == NodeTypeFnCallExpr);
502516
......@@ -689,6 +703,8 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) {
689703 return gen_div_exact(g, node);
690704 case BuiltinFnIdTruncate:
691705 return gen_truncate(g, node);
706 case BuiltinFnIdUnreachable:
707 return gen_unreachable(g, node);
692708 }
693709 zig_unreachable();
694710}
......@@ -2949,15 +2965,6 @@ static LLVMValueRef gen_container_init_expr(CodeGen *g, AstNode *node) {
29492965 }
29502966
29512967 return tmp_struct_ptr;
2952 } else if (type_entry->id == TypeTableEntryIdUnreachable) {
2953 assert(node->data.container_init_expr.entries.length == 0);
2954 set_debug_source_node(g, node);
2955 if (want_debug_safety(g, node) || g->is_test_build) {
2956 gen_debug_safety_crash(g);
2957 } else {
2958 LLVMBuildUnreachable(g->builder);
2959 }
2960 return nullptr;
29612968 } else if (type_entry->id == TypeTableEntryIdVoid) {
29622969 assert(node->data.container_init_expr.entries.length == 0);
29632970 return nullptr;
......@@ -4859,6 +4866,7 @@ static void define_builtin_fns(CodeGen *g) {
48594866 create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2);
48604867 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compileError", 1);
48614868 create_builtin_fn_with_arg_count(g, BuiltinFnIdIntType, "intType", 2);
4869 create_builtin_fn_with_arg_count(g, BuiltinFnIdUnreachable, "unreachable", 0);
48624870}
48634871
48644872static void init(CodeGen *g, Buf *source_path) {
src/eval.cpp+11-7
......@@ -491,13 +491,6 @@ static bool eval_container_init_expr(EvalFn *ef, AstNode *node, ConstExprValue *
491491 }
492492 } else if (container_type->id == TypeTableEntryIdVoid) {
493493 return false;
494 } else if (container_type->id == TypeTableEntryIdUnreachable) {
495 ef->root->abort = true;
496 ErrorMsg *msg = add_node_error(ef->root->codegen, ef->root->fn->fn_def_node,
497 buf_sprintf("function evaluation reached unreachable expression"));
498 add_error_note(ef->root->codegen, msg, ef->root->call_node, buf_sprintf("called from here"));
499 add_error_note(ef->root->codegen, msg, node, buf_sprintf("unreachable expression here"));
500 return true;
501494 } else if (container_type->id == TypeTableEntryIdStruct &&
502495 container_type->data.structure.is_slice &&
503496 kind == ContainerInitKindArray)
......@@ -791,6 +784,15 @@ static bool eval_div_exact(EvalFn *ef, AstNode *node, ConstExprValue *out_val) {
791784 return false;
792785}
793786
787static bool eval_unreachable(EvalFn *ef, AstNode *node, ConstExprValue *out_val) {
788 ef->root->abort = true;
789 ErrorMsg *msg = add_node_error(ef->root->codegen, ef->root->fn->fn_def_node,
790 buf_sprintf("function evaluation reached unreachable expression"));
791 add_error_note(ef->root->codegen, msg, ef->root->call_node, buf_sprintf("called from here"));
792 add_error_note(ef->root->codegen, msg, node, buf_sprintf("unreachable expression here"));
793 return true;
794}
795
794796static bool eval_fn_with_overflow(EvalFn *ef, AstNode *node, ConstExprValue *out_val,
795797 bool (*bignum_fn)(BigNum *dest, BigNum *op1, BigNum *op2))
796798{
......@@ -851,6 +853,8 @@ static bool eval_fn_call_builtin(EvalFn *ef, AstNode *node, ConstExprValue *out_
851853 return false;
852854 case BuiltinFnIdDivExact:
853855 return eval_div_exact(ef, node, out_val);
856 case BuiltinFnIdUnreachable:
857 return eval_unreachable(ef, node, out_val);
854858 case BuiltinFnIdMemcpy:
855859 case BuiltinFnIdMemset:
856860 case BuiltinFnIdSizeof:
std/compiler_rt.zig+1-1
......@@ -266,5 +266,5 @@ fn test_one_udivmoddi4(a: du_int, b: du_int, expected_q: du_int, expected_r: du_
266266}
267267
268268fn assert(b: bool) {
269 if (!b) unreachable{};
269 if (!b) @unreachable();
270270}
std/debug.zig+1-1
......@@ -9,7 +9,7 @@ pub error InvalidDebugInfo;
99pub error UnsupportedDebugInfo;
1010
1111pub fn assert(b: bool) {
12 if (!b) unreachable{}
12 if (!b) @unreachable()
1313}
1414
1515pub fn printStackTrace() -> %void {
std/hash_map.zig+4-4
......@@ -55,7 +55,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
5555 return entry;
5656 }
5757 }
58 unreachable{} // no next item
58 @unreachable() // no next item
5959 }
6060 }
6161
......@@ -137,9 +137,9 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
137137 entry.distance_from_start_index -= 1;
138138 entry = next_entry;
139139 }
140 unreachable{} // shifting everything in the table
140 @unreachable() // shifting everything in the table
141141 }}
142 unreachable{} // key not found
142 @unreachable() // key not found
143143 }
144144
145145 pub fn entryIterator(hm: &Self) -> Iterator {
......@@ -210,7 +210,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
210210 };
211211 return;
212212 }
213 unreachable{} // put into a full map
213 @unreachable() // put into a full map
214214 }
215215
216216 fn internalGet(hm: &Self, key: K) -> ?&Entry {
std/io.zig+7-7
......@@ -116,7 +116,7 @@ pub struct OutStream {
116116 return switch (write_err) {
117117 errno.EINTR => continue,
118118
119 errno.EINVAL => unreachable{},
119 errno.EINVAL => @unreachable(),
120120 errno.EDQUOT => error.DiskQuota,
121121 errno.EFBIG => error.FileTooBig,
122122 errno.EIO => error.Io,
......@@ -165,8 +165,8 @@ pub struct InStream {
165165 return switch (err) {
166166 errno.EINTR => continue,
167167
168 errno.EFAULT => unreachable{},
169 errno.EINVAL => unreachable{},
168 errno.EFAULT => @unreachable(),
169 errno.EINVAL => @unreachable(),
170170 errno.EACCES => error.BadPerm,
171171 errno.EFBIG, errno.EOVERFLOW => error.FileTooBig,
172172 errno.EISDIR => error.IsDir,
......@@ -228,8 +228,8 @@ pub struct InStream {
228228 switch (read_err) {
229229 errno.EINTR => continue,
230230
231 errno.EINVAL => unreachable{},
232 errno.EFAULT => unreachable{},
231 errno.EINVAL => @unreachable(),
232 errno.EFAULT => @unreachable(),
233233 errno.EBADF => return error.BadFd,
234234 errno.EIO => return error.Io,
235235 else => return error.Unexpected,
......@@ -426,9 +426,9 @@ fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
426426fn parseU64DigitTooBig() {
427427 parseUnsigned(u64, "123a", 10) %% |err| {
428428 if (err == error.InvalidChar) return;
429 unreachable{};
429 @unreachable();
430430 };
431 unreachable{};
431 @unreachable();
432432}
433433
434434pub fn openSelfExe(stream: &InStream) -> %void {
std/linux.zig+1-1
......@@ -299,7 +299,7 @@ pub fn lseek(fd: i32, offset: usize, ref_pos: usize) -> usize {
299299
300300pub fn exit(status: i32) -> unreachable {
301301 arch.syscall1(arch.SYS_exit, usize(status));
302 unreachable{}
302 @unreachable()
303303}
304304
305305pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize {
std/net.zig+13-13
......@@ -21,8 +21,8 @@ struct Connection {
2121 const send_err = linux.getErrno(send_ret);
2222 switch (send_err) {
2323 0 => return send_ret,
24 errno.EINVAL => unreachable{},
25 errno.EFAULT => unreachable{},
24 errno.EINVAL => @unreachable(),
25 errno.EFAULT => @unreachable(),
2626 errno.ECONNRESET => return error.ConnectionReset,
2727 errno.EINTR => return error.SigInterrupt,
2828 // TODO there are more possible errors
......@@ -35,8 +35,8 @@ struct Connection {
3535 const recv_err = linux.getErrno(recv_ret);
3636 switch (recv_err) {
3737 0 => return buf[0...recv_ret],
38 errno.EINVAL => unreachable{},
39 errno.EFAULT => unreachable{},
38 errno.EINVAL => @unreachable(),
39 errno.EFAULT => @unreachable(),
4040 errno.ENOTSOCK => return error.NotSocket,
4141 errno.EINTR => return error.SigInterrupt,
4242 errno.ENOMEM => return error.NoMem,
......@@ -50,7 +50,7 @@ struct Connection {
5050 pub fn close(c: Connection) -> %void {
5151 switch (linux.getErrno(linux.close(c.socket_fd))) {
5252 0 => return,
53 errno.EBADF => unreachable{},
53 errno.EBADF => @unreachable(),
5454 errno.EINTR => return error.SigInterrupt,
5555 errno.EIO => return error.Io,
5656 else => return error.Unexpected,
......@@ -74,7 +74,7 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address {
7474// if (family != AF_INET)
7575// buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
7676//
77 unreachable{} // TODO
77 @unreachable() // TODO
7878 }
7979
8080 switch (parseIpLiteral(hostname)) {
......@@ -85,7 +85,7 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address {
8585 else => {},
8686 };
8787
88 unreachable{} // TODO
88 @unreachable() // TODO
8989}
9090
9191pub fn connectAddr(addr: &Address, port: u16) -> %Connection {
......@@ -113,7 +113,7 @@ pub fn connectAddr(addr: &Address, port: u16) -> %Connection {
113113 @memcpy(&os_addr.addr[0], &addr.addr[0], 16);
114114 linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in6))
115115 } else {
116 unreachable{}
116 @unreachable()
117117 };
118118 const connect_err = linux.getErrno(connect_ret);
119119 if (connect_err > 0) {
......@@ -321,11 +321,11 @@ fn parseIp4(buf: []const u8) -> %u32 {
321321#attribute("test")
322322fn testParseIp4() {
323323 assert(%%parseIp4("127.0.0.1") == endian.swapIfLe(u32, 0x7f000001));
324 switch (parseIp4("256.0.0.1")) { Overflow => {}, else => unreachable {}, }
325 switch (parseIp4("x.0.0.1")) { InvalidChar => {}, else => unreachable {}, }
326 switch (parseIp4("127.0.0.1.1")) { JunkAtEnd => {}, else => unreachable {}, }
327 switch (parseIp4("127.0.0.")) { Incomplete => {}, else => unreachable {}, }
328 switch (parseIp4("100..0.1")) { InvalidChar => {}, else => unreachable {}, }
324 switch (parseIp4("256.0.0.1")) { Overflow => {}, else => @unreachable(), }
325 switch (parseIp4("x.0.0.1")) { InvalidChar => {}, else => @unreachable(), }
326 switch (parseIp4("127.0.0.1.1")) { JunkAtEnd => {}, else => @unreachable(), }
327 switch (parseIp4("127.0.0.")) { Incomplete => {}, else => @unreachable(), }
328 switch (parseIp4("100..0.1")) { InvalidChar => {}, else => @unreachable(), }
329329}
330330
331331#attribute("test")
std/os.zig+2-2
......@@ -11,8 +11,8 @@ pub fn getRandomBytes(buf: []u8) -> %void {
1111 const err = linux.getErrno(ret);
1212 if (err > 0) {
1313 return switch (err) {
14 errno.EINVAL => unreachable{},
15 errno.EFAULT => unreachable{},
14 errno.EINVAL => @unreachable(),
15 errno.EFAULT => @unreachable(),
1616 errno.EINTR => error.SigInterrupt,
1717 else => error.Unexpected,
1818 }
test/run_tests.cpp+2-2
......@@ -485,8 +485,8 @@ pub fn main(args: [][]u8) -> %void {
485485const c = @cImport(@cInclude("stdlib.h"));
486486
487487export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
488 const a_int = (&i32)(a ?? unreachable{});
489 const b_int = (&i32)(b ?? unreachable{});
488 const a_int = (&i32)(a ?? @unreachable());
489 const b_int = (&i32)(b ?? @unreachable());
490490 if (*a_int < *b_int) {
491491 -1
492492 } else if (*a_int > *b_int) {
test/self_hosted.zig+27-27
......@@ -25,20 +25,20 @@ fn ifStatements() {
2525}
2626fn shouldBeEqual(a: i32, b: i32) {
2727 if (a != b) {
28 unreachable{};
28 @unreachable();
2929 } else {
3030 return;
3131 }
3232}
3333fn firstEqlThird(a: i32, b: i32, c: i32) {
3434 if (a == b) {
35 unreachable{};
35 @unreachable();
3636 } else if (b == c) {
37 unreachable{};
37 @unreachable();
3838 } else if (a == c) {
3939 return;
4040 } else {
41 unreachable{};
41 @unreachable();
4242 }
4343}
4444
......@@ -58,7 +58,7 @@ fn localVariables() {
5858}
5959fn testLocVars(b: i32) {
6060 const a: i32 = 1;
61 if (a + b != 3) unreachable{};
61 if (a + b != 3) @unreachable();
6262}
6363
6464#attribute("test")
......@@ -145,7 +145,7 @@ fn shortCircuit() {
145145
146146#static_eval_enable(false)
147147fn assertRuntime(b: bool) {
148 if (!b) unreachable{}
148 if (!b) @unreachable()
149149}
150150
151151#attribute("test")
......@@ -328,10 +328,10 @@ fn maybeType() {
328328 if (y) {
329329 // OK
330330 } else {
331 unreachable{};
331 @unreachable();
332332 }
333333 } else {
334 unreachable{};
334 @unreachable();
335335 }
336336
337337 const next_x : ?i32 = null;
......@@ -342,7 +342,7 @@ fn maybeType() {
342342
343343 const final_x : ?i32 = 13;
344344
345 const num = final_x ?? unreachable{};
345 const num = final_x ?? @unreachable();
346346
347347 assert(num == 13);
348348}
......@@ -360,7 +360,7 @@ fn enumType() {
360360 const expected_foo_size = switch (@compileVar("arch")) {
361361 i386 => 20,
362362 x86_64 => 24,
363 else => unreachable{},
363 else => @unreachable(),
364364 };
365365 assert(@sizeOf(EnumTypeFoo) == expected_foo_size);
366366 assert(@sizeOf(EnumTypeBar) == 1);
......@@ -437,7 +437,7 @@ error AnError;
437437error AnError;
438438error SecondError;
439439fn shouldBeNotEqual(a: error, b: error) {
440 if (a == b) unreachable{}
440 if (a == b) @unreachable()
441441}
442442
443443
......@@ -454,13 +454,13 @@ fn constantEnumWithPayload() {
454454fn shouldBeEmpty(x: AnEnumWithPayload) {
455455 switch (x) {
456456 Empty => {},
457 else => unreachable{},
457 else => @unreachable(),
458458 }
459459}
460460
461461fn shouldBeNotEmpty(x: AnEnumWithPayload) {
462462 switch (x) {
463 Empty => unreachable{},
463 Empty => @unreachable(),
464464 else => {},
465465 }
466466}
......@@ -482,7 +482,7 @@ fn continueInForLoop() {
482482 }
483483 break;
484484 }
485 if (sum != 6) unreachable{}
485 if (sum != 6) @unreachable()
486486}
487487
488488
......@@ -514,9 +514,9 @@ enum Fruit {
514514#static_eval_enable(false)
515515fn nonConstSwitchOnEnum(fruit: Fruit) {
516516 switch (fruit) {
517 Apple => unreachable{},
517 Apple => @unreachable(),
518518 Orange => {},
519 Banana => unreachable{},
519 Banana => @unreachable(),
520520 }
521521}
522522
......@@ -532,7 +532,7 @@ fn nonConstSwitch(foo: SwitchStatmentFoo) {
532532 C => 3,
533533 D => 4,
534534 };
535 if (val != 3) unreachable{};
535 if (val != 3) @unreachable();
536536}
537537enum SwitchStatmentFoo {
538538 A,
......@@ -557,10 +557,10 @@ enum SwitchProngWithVarEnum {
557557fn switchProngWithVarFn(a: SwitchProngWithVarEnum) {
558558 switch(a) {
559559 One => |x| {
560 if (x != 13) unreachable{};
560 if (x != 13) @unreachable();
561561 },
562562 Two => |x| {
563 if (x != 13.0) unreachable{};
563 if (x != 13.0) @unreachable();
564564 },
565565 Meh => |x| {
566566 const v: void = x;
......@@ -601,7 +601,7 @@ fn implicitCastFnUnreachableReturn() {
601601fn wantsFnWithVoid(f: fn()) { }
602602
603603fn fnWithUnreachable() -> unreachable {
604 unreachable {}
604 @unreachable()
605605}
606606
607607
......@@ -644,13 +644,13 @@ fn slicing() {
644644
645645 var slice = array[5...10];
646646
647 if (slice.len != 5) unreachable{};
647 if (slice.len != 5) @unreachable();
648648
649649 const ptr = &slice[0];
650 if (ptr[0] != 1234) unreachable{};
650 if (ptr[0] != 1234) @unreachable();
651651
652652 var slice_rest = array[10...];
653 if (slice_rest.len != 10) unreachable{};
653 if (slice_rest.len != 10) @unreachable();
654654}
655655
656656
......@@ -662,7 +662,7 @@ fn memcpyAndMemsetIntrinsics() {
662662 @memset(&foo[0], 'A', foo.len);
663663 @memcpy(&bar[0], &foo[0], bar.len);
664664
665 if (bar[11] != 'A') unreachable{};
665 if (bar[11] != 'A') @unreachable();
666666}
667667
668668
......@@ -807,7 +807,7 @@ exit:
807807 if (it_worked) {
808808 return;
809809 }
810 unreachable{};
810 @unreachable();
811811entry:
812812 defer it_worked = true;
813813 if (b) goto exit;
......@@ -1221,7 +1221,7 @@ fn test3_1(f: Test3Foo) {
12211221 assert(pt.x == 3);
12221222 assert(pt.y == 4);
12231223 },
1224 else => unreachable{},
1224 else => @unreachable(),
12251225 }
12261226}
12271227#static_eval_enable(false)
......@@ -1230,7 +1230,7 @@ fn test3_2(f: Test3Foo) {
12301230 Two => |x| {
12311231 assert(x == 13);
12321232 },
1233 else => unreachable{},
1233 else => @unreachable(),
12341234 }
12351235}
12361236