Sema: improve expression value ignored error

Closes #4483
This commit is contained in:
Veikka Tuominen 2022-07-26 14:04:59 +03:00
parent 3d18c8c130
commit 5d99e5ecd4
7 changed files with 28 additions and 7 deletions

View File

@ -2869,7 +2869,16 @@ fn ensureResultUsed(
};
return sema.failWithOwnedErrorMsg(block, msg);
},
else => return sema.fail(block, src, "expression value is ignored", .{}),
else => {
const msg = msg: {
const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{operand_ty.fmt(sema.mod)});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, src, msg, "all non-void values must be used", .{});
try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
break :msg msg;
};
return sema.failWithOwnedErrorMsg(block, msg);
},
}
}

View File

@ -7,4 +7,6 @@ fn bar() anyerror!i32 { return 0; }
// backend=stage2
// target=native
//
// :2:11: error: expression value is ignored
// :2:11: error: value of type 'i32' ignored
// :2:11: note: all non-void values must be used
// :2:11: note: this error can be suppressed by assigning the value to '_'

View File

@ -6,4 +6,6 @@ export fn foo() void {
// backend=stage2
// target=native
//
// :2:15: error: expression value is ignored
// :2:15: error: value of type 'comptime_int' ignored
// :2:15: note: all non-void values must be used
// :2:15: note: this error can be suppressed by assigning the value to '_'

View File

@ -6,4 +6,6 @@ export fn foo() void {
// backend=stage2
// target=native
//
// :2:5: error: expression value is ignored
// :2:5: error: value of type 'comptime_int' ignored
// :2:5: note: all non-void values must be used
// :2:5: note: this error can be suppressed by assigning the value to '_'

View File

@ -6,4 +6,6 @@ export fn foo() void {
// backend=stage2
// target=native
//
// :2:12: error: expression value is ignored
// :2:12: error: value of type 'comptime_int' ignored
// :2:12: note: all non-void values must be used
// :2:12: note: this error can be suppressed by assigning the value to '_'

View File

@ -7,4 +7,6 @@ fn bar() i32 { return 0; }
// backend=stage2
// target=native
//
// :2:8: error: expression value is ignored
// :2:8: error: value of type 'i32' ignored
// :2:8: note: all non-void values must be used
// :2:8: note: this error can be suppressed by assigning the value to '_'

View File

@ -6,4 +6,6 @@ export fn foo() void {
// backend=stage2
// target=native
//
// :2:5: error: expression value is ignored
// :2:5: error: value of type 'comptime_int' ignored
// :2:5: note: all non-void values must be used
// :2:5: note: this error can be suppressed by assigning the value to '_'