Skip to content

Comments

Document importing path-segment keyword#2136

Open
mu001999 wants to merge 11 commits intorust-lang:masterfrom
mu001999-contrib:doc/import-path-kw
Open

Document importing path-segment keyword#2136
mu001999 wants to merge 11 commits intorust-lang:masterfrom
mu001999-contrib:doc/import-path-kw

Conversation

@mu001999
Copy link

Companion PR to rust-lang/rust#146972

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jan 16, 2026
@mu001999 mu001999 marked this pull request as draft January 16, 2026 15:04
@rustbot rustbot removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jan 16, 2026
@traviscross traviscross added S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository S-waiting-on-review Status: The marked PR is awaiting review from a maintainer I-lang-docs-nominated Nominated for discussion during a lang-docs team meeting. labels Jan 21, 2026
@ehuss
Copy link
Contributor

ehuss commented Jan 25, 2026

It looks like I don't have permission to push to this branch. I have pushed my edits to https://github.com/ehuss/reference/commits/doc/import-path-kw/.

@mu001999
Copy link
Author

It looks like I don't have permission to push to this branch.

@ehuss I have invited you :). Or I can merge your branch directly

@traviscross
Copy link
Contributor

@mu001999: If you could, please check the box that allows maintainers to push to your branch. (See, e.g., here for details.)

@mu001999
Copy link
Author

mu001999 commented Jan 28, 2026

please check the box that allows maintainers to push to your branch

@traviscross Sadly, this seems to be only available for forks in personal accounts (link):

Pull request authors can give these permissions when they initially create a pull request from a fork in a personal account

@ehuss ehuss force-pushed the doc/import-path-kw branch from 6243ecf to ef94e6a Compare January 29, 2026 15:08
@traviscross
Copy link
Contributor

We talked about this on the lang-docs office hours call. This seems to be pending some decisions and clarifications on the lang side over in rust-lang/rust#146972. @ehuss has now posted a question over in rust-lang/rust#146972 (comment).

@mu001999 mu001999 marked this pull request as ready for review February 16, 2026 03:49
@mu001999
Copy link
Author

Added the restriction and marked this PR ready for review.

@traviscross @ehuss Could you review this PR, thanks!

@mu001999 mu001999 requested a review from ehuss February 17, 2026 00:53
@ehuss
Copy link
Contributor

ehuss commented Feb 20, 2026

Thanks! Will merge after rust-lang/rust#146972 has merged.

@ehuss ehuss removed S-waiting-on-review Status: The marked PR is awaiting review from a maintainer I-lang-docs-nominated Nominated for discussion during a lang-docs team meeting. labels Feb 20, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 20, 2026
…ate, r=petrochenkov

Support importing path-segment keyword with renaming

*[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/146972)*

#### Reference PR

- rust-lang/reference#2010
- rust-lang/reference#2136

#### 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]};`. 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 crate as name`;
    - For `use {self};` and `use ::{self};` (in edition 2015) without renaming, they are equivalent to `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](https://doc.rust-lang.org/stable/error_codes/E0429.html#error-code-e0429) and support `self` at the end of paths (rust-lang#146972 (comment)). This language extension and lint for redundant `::self` instead of hard error `E0429` will be landed separately in the future.

---

Fixes rust-lang#29036
Fixes rust-lang#35612
Fixes rust-lang#37156
Fixes rust-lang#146967
Fixes rust-lang#149811

r? petrochenkov
rust-timer added a commit to rust-lang/rust that referenced this pull request Feb 21, 2026
Rollup merge of #146972 - mu001999-contrib:fix/use-dollar-crate, r=petrochenkov

Support importing path-segment keyword with renaming

*[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/146972)*

#### Reference PR

- rust-lang/reference#2010
- rust-lang/reference#2136

#### 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]};`. 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 crate as name`;
    - For `use {self};` and `use ::{self};` (in edition 2015) without renaming, they are equivalent to `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](https://doc.rust-lang.org/stable/error_codes/E0429.html#error-code-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
ehuss and others added 8 commits February 21, 2026 15:46
As with other parts of the reference, the structure is moving more
towards a set of rules, and the bullet list here isn't necessary.
This note was more of a general commentary on `self`, and was not
specifically attached to its previous rule of items.use.self.namespace.
This is somewhat duplicated with the `self` section and the restrictions
section which already describes about renaming.
This moves `$crate` to be next to `crate` since they are closely
related.
This tries to simplify the wording and to maintain consistency, since
they are all essentially saying the same thing.
The rules in the paths chapter don't entirely reflect what a valid path
in a `use` declaration is.

I'm not entirely sure what's the best approach for describing how paths
work since there are several differences between `use` paths and
expression paths.
@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2026

This PR was rebased onto a different master 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.

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

Labels

S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants