Skip to content

Conversation

@mu001999
Copy link
Contributor

@mu001999 mu001999 commented Sep 24, 2025

Reference PR

Description

This PR unifies and extends the behavior of importing path-segment keywords (crate/$crate/super/self), resolving several long-standing inconsistencies.

Previously, Rust only allowed use crate as name; without renaming support for other path keywords. This PR enables importing these keywords with explicit renaming. And it also denies importing these keywords without renaming.

What's now allowed

For crate and $crate:

  • use crate as name;
  • use crate::{self as name};
  • use $crate as name;
  • use $crate::{self as name};

For super (including chained super::super):

  • use super as name;
  • use super::{self as name};
  • use super::super as name;
  • use super::super::{self as name};

For self:

  • use self as name;
  • use self::{self as name};
Removed error codes

Two error codes are no longer emitted:

  • E0430: Previously emitted for duplicate self imports like std::fmt::{self, self}. The existing E0252 ("name defined multiple times") provides sufficient guidance.
  • E0431: Previously emitted for use {self as name}; and use ::{self as name}; (in edition 2015). These patterns are now explicitly allowed. For use {self}; and use ::{self} (in edition 2015) without renaming, the new clearer error suggests adding an explicit rename.
Future

We plan to remove error E0429 and support self at the end of paths (#146972 (comment)). This language extension and lint for redundant ::self instead of hard error E0429 will be landed separately in the future.


Fixes #29036
Fixes #35612
Fixes #37156
Fixes #146967
Fixes #149811

r? petrochenkov

@rustbot
Copy link
Collaborator

rustbot commented Sep 24, 2025

Failed to set assignee to [petrochenkov]: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 24, 2025
ehuss added a commit to ehuss/reference that referenced this pull request Sep 24, 2025
The previous wording for this restriction was pretty confusing to me. I
don't remember what I was thinking when I wrote it, and I can't find any
historical explanation either. `use` paths can use `$crate` as long as
they have more than one segment (`use $crate::foo` is obviously OK).

I have rewritten this to make it clear it is specifically about `use
$crate`. One could say that restriction is already covered by the
previous point that says `use crate;` requires an `as`, but for some
reason `use $crate as foo` doesn't work either. So I have left this as a
separate rule for now.

cc rust-lang/rust#146972 (comment)
for context.
@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from ce2578f to 49c425d Compare September 25, 2025 01:54
@mu001999 mu001999 changed the title Emit error UnnamedCrateRootImport also for DollarCrate Disable use $crate::{self} like use $crate Sep 25, 2025
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 26, 2025
@mu001999 mu001999 marked this pull request as draft October 8, 2025 06:43
@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from 49c425d to d0d3a9d Compare October 9, 2025 03:40
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from d0d3a9d to c8526a5 Compare October 13, 2025 15:10
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from c8526a5 to db9bb42 Compare October 14, 2025 14:40
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch 2 times, most recently from df75a52 to 96820fa Compare October 16, 2025 13:28
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from 96820fa to 1473c4c Compare October 16, 2025 15:47
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from 1473c4c to e0d5fa0 Compare October 23, 2025 14:50
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from e0d5fa0 to 4c38159 Compare October 23, 2025 15:42
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from 4c38159 to aca5e4e Compare October 24, 2025 14:49
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from aca5e4e to 30a19ad Compare October 24, 2025 17:22
@craterbot
Copy link
Collaborator

🎉 Experiment pr-146972-1 is completed!
📊 1 regressed and 0 fixed (1897 total)
📊 122 spurious results on the retry-regressed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-146972-1/retry-regressed-list.txt

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging labels Jan 22, 2026
@traviscross traviscross added relnotes Marks issues that should be documented in the release notes of the next release. and removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Jan 23, 2026
@mu001999
Copy link
Contributor Author

Why did craterbot remove S-waiting-on-documentation?

@rustbot label +S-waiting-on-documentation

@rustbot rustbot added the S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging label Jan 23, 2026
@petrochenkov petrochenkov removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 23, 2026
@petrochenkov
Copy link
Contributor

Blocked on rust-lang/reference#2136.

Veykril added a commit to Veykril/rust-analyzer that referenced this pull request Jan 23, 2026
github-merge-queue bot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Jan 23, 2026
@ehuss
Copy link
Contributor

ehuss commented Jan 25, 2026

I haven't seen it mentioned here, but I just wanted to note a confusing inconsistency with super after self in a path. Usually super is allowed after self when referring to non-module items. However, it seems like it isn't allowed for a module. I'm not entirely certain that is the rule.

mod m {
    fn f() {}
    mod inner {
        // OK: imports f
        use self::super::f;
        // Ok
        use super as m_alias;
        // Error: `super` in paths can only be used in start position or after another `super`
        // Error message is confusing, because `self::super::f` was allowed.
        use self::super as m_alias2;
        // Same error as above.
        use self::super;
    }
}

Is the rule something like this?

If super is used after self, then it is not allowed to import to a module.

Is this intentional, or is there some issue for this?

@ehuss
Copy link
Contributor

ehuss commented Jan 25, 2026

Not mentioned in the description is that the following is now allowed in 2015:

use ::{self as other}; // imports the crate root as other

Before this PR it was an error (error[E0431]: `self` import can only appear in an import list with a non-empty prefix)

It makes sense to me, but I haven't seen much edition-specific differences noted here.

@petrochenkov
Copy link
Contributor

petrochenkov commented Jan 25, 2026

Is the rule something like this?

If super is used after self, then it is not allowed to import to a module.

The rule is "super in paths can only be used in start position or after another super" as the error message says.
If it's allowed elsewhere then I see it as a bug (hopefully, the current compiler's behavior is not documented in the reference), but this PR didn't touch any path resolution logic outside of imports to fix it.

