A Rust library's pub fn returns a private struct and builds with only a private_interfaces warning; why can downstream crates still not call it?
answer
- a warning here, an error there
- RFC 2145, Rust 1.74
- type privacy at the call site
- signature types versus bounds
- pub type, private fields
basics
~20 sSince Rust 1.74 a private type in a pub signature is only the private_interfaces warning in the library, but a downstream call still fails type privacy with "type thermo::Calibration is private". Make the type pub, or the function pub(crate).
solid answer
~40 sRust 1.74 replaced the old `private_in_public` lint with two lints from RFC 2145, both warn by default in 1.99: `private_interfaces` for a private type in an item's signature, and `private_bounds` for a private trait or type in its bounds. So `pub fn factory_calibration() -> Calibration`, with `Calibration` private, builds the library with a warning. Type privacy is still checked where the function is used: another crate calling it gets the error "type `thermo::Calibration` is private", and calling a method on the result fails the same way. The fix is a decision: make `Calibration` a `pub` type, whose fields can stay private, or narrow the function to `pub(crate)`. E0446 remains a hard error for a private type used as an associated type in an impl of a public trait. Publish libraries with these lints denied.
code
rust · 24 lines// src/lib.rs of a library crate named `thermo`
struct Calibration {
offset_kelvin: f64,
}
impl Calibration {
pub fn offset(&self) -> f64 {
self.offset_kelvin
}
}
trait Probe {
fn read_kelvin(&self) -> f64;
}
// warning: type `Calibration` is more private than the item `factory_calibration`
pub fn factory_calibration() -> Calibration {
Calibration { offset_kelvin: 0.25 }
}
// warning: trait `Probe` is more private than the item `sample`
pub fn sample<P: Probe>(probe: &P) -> f64 {
probe.read_kelvin()
}go deeper
Know that a pub function's parameter and return types must be usable by its callers, so a private struct in a pub signature is a mistake even when the library builds.
Name the two lints, private_interfaces for signature types and private_bounds for bounds, say they replaced hard errors in Rust 1.74 and warn by default, and that E0446 survives for associated types.
Diagnose the split: the library builds with a warning, the downstream crate fails type privacy, and the fix is a pub type with private fields or a pub(crate) function.
Make both lints deny-level for published libraries, and require an outside-the-crate test of each public entry point before a release, since in-crate tests cannot see the failure.
## What changed in Rust 1.74 A **public interface** is everything another crate must be able to use to call an item: its parameter and return types, a struct's public field types, and the bounds on its generics. For years rustc rejected a private type in that position outright, with E0446 ("private type in public interface") for types and E0445 for private traits in bounds. Rust 1.74 replaced the old `private_in_public` lint with the rules of **RFC 2145** (type privacy). The error index now marks E0445 as no longer emitted, and two lints, both **warn** by default in 1.99, take over most of the job: - **`private_interfaces`**: a private type in an item's primary interface, its signature. rustc's message is "type `Calibration` is more private than the item `factory_calibration`". - **`private_bounds`**: a private trait or type in the secondary interface, its bounds and where clauses, as in "trait `Probe` is more private than the item `sample`". E0446 has not disappeared: a private type used as the associated type in an impl of a public trait (`impl PubTr for u8 { type Alias = Bar; }` with `Bar` private) is still a hard error. ## Two checks in two places The trap is that the library and its users see different things: | Where | What rustc checks | What you see for `pub fn factory_calibration() -> Calibration` | |---|---|---| | the library itself | the `private_interfaces` lint at the definition | a warning; the crate builds | | a downstream crate | **type privacy** at every use of the item | error: type `thermo::Calibration` is private | | the library's own unit tests | type privacy, from inside the crate | nothing; `Calibration` is visible there | Type privacy means a crate may not use a value whose type the library declared without `pub`, even when the code never writes the type's name. The downstream call `thermo::factory_calibration()` is rejected, and so is `c.offset()` on the result, even though `offset` is marked `pub`. A warning that "only" shows up in the library's build log is therefore a downstream compile error waiting to happen. ## Fixing it Each fix is a decision about what the API is: 1. **Make the type public, keep its fields private.** `pub struct Calibration { offset_kelvin: f64 }` exposes the type and its `pub` methods while callers still cannot build or edit it. This is the usual answer when callers really need the value. 2. **Narrow the function.** If `factory_calibration` was never meant for other crates, `pub(crate) fn` makes the signature as private as its type, and both lints go quiet. 3. **A `pub` type in a private module.** Declaring `pub struct Calibration` inside a private `mod detail` and returning it compiles without warnings, and downstream code can call its methods, but cannot write the type's name in a signature or a `let` annotation. That is occasionally deliberate; more often it just makes the value awkward to store, and a `pub use` of the type from a public path is kinder. For bounds the same choice applies: make `trait Probe` public, or narrow `sample`. Making each trait method `pub` does nothing, since trait items already share the trait's visibility, and `pub` there is E0449. ## Why this slips through review - The library compiles and its unit tests, compiled inside the crate, pass. - `dead_code` adds a hint that is easy to misread: it reports the `pub` method `offset` as never used, because rustc knows no outside crate can reach it. - Nothing fails until another crate, perhaps a user's, calls the function. ## Keeping it from shipping - Deny the lints in a library: `#![deny(private_interfaces, private_bounds)]` at the top of `lib.rs`, or the equivalent entry in the manifest's lint table, which is Cargo's material. - Exercise each public entry point from outside the crate. An integration test under `tests/` is compiled as a separate crate, so it hits type privacy exactly like a downstream user. - When reading old answers, remember that "private type in public interface is a hard error" describes compilers before 1.74; on current Rust it is a warning in the library and an error for its users.
- Why do the library's own unit tests not catch this?Unit tests in a `#[cfg(test)]` child module are part of the same crate, where `Calibration` is visible, so they call `factory_calibration` without trouble. Type privacy bites only in another crate. An integration test under `tests/`, which is compiled as a separate crate, reproduces the downstream error.
- Is returning a pub type declared in a private module an acceptable fix?It compiles without warnings, and downstream code can call methods on the value, but it cannot name the type in a signature or a `let` annotation, because the module on its path is private. That is occasionally deliberate, but usually it only makes the value awkward to store; re-exporting the type from a public path is the friendlier fix.
- What does private_bounds catch that private_interfaces does not?A private trait or type used in a bound or where clause, such as `pub fn sample<P: Probe>(probe: &P)` with `trait Probe` private. rustc warns that "trait `Probe` is more private than the item `sample`". Downstream crates cannot name or implement `Probe`, so they cannot bring their own probe types. Make the trait `pub`, or narrow the function.
- A private type in a pub signature is still a hard error E0446 in every position.
- If the library compiles, every pub function in it is callable from other crates.
- The fix is to make every field of the struct pub along with the struct.
- private_interfaces and private_bounds are allow-by-default lints.
- Unit tests inside the crate prove the API is usable from other crates.
Assumes Rust 1.99 on edition 2024; since Rust 1.74 (RFC 2145) most private-in-public cases are the private_interfaces and private_bounds warnings, where E0445 and E0446 used to reject them.