Skip to content

Support trailing self in paths#152996

Open
mu001999 wants to merge 3 commits intorust-lang:mainfrom
mu001999-contrib:feat/extend-import-self
Open

Support trailing self in paths#152996
mu001999 wants to merge 3 commits intorust-lang:mainfrom
mu001999-contrib:feat/extend-import-self

Conversation

@mu001999
Copy link
Contributor

@mu001999 mu001999 commented Feb 23, 2026

View all comments

As a follow-up PR to #146972, after this PR:

  1. Trailing self can appear in paths (as the consensus in Support importing path-segment keyword with renaming #146972 (comment))
  2. E0429 will be no longer emitted, use ...::self [as target]; will be equivalent to use ...::{self [as target]};
  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 the last commit fixes all warnings emitted by this lint.

But this lint and clippy lint 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

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Feb 23, 2026
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 178d259 to abd6031 Compare February 23, 2026 02:09
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from abd6031 to 3102edf Compare February 23, 2026 02:52
@rustbot rustbot added the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label Feb 23, 2026
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 3102edf to c206a47 Compare February 23, 2026 03:55
@mu001999 mu001999 changed the title Support self appear at the end of any paths in imports Support self at the end of any paths in imports Feb 23, 2026
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from c206a47 to d7a8a0a Compare February 23, 2026 09:27
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Feb 23, 2026
@mu001999 mu001999 force-pushed the feat/extend-import-self branch from d7a8a0a to d845289 Compare February 23, 2026 09:28
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from d845289 to 77169f5 Compare February 23, 2026 10:16
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 77169f5 to ce8d16a Compare February 23, 2026 11:24
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch 2 times, most recently from b4d62d4 to 9ac9e00 Compare February 23, 2026 12:42
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 9ac9e00 to 39dc3a0 Compare February 23, 2026 13:55
@petrochenkov
Copy link
Contributor

It looks like #146972 (comment) suggests to support trailing self in all paths, not just import paths. We can start with import paths if that's more convenient though.

However, if we are doing it, then I think it's time to abandon the whole "self rewriting" hack, and just properly resolve self inside modules.

@mu001999
Copy link
Contributor Author

mu001999 commented Feb 23, 2026

It looks like #146972 (comment) suggests to support trailing self in all paths, not just import paths.

Semantics of trailing self in import paths, like use foo::self;, is explicit and same to use foo::{self};, and this will require foo is in type namespace. So I think trailing self in import paths is usefull to replace use foo::{self}; when we want to import only items in type namespace.

But what will trailing self in non-import paths be? Do we need things like Foo::self? 🤔

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 26, 2026
@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 1d43b7d to 0eee7bc Compare February 26, 2026 15:24
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Feb 26, 2026
…JonathanBrouwer

Remove redundant self usages

Extracted from rust-lang/rust#152996.

r? petrochenkov
@mu001999
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 27, 2026
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 27, 2026
…r=petrochenkov

Print path root when printing path

Extracted from rust-lang#152996.

r? petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 27, 2026
…r=petrochenkov

Print path root when printing path

Extracted from rust-lang#152996.

r? petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 27, 2026
…r=petrochenkov

Print path root when printing path

Extracted from rust-lang#152996.

r? petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 27, 2026
…r=petrochenkov

Print path root when printing path

Extracted from rust-lang#152996.

r? petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 27, 2026
…r=petrochenkov

Print path root when printing path

Extracted from rust-lang#152996.

r? petrochenkov
.map(|parent| parent.ident.name == kw::SelfLower)
.unwrap_or(false);

// Normalize `self::self` to `self`
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? The normalization of foo::self to foo was removed, and I think we shouldn't do any other kind of "overwriting" either, only resolution.

The check is "only first and last segments can be self", and it can be checked without normalization, and self::self passes it too.
(It can be checked in one place too, now the error is split into two.)

Copy link
Contributor Author

@mu001999 mu001999 Feb 28, 2026

Choose a reason for hiding this comment

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

But use foo::self::self; will be accepted

Copy link
Contributor

Choose a reason for hiding this comment

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

foo::self::self has self that is not first and not last, so it doesn't pass the check and shouldn't be accepted.

Copy link
Contributor Author

@mu001999 mu001999 Feb 28, 2026

Choose a reason for hiding this comment

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

In resolve_path_with_ribs, the path will be foo::self not foo::self::self, so the second self would be the last one when checking "only first and last segments can be self".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

use foo::self::self will have module_path = foo::self and source = self, although we have abandoned the "self rewriting" hack

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 27, 2026
rust-timer added a commit that referenced this pull request Feb 28, 2026
Rollup merge of #153138 - mu001999-contrib:print-path-root, r=petrochenkov

Print path root when printing path

Extracted from #152996.

r? petrochenkov
@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 0eee7bc to bcd6516 Compare February 28, 2026 12:42
@mu001999
Copy link
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 28, 2026
@rust-log-analyzer

This comment has been minimized.

@mu001999
Copy link
Contributor Author

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 28, 2026
@mu001999 mu001999 force-pushed the feat/extend-import-self branch from bcd6516 to 7372043 Compare February 28, 2026 13:56
@rustbot
Copy link
Collaborator

rustbot commented Feb 28, 2026

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

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants