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

The compiler should report publicly exported type names if possible #21934

Open
pcwalton opened this Issue Feb 4, 2015 · 5 comments

Comments

Projects
None yet
6 participants
@pcwalton
Copy link
Contributor

pcwalton commented Feb 4, 2015

If a private type is reexported at some public location, the Rust compiler will report the private location when referring to the type. It'd be more helpful if the compiler reported the public alias for it.

This was reported by Coda Hale on Twitter.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Feb 4, 2015

This happens a lot with core/std.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Mar 4, 2016

Triage: it seems like a private type can no longer be exported as a public type at all

pub mod A {
    struct Foo;
    pub use A::Foo as Bar;
}

fn main() {
    let b = A::Bar;
}

gives

hello.rs:5:13: 5:26 error: `Foo` is private, and cannot be reexported [E0364]
hello.rs:5     pub use A::Foo as Bar;
                       ^~~~~~~~~~~~~
hello.rs:5:13: 5:26 help: run `rustc --explain E0364` to see a detailed explanation
hello.rs:5:13: 5:26 note: consider marking `Foo` as `pub` in the imported module
hello.rs:5     pub use A::Foo as Bar;
                       ^~~~~~~~~~~~~

So I believe this is effectively fixed? My core/std comment is more about reexports where both are public:

pub mod A {
    pub struct Foo;
    pub use A::Foo as Bar;
}

fn main() {
    let b: i32 = A::Bar;
}

which gives

hello.rs:9:18: 9:24 error: mismatched types:
 expected `i32`,
    found `A::Foo`
(expected i32,
    found struct `A::Foo`) [E0308]
hello.rs:9     let b: i32 = A::Bar;
                            ^~~~~~
hello.rs:9:18: 9:24 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error

which is covered by another ticket whose number escapes me.

@petrochenkov

This comment has been minimized.

Copy link
Contributor

petrochenkov commented Mar 4, 2016

The issue is relevant to "unnameable" pub types which are still allowed.

mod m {
    pub struct PrivateName;
}

pub use m::PrivateName as PublicName.

m::PrivateName is still used in error messages, while it's preferable to use PublicName.

@torkleyy

This comment has been minimized.

Copy link

torkleyy commented Feb 19, 2017

It also reports names which don't exist:

Consider this library:

// lib.rs of crate foo

mod a {
    pub trait Foo {}
}

pub use a::Foo as Afoo;

pub fn foo<T: Afoo>() {}

And some binary crate using it:

extern crate foo;

use foo::foo;

fn main() {
    foo::<()>(); // Afoo is not implemented for `()`
}

The error message is:

error[E0277]: the trait bound `(): foo::Foo` is not satisfied
 --> src/main.rs:6:5
  |
6 |     foo::<()>();
  |     ^^^^^^^^^ the trait `foo::Foo` is not implemented for `()`
  |
  = note: required by `foo::foo`

So it does not report the private name but a name that does not exist.

@leodasvacas

This comment has been minimized.

Copy link
Contributor

leodasvacas commented Jan 16, 2018

Notes from my attempt at solving this:

  • The module_exports query is helpful as it returns a list of Exports in a module, the ident field is the name or rename.
  • Modifying item_path.rs directly is problematic because it is used in many different parts of the compiler. It's unclear where exactly the logic should go.

Also linking relevant internal's thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.