Skip to content

Suggest removing self import when not in a list #63741

Closed
@varkor

Description

@varkor
Member
use std::self; // error[E0429]: `self` imports are only allowed within a { } list

we should offer the suggestion to remove ::self, as this is likely what the user intended.

This issue has been assigned to @mibac138 via this comment.

Activity

added
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
on Aug 20, 2019
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 20, 2019
Centril

Centril commented on Aug 20, 2019

@Centril
Contributor

@petrochenkov Would it simplify or complicate things if we simply allowed this instead of rejecting it?

petrochenkov

petrochenkov commented on Aug 20, 2019

@petrochenkov
Contributor

~Simplify.

Centril

Centril commented on Aug 20, 2019

@Centril
Contributor

Might be worth doing that in that case.

(Although use foo::bar::self; is less clean from a readability perspective but perhaps that should merely be a lint.)

petrochenkov

petrochenkov commented on Aug 20, 2019

@petrochenkov
Contributor

It needs some amount design work though (not large).

The idea is to treat self and super in Rust paths as . and .. in filesystem paths.
Some questions arise in the process, e.g. if bar is an import (symlink), then whose parent bar::super refers too, or whether use foo::super [as name]; is allowed without the as name part and what name it introduces (use foo::self; needs to be allowed for compatibility and introduces foo).

Also cc #48067 (path concatenation in macros).

nikomatsakis

nikomatsakis commented on Aug 29, 2019

@nikomatsakis
Contributor

Visited in compiler triage. I think this is medium priority, clearly, but I am guessing that @Centril intended the nomination for the lang team.

Wearing my lang team hat, I think I'd expect this to compile, but I don't have strong opinions about it.

nikomatsakis

nikomatsakis commented on Sep 5, 2019

@nikomatsakis
Contributor

We discussed this in the lang team meeting last week. We decided that there were some reasons to tread carefully here: in particular, we didn't really want to admit the possibility of "non-canonical" paths like use foo::bar::self::super::bar. Moreover, there isn't a strong reason to accept this pattern -- it would definitely be linted against, and if you have some kind of tooling that wishes to emit self for simplicity, they can always emit things like use foo::bar::{self}.

So the conclusion was "probably better to just leave it alone for now" from a language perspective.

If we were to accept foo::self, we'd want some conditions:

  • always the last item in a path
  • do not accept super in that position, just self
comex

comex commented on Mar 23, 2020

@comex
Contributor

In addition to a suggestion, the compiler could have better error recovery. Currently, the following produces errors on both the first and second line; it should only error on the first line:

use std::mem; // `self` imports are only allowed within a { } list
fn main() { mem::drop(42); } // use of undeclared type or module `mem`
mibac138

mibac138 commented on May 3, 2020

@mibac138
Contributor

@rustbot claim

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @comex@nikomatsakis@Centril@jonas-schievink@varkor

    Issue actions

      Suggest removing `self` import when not in a list · Issue #63741 · rust-lang/rust