Skip to content

Clarify that {self} imports require a module parent#2221

Open
traviscross wants to merge 2 commits intomasterfrom
TC/use-self-module-restriction
Open

Clarify that {self} imports require a module parent#2221
traviscross wants to merge 2 commits intomasterfrom
TC/use-self-module-restriction

Conversation

@traviscross
Copy link
Copy Markdown
Contributor

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.

See:

cc @ehuss @mu001999 @petrochenkov

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.
@traviscross traviscross added the S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository label Apr 4, 2026
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer 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.

2 participants