authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-05-21 22:37:00+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-05-21 22:37:00+03:00
logeb0f871cb90cf7e3a1cfa7a160d3f224b42fd799
tree9f7eb7266e469439ecb3ab3205060b1b6eb47914
parentae44e199a8caa8d26db04b81867acb81d54ba217
parentac55685a94b3db97e9d2eadef0432948b3c16a03
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19961 from wooster0/errorstuff

Sema: improvements to error messages related to the handling of (error) values

16 files changed, 108 insertions(+), 32 deletions(-)

src/Sema.zig+26-9
......@@ -2228,8 +2228,20 @@ fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: T
22282228 });
22292229}
22302230
2231fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, optional_ty: Type) CompileError {
2232 return sema.fail(block, src, "expected optional type, found '{}'", .{optional_ty.fmt(sema.mod)});
2231fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, non_optional_ty: Type) CompileError {
2232 const mod = sema.mod;
2233 const msg = msg: {
2234 const msg = try sema.errMsg(block, src, "expected optional type, found '{}'", .{
2235 non_optional_ty.fmt(mod),
2236 });
2237 errdefer msg.destroy(sema.gpa);
2238 if (non_optional_ty.zigTypeTag(mod) == .ErrorUnion) {
2239 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
2240 }
2241 try addDeclaredHereNote(sema, msg, non_optional_ty);
2242 break :msg msg;
2243 };
2244 return sema.failWithOwnedErrorMsg(block, msg);
22332245}
22342246
22352247fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError {
......@@ -3557,9 +3569,10 @@ fn ensureResultUsed(
35573569 const mod = sema.mod;
35583570 switch (ty.zigTypeTag(mod)) {
35593571 .Void, .NoReturn => return,
3560 .ErrorSet, .ErrorUnion => {
3572 .ErrorSet => return sema.fail(block, src, "error set is ignored", .{}),
3573 .ErrorUnion => {
35613574 const msg = msg: {
3562 const msg = try sema.errMsg(block, src, "error is ignored", .{});
3575 const msg = try sema.errMsg(block, src, "error union is ignored", .{});
35633576 errdefer msg.destroy(sema.gpa);
35643577 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
35653578 break :msg msg;
......@@ -3571,7 +3584,7 @@ fn ensureResultUsed(
35713584 const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{ty.fmt(sema.mod)});
35723585 errdefer msg.destroy(sema.gpa);
35733586 try sema.errNote(block, src, msg, "all non-void values must be used", .{});
3574 try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
3587 try sema.errNote(block, src, msg, "to discard the value, assign it to '_'", .{});
35753588 break :msg msg;
35763589 };
35773590 return sema.failWithOwnedErrorMsg(block, msg);
......@@ -3589,9 +3602,10 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
35893602 const src = inst_data.src();
35903603 const operand_ty = sema.typeOf(operand);
35913604 switch (operand_ty.zigTypeTag(mod)) {
3592 .ErrorSet, .ErrorUnion => {
3605 .ErrorSet => return sema.fail(block, src, "error set is discarded", .{}),
3606 .ErrorUnion => {
35933607 const msg = msg: {
3594 const msg = try sema.errMsg(block, src, "error is discarded", .{});
3608 const msg = try sema.errMsg(block, src, "error union is discarded", .{});
35953609 errdefer msg.destroy(sema.gpa);
35963610 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
35973611 break :msg msg;
......@@ -9038,7 +9052,7 @@ fn analyzeOptionalPayloadPtr(
90389052
90399053 const opt_type = optional_ptr_ty.childType(zcu);
90409054 if (opt_type.zigTypeTag(zcu) != .Optional) {
9041 return sema.fail(block, src, "expected optional type, found '{}'", .{opt_type.fmt(zcu)});
9055 return sema.failWithExpectedOptionalType(block, src, opt_type);
90429056 }
90439057
90449058 const child_type = opt_type.optionalChild(zcu);
......@@ -9531,7 +9545,7 @@ fn handleExternLibName(
95319545 return sema.fail(
95329546 block,
95339547 src_loc,
9534 "dependency on dynamic library '{s}' requires enabling Position Independent Code. Fixed by '-l{s}' or '-fPIC'.",
9548 "dependency on dynamic library '{s}' requires enabling Position Independent Code; fixed by '-l{s}' or '-fPIC'",
95359549 .{ lib_name, lib_name },
95369550 );
95379551 }
......@@ -27875,6 +27889,9 @@ fn fieldCallBind(
2787527889 const decl = mod.declPtr(decl_idx);
2787627890 try mod.errNoteNonLazy(decl.srcLoc(mod), msg, "'{}' is not a member function", .{field_name.fmt(ip)});
2787727891 }
27892 if (concrete_ty.zigTypeTag(mod) == .ErrorUnion) {
27893 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
27894 }
2787827895 break :msg msg;
2787927896 };
2788027897 return sema.failWithOwnedErrorMsg(block, 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/expected_optional_type_got_container.zig created+16
......@@ -0,0 +1,16 @@
1export fn foo() void {
2 while (bar()) |x| {
3 _ = x;
4 }
5}
6const X = enum { a };
7fn bar() X {
8 return .a;
9}
10
11// error
12// backend=stage2
13// target=native
14//
15// :2:15: error: expected optional type, found 'tmp.X'
16// :6:11: note: enum declared here
test/cases/compile_errors/for_loop_body_expression_ignored.zig+4-4
......@@ -23,13 +23,13 @@ export fn f4() void {
2323//
2424// :5:30: error: value of type 'usize' ignored
2525// :5:30: note: all non-void values must be used
26// :5:30: note: this error can be suppressed by assigning the value to '_'
26// :5:30: note: to discard the value, assign it to '_'
2727// :9:30: error: value of type 'usize' ignored
2828// :9:30: note: all non-void values must be used
29// :9:30: note: this error can be suppressed by assigning the value to '_'
29// :9:30: note: to discard the value, assign it to '_'
3030// :13:31: error: value of type 'bool' ignored
3131// :13:31: note: all non-void values must be used
32// :13:31: note: this error can be suppressed by assigning the value to '_'
32// :13:31: note: to discard the value, assign it to '_'
3333// :16:42: error: value of type 'usize' ignored
3434// :16:42: note: all non-void values must be used
35// :16:42: note: this error can be suppressed by assigning the value to '_'
35// :16:42: note: to discard the value, assign it to '_'
test/cases/compile_errors/generic_instantiation_failure.zig+1-1
......@@ -24,4 +24,4 @@ pub export fn entry() void {
2424//
2525// :18:43: error: value of type 'type' ignored
2626// :18:43: note: all non-void values must be used
27// :18:43: note: this error can be suppressed by assigning the value to '_'
27// :18:43: note: to discard the value, assign it to '_'
test/cases/compile_errors/ignored_assert-err-ok_return_value.zig+1-1
......@@ -11,4 +11,4 @@ fn bar() anyerror!i32 {
1111//
1212// :2:11: error: value of type 'i32' ignored
1313// :2:11: note: all non-void values must be used
14// :2:11: note: this error can be suppressed by assigning the value to '_'
14// :2:11: note: to discard the value, assign it to '_'
test/cases/compile_errors/ignored_comptime_statement_value.zig+1-1
......@@ -10,4 +10,4 @@ export fn foo() void {
1010//
1111// :3:9: error: value of type 'comptime_int' ignored
1212// :3:9: note: all non-void values must be used
13// :3:9: note: this error can be suppressed by assigning the value to '_'
13// :3:9: note: to discard the value, assign it to '_'
test/cases/compile_errors/ignored_comptime_value.zig+2-2
......@@ -14,7 +14,7 @@ fn bar() u8 {
1414//
1515// :2:5: error: value of type 'comptime_int' ignored
1616// :2:5: note: all non-void values must be used
17// :2:5: note: this error can be suppressed by assigning the value to '_'
17// :2:5: note: to discard the value, assign it to '_'
1818// :5:5: error: value of type 'u8' ignored
1919// :5:5: note: all non-void values must be used
20// :5:5: note: this error can be suppressed by assigning the value to '_'
20// :5:5: note: to discard the value, assign it to '_'
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_deferred_statement_value.zig+1-1
......@@ -10,4 +10,4 @@ export fn foo() void {
1010//
1111// :3:9: error: value of type 'comptime_int' ignored
1212// :3:9: note: all non-void values must be used
13// :3:9: note: this error can be suppressed by assigning the value to '_'
13// :3:9: note: to discard the value, assign it to '_'
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
test/cases/compile_errors/ignored_return_value.zig+1-1
......@@ -11,4 +11,4 @@ fn bar() i32 {
1111//
1212// :2:8: error: value of type 'i32' ignored
1313// :2:8: note: all non-void values must be used
14// :2:8: note: this error can be suppressed by assigning the value to '_'
14// :2:8: note: to discard the value, assign it to '_'
test/cases/compile_errors/ignored_statement_value.zig+1-1
......@@ -8,4 +8,4 @@ export fn foo() void {
88//
99// :2:5: error: value of type 'comptime_int' ignored
1010// :2:5: note: all non-void values must be used
11// :2:5: note: this error can be suppressed by assigning the value to '_'
11// :2:5: note: to discard the value, assign it to '_'
test/cases/compile_errors/method_call_on_error_union.zig created+21
......@@ -0,0 +1,21 @@
1const X = struct {
2 fn init() !X {
3 return error.a;
4 }
5
6 fn a(x: X) void {
7 _ = x;
8 }
9};
10
11export fn entry() void {
12 const x = X.init();
13 x.a();
14}
15
16// error
17// backend=stage2
18// target=native
19//
20// :13:6: error: no field or member function named 'a' in '@typeInfo(@typeInfo(@TypeOf(tmp.X.init)).Fn.return_type.?).ErrorUnion.error_set!tmp.X'
21// :13:6: note: consider using 'try', 'catch', or 'if'
test/cases/compile_errors/while_expected_optional_got_error_union.zig+1
......@@ -12,3 +12,4 @@ fn bar() anyerror!i32 {
1212// target=native
1313//
1414// :2:15: error: expected optional type, found 'anyerror!i32'
15// :2:15: note: consider using 'try', 'catch', or 'if'
test/cases/compile_errors/while_loop_body_expression_ignored.zig+5-5
......@@ -32,16 +32,16 @@ export fn f5() void {
3232//
3333// :5:25: error: value of type 'usize' ignored
3434// :5:25: note: all non-void values must be used
35// :5:25: note: this error can be suppressed by assigning the value to '_'
35// :5:25: note: to discard the value, assign it to '_'
3636// :10:26: error: value of type 'usize' ignored
3737// :10:26: note: all non-void values must be used
38// :10:26: note: this error can be suppressed by assigning the value to '_'
38// :10:26: note: to discard the value, assign it to '_'
3939// :15:26: error: value of type 'usize' ignored
4040// :15:26: note: all non-void values must be used
41// :15:26: note: this error can be suppressed by assigning the value to '_'
41// :15:26: note: to discard the value, assign it to '_'
4242// :20:23: error: value of type 'bool' ignored
4343// :20:23: note: all non-void values must be used
44// :20:23: note: this error can be suppressed by assigning the value to '_'
44// :20:23: note: to discard the value, assign it to '_'
4545// :25:34: error: value of type 'usize' ignored
4646// :25:34: note: all non-void values must be used
47// :25:34: note: this error can be suppressed by assigning the value to '_'
47// :25:34: note: to discard the value, assign it to '_'