Z015: Public function exposes private error set
Detects public functions that return a private (non-pub) error-set type. Callers can still match globally named error values such as error.Failed, but they cannot name the private error-set type in declarations, function pointers, or API documentation.
Examples
Bad
const InternalError = error{ Failed, Timeout };
// expect: Z015
pub fn doWork() InternalError!void {
return error.Failed;
}Good
Make the error set public:
pub const WorkError = error{ Failed, Timeout };
pub fn doWork() WorkError!void {
return error.Failed;
}
Composed error sets are public when every part is public:
const frame = @import("frame.zig");
pub const ParseError = frame.EncodeError || error{ Unexpected };
pub fn parse() ParseError!void {}
Or intentionally expose an inferred error set:
pub fn doWork() !void {
return error.Failed;
}
An inferred error set is not an equivalent mechanical replacement: its public contract can change when the function implementation changes. Prefer a public named error set when callers need a stable API.
Or make the function private:
const InternalError = error{ Failed, Timeout };
fn doWork() InternalError!void {
return error.Failed;
}Rationale
When a public function returns a private error set, callers cannot:
- Name the error-set type in their own declarations
- Declare function pointers or interfaces with that exact return type
- Refer to the named set when documenting the API contract
Error values themselves remain globally named, so callers can still match error.Failed. Expose the named set when it is part of the public contract, use inference when an implementation-derived contract is intentional, or keep the function private.