authorgravatar for vvvv0932313@gmail.comshwqf <vvvv0932313@gmail.com> 2022-12-18 10:09:35+08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-18 16:04:33+02:00
log11a81e1b29ecefd7aad2ebe951743c44272e1817
tree63d552572489cee8086e188aac899b41481ae871
parentf24c77fc48b7272618b48cac7bb15d6997e95c5a

Call ensureResultUsed before comptime .call is evaluated.

Fixes #12580

2 files changed, 26 insertions(+), 8 deletions(-)

src/Sema.zig+10-7
......@@ -3254,17 +3254,16 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compile
32543254 const operand = try sema.resolveInst(inst_data.operand);
32553255 const src = inst_data.src();
32563256
3257 return sema.ensureResultUsed(block, operand, src);
3257 return sema.ensureResultUsed(block, sema.typeOf(operand), src);
32583258}
32593259
32603260fn ensureResultUsed(
32613261 sema: *Sema,
32623262 block: *Block,
3263 operand: Air.Inst.Ref,
3263 ty: Type,
32643264 src: LazySrcLoc,
32653265) CompileError!void {
3266 const operand_ty = sema.typeOf(operand);
3267 switch (operand_ty.zigTypeTag()) {
3266 switch (ty.zigTypeTag()) {
32683267 .Void, .NoReturn => return,
32693268 .ErrorSet, .ErrorUnion => {
32703269 const msg = msg: {
......@@ -3277,7 +3276,7 @@ fn ensureResultUsed(
32773276 },
32783277 else => {
32793278 const msg = msg: {
3280 const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{operand_ty.fmt(sema.mod)});
3279 const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{ty.fmt(sema.mod)});
32813280 errdefer msg.destroy(sema.gpa);
32823281 try sema.errNote(block, src, msg, "all non-void values must be used", .{});
32833282 try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
......@@ -6641,6 +6640,10 @@ fn analyzeCall(
66416640 };
66426641 }
66436642
6643 if (is_comptime_call and ensure_result_used) {
6644 try sema.ensureResultUsed(block, fn_ret_ty, call_src);
6645 }
6646
66446647 const result = result: {
66456648 sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) {
66466649 error.ComptimeReturn => break :result inlining.comptime_result,
......@@ -6763,7 +6766,7 @@ fn analyzeCall(
67636766 };
67646767
67656768 if (ensure_result_used) {
6766 try sema.ensureResultUsed(block, result, call_src);
6769 try sema.ensureResultUsed(block, sema.typeOf(result), call_src);
67676770 }
67686771 if (call_tag == .call_always_tail) {
67696772 return sema.handleTailCall(block, call_src, func_ty, result);
......@@ -7406,7 +7409,7 @@ fn instantiateGenericCall(
74067409 sema.appendRefsAssumeCapacity(runtime_args);
74077410
74087411 if (ensure_result_used) {
7409 try sema.ensureResultUsed(block, result, call_src);
7412 try sema.ensureResultUsed(block, sema.typeOf(result), call_src);
74107413 }
74117414 if (call_tag == .call_always_tail) {
74127415 return sema.handleTailCall(block, call_src, func_ty, result);
test/cases/compile_errors/ignored_comptime_value.zig+16-1
......@@ -1,6 +1,18 @@
1export fn foo() void {
1export fn a() void {
22 comptime 1;
33}
4export fn b() void {
5 comptime bar();
6}
7fn bar() u8 {
8 const u32_max = @import("std").math.maxInt(u32);
9
10 @setEvalBranchQuota(u32_max);
11 var x: u32 = 0;
12 while (x != u32_max) : (x +%= 1) {}
13
14 return 0;
15}
416
517// error
618// backend=stage2
......@@ -9,3 +21,6 @@ export fn foo() void {
921// :2:5: error: value of type 'comptime_int' ignored
1022// :2:5: note: all non-void values must be used
1123// :2:5: note: this error can be suppressed by assigning the value to '_'
24// :5:17: error: value of type 'u8' ignored
25// :5:17: note: all non-void values must be used
26// :5:17: note: this error can be suppressed by assigning the value to '_'