Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
insaneinside opened this issue Aug 11, 2016 · 6 comments
Assignees
Labels
A-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@insaneinside
Copy link
Contributor

insaneinside commented Aug 11, 2016

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.
@insaneinside
Copy link
Contributor Author

I'd be happy to take a shot at fixing these issues, but I'd like some feedback on my summary of what's going on before I do.

@brson brson added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Aug 12, 2016
@eddyb
Copy link
Member

eddyb commented Aug 12, 2016

use super::{self, ...}; doesn't special-case the self keyword as a keyword like use foo::bar::{self, ...}; does.

Why do you say this? Judging by the errors, use super::{self}; is equivalent to use super;.

@petrochenkov
Copy link
Contributor

This is similar to #29036

@petrochenkov
Copy link
Contributor

Inconsistent handling of keyword status for self and super as module names

This is not true, try to experiment more. Keywords in identifier positions do the next things: 1) report a non-fatal error, 2) define an identifier with keyword name - this is a part of error recovery in parser.

So, mod super actually defines a module named super. This is ok because the code is already erroneous and we can do anything more or less reasonable, perfect error recovery is not guaranteed.

mod super { // reports an error and defines a module
    mod m {
        pub struct S;
    }

    // no error, module super is imported, this is bug #29036,
    // naked self and super are not treated correctly in imports
    use super as ident;
    // no error, everything is correctly routed to "super"::m::S
    type Z = ident::m::S;
}

@petrochenkov petrochenkov self-assigned this Feb 19, 2017
@petrochenkov petrochenkov added the A-resolve Area: Name resolution label Feb 19, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Jan 25, 2020

I'm not sure what action should be taken here. @petrochenkov do we need to change any behaviour or should we just improve diagnostics?

@petrochenkov
Copy link
Contributor

In general, the behavior needs to be changed.
Imports of self and super need to be accepted if they are renamed into something that is not a keyword, similarly to crate.

use crate as name; // OK
use crate; // ERROR, needs the "as name" part

For the examples in the issue it will only result in diagnostic changes though, because they are all erroneous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants