Skip to content

Support trailing self in paths#152996

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

Support trailing self in paths#152996
mu001999 wants to merge 4 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? 🤔

@rustbot
Copy link
Collaborator

rustbot commented Feb 25, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

ident = Ident::new(source.ident.name, self_span);
}
}
Ident::new(parent.ident.name, self_span)
Copy link
Contributor

Choose a reason for hiding this comment

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

So things like foo::self::self are still unsupported and will be reported by UnnamedImport.
Seems fine for a start.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, use foo::self::self as name is probably supported now, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

Copy link
Contributor Author

@mu001999 mu001999 Feb 25, 2026

Choose a reason for hiding this comment

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

Updated, use foo::self::self; is allowed.

let mut span = binding_span;
match import.kind {
ImportKind::Single { type_ns_only: true, .. } => {
suggestion = Some(format!("self as {suggested_name}"))
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this change responsible for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this, use foo::self; will have wrong suggestion use self as other_foo;. With this we will get use foo::self as other_foo; correctly

return Ok(module.self_decl.unwrap());
if ns == TypeNS {
if ident.name == kw::SelfLower {
return Ok(module.self_decl.unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, why doesn't this enable paths like foo::self in non-import positions?
Is there some separate check in resolve_path or similar reporting an error for this case?

Copy link
Contributor Author

@mu001999 mu001999 Feb 25, 2026

Choose a reason for hiding this comment

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

Yes, resolve_path_with_ribs will check any path-kw used in the middle

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems we can support foo::self in non-import paths if we remove that check for self at last position.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, foo::self is allowed in non-import positions now.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch 2 times, most recently from b1d5b46 to 31857aa Compare February 25, 2026 16:37
@mu001999 mu001999 changed the title Support trailing self in import paths Support trailing self in paths Feb 25, 2026
@petrochenkov
Copy link
Contributor

(I just hope force pushes won't break the perf run.)

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 25, 2026

☀️ Try build successful (CI)
Build commit: a35a6e8 (a35a6e89bc39ea2ad9cbed9dd69128944a2930fa, parent: 3f9853562c73af38a5e6af8b0da1b2734a327e19)

@rust-timer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 31857aa to 8552939 Compare February 25, 2026 16:57
@mu001999
Copy link
Contributor Author

mu001999 commented Feb 25, 2026

I will remove type_ns_only if the perf run doesn't show perf regression. (if it run successfully)

@rust-log-analyzer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a35a6e8): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 2
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary -7.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-7.8% [-7.9%, -7.7%] 2
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 480.701s -> 480.064s (-0.13%)
Artifact size: 397.77 MiB -> 395.76 MiB (-0.50%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 25, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 25, 2026
…-self, r=JonathanBrouwer

Remove redundant self usages

Extracted from rust-lang#152996.

r? petrochenkov
@mu001999
Copy link
Contributor Author

Cool, seems the perf run shows no perf regression, so maybe I could remove the type_ns_only? Or still keep it in current implementation, how do you think about it?

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the feat/extend-import-self branch from 47d969d to 1697857 Compare February 26, 2026 01:01
@mu001999 mu001999 force-pushed the feat/extend-import-self branch 3 times, most recently from 251e4e3 to cd94176 Compare February 26, 2026 02:04
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 26, 2026
…-self, r=JonathanBrouwer

Remove redundant self usages

Extracted from rust-lang#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 26, 2026
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-review Status: Awaiting review from the assignee but also interested parties. 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