@ehuss
Copy link
Contributor

ehuss commented Jan 25, 2026

I was contrasting it with use self::super::f; where super is allowed after self.
The current documentation for super is here: https://doc.rust-lang.org/nightly/reference/paths.html#r-paths.qualifiers.super.allowed-positions where it says:

It may only be used in leading segments of the path, possibly after an initial self segment.

What I'm getting at is that the error message says "it can only be used after another super", but that seems to be missing "possibly after an initial self...sometimes".

@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Jan 25, 2026
@mu001999
Copy link
Contributor Author

mu001999 commented Jan 26, 2026

Is the rule something like this?

If super is used after self, then it is not allowed to import to a module.

Is this intentional, or is there some issue for this?

This is intentional in current impl of this PR (#146972 (comment)).

If it's allowed elsewhere then I see it as a bug

There are some usages of self::super::name in fact, quick search on github (https://github.com/search?q=self%3A%3Asuper+language%3ARust&type=code&l=Rust). I think we shouldn't treat such usages as bug, especially considering that we already documented such usage in reference (#146972 (comment)).

I think supporting this for importing super in this PR is reasonable.

@mu001999
Copy link
Contributor Author

mu001999 commented Jan 26, 2026

Not mentioned in the description is that the following is now allowed in 2015:

use ::{self as other}; // imports the crate root as other

Before this PR it was an error (error[E0431]: `self` import can only appear in an import list with a non-empty prefix)

It makes sense to me, but I haven't seen much edition-specific differences noted here.

Oh, yes, this is missed in the description. I will update the PR description. (Updated)

lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 27, 2026
lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 27, 2026
@traviscross
Copy link
Contributor

traviscross commented Jan 28, 2026

@rfcbot concern self-super-behavior

It seems there are somewhat diverging answers here; let's hold this for a moment and review this question. I've renominated.

I haven't seen it mentioned here, but I just wanted to note a confusing inconsistency with super after self in a path. Usually super is allowed after self when referring to non-module items. However, it seems like it isn't allowed for a module. I'm not entirely certain that is the rule.

mod m {
    fn f() {}
    mod inner {
        // OK: imports f
        use self::super::f;
        // Ok
        use super as m_alias;
        // Error: `super` in paths can only be used in start position or after another `super`
        // Error message is confusing, because `self::super::f` was allowed.
        use self::super as m_alias2;
        // Same error as above.
        use self::super;
    }
}

Let's also explicitly consider the edition change for Rust 2015 that's part of this:

Not mentioned in the description is that the following is now allowed in 2015:

use ::{self as other}; // imports the crate root as other

Before this PR it was an error (error[E0431]: `self` import can only appear in an import list with a non-empty prefix)

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jan 28, 2026
@petrochenkov
Copy link
Contributor

My opinion regarding self::super is the same as regarding something::self, we either need to support self and super in any positions in paths like . and .. in filesystem paths, or not support these random special cases just for bug compatibility.

@joshtriplett
Copy link
Member

joshtriplett commented Jan 28, 2026

I just tested with current rust 1.93. The following code does not compile in any edition:

use ::{self as abc};

It gives E0431 in every edition. So, changing that to compile does not appear to be an edition-specific change.

Note that the following code does compile in every edition:

use std::{io::self as abc};
use std::io::{self as abcd}; 

@nikomatsakis
Copy link
Contributor

Hmm. I read over the comments. My take is generally:

  • I didn't realize we support use super and use self::super but I would expect them to be equivalent.
  • I think it's fine for us to have special things like super that can only appear in the prefix; I do not think this means we have to support use foo::bar::super::baz, for the reasons we articulated before (it introduces ambiguity when foo::bar is a pub use about "which super" is intended).
  • I do not understand the rationale for forbidding use self::super::bar from importing a module, near as I can tell that works on stable.

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

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet