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 3c4b5af to c8f12de Compare December 2, 2025 14:52
@rustbot
Copy link
Collaborator

rustbot commented Dec 2, 2025

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.

@craterbot
Copy link
Collaborator

🚧 Experiment pr-146972 is now running

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

@craterbot
Copy link
Collaborator

🎉 Experiment pr-146972 is completed!
📊 21 regressed and 2 fixed (746877 total)
📊 2104 spurious results on the retry-regessed-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/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. labels Dec 9, 2025
@petrochenkov
Copy link
Contributor

There are two groups of regressions.

One (17 cases, self without braces at the end):

  • csv2svg-0.1.12 use {std::{io::self}}
  • ChangedUser.sabi.ebc01e2200acd6d16c243015faf05a3355566158 - use std::{io::self}
  • GoldsteinE.uuis.56228af2c5f7bdc31734d1ab3e9bb192f5675102 - use crate::{ui::self}
  • JRF63.Rust-Practice-Rosalind.1f97206e167b276c936edf5702e97f005ce9cb08 - use std::{env::self}
  • Schaudge.MisMatchFinder.145cf4c02ff7dfc1240ae78f41e239126299468c - use mismatchfinder::{bamreader::self}
  • SebastianHollizeck.rust-mismatchfinder.1101a81b439e6cd431882cb22af8ef505214fb1a - use mismatchfinder::{bamreader::self}
  • ZGUNNERL1.caesar-cipher.046d699719fa1253b002c261dc59c2b42509679c - use std::{process::self}
  • anders94.postgres-evm.a8df860f976845082f26d00da0b78a700eac13e2 - use tokio::{time::self}
  • brassardfelix-ASTAR.MisMatchFinder_revised.6447066f25651b3b9c7f3ada94560d6e794306f2 - use mismatchfinder::{bamreader::self}
  • eugenesvk.helpFuturesSignals.78b11808fa4d2874fad5e1b0e5c274b59b1efb0d - use futures::{future::self}
  • kai-thanner.emoji-picker.8610e7c1669e26defc58793cb1b5ff18acaee123 - use std::{fs::self}
  • nataliescottdavidson.walkbuilder-example.8624c9bb7373b44ee1e126c64a4a260898f44a96 - use std::{fs::self}
  • solanamonk.lwus-gumdrop.65531329a2f48a5f0a9d008926a281e2fee7534b - use solana_program::{system_instruction::self}
  • xinyu-zheng.umalloc.feeb120703fea7a81fb52c0a92a1d1c68c7b7442 - use core::{cmp::self}
  • extreme-666.666.666666 - use std::{thread::self}
  • patoka-0.1.0 - use crate::{worker::{task_assistant::self}}
  • volga-0.7.2 - use tokio::{io::self}

And another (1 case, import without a name):

  • rutie-serde-0.3.0 use $crate::{self}

@petrochenkov
Copy link
Contributor

@mu001999
Could you write the change description for lang team?
#146978 (comment) or #147984 (comment) or #149596 (comment) could be used as examples.
@rustbot author

@rustbot rustbot 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 Dec 9, 2025
@mu001999
Copy link
Contributor Author

mu001999 commented Dec 9, 2025

Could you write the change description for lang team?

Sure! I'll do this later

@mu001999
Copy link
Contributor Author

mu001999 commented Dec 10, 2025

Change description for lang team

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};
Tighter error checking for self imports

Non-keyword path segments using self continue to work as before, but we now more strictly enforce the rule that self imports must appear within braces. Previously, nested use-trees like use a::{b::self}; were accepted. These are now rejected.

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.

Crater run shows 2 groups of regressions, 18 in total: 17 of these are due to the stricter checking of self imports in nested positions, 1 of these is due to denying importing $crate without renaming, i.e., use $crate::{self}; - #146972 (comment).

@mu001999
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 10, 2025
@petrochenkov petrochenkov added S-waiting-on-t-lang Status: Awaiting decision from T-lang I-lang-nominated Nominated for discussion during a lang team meeting. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 10, 2025
@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 Dec 17, 2025
@traviscross
Copy link
Contributor

Thanks to @mu001999 for putting this forward and to @petrochenkov for supporting this. This makes sense to me. I propose that we do it, and I'll then file a concern we discussed in the meeting (thanks @Nadrieril) about documentation.

@rfcbot fcp merge lang

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Dec 17, 2025

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@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. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Dec 17, 2025
@traviscross
Copy link
Contributor

@rfcbot concern documentation

The linked Reference PR (rust-lang/reference#2010) is a minor clarification. Let's please go ahead and also make a Reference PR documenting the rules described in this stabilization report.

@tmandry
Copy link
Member

tmandry commented Dec 17, 2025

@rfcbot reviewed

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Dec 17, 2025

@rfcbot concern consistent-axioms

I am a bit confused by why we would make errors in some of these cases and also the overall philosophical approach.

In particular I expect as an "axiom" that, for all $path, the following should EITHER both work OR both error:

use $path::{self};
use $path::self;

...but the PR description suggests that where use foo::{self} would work but use foo::self would not. I understand why the latter would be discouraged (with a lint) but I think it should be allowed. I can imagine code generators or macros wanting to add self or wanting to be able to simplify a {identifier} to identifier without having to special-case self.

My rule of thumb is that the language should accept things that have only one obvious meaning but, if they seem "redundant" or may suggest a broken mental model for users, issue a lint.

Thinking over my model for how this works, I think of it like this...

  • an import path has two things: (a) the absolute path and (b) optionally, a name;
  • something like use foo::bar has (a) the absolute path foo::bar with (b) the name bar
  • $path::self is "identity", it preserves both
  • self, super, crate, and $crat, when used as the prefix of a path, expand to (a) the absolute path you'd expect but (b) no name.
  • it is an error to import something with no name without a use.

This implies that e.g. use super is an error but use super as X, use super::{self as X} and use super::self as X should all work.

@petrochenkov
Copy link
Contributor

petrochenkov commented Dec 17, 2025

@nikomatsakis
Right now the model is that self in use prefix::{self as name}; is not a part of the path, but rather a part of the braced import {...} syntax.
This self is never resolved as a name, but gets rewritten into use prefix as name instead.
So it could e.g. use some non-keyword operator instead (use prefix::{<- as name} or something).
This PR continues to follow that model.

We could certainly extend the language to make it a part of the path, but then I'd rather support self and super in all positions in paths, like . and .. are supported everywhere in filesystem paths.
If this is a desirable direction, then this PR could bring back the support for use std::{io::self} (without extending it), and unbreak most of the regressions.

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. S-waiting-on-t-lang Status: Awaiting decision from T-lang 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