Skip to content

Comments

Support self at the end of any paths in imports#152996

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

Support self at the end of any paths in imports#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

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

  1. self can appear at the end of any paths in imports (consensus in Support importing path-segment keyword with renaming #146972 (comment))
  2. E0429 will be no longer emitted, and new lint redundant_self will be emitted for use ...::self [as target]; or use ...::{self [as target]};

For the second, the new lint and clippy lint unnecessary_self_imports have some overlap and also cause the following known issues:

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 perhaps this new lint should not be added but extend 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
Copy link
Contributor

Same as with imports, for foo::self to resolve foo must be a module in name resolution sense (i.e. module, enum or trait), otherwise it's a resolution error.

@petrochenkov
Copy link
Contributor

Ugh, apparently things like this work.

struct S {}

use S::{self as other};

This isn't supposed to happen.

@mu001999
Copy link
Contributor Author

Same as with imports, for foo::self to resolve foo must be a module in name resolution sense (i.e. module, enum or trait), otherwise it's a resolution error.

What I mean is that trailing self in imports can be used to restrict the resolve behavior, but what can we use trailing self in non-import paths to do? 🤔 Maybe pub(crate::x::foo::self), but do we really need such things?

@mu001999
Copy link
Contributor Author

Ugh, apparently things like this work.

struct S {}

use S::{self as other};

This isn't supposed to happen.

😂 Looks like we need module_only rather than type_ns_only when importing

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.

4 participants