Skip to content

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

Merged
ehuss merged 2 commits intomasterfrom
TC/use-self-module-restriction
Apr 13, 2026
Merged

Clarify that {self} imports require a module parent#2221
ehuss merged 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
Copy link
Copy Markdown
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@traviscross traviscross removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label 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
@ehuss ehuss added this pull request to the merge queue Apr 13, 2026
Merged via the queue into master with commit e7c0021 Apr 13, 2026
10 of 12 checks passed
@traviscross traviscross removed the S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository label Apr 14, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants