authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-07-21 03:22:59+02:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2024-05-14 01:13:49+09:00
log9ae43567a3ef14a815482ccaa2a5b412a716743e
treecdd535a650d7fdde5eb6c43e1d8e2b040fee5366
parent8579904ddd489b73cb61721c31b9e9c14ed9e264

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.

4 files changed, 33 insertions(+), 10 deletions(-)

src/Sema.zig+6-4
......@@ -3568,9 +3568,10 @@ fn ensureResultUsed(
35683568 const mod = sema.mod;
35693569 switch (ty.zigTypeTag(mod)) {
35703570 .Void, .NoReturn => return,
3571 .ErrorSet, .ErrorUnion => {
3571 .ErrorSet => return sema.fail(block, src, "error set is ignored", .{}),
3572 .ErrorUnion => {
35723573 const msg = msg: {
3573 const msg = try sema.errMsg(block, src, "error is ignored", .{});
3574 const msg = try sema.errMsg(block, src, "error union is ignored", .{});
35743575 errdefer msg.destroy(sema.gpa);
35753576 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
35763577 break :msg msg;
......@@ -3600,9 +3601,10 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
36003601 const src = inst_data.src();
36013602 const operand_ty = sema.typeOf(operand);
36023603 switch (operand_ty.zigTypeTag(mod)) {
3603 .ErrorSet, .ErrorUnion => {
3604 .ErrorSet => return sema.fail(block, src, "error set is discarded", .{}),
3605 .ErrorUnion => {
36043606 const msg = msg: {
3605 const msg = try sema.errMsg(block, src, "error is discarded", .{});
3607 const msg = try sema.errMsg(block, src, "error union is discarded", .{});
36063608 errdefer msg.destroy(sema.gpa);
36073609 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
36083610 break :msg msg;
test/cases/compile_errors/discarding_error_value.zig+7-2
......@@ -1,13 +1,18 @@
1export fn entry() void {
1export fn entry1() void {
22 _ = foo();
33}
44fn foo() !void {
55 return error.OutOfMemory;
66}
7export fn entry2() void {
8 const x: error{a} = undefined;
9 _ = x;
10}
711
812// error
913// backend=stage2
1014// target=native
1115//
12// :2:12: error: error is discarded
16// :2:12: error: error union is discarded
1317// :2:12: note: consider using 'try', 'catch', or 'if'
18// :9:9: error: error set is discarded
test/cases/compile_errors/ignored_deferred_function_call.zig+9-1
......@@ -5,9 +5,17 @@ fn bar() anyerror!i32 {
55 return 0;
66}
77
8export fn foo2() void {
9 defer bar2();
10}
11fn bar2() anyerror {
12 return error.a;
13}
14
815// error
916// backend=stage2
1017// target=native
1118//
12// :2:14: error: error is ignored
19// :2:14: error: error union is ignored
1320// :2:14: note: consider using 'try', 'catch', or 'if'
21// :9:15: error: error set is ignored
test/cases/compile_errors/ignored_expression_in_while_continuation.zig+11-3
......@@ -15,13 +15,21 @@ fn bad() anyerror!void {
1515 return error.Bad;
1616}
1717
18export fn d() void {
19 while (true) : (bad2()) {}
20}
21fn bad2() anyerror {
22 return error.Bad;
23}
24
1825// error
1926// backend=stage2
2027// target=native
2128//
22// :2:24: error: error is ignored
29// :2:24: error: error union is ignored
2330// :2:24: note: consider using 'try', 'catch', or 'if'
24// :7:25: error: error is ignored
31// :7:25: error: error union is ignored
2532// :7:25: note: consider using 'try', 'catch', or 'if'
26// :12:25: error: error is ignored
33// :12:25: error: error union is ignored
2734// :12:25: note: consider using 'try', 'catch', or 'if'
35// :19:25: error: error set is ignored