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
@petrochenkov
Copy link
Contributor

Thanks!
I'll start crater next.
@bors try

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 28, 2025
Support importing path-segment keyword with renaming
@petrochenkov petrochenkov added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 28, 2025
@rust-bors
Copy link

rust-bors bot commented Nov 28, 2025

☀️ Try build successful (CI)
Build commit: 18c1d18 (18c1d185ac7a6a3bf399296b35e4b2736809a756, parent: 9050733395f7c10560d49f549e022f411fa28cea)

@petrochenkov
Copy link
Contributor

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-146972 created and queued.
🤖 Automatically detected try build 18c1d18
🔍 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

@mu001999
Copy link
Contributor Author

@petrochenkov Thanks a ton! I appreciate you taking the time to review this.

@bors
Copy link
Collaborator

bors commented Nov 30, 2025

☔ The latest upstream changes (presumably #149462) made this pull request unmergeable. Please resolve the merge conflicts.

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. 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

8 participants