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

private_in_public lint triggered for pub associated type computed using non-pub trait #45713

Open
scottmcm opened this issue Nov 2, 2017 · 6 comments · May be fixed by #126076
Open

private_in_public lint triggered for pub associated type computed using non-pub trait #45713

scottmcm opened this issue Nov 2, 2017 · 6 comments · May be fixed by #126076
Assignees
Labels
A-visibility Area: Visibility / privacy. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Nov 2, 2017

I was surprised to get a private_in_public deprecation warning for using a private trait to compute an associated type that's itself pub.

De-macro'd, shortened example of what I was doing:

#![feature(try_from)]

trait Bar {
    type Inner;
}

pub struct B16(u16);
pub struct B32(u32);

impl Bar for B16 {
    type Inner = u16;
}
impl Bar for B32 {
    type Inner = u32;
}

use std::convert::{TryFrom, TryInto};
impl TryFrom<B32> for B16 {
    type Error = <<B16 as Bar>::Inner as TryFrom<<B32 as Bar>::Inner>>::Error;
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//  warning: private type `<B16 as Bar>::Inner` in public interface (error E0446)
//  (Despite the fact that that actual type is core::num::TryFromIntError)
    fn try_from(x: B32) -> Result<Self, Self::Error> {
       Ok(B16(x.0.try_into()?))
    }
}

fn main() {}

Repro: https://play.rust-lang.org/?gist=1f84c630e07ddd54d2bf208aa85ed8bb&version=nightly

I don't understand how that type is part of the public interface, since I can't get to it using TryFrom.

(Do close if this is known and covered by things like rust-lang/rfcs#1671 (comment), but I couldn't find any issues talking about this part at least.)

@petrochenkov petrochenkov self-assigned this Nov 2, 2017
@petrochenkov petrochenkov added the A-visibility Area: Visibility / privacy. label Nov 2, 2017
@petrochenkov
Copy link
Contributor

This is a limitation of implementation, not some language privacy rule.

From RFC 2145:

For technical reasons it's not always desirable or possible to fully normalize associated types before checking them for privacy. So, if we see <Type as Trait>::AssocType we can guaranteedly check only Type and Trait, but not the resulting type.

That's exactly the case here, the type turns out to not be normalized enough before privacy checking.
The reason lies in parts of the compiler totally unrelated to privacy and I didn't dig into why exactly this happens (types are not fully normalized in signatures).
Maybe we should somehow force normalization in the privacy pass, or do some custom normalization, I don't know.
Pinging @nikomatsakis for more information.

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 12, 2018
@petrochenkov
Copy link
Contributor

petrochenkov commented Jun 16, 2023

ping @lcnr

I strongly suspect that we have some "fully normalize" type operation now that would work in signatures and would help us to eliminate all intermediate (associated) type aliases from types during privacy checking.

(I.e. going from <<B16 as Bar>::Inner as TryFrom<<B32 as Bar>::Inner>>::Error to core::num::TryFromIntError in the example above.)

If you could point to the right tools, then we could eliminate this piece of technical debt from privacy checking.

@petrochenkov
Copy link
Contributor

#113671 is starting to address this issue.

@joshlf
Copy link
Contributor

joshlf commented May 20, 2024

This is affecting zerocopy: google/zerocopy#1292

@lcnr
Copy link
Contributor

lcnr commented May 20, 2024

ping @lcnr

I strongly suspect that we have some "fully normalize" type operation now that would work in signatures and would help us to eliminate all intermediate (associated) type aliases from types during privacy checking.

(I.e. going from <<B16 as Bar>::Inner as TryFrom<<B32 as Bar>::Inner>>::Error to core::num::TryFromIntError in the example above.)

If you could point to the right tools, then we could eliminate this piece of technical debt from privacy checking.

If you're dealing with ty::Ty now, you can normalize them by creating an ObligationCtxt and then using ocx.deelpy_normalize(whatever) to get a fully normalized version of whatever.

@jswrenn
Copy link
Member

jswrenn commented Jun 3, 2024

I've put up a PR: #126076

jswrenn added a commit to jswrenn/rust that referenced this issue Jun 6, 2024
This permits associated types to reference private types and
traits, so long as the normalized type does not itself violate
type privacy.

Fixes rust-lang#45713
@jswrenn jswrenn linked a pull request Jun 6, 2024 that will close this issue
jswrenn added a commit to jswrenn/rust that referenced this issue Jun 6, 2024
This permits associated types to reference private types and
traits, so long as the normalized type does not itself violate
type privacy.

Fixes rust-lang#45713
bors pushed a commit to rust-lang-ci/rust that referenced this issue Jun 12, 2024
This permits associated types to reference private types and
traits, so long as the normalized type does not itself violate
type privacy.

Fixes rust-lang#45713
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 12, 2024
privacy: normalize associated types before visiting

This permits associated types to reference private types and traits, so long as the normalized type does not itself violate type privacy.

Fixes rust-lang#45713

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 18, 2024
privacy: normalize associated types before visiting

This permits associated types to reference private types and traits, so long as the normalized type does not itself violate type privacy.

Fixes rust-lang#45713

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->
jswrenn added a commit to jswrenn/rust that referenced this issue Aug 21, 2024
This permits associated types to reference private types and
traits, so long as the normalized type does not itself violate
type privacy.

Fixes rust-lang#45713
jswrenn added a commit to jswrenn/rust that referenced this issue Sep 7, 2024
This permits associated types to reference private types and
traits, so long as the normalized type does not itself violate
type privacy.

Fixes rust-lang#45713
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-visibility Area: Visibility / privacy. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants