Z012: Public function exposes private type
Detects public functions that return or accept private (non-pub) types in their signature. This creates an unusable API since callers cannot name the type.
Examples
Bad
const PrivateType = struct { value: u32 };
// expect: Z012
pub fn getPrivate() PrivateType {
return .{ .value = 42 };
}const InternalConfig = struct { debug: bool };
// expect: Z012
pub fn configure(config: InternalConfig) void {
_ = config;
}Good
Make the type public if the function is public:
pub const PublicType = struct { value: u32 };
pub fn getPublic() PublicType {
return .{ .value = 42 };
}
Types pub in any enclosing container are fine, including the container itself and pointer-wrapped forms:
pub const Connection = struct {
pub const Config = struct { retries: u32 };
pub const Io = struct {
pub fn get(self: *Io, cfg: *Config) void {
_ = self;
_ = cfg;
}
};
};
Qualified paths need both halves nameable, so a pub type inside a pub container is fine:
pub const Outer = struct {
pub const Inner = struct { v: u8 };
};
pub fn getInner() Outer.Inner {
return .{ .v = 0 };
}
A non-pub type inside one is not:
pub const Outer = struct {
const Inner = struct { v: u8 };
};
// expect: Z012
pub fn getInner() Outer.Inner {
return .{ .v = 0 };
}
Zig aliases are transparent. An alias that reduces to a type the caller can
spell without it gives that caller nothing, so its pub status does not
matter:
const Code = u40;
pub fn pack() Code {
return 0;
}pub const Bar = struct { v: u8 };
const Foo = Bar;
pub fn getFoo() Foo {
return .{ .v = 0 };
}
The same holds for builtin shapes wrapping a builtin, such as []const u8 or
?u40. A private type inside the shape is still reported:
const Hidden = struct {};
const Wrapped = [2]Hidden;
// expect: Z012
pub fn getWrapped() Wrapped {
return undefined;
}
Or make the function private:
const PrivateType = struct { value: u32 };
fn getPrivate() PrivateType {
return .{ .value = 42 };
}Rationale
A public function with a private type in its signature is effectively unusable from outside the module. Callers cannot:
- Declare variables of the return type
- Pass the correct parameter type
- Store the result in a typed container
This is almost always a mistake—either the function should be private, or the type should be public.