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 structure can escape from its module through impl for another private structure #30079

Closed
petrochenkov opened this issue Nov 27, 2015 · 1 comment · Fixed by #32674
Closed

Comments

@petrochenkov
Copy link
Contributor

struct PrivOuter;

mod m {
    struct PrivInner;
    impl PrivInner {
        pub fn secret(&self) { println!("Hello!"); }
    }

    // This impl is considered private because its type `super::PrivOuter` is private
    // Probably, impls should be considered public if their type is defined outside of the current module even if this type is private
    impl super::PrivOuter {
        pub fn reveal(&self) -> PrivInner { PrivInner }
    }

}

fn main() {
    PrivOuter.reveal().secret();
}

This is not fixed in #29973.
It shouldn't be a common scenario, so it can be fixed later.

@petrochenkov
Copy link
Contributor Author

This problem exists for trait impls as well:

struct PrivOuter;
trait PrivOuterTrait {
    type Output;
    fn reveal(&self) -> Self::Output;
}

mod m {
    struct PrivInner;
    impl PrivInner {
        pub fn secret(&self) { println!("Hello!"); }
    }

    impl super::PrivOuterTrait for super::PrivOuter {
        type Output = PrivInner;
        fn reveal(&self) -> PrivInner { PrivInner }
    }

}

fn main() {
    PrivOuter.reveal().secret();
}

jseyfried added a commit to jseyfried/rust that referenced this issue Apr 7, 2016
Manishearth added a commit to Manishearth/rust that referenced this issue Apr 7, 2016
…tsakis

Lay groundwork for RFC 1422  and improve `PrivateItemsInPublicInterfacesVisitor`

This PR lays groundwork for RFC 1422 (cc rust-lang#32409) and improves `PrivateItemsInPublicInterfacesVisitor`. More specifically, it
 - Refactors away `hir::Visibility::inherit_from`, the semantics of which are obsolete.
 - Makes `hir::Visibility` non-`Copy` so that we will be able to add new variants to represent `pub(restricted)` (for example, `Visibility::Restricted(Path)`).
 - Adds a new `Copy` type `ty::Visibility` that represents a visibility value, i.e. a characterization of where an item is accessible. This is able to represent `pub(restricted)` visibilities.
 - Improves `PrivateItemsInPublicInterfacesVisitor` so that it checks for items in an interface that are less visible than the interface. This fixes rust-lang#30079 but doesn't change any other behavior.

r? @nikomatsakis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant