From f14cf13ff8553030c47748a0cbd455514cd1f4a3 Mon Sep 17 00:00:00 2001 From: Wooster Date: Sun, 30 Jul 2023 11:10:41 +0200 Subject: [PATCH] Sema: suggest using try/catch/if on method call on error union --- src/Sema.zig | 3 +++ .../method_call_on_error_union.zig | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 test/cases/compile_errors/method_call_on_error_union.zig diff --git a/src/Sema.zig b/src/Sema.zig index c55531d924..a5c5f37e4c 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -27888,6 +27888,9 @@ fn fieldCallBind( const decl = mod.declPtr(decl_idx); try mod.errNoteNonLazy(decl.srcLoc(mod), msg, "'{}' is not a member function", .{field_name.fmt(ip)}); } + if (concrete_ty.zigTypeTag(mod) == .ErrorUnion) { + try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{}); + } break :msg msg; }; return sema.failWithOwnedErrorMsg(block, msg); diff --git a/test/cases/compile_errors/method_call_on_error_union.zig b/test/cases/compile_errors/method_call_on_error_union.zig new file mode 100644 index 0000000000..4ce6b3baa8 --- /dev/null +++ b/test/cases/compile_errors/method_call_on_error_union.zig @@ -0,0 +1,21 @@ +const X = struct { + fn init() !X { + return error.a; + } + + fn a(x: X) void { + _ = x; + } +}; + +export fn entry() void { + const x = X.init(); + x.a(); +} + +// error +// backend=stage2 +// target=native +// +// :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' +// :13:6: note: consider using 'try', 'catch', or 'if'