Skip to content

Support importing path-segment keyword with renaming - #146972

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
mu001999-contrib:fix/use-dollar-crate
Feb 21, 2026
Merged

Support importing path-segment keyword with renaming#146972
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
mu001999-contrib:fix/use-dollar-crate

Conversation

@mu001999

@mu001999mu001999 commented Sep 24, 2025

Copy link
Copy Markdown
Member

View all comments

Reference PR


What was FCP'd by lang (up to date)

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};
What is denied after this PR

Importing $crate without renaming, i.e. use $crate::{self};, is now no longer permitted (#146967).

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]};. These patterns are now allowed or denied but with new clearer errors.
    • For use {self as name}; and use ::{self as name}; (in edition 2015), they are allowed now and equivalent to use self as name; and use crate as name;
    • For use {self}; and use ::{self}; (in edition 2015) without renaming, they are equivalent to use self; and use crate;, the new clearer error suggests adding an explicit rename.
    • For use ::{self [as name]}; after edition 2015, it is equivalent to use ${extern-prelude} [as name];, it is denied with new errors.
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
Copy Markdown
Collaborator

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.

@rustbotrustbot 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
Comment threadcompiler/rustc_resolve/messages.ftl Outdated
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.
@mu001999mu001999 changed the title Emit error UnnamedCrateRootImport also for DollarCrateDisable use $crate::{self} like use $crateSep 25, 2025
@petrochenkovpetrochenkov 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
mu001999force-pushed the fix/use-dollar-crate branch from 49c425d to d0d3a9dCompareOctober 9, 2025 03:40
@rust-log-analyzer

This comment has been minimized.

@mu001999
mu001999force-pushed the fix/use-dollar-crate branch from d0d3a9d to c8526a5CompareOctober 13, 2025 15:10
@rust-log-analyzer

This comment has been minimized.

@mu001999
mu001999force-pushed the fix/use-dollar-crate branch from c8526a5 to db9bb42CompareOctober 14, 2025 14:40
@rust-log-analyzer

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

@mu001999
mu001999force-pushed the fix/use-dollar-crate branch from aca5e4e to 30a19adCompareOctober 24, 2025 17:22
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mu001999

mu001999 commented Feb 5, 2026

Copy link
Copy Markdown
MemberAuthor

let me know when the implementation is aligned with this (and please point to the relevant tests)

@traviscross Aligned now and you can see the changed test and result in the last commit (e2f18e7)

@rust-log-analyzer

This comment has been minimized.

Comment threadcompiler/rustc_resolve/src/build_reduced_graph.rs Outdated
Comment threadcompiler/rustc_resolve/src/build_reduced_graph.rs Outdated
@traviscross

Copy link
Copy Markdown
Contributor

Thanks for the update.

@rfcbot resolve self-super-behavior

Note that in the Reference PR, we need to somehow capture what @ehuss said here:

What would be the rules after this PR? Given the PR as it is now, I'm thinking adding a new restriction like:

  • :: as the extern prelude cannot be imported.

@rust-rfcbot

Copy link
Copy Markdown
Collaborator

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

@rust-log-analyzer

This comment has been minimized.

@mu001999

Copy link
Copy Markdown
MemberAuthor

Spurious failure

@rustbot

This comment has been minimized.

@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@traviscross

Copy link
Copy Markdown
Contributor

@mu001999: Please be sure to see the comment in #146972 (comment). Let us know what you'd propose in rust-lang/reference#2136 for that, then mark that PR ready for review. When the Reference PR is approved, this PR can land.

@rust-bors

This comment has been minimized.

@rustbot

Copy link
Copy Markdown
Collaborator

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.

@ehuss

Copy link
Copy Markdown
Contributor

The documentation should be ready now.

@petrochenkov Do you want to give this a final review?

@petrochenkov

Copy link
Copy Markdown
Contributor

Yay, finally.
@bors r+

@rust-bors

rust-borsBot commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 6609e4b has been approved by petrochenkov

It is now in the queue for this repository.

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

Labels

disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.needs-fcpThis change is insta-stable, or significant enough to need a team FCP to proceed.P-lang-drag-1Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-langrelnotesMarks issues that should be documented in the release notes of the next release.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamto-announceAnnounce this issue on triage meeting

Projects

None yet

12 participants

@mu001999@rustbot@rust-log-analyzer@petrochenkov@craterbot@bors@traviscross@rust-rfcbot@tmandry@nikomatsakis@joshtriplett@ehuss