Skip to content

privacy: error messages could do better at telling me why something is private #13641

@pnkfelix

Description

@pnkfelix
Contributor

Consider this code:

mod a {
    #[cfg(not(fixed))]
    struct Foo;
    #[cfg(fixed)]
    pub struct Foo;

    impl Foo {
        pub fn new() -> Foo {
            Foo
        }
    }
}

fn main() {
    let foo = a::Foo::new();

    println!("foo: {:?}", foo);
}

When I run rustc today:

% rustc --version
/Users/pnkfelix/opt/rust-dbg/bin/rustc 0.10-pre (d79fbba 2014-03-29 18:56:36 -0700)
host: x86_64-apple-darwin
% rustc /tmp/f.rs
/tmp/f.rs:15:15: 15:26 error: static method `new` is inaccessible
/tmp/f.rs:15     let foo = a::Foo::new();
                           ^~~~~~~~~~~
error: aborting due to previous error
% 

The reason that new is inaccessible, AFAICT, is because Foo is private. However, this is not really relayed in the error message; my first instinct is to look at the definition of new itself, which has been declared pub.

(I had thought there was a related bug filed somewhere about saying that if something is private due to an intermediate non-pub mod along a chain, we could do a better job of pointing the user at one or more of the non-pub mods that would need to be made pub. Obviously that's not perfect since an item may be accessible via more than one path, and so any such suggestion coukd be misleading. But I could not find that related bug. Anyway, the situation today is not ideal.)

Activity

added a commit that references this issue on Apr 20, 2014
c33cedf
added a commit that references this issue on Apr 20, 2014
30348f4
pnkfelix

pnkfelix commented on Apr 22, 2014

@pnkfelix
ContributorAuthor

I had thought there was a related bug filed somewhere about saying that if something is private due to an intermediate non-pub mod along a chain

I was probably thinking of #13422 when I wrote that. (There are probably many places where it is hard to tell what the source of non-pubness is...)

alexcrichton

alexcrichton commented on Apr 22, 2014

@alexcrichton
Member

It's true that there's a bunch of cases, but I'd love to know about missing ones like this!

added a commit that references this issue on Nov 23, 2022
added a commit that references this issue on Nov 7, 2024
ccf7c88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @alexcrichton@pnkfelix

      Issue actions

        privacy: error messages could do better at telling me why something is private · Issue #13641 · rust-lang/rust