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

Inconsistent privacy of methods of orphan non-trait impls #16398

Closed
huonw opened this issue Aug 10, 2014 · 3 comments
Closed

Inconsistent privacy of methods of orphan non-trait impls #16398

huonw opened this issue Aug 10, 2014 · 3 comments
Labels
A-visibility Area: Visibility / privacy.

Comments

@huonw
Copy link
Member

huonw commented Aug 10, 2014

mod foo {
    pub struct Foo;
    impl Foo {
        pub fn normal(&self) {
            self.private();
            self.public();
        }
    }
    mod bar {
        impl super::Foo {
            fn private(&self) {}
            pub fn public(&self) { self.private() }
        }
    }
}

fn main() {
    foo::Foo.normal();
    foo::Foo.private();
    foo::Foo.public();
}

Compilation fails with

<anon>:5:13: 5:27 error: method `private` is private
<anon>:5             self.private();
                     ^~~~~~~~~~~~~~
<anon>:19:5: 19:23 error: method `private` is private
<anon>:19     foo::Foo.private();
              ^~~~~~~~~~~~~~~~~~

(Removing those two .private calls compiles fine.)

This is inconsistent because:

  • private is only accessible inside bar, i.e. being private means it is being scoped with the module
  • public is accessible everywhere despite bar being private (if it were a freestanding function, one could only call foo::bar::public inside foo, not inside main); i.e. it appears to be being scoped with the type

I would expect both pub and non-pub methods to have the same scoping either both with the type (so foo and any descendants can call private, as well as main calling public), or both with the module (so main cannot call public).

@apoelstra
Copy link
Contributor

This is fixed by #17163, recommend close.

@alexcrichton
Copy link
Member

Thanks @apoelstra!

@nuts-n-bits
Copy link

nuts-n-bits commented May 8, 2022

@apoelstra But the behaviour has not changed, if I cargo check it today:

7  |             self.private();
   |                  ^^^^^^^ private associated function

21 |     foo::Foo.private();
   |              ^^^^^^^ private associated function

> rustc --version
rustc 1.60.0 (7737e0b5c 2022-04-04)

Removing the two private calls makes the code compile.

bors added a commit to rust-lang-ci/rust that referenced this issue Jan 21, 2024
`cargo clippy --fix`

This PR is the result of running `cargo clippy --fix && cargo fmt` in the root of the repository. I did not manually review all the changes, but just skimmed through a few of them. The tests still pass, so it seems fine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-visibility Area: Visibility / privacy.
Projects
None yet
Development

No branches or pull requests

4 participants