authorgravatar for vvvv0932313@gmail.comshwqf <vvvv0932313@gmail.com> 2022-12-18 10:09:35+08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 15:09:40-07:00
log23c09598c734c3668611c4fc81d644acfd5cb1c4
treefd043fb56a9f37707be47d2ebbd5d9399df0b770
parent2acdea7dfdb4db07e546fb6dfbaef2af5a2e591c

Call ensureResultUsed before comptime .call is evaluated.

Fixes #12580

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

src/Sema.zig+10-7
......@@ -3153,17 +3153,16 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compile
31533153 const operand = try sema.resolveInst(inst_data.operand);
31543154 const src = inst_data.src();
31553155
3156 return sema.ensureResultUsed(block, operand, src);
3156 return sema.ensureResultUsed(block, sema.typeOf(operand), src);
31573157}
31583158
31593159fn ensureResultUsed(
31603160 sema: *Sema,
31613161 block: *Block,
3162 operand: Air.Inst.Ref,
3162 ty: Type,
31633163 src: LazySrcLoc,
31643164) CompileError!void {
3165 const operand_ty = sema.typeOf(operand);
3166 switch (operand_ty.zigTypeTag()) {
3165 switch (ty.zigTypeTag()) {
31673166 .Void, .NoReturn => return,
31683167 .ErrorSet, .ErrorUnion => {
31693168 const msg = msg: {
......@@ -3176,7 +3175,7 @@ fn ensureResultUsed(
31763175 },
31773176 else => {
31783177 const msg = msg: {
3179 const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{operand_ty.fmt(sema.mod)});
3178 const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{ty.fmt(sema.mod)});
31803179 errdefer msg.destroy(sema.gpa);
31813180 try sema.errNote(block, src, msg, "all non-void values must be used", .{});
31823181 try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
......@@ -6487,6 +6486,10 @@ fn analyzeCall(
64876486 };
64886487 }
64896488
6489 if (is_comptime_call and ensure_result_used) {
6490 try sema.ensureResultUsed(block, fn_ret_ty, call_src);
6491 }
6492
64906493 const result = result: {
64916494 sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) {
64926495 error.ComptimeReturn => break :result inlining.comptime_result,
......@@ -6609,7 +6612,7 @@ fn analyzeCall(
66096612 };
66106613
66116614 if (ensure_result_used) {
6612 try sema.ensureResultUsed(block, result, call_src);
6615 try sema.ensureResultUsed(block, sema.typeOf(result), call_src);
66136616 }
66146617 if (call_tag == .call_always_tail) {
66156618 return sema.handleTailCall(block, call_src, func_ty, result);
......@@ -7251,7 +7254,7 @@ fn instantiateGenericCall(
72517254 sema.appendRefsAssumeCapacity(runtime_args);
72527255
72537256 if (ensure_result_used) {
7254 try sema.ensureResultUsed(block, result, call_src);
7257 try sema.ensureResultUsed(block, sema.typeOf(result), call_src);
72557258 }
72567259 if (call_tag == .call_always_tail) {
72577260 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 '_'