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...@@ -3254,17 +3254,16 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compile
3254 const operand = try sema.resolveInst(inst_data.operand);3254 const operand = try sema.resolveInst(inst_data.operand);
3255 const src = inst_data.src();3255 const src = inst_data.src();
32563256
3257 return sema.ensureResultUsed(block, operand, src);3257 return sema.ensureResultUsed(block, sema.typeOf(operand), src);
3258}3258}
32593259
3260fn ensureResultUsed(3260fn ensureResultUsed(
3261 sema: *Sema,3261 sema: *Sema,
3262 block: *Block,3262 block: *Block,
3263 operand: Air.Inst.Ref,3263 ty: Type,
3264 src: LazySrcLoc,3264 src: LazySrcLoc,
3265) CompileError!void {3265) CompileError!void {
3266 const operand_ty = sema.typeOf(operand);3266 switch (ty.zigTypeTag()) {
3267 switch (operand_ty.zigTypeTag()) {
3268 .Void, .NoReturn => return,3267 .Void, .NoReturn => return,
3269 .ErrorSet, .ErrorUnion => {3268 .ErrorSet, .ErrorUnion => {
3270 const msg = msg: {3269 const msg = msg: {
...@@ -3277,7 +3276,7 @@ fn ensureResultUsed(...@@ -3277,7 +3276,7 @@ fn ensureResultUsed(
3277 },3276 },
3278 else => {3277 else => {
3279 const msg = msg: {3278 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)});
3281 errdefer msg.destroy(sema.gpa);3280 errdefer msg.destroy(sema.gpa);
3282 try sema.errNote(block, src, msg, "all non-void values must be used", .{});3281 try sema.errNote(block, src, msg, "all non-void values must be used", .{});
3283 try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});3282 try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
...@@ -6641,6 +6640,10 @@ fn analyzeCall(...@@ -6641,6 +6640,10 @@ fn analyzeCall(
6641 };6640 };
6642 }6641 }
66436642
6643 if (is_comptime_call and ensure_result_used) {
6644 try sema.ensureResultUsed(block, fn_ret_ty, call_src);
6645 }
6646
6644 const result = result: {6647 const result = result: {
6645 sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) {6648 sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) {
6646 error.ComptimeReturn => break :result inlining.comptime_result,6649 error.ComptimeReturn => break :result inlining.comptime_result,
...@@ -6763,7 +6766,7 @@ fn analyzeCall(...@@ -6763,7 +6766,7 @@ fn analyzeCall(
6763 };6766 };
67646767
6765 if (ensure_result_used) {6768 if (ensure_result_used) {
6766 try sema.ensureResultUsed(block, result, call_src);6769 try sema.ensureResultUsed(block, sema.typeOf(result), call_src);
6767 }6770 }
6768 if (call_tag == .call_always_tail) {6771 if (call_tag == .call_always_tail) {
6769 return sema.handleTailCall(block, call_src, func_ty, result);6772 return sema.handleTailCall(block, call_src, func_ty, result);
...@@ -7406,7 +7409,7 @@ fn instantiateGenericCall(...@@ -7406,7 +7409,7 @@ fn instantiateGenericCall(
7406 sema.appendRefsAssumeCapacity(runtime_args);7409 sema.appendRefsAssumeCapacity(runtime_args);
74077410
7408 if (ensure_result_used) {7411 if (ensure_result_used) {
7409 try sema.ensureResultUsed(block, result, call_src);7412 try sema.ensureResultUsed(block, sema.typeOf(result), call_src);
7410 }7413 }
7411 if (call_tag == .call_always_tail) {7414 if (call_tag == .call_always_tail) {
7412 return sema.handleTailCall(block, call_src, func_ty, result);7415 return sema.handleTailCall(block, call_src, func_ty, result);
test/cases/compile_errors/ignored_comptime_value.zig+16-1
...@@ -1,6 +1,18 @@...@@ -1,6 +1,18 @@
1export fn foo() void {1export fn a() void {
2 comptime 1;2 comptime 1;
3}3}
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
5// error17// error
6// backend=stage218// backend=stage2
...@@ -9,3 +21,6 @@ export fn foo() void {...@@ -9,3 +21,6 @@ export fn foo() void {
9// :2:5: error: value of type 'comptime_int' ignored21// :2:5: error: value of type 'comptime_int' ignored
10// :2:5: note: all non-void values must be used22// :2:5: note: all non-void values must be used
11// :2:5: note: this error can be suppressed by assigning the value to '_'23// :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 '_'