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

Should use super::{self, ...} work? #37156

Open
alexreg opened this issue Oct 14, 2016 · 9 comments
Open

Should use super::{self, ...} work? #37156

alexreg opened this issue Oct 14, 2016 · 9 comments
Assignees
Labels
A-resolve Area: Name resolution C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@alexreg
Copy link
Contributor

alexreg commented Oct 14, 2016

Obviously use super::self would be a bit redundant, but what about if you're importing lots of items from super?

We can already do something like:

use foo::{self, a, b, c};

So why not something like this?

use super::{self, a, b, c};

For reference, the specific error message on nightly is:

error[E0432]: unresolved import `super`
 --> ...
  |
1 | use super::{self, a, b, c};
  |             ^^^^ no `super` in the root
@petrochenkov
Copy link
Contributor

petrochenkov commented Oct 14, 2016

I suppose, any sequences of super, self and other segments should "just work" *, similarly to how .. and . work in filesystem paths - .././src/./../README / super::self::src::self::super::README - both valid.
I't just not very useful, so nobody implemented it.
* unless it complicates or slows down the implementation

cc @jseyfried

@jseyfried
Copy link
Contributor

@petrochenkov
Making self and super in path interiors "just work" would be simple to implement, but I'm not sure if it would ever be useful in practice (maybe macros?).

@alexreg
One downside of allowing use super::{self, ...}; is that you can't tell what name the self is importing just by looking at the import (whereas with use foo::{self, ...}; you know it imports foo).

Also, if super resolved to the crate root, what would use super::{self, ...}; import?

@petrochenkov
Copy link
Contributor

but I'm not sure if it would ever be useful in practice (maybe macros?).

Maybe macros, yes. Is path concatenation ever useful? Something like "base path" + "relative path" -> "absolute path". I don't know.
My logic is that super and self already work in path interiors sometimes

super::super::super::a
              ^^^^^ super is based on some path, not relative to the current module
a::b::c::{self, d}
          ^^^^ self is based on some path, not relative to the current module

, so maybe the restrictions on where they can be used may be lifted.
This is certainly not an urgent matter, just some reflections.

@petrochenkov
Copy link
Contributor

Also, if super resolved to the crate root, what would use super::{self, ...}; import?

Empty unusable name? 😄
This doesn't work either:

struct S;

mod m {
    use super::{self as root};
                ^^^^^^^^^^^^ no `super` in the root

    type A = root::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 C-bug Category: This is a bug. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-bug Category: This is a bug. labels Jul 26, 2017
@alexreg
Copy link
Contributor Author

alexreg commented Feb 3, 2018

Any update on this, @petrochenkov?

@petrochenkov
Copy link
Contributor

The consensus seems to always require the rename part (... as some_name) on imports like use super; or use crate; (rust-lang/rfcs#2126 (comment)).
Nothing new on the implementation front though.

I think it's possible to hack up some working solution quickly, but the proper solution (as I see it) is to avoid splitting import paths into two parts (the last segment and everything else) like it's done now (this splitting causes quite a bit of issues) and desugar
use a::b::c; into use a::b::c::{{type}}; use a::b::c::{{value}}; use a::b::c::{{macro}}; while keeping these imports "fused" for error reporting purposes.

@alexreg
Copy link
Contributor Author

alexreg commented Feb 4, 2018

The consensus seems to always require the rename part (... as some_name) on imports like use super; or use crate; (rust-lang/rfcs#2126 (comment)).

Why the obligatory rename on super? Would parent/ancestor modules automatically be renamed, like with crates in this RFC? I can't tell.

I think it's possible to hack up some working solution quickly, but the proper solution (as I see it) is to avoid splitting import paths into two parts (the last segment and everything else) like it's done now (this splitting causes quite a bit of issues) and desugar
use a::b::c; into use a::b::c::{{type}}; use a::b::c::{{value}}; use a::b::c::{{macro}}; while keeping these imports "fused" for error reporting purposes.

If I understand this right, then yes, I tend to agree. What are the double brackets {{ for though?

@petrochenkov
Copy link
Contributor

Why the obligatory rename on super?

Conservative choice avoiding issues in various corner cases (rust-lang/rfcs#2126 (comment) contains all the details).

What are the double brackets {{ for though?

Just some way to express "resolve this path only in the specified namespace" in my comment.

@alexreg
Copy link
Contributor Author

alexreg commented Feb 5, 2018

Okay, makes sense. I think I can get on board this then.

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-feature-request Category: A feature request, i.e: not implemented / a PR. 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

5 participants