Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference results in accessing a private type, causing a compilation error #77986

Open
joshlf opened this issue Oct 15, 2020 · 3 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-visibility Area: Visibility / privacy. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshlf
Copy link
Contributor

joshlf commented Oct 15, 2020

I tried this code (playground):

mod foo {
    pub struct Public;

    impl AsRef<Private> for Public {
        fn as_ref(&self) -> &Private {
            &Private
        }
    }

    struct Private;

    pub fn bar() -> impl AsRef<Private> {
        Public
    }
}

pub fn baz() {
    fn blah<T>(_: T) {}

    blah(foo::bar);
}

I expected to see this happen: Code should compile successfully

Instead, this happened:

error: type `foo::Private` is private
  --> src/lib.rs:20:5
   |
20 |     blah(foo::bar);
   |     ^^^^ private type

error: type `foo::Private` is private
  --> src/lib.rs:20:10
   |
20 |     blah(foo::bar);
   |          ^^^^^^^^ private type

Note also that no error code is given.

Meta

Rust playground version 1.47.0

cc @jswrenn

@joshlf joshlf added the C-bug Category: This is a bug. label Oct 15, 2020
@jonas-schievink
Copy link
Contributor

The actual error happens on the definition of foo::bar:

warning: private type `foo::Private` in public interface (error E0446)
  --> src/lib.rs:12:5
   |
12 |     pub fn bar() -> impl AsRef<Private> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(private_in_public)]` on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>

@joshlf
Copy link
Contributor Author

joshlf commented Oct 15, 2020

That's true, although a) it's currently a warning and, b) the error that is reported later on is a different error ("type foo::Private is private" vs "private type foo::Private in public interface").

@petrochenkov
Copy link
Contributor

The behavior here is specified in https://github.com/rust-lang/rfcs/blob/master/text/2145-type-privacy.md

The error is correct.
The warning should be slightly different according to the RFC, but it should still be a warning to notify the author that a type privacy error is likely to happen, potentially in a different crate.

@camelid camelid added A-inference Area: Type inference A-diagnostics Area: Messages for errors, warnings, and lints A-visibility Area: Visibility / privacy. labels Oct 15, 2020
@Nilstrieb Nilstrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-visibility Area: Visibility / privacy. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants