Is it possible to import a module into itself?


#1

Today I’ve discovered this apparently valid program:

pub mod a {
    pub use super::*;
    pub fn a() {}
}

fn main() { a::a::a::a::a::a::a::a(); }

Are such “infinite” paths valid?


#2

The paths are not infinite. You’d need an infinite source file to make it so.

Apart from that, I could see a clippy lint against this. What do you think?


#3

Challenge accepted!

$ (echo "pub mod a { use super::*; pub fn a() {} } fn main() {"; yes "a::"; echo "a(); }") | rustc -

But memory is not infinite, sadly… (Don’t OOM yourself trying this!)


#4

Yep, this is valid. More generally, module paths are allowed to be cyclic.