Skip to content

Private type in exported type signature when it shouldn't #23585

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

Closed
tomaka opened this issue Mar 21, 2015 · 7 comments
Closed

Private type in exported type signature when it shouldn't #23585

tomaka opened this issue Mar 21, 2015 · 7 comments

Comments

@tomaka
Copy link
Contributor

tomaka commented Mar 21, 2015

This code:

mod A {
    struct X;

    mod B {
        use A::X;
        pub fn foo(_: X) {}
    }
}

Triggers:

src\main.rs:6:23: 6:24 error: private type in exported type signature
src\main.rs:6         pub fn foo(_: X) {}

The foo function is only visible to A and B, and X is also visible to both A and B, so I don't see why this would be forbidden.

This was introduced in the rustc 1.0.0-nightly (f4e0ce66a 2015-03-19) (built 2015-03-20) nightly. With the 2015-03-17, this works.

@tomaka
Copy link
Contributor Author

tomaka commented Mar 21, 2015

Probably related to #23290 #22261

cc @nrc @nikomatsakis then

@alexcrichton
Copy link
Member

I believe that this is intentional behavior, this is a safeguard against reexporting foo at a reachable location for the crate, for example:

pub use A::foo;

mod A {
    pub use A::B::foo;
    struct X;

    mod B {
        pub fn foo(_: super::X) {}
    }
}

@tomaka
Copy link
Contributor Author

tomaka commented Mar 21, 2015

Shouldn't the reexport trigger an error then?

This removes a lot of flexibility in the modules system.
Instead of splitting functionnalities in multiple submodules, this will force users to put everything in the same file, leading to big blobby files with thousands of lines of code.

@nrc
Copy link
Member

nrc commented Mar 21, 2015

This is indeed intended behaviour (although not optimal). Basically, we have a very simple rule which is safe - public signatures can never include public types. There is a more optimal rule which we might implement in the future which takes in to account the privateness of the modules (see discussion on the bugs).

The re-export is not an error because it is not public - it is perfectly fine to use X inside B in private fns, etc.

@nrc nrc closed this as completed Mar 21, 2015
@tomaka
Copy link
Contributor Author

tomaka commented Mar 22, 2015

That is disappointing, I was convinced that it was a bug.
In glutin I now have to use a few dirty hacks to bypass this restriction, and I have no alternative except code duplication.

@tomaka
Copy link
Contributor Author

tomaka commented Mar 22, 2015

Also, this compiles when it shouldn't:

mod B {
    pub use self::A::foo;

    mod A {
        pub struct X;
        pub fn foo(_: X) {}
    }
}

foo is now accessible from the outside but not X.

This new rule is too restrictive and doesn't even totally protect you.

@nrc
Copy link
Member

nrc commented Mar 22, 2015

I filed #23621 for the bug above

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

No branches or pull requests

3 participants