N00b question on "A private type was used in a public type signature"

I have some difficulty understanding the below: since foo is private, isn't the function bar only accessible within the crate anyway? So why is this a violation?

Erroneous code example:

#![deny(private_in_public)]
struct Bar(u32);

mod foo {
    use crate::Bar;
    pub fn bar() -> Bar { // error: private type in public interface
        Bar(0)
    }
}

I've never seen or used the private_in_public lint, and getting a clear description of what it's supposed to do is inexplicably difficult. But from context I infer that that's the cause of the compile error.

Unfortunately the compile error index only really describes how to fix E0446, not the reasoning behind it.

The private_in_public tracking issue goes into a lot more detail and includes links to the RFC introducing that lint. Maybe that'll help shed some light on things.

I checked that RFC but am still confused: it seems to refer to a different scenario, where

mod m {
    struct S;
    pub fn f() -> S { S } // ERROR
}

This I understand because the struct S is private so it shouldn't be exposed. But in my example, the struct Bar is already visible to the parent of foo, so why is it still a problem?

If something is visible to the parent module, this module can make it visible to the outside:

#![deny(private_in_public)]
struct Bar(u32);

mod foo {
    use crate::Bar;
    pub fn bar() -> Bar { // error: private type in public interface
        Bar(0)
    }
}

// this line would expose private type
pub use foo::bar;
1 Like