Skip to content

Conversation

@mu001999
Copy link
Contributor

@mu001999 mu001999 commented Sep 24, 2025

Reference PR:

Description:

  • Make imports consistent, self/super/crate/$crate can be imported with renaming now
    • Allow use self/super/crate/$crate as name; and also use super/crate/$crate::{self as name};
    • Deny use self/super/crate/$crate; and also use super/crate/$crate::{self};
    • Deny use ::self;, use ::{self};, use ::self as name; and use ::{self as name};
  • Proper error messages, and E0430 and E0431 are no longer emitted now
    • [E0430]: self import can only appear once in an import list was emitted before when std::fmt::{self, self}, but the emitted [E0252]: the name fmt is defined multiple times is good enough
    • [E0431]: self import can only appear in an import list with a non-empty prefix was emiited before when use {self as name};, now use {self as name}; is allowed; for use {self};, it's denied because it lacks renaming and will emit imports need to be explicitly named with suggesting renaming.

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
@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from 6439a5a to 37c4a5b Compare January 16, 2026 13:24
@rustbot
Copy link
Collaborator

rustbot commented Jan 16, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@mu001999
Copy link
Contributor Author

Previous summary: #146972 (comment)

Update:

  1. Recover previous logic making things like use std::{io::self} work, and unbreak most of the regressions
  2. Draft a reference update for this change: Document importing path-segment keyword reference#2136

The language extension (supporting self at the end of paths, and lints for cases like redundant use of ::self) will be landed separately, and keep this PR doing what it does (i.e. bugfixing), as @petrochenkov suggested in #146972 (comment)

@mu001999
Copy link
Contributor Author

mu001999 commented Jan 16, 2026

@petrochenkov Could you review the new commit? If it's okay, I will re-nominate then. Thanks ;)

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

@bors try

TODO: @craterbot check p=1 crates=https://crater-reports.s3.amazonaws.com/pr-146972/retry-regressed-list.txt

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Jan 21, 2026
Support importing path-segment keyword with renaming
@rust-lang rust-lang deleted a comment from rust-rfcbot Jan 21, 2026
@joshtriplett
Copy link
Member

Based on the summary
#146972 (comment) (and the previously linked summary in #146972 (comment) ), this seems reasonable, so I'm starting an FCP.

@rfcbot merge lang

@nikomatsakis
Copy link
Contributor

Thank you all for the discussion and iteration, the current state seems good to me. I understand it to be:

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 the crate/$crate/super keywords without renaming. (<-- this is the key change we made during discussion)

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};`

* `use a::{b::self};`
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};`. This pattern is now explicitly allowed. For `use {self};` (without renaming), a clearer error suggests adding an explicit rename.
Impact

This change makes the language more consistent and predictable when working with path keywords in imports.

Previous crater run shows 2 groups of regressions, 18 in total. 17 of these no longer apply as they were due to the stricter checking of self imports in nested positions. 1 regression still applies due to denying importing $crate without renaming, i.e., use $crate::{self}; - #146972 (comment).

@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

@joshtriplett
Copy link
Member

Trying again:

@rfcbot merge lang

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 21, 2026

☀️ Try build successful (CI)
Build commit: 310c4dc (310c4dc96b5921ed757c4cd00c4b33924ee903c6, parent: 625b63f9e148d511e187c71e5f70643ee62c77fb)

@traviscross
Copy link
Contributor

@craterbot
Copy link
Collaborator

👌 Experiment pr-146972-1 created and queued.
🤖 Automatically detected try build 310c4dc
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-t-lang Status: Awaiting decision from T-lang labels Jan 21, 2026
@traviscross
Copy link
Contributor

traviscross commented Jan 21, 2026

The FCP situation here is that we have an open proposed FCP from December with all boxes checked and two concerns.

One was mine, about the absence of a corresponding Reference PR. There now is one:

It'll need to be approved by @rust-lang/lang-docs before we merge this PR:

@rustbot labels +S-waiting-on-documentation

But we can resolve the concern:

@rfcbot resolve documentation

The other concern, consistent-axioms, was filed by @nikomatsakis. That's probably resolved now, but he'll have to resolve that.

@rustbot rustbot added the S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging label Jan 21, 2026
@traviscross
Copy link
Contributor

cc @rust-lang/fls

@nikomatsakis
Copy link
Contributor

@rfcbot resolve consistent-axioms

@rust-rfcbot rust-rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jan 21, 2026
@rust-rfcbot
Copy link
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@rust-rfcbot rust-rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jan 21, 2026
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. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. 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 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 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