enum A {}
impl A {
fn foo() {
use Self::*; // error: unresolved import `Self`
}
}
It's counterintuitive that this fails. At the very least (if it's not possible to fix this), we should special case the error message to explain the problem.
https://github.com/rust-lang/rust/issues/49683#issuecomment-458886172 may be related.
cc @petrochenkov @estebank
I would like to try and write a patch for this. @Centril, do you think this would require an RFC first? (Apologies for the ping, you were the last commenter)
It depends on how we close this ticket: if we extend the language to allow Self, then the lang team should chime in. If we continue to deny it rustc should suggest using A instead of Self here. For the later you can go ahead and implement it directly without much fuzz (and likely it would be 90% of the machinery to implement the actual lang feature). For the former, you _could_ make the argument that this falls under the existing RFC (although there's no direct mention about this, one way or another).
I think we decided to "interpret the RFC" when we actually stabilized the feature as not legitimizing this, or to be more accurate, the language team essentially overrode that section of the RFC. (See https://github.com/rust-lang/rust/pull/61682 for details.) I think it would be best to focus on the diagnostics work right now as supporting use Alias::Variant; would also need to be supported (and that doesn't seem as likely.)
@Centril: could you point out where in #61682 this is mentioned? I couldn't spot it by glancing through, though the discussion thread is quite long, so I could easily have missed it.
would also need to be supported (and that doesn't seem as likely.)
Is this also mentioned in that pull request? Why is this?
@varkor See the heading "Divergences from RFC". In particular notice how we don't allow pub use Alias::Some; contrary to the RFC. This is then discussed (see in particular comments referenced by @petrochenkov and @eddyb). In particular:
For
use E2::*;to work, it would have to be sugar foruse <E2>::*;, and involve the typesystem in name resolution (if any generic arguments are involved, you would have to transport them faithfully), which isn't really doable atm.
This is a more general problem, in thatuse Foo::...;won't import associated consts/types/methods from types/traits.
Is this also mentioned in that pull request? Why is this?
It is not, but it would be ad-hoc, and contrary to the spirit of https://github.com/rust-lang/rust/pull/61682 (in my view) to support type relative resolution for use Self::*; specifically but not for use Alias::*;. I don't see a strong justification for such a special case.
[triagebot] Yeah, Self::anything is a type-based resolution, so it isn't supposed to work in imports.
Ideally, this fact should be conveyed in the error message reported when someone attempts to use a any path that may use type-based resolution, not only Self::..., in an import.
Most helpful comment
[triagebot] Yeah,
Self::anythingis a type-based resolution, so it isn't supposed to work in imports.Ideally, this fact should be conveyed in the error message reported when someone attempts to use a any path that may use type-based resolution, not only
Self::..., in an import.