Since I’m on a roll today thinking about modules, here’s something I consider an obnoxious papercut in Rust’s module system is the following:
struct Foo;
// a module used to introduce an additional path component
// to some functions that all belong together, but I don't intend
// to expose outside this module
mod internal {
fn bar(x: Foo) {} // error[E0412]: cannot find type `Foo` in this scope
}
internal::bar(Foo); // error[E0433]: failed to resolve. Use of undeclared type or module `internal`
I think this is a sane default, since it prevents namespace pollution due to pub uses. However, I’m not a fan of having to write extraneous uses to achieve this pattern.
The weakest form of what I propose is the following (syntax is a strawman):
#[sealed]
mod foo {
}
// desugars to
mod foo {
use super::*;
}
use foo;
Note that this is the braces form of module declaration; #[sealed] mod foo; seems very questionable, since it desugars to adding a use statement in another file. Ostensibly you could allow #[sealed] pub mod, but that’s kind of against the narrow pattern I’m proposing. I’m pretty sure this could be done with a proc macro, so maybe I should write a toy implementation to experiment with this.