rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.96k stars 12.69k forks source link

Type is not exported if type with same name is introduced via use #105235

Closed wusticality closed 1 year ago

wusticality commented 1 year ago

I'm seeing a strange issue where a type is not being exported from a module if that module imports a type with the same name. Here is an example that demonstrates this issue:

mod abc {
    pub struct Beeblebrox;
    pub struct Zaphod;
}

mod foo {
    pub mod bar {
        use crate::abc::*;

        #[derive(Debug)]
        pub enum Zaphod {
            Whale,
            President,
        }
    }

    pub use bar::*;
}
mod baz {
    pub fn do_something() {
        println!("{:?}", crate::foo::Zaphod::Whale);
    }
}

fn main() {
    baz::do_something();
}

In module bar, we're importing abc::Zaphod, and then defining a type of the same name. When exporting everything from bar via pub use bar::*, the Zaphod type is not exported. Is this expected behavior? It seems wrong to me.

I would have expected the type that's actually declared in bar to take precedence and be exported. Either that, or the compiler should emit a useful error describing the collision. At present, the type is silently omitted.

Meta

rustc 1.67.0-nightly (215e3cd21 2022-11-03)
binary: rustc
commit-hash: 215e3cd218b83b8a3152d84d92f17109253c25e1
commit-date: 2022-11-03
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Backtrace

``` Compiling playground v0.0.1 (/playground) error[[E0433]](https://doc.rust-lang.org/nightly/error-index.html#E0433): failed to resolve: could not find `Zaphod` in `foo` --> src/main.rs:20:38 | 20 | println!("{:?}", crate::foo::Zaphod::Whale); | ^^^^^^ could not find `Zaphod` in `foo` warning: unused import: `crate::abc::*` --> src/main.rs:8:13 | 8 | [use crate::abc::*;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=1980019cec4c271a2bfddb14e2f016f0#) | ^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default For more information about this error, try `rustc --explain E0433`. warning: `playground` (bin "playground") generated 1 warning error: could not compile `playground` due to previous error; 1 warning emitted ```

SNCPlay42 commented 1 year ago

Duplicate of #56593

wusticality commented 1 year ago

@SNCPlay42 Should I close this issue?