Sema: improve error set/union discard/ignore errors

Previously the error had a note suggesting to use `try`, `catch`, or
`if`, even for error sets where none of those work.
Instead, in case of an error set the way you can handle the error
depends very much on the specific case. For example you might be in a
`catch` where you are discarding or ignoring the error set capture
value, in which case one way to handle the error might be to `return`
the error.
So, in that case, we do not attach that error note.

Additionally, this makes the error tell you what kind of an error it is:
is it an error set or an error union? This distinction is very relevant
in how to handle the error.
This commit is contained in:
r00ster91 2023-07-21 03:22:59 +02:00 committed by wooster0
parent 8579904ddd
commit 9ae43567a3
4 changed files with 33 additions and 10 deletions

View File

@ -3568,9 +3568,10 @@ fn ensureResultUsed(
const mod = sema.mod;
switch (ty.zigTypeTag(mod)) {
.Void, .NoReturn => return,
.ErrorSet, .ErrorUnion => {
.ErrorSet => return sema.fail(block, src, "error set is ignored", .{}),
.ErrorUnion => {
const msg = msg: {
const msg = try sema.errMsg(block, src, "error is ignored", .{});
const msg = try sema.errMsg(block, src, "error union is ignored", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
break :msg msg;
@ -3600,9 +3601,10 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
const src = inst_data.src();
const operand_ty = sema.typeOf(operand);
switch (operand_ty.zigTypeTag(mod)) {
.ErrorSet, .ErrorUnion => {
.ErrorSet => return sema.fail(block, src, "error set is discarded", .{}),
.ErrorUnion => {
const msg = msg: {
const msg = try sema.errMsg(block, src, "error is discarded", .{});
const msg = try sema.errMsg(block, src, "error union is discarded", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
break :msg msg;

View File

@ -1,13 +1,18 @@
export fn entry() void {
export fn entry1() void {
_ = foo();
}
fn foo() !void {
return error.OutOfMemory;
}
export fn entry2() void {
const x: error{a} = undefined;
_ = x;
}
// error
// backend=stage2
// target=native
//
// :2:12: error: error is discarded
// :2:12: error: error union is discarded
// :2:12: note: consider using 'try', 'catch', or 'if'
// :9:9: error: error set is discarded

View File

@ -5,9 +5,17 @@ fn bar() anyerror!i32 {
return 0;
}
export fn foo2() void {
defer bar2();
}
fn bar2() anyerror {
return error.a;
}
// error
// backend=stage2
// target=native
//
// :2:14: error: error is ignored
// :2:14: error: error union is ignored
// :2:14: note: consider using 'try', 'catch', or 'if'
// :9:15: error: error set is ignored

View File

@ -15,13 +15,21 @@ fn bad() anyerror!void {
return error.Bad;
}
export fn d() void {
while (true) : (bad2()) {}
}
fn bad2() anyerror {
return error.Bad;
}
// error
// backend=stage2
// target=native
//
// :2:24: error: error is ignored
// :2:24: error: error union is ignored
// :2:24: note: consider using 'try', 'catch', or 'if'
// :7:25: error: error is ignored
// :7:25: error: error union is ignored
// :7:25: note: consider using 'try', 'catch', or 'if'
// :12:25: error: error is ignored
// :12:25: error: error union is ignored
// :12:25: note: consider using 'try', 'catch', or 'if'
// :19:25: error: error set is ignored