mirror of
https://github.com/ziglang/zig.git
synced 2026-01-03 03:53:20 +00:00
Sema: suggest using try/catch/if on method call on error union
This commit is contained in:
parent
9ae43567a3
commit
f14cf13ff8
@ -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);
|
||||
|
||||
21
test/cases/compile_errors/method_call_on_error_union.zig
Normal file
21
test/cases/compile_errors/method_call_on_error_union.zig
Normal file
@ -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'
|
||||
Loading…
x
Reference in New Issue
Block a user