Skip to content

fix(resolve): Warn on Self-Type Lifetime Elision for Non-Self Types#153692

Open
TKanX wants to merge 4 commits intorust-lang:mainfrom
TKanX:bugfix/140611-self-lifetime-elision-name-match
Open

fix(resolve): Warn on Self-Type Lifetime Elision for Non-Self Types#153692
TKanX wants to merge 4 commits intorust-lang:mainfrom
TKanX:bugfix/140611-self-lifetime-elision-name-match

Conversation

@TKanX
Copy link
Contributor

@TKanX TKanX commented Mar 11, 2026

View all comments

Summary

FCW lint self_lifetime_elision_not_applicable: fires when a self parameter contains & but does not syntactically contain Self.

Tracking: #140611

Context

Self-type lifetime elision in rustc_resolve relies on a name-matching hack (is_self_ty) that shallow-compares Res values between the self parameter type and the impl self type. As @compiler-errors identified in #140611, this hack cannot see through type aliases (different Res) and only fires for Struct|Union|Enum|PrimTy impls (excludes TyAlias). When it misses, find_lifetime_for_self returns Empty, self-elision silently fails, and a later parameter's lifetime may leak into the return type.

@lcnr proposed restricting self-elision to genuine Self and FCW'ing everything else. This PR implements that.

Behavior

pub struct W<'a>(&'a ());
pub type Alias<'a> = W<'a>;
Case Code Before After
f1 impl Alias { fn f(self: &W) -> &() } silent wrong '_ FCW
f2 impl Alias { fn f(self: &Alias) -> &() } silent wrong '_ FCW
f3 impl Alias { fn f(&self) -> &() } correct unchanged
f4 impl W { fn f(self: &W) -> &() } correct (hack fires) FCW
f5 impl W { fn f(self: &Alias) -> &() } silent wrong '_ FCW
f6 impl W { fn f(&self) -> &() } correct unchanged

A follow-up PR will remove the hack after a warning period.

r? @lcnr
cc @compiler-errors @traviscross

@rustbot rustbot added 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. labels Mar 11, 2026
@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from 7f7909c to 9922812 Compare March 11, 2026 05:35
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Mar 11, 2026
@rust-log-analyzer

This comment has been minimized.

@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from ea23a3d to 7e85048 Compare March 11, 2026 07:18
@rust-log-analyzer

This comment has been minimized.

@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from 7e85048 to 7a90db6 Compare March 11, 2026 08:14
@TKanX TKanX marked this pull request as ready for review March 11, 2026 09:50
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 11, 2026
@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Mar 11, 2026
@lcnr
Copy link
Contributor

lcnr commented Mar 12, 2026

before discussing this in a lang meeting, can you extend the PR description a bit to summarize the reasoning and discussion behind this?

@TKanX TKanX marked this pull request as draft March 12, 2026 19:02
@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 Mar 12, 2026
Co-authored-by: lcnr <rust@lcnr.de>
Co-authored-by: Michael Goulet <michael@errs.io>
@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch 2 times, most recently from f6ec003 to dfcd644 Compare March 12, 2026 20:23
@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from dfcd644 to 805f449 Compare March 12, 2026 20:38
@TKanX TKanX marked this pull request as ready for review March 12, 2026 20:39
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 12, 2026
@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 18, 2026
…e-match, r=<try>

fix(resolve): Warn on Self-Type Lifetime Elision for Non-`Self` Types
@rust-log-analyzer

This comment has been minimized.

@TKanX

This comment was marked as resolved.

@rust-bors rust-bors bot 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 Mar 18, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 18, 2026

💔 Test for b9f802f failed: CI

@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2026

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

@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from 52625a2 to 5d87eb2 Compare March 18, 2026 20:18
@rust-log-analyzer

This comment has been minimized.

@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from 5d87eb2 to 347424a Compare March 18, 2026 21:25
@rust-log-analyzer

This comment has been minimized.

@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from 347424a to ddda958 Compare March 18, 2026 22:50
@rust-log-analyzer

This comment has been minimized.

…deny for crater run (WILL BE REVERTED)

Co-authored-by: Travis Cross <tc@traviscross.com>
@TKanX TKanX force-pushed the bugfix/140611-self-lifetime-elision-name-match branch from ddda958 to e46d6c7 Compare March 18, 2026 23:35
@TKanX

This comment was marked as resolved.

@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 Mar 19, 2026
@TKanX

This comment was marked as resolved.

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 19, 2026

@TKanX: 🔑 Insufficient privileges: not in try users

@traviscross
Copy link
Contributor

@bors try

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…e-match, r=<try>

fix(resolve): Warn on Self-Type Lifetime Elision for Non-`Self` Types
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 19, 2026

☀️ Try build successful (CI)
Build commit: 3960581 (39605810efb1a0d52f955bfba9e4eec7a01d5deb, parent: fd0c901b00ee1e08a250039cdb90258603497e20)

@traviscross
Copy link
Contributor

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-153692 created and queued.
🤖 Automatically detected try build 3960581
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 19, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 20, 2026

☔ The latest upstream changes (presumably #154123) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang S-waiting-on-crater Status: Waiting on a crater run to be completed. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants