Clarify that {self} imports require a module parent#2221
Merged
Conversation
The `self` keyword in a use brace (e.g., `use m::{self}`) creates a
binding for the parent entity. The note in `items.use.self.intro`
says that `self` "means the current module of the parent segment", but
there's no rule restricting what the parent can be.
The parent path must resolve to a module, enumeration, or trait --
i.e., entities that act as modules for name resolution. Using `self`
with other entities such as structs or unions is rejected by the
compiler (as of rust-lang/rust#152996).
Let's add a rule to make this restriction explicit.
In the prior commit, we added a link reference definition for "brace syntax". Let's update the other use in the chapter to use that.
ehuss
approved these changes
Apr 7, 2026
rust-bors Bot
pushed a commit
to rust-lang/rust
that referenced
this pull request
Apr 10, 2026
…etrochenkov Replacing `self` overwriting with proper resolution Reference PR: - rust-lang/reference#2221 As a follow-up PR to #146972 ([step 1](#152996 (comment))), after this PR: ~~1. Trailing `self` can appear in paths (as the consensus in #146972 (comment) (in future) ~~2. [E0429](https://doc.rust-lang.org/stable/error_codes/E0429.html#error-code-e0429) will be no longer emitted, `use ...::self [as target];` will be equivalent to `use ...::{self [as target]};`~~ (in future) 3. Things like `struct S {}; use S::{self as Other};` will be rejected --- This PR used to add a new lint `redundant_self`, which would lint `use ...::self [as target];` and `use ...::{self [as target]};`, and fixes all warnings emitted by this lint. But this lint and clippy lint [unnecessary_self_imports](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports) have some overlap. And `use std::io::self;` is not equivalent to `use std::io` in fact for now, the new lint will also cause the following known issue: > Removing `::{self}` will cause any non-module items at the same path to also be imported. This might cause a naming conflict (rust-lang/rustfmt#3568). So I removed this lint, and I think what it does should be done by extending the clippy lint `unnecessary_self_imports`. r? petrochenkov
github-actions Bot
pushed a commit
to rust-lang/rust-analyzer
that referenced
this pull request
Apr 16, 2026
…etrochenkov Replacing `self` overwriting with proper resolution Reference PR: - rust-lang/reference#2221 As a follow-up PR to rust-lang/rust#146972 ([step 1](rust-lang/rust#152996 (comment))), after this PR: ~~1. Trailing `self` can appear in paths (as the consensus in rust-lang/rust#146972 (comment) (in future) ~~2. [E0429](https://doc.rust-lang.org/stable/error_codes/E0429.html#error-code-e0429) will be no longer emitted, `use ...::self [as target];` will be equivalent to `use ...::{self [as target]};`~~ (in future) 3. Things like `struct S {}; use S::{self as Other};` will be rejected --- This PR used to add a new lint `redundant_self`, which would lint `use ...::self [as target];` and `use ...::{self [as target]};`, and fixes all warnings emitted by this lint. But this lint and clippy lint [unnecessary_self_imports](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports) have some overlap. And `use std::io::self;` is not equivalent to `use std::io` in fact for now, the new lint will also cause the following known issue: > Removing `::{self}` will cause any non-module items at the same path to also be imported. This might cause a naming conflict (rust-lang/rustfmt#3568). So I removed this lint, and I think what it does should be done by extending the clippy lint `unnecessary_self_imports`. r? petrochenkov
jhpratt
added a commit
to jhpratt/rust
that referenced
this pull request
Apr 21, 2026
Update books ## rust-embedded/book 2 commits in 2463edeb8003c5743918b3739a9f6870b86396f5..0789b0f29e73ecb91213cac10ad0eec1b9333770 2026-04-11 08:46:22 UTC to 2026-04-10 14:01:38 UTC - remove IO chapter, is covered in next chapter (rust-embedded/book#411) - add embassy chapter (rust-embedded/book#412) ## rust-lang/reference 10 commits in d2715c07e9dd9839c0c7675ecfa18bec539a6ee9..8c88f9d0bdd75ffdc0691676d83212ae22a18cee 2026-04-20 15:20:24 UTC to 2026-04-07 16:25:09 UTC - typo in example code (rust-lang/reference#2232) - oxford comma (rust-lang/reference#2233) - improve readability (rust-lang/reference#2234) - typo (rust-lang/reference#2230) - Clarify items.fn.generics.param-bounds (rust-lang/reference#2229) - follow formatting style guideline (rust-lang/reference#2228) - Update `derive` export locations (rust-lang/reference#2223) - Clarify that `{self}` imports require a module parent (rust-lang/reference#2221) - stabilize s390x vector registers (rust-lang/reference#2215) - Fix misuse of *usage* throughout the Reference (rust-lang/reference#2222) ## rust-lang/rust-by-example 2 commits in b31e3b8da01eeba0460f86a52a55af82709fadf5..898f0ac1479223d332309e0fce88d44b39927d28 2026-04-19 11:40:28 UTC to 2026-04-19 11:38:45 UTC - remove debug printing for the u32 and bool in variable_bindings.md (rust-lang/rust-by-example#1999) - Update for.md (rust-lang/rust-by-example#2008)
rust-timer
added a commit
to rust-lang/rust
that referenced
this pull request
Apr 21, 2026
Rollup merge of #155568 - rustbot:docs-update, r=ehuss Update books ## rust-embedded/book 2 commits in 2463edeb8003c5743918b3739a9f6870b86396f5..0789b0f29e73ecb91213cac10ad0eec1b9333770 2026-04-11 08:46:22 UTC to 2026-04-10 14:01:38 UTC - remove IO chapter, is covered in next chapter (rust-embedded/book#411) - add embassy chapter (rust-embedded/book#412) ## rust-lang/reference 10 commits in d2715c07e9dd9839c0c7675ecfa18bec539a6ee9..8c88f9d0bdd75ffdc0691676d83212ae22a18cee 2026-04-20 15:20:24 UTC to 2026-04-07 16:25:09 UTC - typo in example code (rust-lang/reference#2232) - oxford comma (rust-lang/reference#2233) - improve readability (rust-lang/reference#2234) - typo (rust-lang/reference#2230) - Clarify items.fn.generics.param-bounds (rust-lang/reference#2229) - follow formatting style guideline (rust-lang/reference#2228) - Update `derive` export locations (rust-lang/reference#2223) - Clarify that `{self}` imports require a module parent (rust-lang/reference#2221) - stabilize s390x vector registers (rust-lang/reference#2215) - Fix misuse of *usage* throughout the Reference (rust-lang/reference#2222) ## rust-lang/rust-by-example 2 commits in b31e3b8da01eeba0460f86a52a55af82709fadf5..898f0ac1479223d332309e0fce88d44b39927d28 2026-04-19 11:40:28 UTC to 2026-04-19 11:38:45 UTC - remove debug printing for the u32 and bool in variable_bindings.md (rust-lang/rust-by-example#1999) - Update for.md (rust-lang/rust-by-example#2008)
github-actions Bot
pushed a commit
to rust-lang/rustc-dev-guide
that referenced
this pull request
Apr 21, 2026
Update books ## rust-embedded/book 2 commits in 2463edeb8003c5743918b3739a9f6870b86396f5..0789b0f29e73ecb91213cac10ad0eec1b9333770 2026-04-11 08:46:22 UTC to 2026-04-10 14:01:38 UTC - remove IO chapter, is covered in next chapter (rust-embedded/book#411) - add embassy chapter (rust-embedded/book#412) ## rust-lang/reference 10 commits in d2715c07e9dd9839c0c7675ecfa18bec539a6ee9..8c88f9d0bdd75ffdc0691676d83212ae22a18cee 2026-04-20 15:20:24 UTC to 2026-04-07 16:25:09 UTC - typo in example code (rust-lang/reference#2232) - oxford comma (rust-lang/reference#2233) - improve readability (rust-lang/reference#2234) - typo (rust-lang/reference#2230) - Clarify items.fn.generics.param-bounds (rust-lang/reference#2229) - follow formatting style guideline (rust-lang/reference#2228) - Update `derive` export locations (rust-lang/reference#2223) - Clarify that `{self}` imports require a module parent (rust-lang/reference#2221) - stabilize s390x vector registers (rust-lang/reference#2215) - Fix misuse of *usage* throughout the Reference (rust-lang/reference#2222) ## rust-lang/rust-by-example 2 commits in b31e3b8da01eeba0460f86a52a55af82709fadf5..898f0ac1479223d332309e0fce88d44b39927d28 2026-04-19 11:40:28 UTC to 2026-04-19 11:38:45 UTC - remove debug printing for the u32 and bool in variable_bindings.md (rust-lang/rust-by-example#1999) - Update for.md (rust-lang/rust-by-example#2008)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
selfkeyword in a use brace (e.g.,use m::{self}) creates a binding for the parent entity. The note initems.use.self.introsays thatself"means the current module of the parent segment", but there's no rule restricting what the parent can be.The parent path must resolve to a module, enumeration, or trait -- i.e., entities that act as modules for name resolution. Using
selfwith other entities such as structs or unions is rejected by the compiler (as of rust-lang/rust#152996).Let's add a rule to make this restriction explicit.
See:
selfoverwriting with proper resolution rust#152996cc @ehuss @mu001999 @petrochenkov