Skip to content

Unexpected warning with use statement #30159

Closed
@Nashenas88

Description

@Nashenas88

Playground link for all of the examples.

Using this code:

trait Trait {
    fn func(&self);
}

pub struct Struct;

impl Trait for Struct {
    fn func(&self) { }
}

#[cfg(test)]
mod test_level_0 {
    use super::Trait;
    pub use super::*;

    mod test_level_1 {
        pub use super::*;

        #[test]
        fn test() {
            Struct.func();
        }
    }
}

Gives me this warning:

<anon>:13:9: 13:21 warning: unused import, #[warn(unused_imports)] on by default
<anon>:13     use super::Trait;
                  ^~~~~~~~~~~~

But, removing that line gives me this error:

<anon>:20:20: 20:26 error: no method named `func` found for type `Struct` in the current scope
<anon>:20             Struct.func();
                             ^~~~~~
<anon>:20:20: 20:26 help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
<anon>:20:20: 20:26 help: candidate #1: use `Trait`
error: aborting due to previous error
playpen: application terminated with error code 101

Switching the code to use these use statements:

trait Trait {
    fn func(&self);
}

pub struct Struct;

impl Trait for Struct {
    fn func(&self) { }
}

#[cfg(test)]
mod test_level_0 {
    use super::Trait;
    pub use super::{Trait, Struct};

    mod test_level_1 {
        pub use super::*;

        #[test]
        fn test() {
            Struct.func();
        }
    }
}

GIves me:

<anon>:14:21: 14:26 error: `Trait` is private, and cannot be reexported [E0365]
<anon>:14     pub use super::{Trait, Struct};
                              ^~~~~
<anon>:14:21: 14:26 help: see the detailed explanation for E0365
<anon>:14:21: 14:26 note: Consider declaring module `Trait` as a `pub mod`
<anon>:14     pub use super::{Trait, Struct};
                              ^~~~~
<anon>:14:21: 14:26 error: a trait named `Trait` has already been imported in this module [E0252]
<anon>:14     pub use super::{Trait, Struct};
                              ^~~~~
<anon>:14:21: 14:26 help: see the detailed explanation for E0252
<anon>:13:5: 13:22 note: previous import of `Trait` here
<anon>:13     use super::Trait;
              ^~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101

And my last attempt:

trait Trait {
    fn func(&self);
}

pub struct Struct;

impl Trait for Struct {
    fn func(&self) { }
}

#[cfg(test)]
mod test_level_0 {
    use super::Trait;
    pub use super::Struct;

    mod test_level_1 {
        pub use super::*;

        #[test]
        fn test() {
            Struct.func();
        }
    }
}

Gives me:

<anon>:21:20: 21:26 error: no method named `func` found for type `Struct` in the current scope
<anon>:21             Struct.func();
                             ^~~~~~
<anon>:21:20: 21:26 help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
<anon>:21:20: 21:26 help: candidate #1: use `Trait`
error: aborting due to previous error
playpen: application terminated with error code 101

So it seems that useing a private item followed by pub use super::* will export the private item publicly. I could be misunderstanding something here, but where I'm running into this, test_level_1 is actually a plugin that does pub use super::*, so the warning version is the only one that will compile. In other words, I can't just write the mods as:

#[cfg(test)]
mod test_level_0 {
    pub use super::*;

    mod test_level_1 {
        use Trait;
        pub use super::*;

        #[test]
        fn test() {
            Struct.func();
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions