# Modules that inherit \`super\` scope

**URL:** <https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809>\
**Category:** language design\
**Created:** [June 27, 2018, 3:02am UTC](https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809 "2018-06-27T03:02:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [June 27, 2018, 3:02am UTC](https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809/1 "2018-06-27T03:02:00Z")

</div>

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:

```rust
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 use`s. However, I’m not a fan of having to write extraneous `use`s to achieve this pattern.

The weakest form of what I propose is the following (syntax is a strawman):

```rust
#[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.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [June 27, 2018, 4:18am UTC](https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809/2 "2018-06-27T04:18:38Z")

</div>

On phone, so expect terseness and 🐕

- What does the `use foo` accomplish?
- `use super::*;` is one line. I don’t understand how any proposal could make this more ergonomic.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [June 27, 2018, 6:10am UTC](https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809/3 "2018-06-27T06:10:16Z")

</div>

So I wrote this big long response to your points, stared at them again, and realized that what I’ve described isn’t really what I want. I think I _really_ want two things:

- For `mod foo { .. }` automatically dump their contents into local scope, so they can be referred to by `foo::bar` instead of `self::foo::bar`. I’ve been writing Rust for over a year now and I _still_ forget that I need to do that.
- To not have to write `use super::*` in block-style modules, and I _always_ forget to include it, because I sort-of expect that I can just wrap a bunch of items in a file in a `mod foo { .. }` and for everything to still work (after I add `foo::` in the necessary places). Maybe, what I secretly want is for diagnostics to suggest importing with `super` in situations where it’d be better than a fully-qualified path?

Like, the more I think about it the more I feel like Rust’s module system, with all its problems, already does a better job of organizing code than, say, packages in Scala.

Also it’s late, so this might all be much more coherent in my head than on paper… -\_-

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [June 27, 2018, 11:53am UTC](https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809/4 "2018-06-27T11:53:20Z")

</div>

> [@mcy](#):
>
> For `mod foo { .. }` automatically dump their contents into local scope, so they can be referred to by `foo::bar` instead of `self::foo::bar` . I’ve been writing Rust for over a year now and I _still_ forget that I need to do that.

But it doesn't actually do that. Paths outside of `use` are already relative and do not require `self`, and there is nothing you can `use` to avoid writing `self` in the places where it is required. (which are `use` statements)

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [June 27, 2018, 3:27pm UTC](https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809/5 "2018-06-27T15:27:36Z")

</div>

Huh… ok. You’re right, I was totally hallucinating how paths outside of `use` worked last night. =S Thanks for pointing that out for me.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/modules-that-inherit-super-scope/7809/6 "2019-03-25T08:30:26Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
