Skip to content

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

Closed
@wusticality

Description

@wusticality

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions