authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-07-30 11:10:41+02:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2024-05-14 01:13:49+09:00
logf14cf13ff8553030c47748a0cbd455514cd1f4a3
tree3bc7dbe10a781f500800a45569fb5786baef6c54
parent9ae43567a3ef14a815482ccaa2a5b412a716743e

Sema: suggest using try/catch/if on method call on error union


2 files changed, 24 insertions(+), 0 deletions(-)

src/Sema.zig+3
......@@ -27888,6 +27888,9 @@ fn fieldCallBind(
2788827888 const decl = mod.declPtr(decl_idx);
2788927889 try mod.errNoteNonLazy(decl.srcLoc(mod), msg, "'{}' is not a member function", .{field_name.fmt(ip)});
2789027890 }
27891 if (concrete_ty.zigTypeTag(mod) == .ErrorUnion) {
27892 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
27893 }
2789127894 break :msg msg;
2789227895 };
2789327896 return sema.failWithOwnedErrorMsg(block, msg);
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'