aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorr00ster91 <r00ster91@proton.me>2023-07-21 03:22:59 +0200
committerwooster0 <wooster0@proton.me>2024-05-14 01:13:49 +0900
commit9ae43567a3ef14a815482ccaa2a5b412a716743e (patch)
treecdd535a650d7fdde5eb6c43e1d8e2b040fee5366 /src
parent8579904ddd489b73cb61721c31b9e9c14ed9e264 (diff)
downloadzig-9ae43567a3ef14a815482ccaa2a5b412a716743e.tar.gz
zig-9ae43567a3ef14a815482ccaa2a5b412a716743e.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/Sema.zig10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index f286b6385f..c55531d924 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -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;