Commit 9ae43567a3
Changed files (4)
src
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;
test/cases/compile_errors/discarding_error_value.zig
@@ -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
test/cases/compile_errors/ignored_deferred_function_call.zig
@@ -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
test/cases/compile_errors/ignored_expression_in_while_continuation.zig
@@ -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