Skip to content

Detect when unmet lifetime obligation comes from use of Self literal - #162701

Open
estebank wants to merge 2 commits into
rust-lang:mainfrom
estebank:issue-101393
Open

estebank wants to merge 2 commits into
rust-lang:mainfrom
estebank:issue-101393

Conversation

@estebank

@estebank estebank commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

When using Self { ... }, it introduces lifetime requirements from the impl that might be more strict than using the actual type's literal. We now point that out. When the return type has a type with implicit lifetimes, point those out and suggest making them explicit.

error: lifetime may not live long enough
  --> $DIR/conflicting-lifetime-bounds-from-impl-in-self-literal.rs:21:25
   |
LL |     impl<'a> Path<'a> {
   |          -- lifetime `'a` defined here
...
LL |         pub fn sub(&mut self) -> Path {
   |                    -             ---- this returned type has an implicit lifetime `'1`
   |                    |
   |                    let's call the lifetime of this reference `'1`
...
LL |             Self {
   |             ---- requires lifetime `'a` from the `impl`
...
LL |                 string: self.string,
   |                         ^^^^^^^^^^^ this usage requires that `'1` must outlive `'a`
   |
help: consider using the type's name directly instead, to avoid conflicting lifetime bounds from the `impl`
   |
LL -             Self {
LL +             Path {
   |
help: consider making explicit the implicit `'1` lifetime in the return type
   |
LL |         pub fn sub(&mut self) -> Path<'_> {
   |                                      ++++

Fix #101393.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 12, 2026
@rustbot

rustbot commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 76 candidates
  • Random selection from 21 candidates

@petrochenkov

Copy link
Copy Markdown
Contributor

@rustbot reroll

@rust-log-analyzer

This comment was marked as resolved.

@rust-bors

This comment has been minimized.

When using `Self { ... }`, it introduces lifetime requirements from the `impl` that might be more strict than using the actual type's literal. We now point that out.

```
error: lifetime may not live long enough
  --> $DIR/conflicting-lifetime-bounds-from-impl-in-self-literal.rs:21:25
   |
LL |     impl<'a> Path<'a> {
   |          -- lifetime `'a` defined here
...
LL |         pub fn sub(&mut self) -> Path/*<'_>*/ {
   |                    - let's call the lifetime of this reference `'1`
...
LL |             Self {
   |             ---- requires lifetime `'a` from the `impl`
...
LL |                 string: self.string,
   |                         ^^^^^^^^^^^ this usage requires that `'1` must outlive `'a`
   |
help: consider using the type's name directly instead, to avoid conflicting lifetime bounds from the `impl`
   |
LL -             Self {
LL +             Path {
   |
```
…licit

```
error: lifetime may not live long enough
  --> $DIR/conflicting-lifetime-bounds-from-impl-in-self-literal.rs:21:25
   |
LL |     impl<'a> Path<'a> {
   |          -- lifetime `'a` defined here
...
LL |         pub fn sub(&mut self) -> Path {
   |                    -             ---- this returned type has an implicit lifetime `'1`
   |                    |
   |                    let's call the lifetime of this reference `'1`
...
LL |             Self {
   |             ---- requires lifetime `'a` from the `impl`
...
LL |                 string: self.string,
   |                         ^^^^^^^^^^^ this usage requires that `'1` must outlive `'a`
   |
help: consider using the type's name directly instead, to avoid conflicting lifetime bounds from the `impl`
   |
LL -             Self {
LL +             Path {
   |
help: consider making explicit the implicit `'1` lifetime in the return type
   |
LL |         pub fn sub(&mut self) -> Path<'_> {
   |                                      ++++
```
@rustbot

rustbot commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

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.

This branch has not been deployed

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest Struct::<'_> instead of Self when lifetime does not match

5 participants