authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-24 15:00:47-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-24 15:01:19-05:00
loga665872e881920e8020a513340d2427b88a55bd6
treeff81a6b509584d737d0df15d293dc01ae1390469
parent3b40aaa01fb15799cf27d662229597ac98e2ab77

add compile error for ignoring return value

also introduce the _ identifier which you can assign to to discard a return value closes #219

10 files changed, 102 insertions(+), 53 deletions(-)

src/all_types.hpp+3
......@@ -117,6 +117,9 @@ enum ConstPtrSpecial {
117117 // emit a binary with a compile time known address.
118118 // In this case index is the numeric address value.
119119 ConstPtrSpecialHardCodedAddr,
120 // This means that the pointer represents memory of assigning to _.
121 // That is, storing discards the data, and loading is invalid.
122 ConstPtrSpecialDiscard,
120123};
121124
122125enum ConstPtrMut {
src/analyze.cpp+8
......@@ -3091,6 +3091,9 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
30913091 hash_val += 4048518294;
30923092 hash_val += hash_size(const_val->data.x_ptr.data.hard_coded_addr.addr);
30933093 return hash_val;
3094 case ConstPtrSpecialDiscard:
3095 hash_val += 2010123162;
3096 return hash_val;
30943097 }
30953098 zig_unreachable();
30963099 }
......@@ -3601,6 +3604,8 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
36013604 if (a->data.x_ptr.data.hard_coded_addr.addr != b->data.x_ptr.data.hard_coded_addr.addr)
36023605 return false;
36033606 return true;
3607 case ConstPtrSpecialDiscard:
3608 return true;
36043609 }
36053610 zig_unreachable();
36063611 case TypeTableEntryIdArray:
......@@ -3785,6 +3790,9 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
37853790 buf_appendf(buf, "(&%s)(%" PRIx64 ")", buf_ptr(&canon_type->data.pointer.child_type->name),
37863791 const_val->data.x_ptr.data.hard_coded_addr.addr);
37873792 return;
3793 case ConstPtrSpecialDiscard:
3794 buf_append_str(buf, "&_");
3795 return;
37883796 }
37893797 zig_unreachable();
37903798 case TypeTableEntryIdFn:
src/codegen.cpp+1
......@@ -2928,6 +2928,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
29282928 render_const_val_global(g, const_val, "");
29292929 switch (const_val->data.x_ptr.special) {
29302930 case ConstPtrSpecialInvalid:
2931 case ConstPtrSpecialDiscard:
29312932 zig_unreachable();
29322933 case ConstPtrSpecialRef:
29332934 {
src/ir.cpp+31
......@@ -77,6 +77,8 @@ ConstExprValue *const_ptr_pointee(ConstExprValue *const_val) {
7777 const_val->data.x_ptr.data.base_struct.field_index];
7878 case ConstPtrSpecialHardCodedAddr:
7979 zig_unreachable();
80 case ConstPtrSpecialDiscard:
81 zig_unreachable();
8082 }
8183 zig_unreachable();
8284}
......@@ -3656,6 +3658,15 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
36563658
36573659 Buf *variable_name = node->data.symbol_expr.symbol;
36583660
3661 if (buf_eql_str(variable_name, "_") && lval.is_ptr) {
3662 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, node);
3663 const_instruction->base.value.type = get_pointer_to_type(irb->codegen,
3664 irb->codegen->builtin_types.entry_void, false);
3665 const_instruction->base.value.special = ConstValSpecialStatic;
3666 const_instruction->base.value.data.x_ptr.special = ConstPtrSpecialDiscard;
3667 return &const_instruction->base;
3668 }
3669
36593670 auto primitive_table_entry = irb->codegen->primitive_type_table.maybe_get(variable_name);
36603671 if (primitive_table_entry) {
36613672 IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_table_entry->value);
......@@ -8927,6 +8938,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
89278938 size_t old_size;
89288939 switch (array_ptr_val->data.x_ptr.special) {
89298940 case ConstPtrSpecialInvalid:
8941 case ConstPtrSpecialDiscard:
89308942 zig_unreachable();
89318943 case ConstPtrSpecialRef:
89328944 mem_size = 1;
......@@ -8977,6 +8989,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
89778989 out_val->data.x_ptr.mut = ptr_field->data.x_ptr.mut;
89788990 switch (ptr_field->data.x_ptr.special) {
89798991 case ConstPtrSpecialInvalid:
8992 case ConstPtrSpecialDiscard:
89808993 zig_unreachable();
89818994 case ConstPtrSpecialRef:
89828995 out_val->data.x_ptr.special = ConstPtrSpecialRef;
......@@ -9384,6 +9397,10 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
93849397 return value->value.type;
93859398
93869399 assert(ptr->value.type->id == TypeTableEntryIdPointer);
9400 if (ptr->value.data.x_ptr.special == ConstPtrSpecialDiscard) {
9401 return ir_analyze_void(ira, &store_ptr_instruction->base);
9402 }
9403
93879404 if (ptr->value.type->data.pointer.is_const && !store_ptr_instruction->base.is_gen) {
93889405 ir_add_error(ira, &store_ptr_instruction->base, buf_sprintf("cannot assign to constant"));
93899406 return ira->codegen->builtin_types.entry_invalid;
......@@ -11365,6 +11382,7 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
1136511382 size_t bound_end;
1136611383 switch (dest_ptr_val->data.x_ptr.special) {
1136711384 case ConstPtrSpecialInvalid:
11385 case ConstPtrSpecialDiscard:
1136811386 zig_unreachable();
1136911387 case ConstPtrSpecialRef:
1137011388 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
......@@ -11455,6 +11473,7 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1145511473 size_t dest_end;
1145611474 switch (dest_ptr_val->data.x_ptr.special) {
1145711475 case ConstPtrSpecialInvalid:
11476 case ConstPtrSpecialDiscard:
1145811477 zig_unreachable();
1145911478 case ConstPtrSpecialRef:
1146011479 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
......@@ -11487,6 +11506,7 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1148711506
1148811507 switch (src_ptr_val->data.x_ptr.special) {
1148911508 case ConstPtrSpecialInvalid:
11509 case ConstPtrSpecialDiscard:
1149011510 zig_unreachable();
1149111511 case ConstPtrSpecialRef:
1149211512 src_elements = src_ptr_val->data.x_ptr.data.ref.pointee;
......@@ -11595,6 +11615,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1159511615 parent_ptr = const_ptr_pointee(&ptr_ptr->value);
1159611616 switch (parent_ptr->data.x_ptr.special) {
1159711617 case ConstPtrSpecialInvalid:
11618 case ConstPtrSpecialDiscard:
1159811619 zig_unreachable();
1159911620 case ConstPtrSpecialRef:
1160011621 array_val = nullptr;
......@@ -11619,6 +11640,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1161911640
1162011641 switch (parent_ptr->data.x_ptr.special) {
1162111642 case ConstPtrSpecialInvalid:
11643 case ConstPtrSpecialDiscard:
1162211644 zig_unreachable();
1162311645 case ConstPtrSpecialRef:
1162411646 array_val = nullptr;
......@@ -11676,6 +11698,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1167611698 } else {
1167711699 switch (parent_ptr->data.x_ptr.special) {
1167811700 case ConstPtrSpecialInvalid:
11701 case ConstPtrSpecialDiscard:
1167911702 zig_unreachable();
1168011703 case ConstPtrSpecialRef:
1168111704 init_const_ptr_ref(ira->codegen, ptr_val,
......@@ -12302,6 +12325,14 @@ static TypeTableEntry *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *ins
1230212325 instruction_type->id == TypeTableEntryIdUnreachable);
1230312326 instruction->other = instruction;
1230412327 }
12328 if (instruction_type->id != TypeTableEntryIdInvalid &&
12329 instruction_type->id != TypeTableEntryIdVoid &&
12330 instruction_type->id != TypeTableEntryIdUnreachable &&
12331 instruction->ref_count == 0)
12332 {
12333 ir_add_error(ira, instruction, buf_sprintf("return value ignored"));
12334 }
12335
1230512336 return instruction_type;
1230612337}
1230712338
std/compiler_rt.zig+1-1
......@@ -218,7 +218,7 @@ export fn __umoddi3(a: du_int, b: du_int) -> du_int {
218218 @setDebugSafety(this, false);
219219
220220 var r: du_int = undefined;
221 __udivmoddi4(a, b, &r);
221 _ = __udivmoddi4(a, b, &r);
222222 return r;
223223}
224224
std/io.zig+3-3
......@@ -170,7 +170,7 @@ pub const OutStream = struct {
170170 },
171171 State.Integer => switch (c) {
172172 '}' => {
173 self.printInt(args[next_arg], radix, uppercase, width);
173 %return self.printInt(args[next_arg], radix, uppercase, width);
174174 next_arg += 1;
175175 state = State.Start;
176176 start_index = i + 1;
......@@ -184,7 +184,7 @@ pub const OutStream = struct {
184184 State.IntegerWidth => switch (c) {
185185 '}' => {
186186 width = comptime %%parseUnsigned(usize, format[width_start...i], 10);
187 self.printInt(args[next_arg], radix, uppercase, width);
187 %return self.printInt(args[next_arg], radix, uppercase, width);
188188 next_arg += 1;
189189 state = State.Start;
190190 start_index = i + 1;
......@@ -194,7 +194,7 @@ pub const OutStream = struct {
194194 },
195195 State.Character => switch (c) {
196196 '}' => {
197 self.printAsciiChar(args[next_arg]);
197 %return self.printAsciiChar(args[next_arg]);
198198 next_arg += 1;
199199 state = State.Start;
200200 start_index = i + 1;
std/linux.zig+6-6
......@@ -298,7 +298,7 @@ pub fn lseek(fd: i32, offset: usize, ref_pos: usize) -> usize {
298298}
299299
300300pub fn exit(status: i32) -> unreachable {
301 arch.syscall1(arch.SYS_exit, usize(status));
301 _ = arch.syscall1(arch.SYS_exit, usize(status));
302302 @unreachable()
303303}
304304
......@@ -325,15 +325,15 @@ pub fn raise(sig: i32) -> i32 {
325325}
326326
327327fn blockAllSignals(set: &sigset_t) {
328 arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8);
328 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8);
329329}
330330
331331fn blockAppSignals(set: &sigset_t) {
332 arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8);
332 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8);
333333}
334334
335335fn restoreSignals(set: &sigset_t) {
336 arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8);
336 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8);
337337}
338338
339339
......@@ -432,8 +432,8 @@ pub fn shutdown(fd: i32, how: i32) -> usize {
432432 arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how))
433433}
434434
435pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) {
436 arch.syscall3(arch.SYS_bind, usize(fd), usize(addr), usize(len));
435pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize {
436 arch.syscall3(arch.SYS_bind, usize(fd), usize(addr), usize(len))
437437}
438438
439439pub fn listen(fd: i32, backlog: i32) -> usize {
std/os.zig+2-2
......@@ -30,8 +30,8 @@ pub fn getRandomBytes(buf: []u8) -> %void {
3030pub coldcc fn abort() -> unreachable {
3131 switch (@compileVar("os")) {
3232 Os.linux, Os.darwin => {
33 system.raise(system.SIGABRT);
34 system.raise(system.SIGKILL);
33 _ = system.raise(system.SIGABRT);
34 _ = system.raise(system.SIGKILL);
3535 while (true) {}
3636 },
3737 else => @compileError("unsupported os"),
test/cases/struct.zig+1-1
......@@ -193,7 +193,7 @@ const EmptyStruct = struct {
193193fn returnEmptyStructFromFn() {
194194 @setFnTest(this);
195195
196 testReturnEmptyStructFromFn();
196 _ = testReturnEmptyStructFromFn();
197197}
198198const EmptyStruct2 = struct {};
199199fn testReturnEmptyStructFromFn() -> EmptyStruct2 {
test/run_tests.cpp+46-40
......@@ -230,7 +230,7 @@ static void add_compiling_test_cases(void) {
230230 add_simple_case_libc("hello world with libc", R"SOURCE(
231231const c = @cImport(@cInclude("stdio.h"));
232232export fn main(argc: c_int, argv: &&u8) -> c_int {
233 c.puts(c"Hello, world!");
233 _ = c.puts(c"Hello, world!");
234234 return 0;
235235}
236236 )SOURCE", "Hello, world!" NL);
......@@ -344,84 +344,84 @@ pub fn main(args: [][]u8) -> %void {
344344const c = @cImport(@cInclude("stdio.h"));
345345
346346export fn main(argc: c_int, argv: &&u8) -> c_int {
347 c.printf(c"\n");
347 _ = c.printf(c"\n");
348348
349 c.printf(c"0: %llu\n",
349 _ = c.printf(c"0: %llu\n",
350350 u64(0));
351 c.printf(c"320402575052271: %llu\n",
351 _ = c.printf(c"320402575052271: %llu\n",
352352 u64(320402575052271));
353 c.printf(c"0x01236789abcdef: %llu\n",
353 _ = c.printf(c"0x01236789abcdef: %llu\n",
354354 u64(0x01236789abcdef));
355 c.printf(c"0xffffffffffffffff: %llu\n",
355 _ = c.printf(c"0xffffffffffffffff: %llu\n",
356356 u64(0xffffffffffffffff));
357 c.printf(c"0x000000ffffffffffffffff: %llu\n",
357 _ = c.printf(c"0x000000ffffffffffffffff: %llu\n",
358358 u64(0x000000ffffffffffffffff));
359 c.printf(c"0o1777777777777777777777: %llu\n",
359 _ = c.printf(c"0o1777777777777777777777: %llu\n",
360360 u64(0o1777777777777777777777));
361 c.printf(c"0o0000001777777777777777777777: %llu\n",
361 _ = c.printf(c"0o0000001777777777777777777777: %llu\n",
362362 u64(0o0000001777777777777777777777));
363 c.printf(c"0b1111111111111111111111111111111111111111111111111111111111111111: %llu\n",
363 _ = c.printf(c"0b1111111111111111111111111111111111111111111111111111111111111111: %llu\n",
364364 u64(0b1111111111111111111111111111111111111111111111111111111111111111));
365 c.printf(c"0b0000001111111111111111111111111111111111111111111111111111111111111111: %llu\n",
365 _ = c.printf(c"0b0000001111111111111111111111111111111111111111111111111111111111111111: %llu\n",
366366 u64(0b0000001111111111111111111111111111111111111111111111111111111111111111));
367367
368 c.printf(c"\n");
368 _ = c.printf(c"\n");
369369
370 c.printf(c"0.0: %a\n",
370 _ = c.printf(c"0.0: %a\n",
371371 f64(0.0));
372 c.printf(c"0e0: %a\n",
372 _ = c.printf(c"0e0: %a\n",
373373 f64(0e0));
374 c.printf(c"0.0e0: %a\n",
374 _ = c.printf(c"0.0e0: %a\n",
375375 f64(0.0e0));
376 c.printf(c"000000000000000000000000000000000000000000000000000000000.0e0: %a\n",
376 _ = c.printf(c"000000000000000000000000000000000000000000000000000000000.0e0: %a\n",
377377 f64(000000000000000000000000000000000000000000000000000000000.0e0));
378 c.printf(c"0.000000000000000000000000000000000000000000000000000000000e0: %a\n",
378 _ = c.printf(c"0.000000000000000000000000000000000000000000000000000000000e0: %a\n",
379379 f64(0.000000000000000000000000000000000000000000000000000000000e0));
380 c.printf(c"0.0e000000000000000000000000000000000000000000000000000000000: %a\n",
380 _ = c.printf(c"0.0e000000000000000000000000000000000000000000000000000000000: %a\n",
381381 f64(0.0e000000000000000000000000000000000000000000000000000000000));
382 c.printf(c"1.0: %a\n",
382 _ = c.printf(c"1.0: %a\n",
383383 f64(1.0));
384 c.printf(c"10.0: %a\n",
384 _ = c.printf(c"10.0: %a\n",
385385 f64(10.0));
386 c.printf(c"10.5: %a\n",
386 _ = c.printf(c"10.5: %a\n",
387387 f64(10.5));
388 c.printf(c"10.5e5: %a\n",
388 _ = c.printf(c"10.5e5: %a\n",
389389 f64(10.5e5));
390 c.printf(c"10.5e+5: %a\n",
390 _ = c.printf(c"10.5e+5: %a\n",
391391 f64(10.5e+5));
392 c.printf(c"50.0e-2: %a\n",
392 _ = c.printf(c"50.0e-2: %a\n",
393393 f64(50.0e-2));
394 c.printf(c"50e-2: %a\n",
394 _ = c.printf(c"50e-2: %a\n",
395395 f64(50e-2));
396396
397 c.printf(c"\n");
397 _ = c.printf(c"\n");
398398
399 c.printf(c"0x1.0: %a\n",
399 _ = c.printf(c"0x1.0: %a\n",
400400 f64(0x1.0));
401 c.printf(c"0x10.0: %a\n",
401 _ = c.printf(c"0x10.0: %a\n",
402402 f64(0x10.0));
403 c.printf(c"0x100.0: %a\n",
403 _ = c.printf(c"0x100.0: %a\n",
404404 f64(0x100.0));
405 c.printf(c"0x103.0: %a\n",
405 _ = c.printf(c"0x103.0: %a\n",
406406 f64(0x103.0));
407 c.printf(c"0x103.7: %a\n",
407 _ = c.printf(c"0x103.7: %a\n",
408408 f64(0x103.7));
409 c.printf(c"0x103.70: %a\n",
409 _ = c.printf(c"0x103.70: %a\n",
410410 f64(0x103.70));
411 c.printf(c"0x103.70p4: %a\n",
411 _ = c.printf(c"0x103.70p4: %a\n",
412412 f64(0x103.70p4));
413 c.printf(c"0x103.70p5: %a\n",
413 _ = c.printf(c"0x103.70p5: %a\n",
414414 f64(0x103.70p5));
415 c.printf(c"0x103.70p+5: %a\n",
415 _ = c.printf(c"0x103.70p+5: %a\n",
416416 f64(0x103.70p+5));
417 c.printf(c"0x103.70p-5: %a\n",
417 _ = c.printf(c"0x103.70p-5: %a\n",
418418 f64(0x103.70p-5));
419419
420 c.printf(c"\n");
420 _ = c.printf(c"\n");
421421
422 c.printf(c"0b10100.00010e0: %a\n",
422 _ = c.printf(c"0b10100.00010e0: %a\n",
423423 f64(0b10100.00010e0));
424 c.printf(c"0o10700.00010e0: %a\n",
424 _ = c.printf(c"0o10700.00010e0: %a\n",
425425 f64(0o10700.00010e0));
426426
427427 return 0;
......@@ -520,7 +520,7 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
520520 const x: f64 = small;
521521 const y = i32(x);
522522 const z = f64(y);
523 c.printf(c"%.2f\n%d\n%.2f\n%.2f\n", x, y, z, f64(-0.4));
523 _ = c.printf(c"%.2f\n%d\n%.2f\n%.2f\n", x, y, z, f64(-0.4));
524524 return 0;
525525}
526526 )SOURCE", "3.25\n3\n3.00\n-0.40\n");
......@@ -1691,6 +1691,12 @@ fn bar() { }
16911691 ".tmp_source.zig:4:5: error: control flow attempts to use compile-time variable at runtime",
16921692 ".tmp_source.zig:4:21: note: compile-time variable assigned here");
16931693
1694 add_compile_fail_case("ignored return value", R"SOURCE(
1695fn foo() {
1696 bar();
1697}
1698fn bar() -> i32 { 0 }
1699 )SOURCE", 1, ".tmp_source.zig:3:8: error: return value ignored");
16941700}
16951701
16961702//////////////////////////////////////////////////////////////////////////////