Skip to content

self and super behave inconsistently in use statements and as module names #35612

@cjsut

Description

@cjsut

Handling of self and super in use and mod statements is inconsistent. Playpen:

use self;                        // error: `self` imports are only allowed within a { } list [E0429] (expected)
                                 // error: unresolved import `self`. There is no `self` in the crate root [E0432]

mod super {                      // error: expected identifier, found keyword `super`
    type Test = u32;
    mod submodule {
        use super;               // error: a module named `super` has already been imported in this module [E0252]
        use super::{self, Test}; // (^^^ apparently "already" means "on the next line"?)
    }
    use super;                  // (no error for this one?)
}

There are probably several (related) issues here.

  • Inconsistent handling of keyword status for self and super as module names: if we add a mod self { } to the above, we get the two "expected identifier, found keyword" errors we'd expect -- and no other output. It appears that mod self { } causes an immediate "aborting due to previous error", while mod super { } does not.
  • use super::{self, ...}; doesn't special-case the self keyword as a keyword like use foo::bar::{self, ...}; does.
  • use super; doesn't special-case the super keyword as a keyword.
  • use self; does do some special-casing so it can produce "error: self imports are only allowed within a { } list [E0429]", but it then continues by attempting to resolve self as an ident.

Metadata

Metadata

Assignees

Labels

A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions