mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
ff7d14e17d
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 - https://github.com/rust-lang/reference/pull/2010 - https://github.com/rust-lang/reference/pull/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 (https://github.com/rust-lang/rust/pull/146972#issuecomment-3719825627). This language extension and lint for redundant `::self` instead of hard error `E0429` will be landed separately in the future. --- Fixes rust-lang/rust#29036 Fixes rust-lang/rust#35612 Fixes rust-lang/rust#37156 Fixes rust-lang/rust#146967 Fixes rust-lang/rust#149811 r? petrochenkov