Skip to content

Rust 1.16 leaking private types out of a crate library through "pub use" #40831

Closed
@erickt

Description

@erickt
Contributor

Hello!

We found a case where it's possible to leak a private type through the use of pub use. Consider this crate with a single file buffer.rs:

mod buffer {
    pub struct EnumeratePixels;
    pub struct ImageBuffer;

    impl ImageBuffer {
        pub fn enumerate_pixels(&self) -> EnumeratePixels {
            EnumeratePixels
        }
    }
}

pub use buffer::ImageBuffer;

Rust allows it to compile, even though EnumeratePixels is not publicly exposed from root:

% rustc --crate-type lib buffer.rs
%

It's possible to then use this crate like so:

extern crate buffer;

fn main() {
    let buffer = buffer::ImageBuffer;
    let _pixels = buffer.enumerate_pixels();
}

Rust 1.16 allows this to compile:

% rustc -L . bug.rs
%

However, it's impossible to name the type of enumerate_pixels():

extern crate buffer;

fn main() {
    let buffer = buffer::ImageBuffer;
    let _pixels: buffer::buffer::EnumeratePixels = buffer.enumerate_pixels();
}

This produces:

% rustc -L . bug.rs
error: module `buffer` is private
 --> bug.rs:5:18
  |
5 |     let _pixels: buffer::buffer::EnumeratePixels = buffer.enumerate_pixels();
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

I suspect this bug may have been reported in a few other tickets: #33077, #39437, #38844, #39437

Activity

Thinkofname

Thinkofname commented on Mar 26, 2017

@Thinkofname

This seems to be intended according to #34537 which documents using private modules to leak private types.

petrochenkov

petrochenkov commented on Mar 26, 2017

@petrochenkov
Contributor

@Thinkofname is right, this is the intended behavior.

retep998

retep998 commented on Mar 26, 2017

@retep998
Member

The fact that this is intended behavior is why I'm so opposed to the current private in public rules.

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

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @erickt@retep998@Thinkofname@petrochenkov

        Issue actions

          Rust 1.16 leaking private types out of a crate library through "pub use" · Issue #40831 · rust-lang/rust