Skip to content

Point at 'static obligations from an fn being called - #163115

Open
estebank wants to merge 6 commits into
rust-lang:mainfrom
estebank:static-obligations-in-call
Open

estebank wants to merge 6 commits into
rust-lang:mainfrom
estebank:static-obligations-in-call

Conversation

@estebank

@estebank estebank commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Add this note to lifetime errors:

error[E0521]: borrowed data escapes outside of function
  --> $DIR/static-impl-obligation.rs:163:9
   |
LL |     fn bar<'a>(x: &'a &'a u32) {
   |            --  - `x` is only valid in the function body
   |            |
   |            lifetime `'a` defined here
LL |         let y: &dyn Foo = x;
LL |         y.hello();
   |         ^^^^^^^^^
   |         |
   |         `x` escapes the function body here
   |         argument requires that `'a` must outlive `'static`
   |
note: `'static` lifetime requirement from `<(dyn o::Foo + 'static)>::hello` introduced here
  --> $DIR/static-impl-obligation.rs:158:20
   |
LL |     impl dyn Foo + 'static where Self: 'static {
   |                    ^^^^^^^             ^^^^^^^ lifetime requirement introduced here
   |                    |
   |                    lifetime requirement introduced here
LL |         fn hello(&'static self) where Self: 'static {}
   |                  ^^^^^^^^^^^^^              ^^^^^^^ lifetime requirement introduced here
   |                  |
   |                  lifetime requirement introduced here

Follow up to #121274, taking only a subset of that unmerged PR's functionality.

@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 21, 2026
@rustbot

rustbot commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

r? @davidtwco

rustbot has assigned @davidtwco.
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 77 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

Add this note to lifetime errors:

```text
error[E0521]: borrowed data escapes outside of function
  --> $DIR/static-impl-obligation.rs:163:9
   |
LL |     fn bar<'a>(x: &'a &'a u32) {
   |            --  - `x` is a reference that is only valid in the function body
   |            |
   |            lifetime `'a` defined here
LL |         let y: &dyn Foo = x;
LL |         y.hello();
   |         ^^^^^^^^^
   |         |
   |         `x` escapes the function body here
   |         argument requires that `'a` must outlive `'static`
   |
note: `'static` requirement for `<(dyn o::Foo + 'static)>::hello` introduced here
  --> $DIR/static-impl-obligation.rs:158:40
   |
LL |     impl dyn Foo + 'static where Self: 'static {
   |                                        ^^^^^^^ `'static` requirement introduced here
LL |         fn hello(&'static self) where Self: 'static {}
   |                                             ^^^^^^^ `'static` requirement introduced here
```

This currently only accounts for explicit bounds, other obligations like those arising from `Self` in a `impl dyn Trait {}` or from the receiver in `fn foo(&'static self)` are not accounted for.
@estebank
estebank force-pushed the static-obligations-in-call branch from 6e47c50 to e478abb Compare September 21, 2026 16:44
@rustbot

rustbot commented Sep 21, 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.

Comment thread compiler/rustc_borrowck/src/diagnostics/region_errors.rs Outdated
```
note: `'static` lifetime requirement from `<(dyn b::Foo + 'static)>::hello` introduced here
  --> $DIR/static-impl-obligation.rs:16:18
   |
LL |     impl dyn Foo {
LL |         fn hello(&'static self) {}
   |                  ^^^^^^^^^^^^^ lifetime requirement introduced here
```
```
note: `'static` lifetime requirement from `<(dyn a::Foo + 'static)>::hello` introduced here
  --> $DIR/static-impl-obligation.rs:4:5
   |
LL |     impl dyn Foo {
   |     ^^^^^^^^^^^^ lifetime requirement introduced here
LL |         fn hello(&self) {}
   |
```
… lifetime

```
note: `'static` lifetime requirement from `<(dyn a::Foo + 'static)>::hello` introduced here
  --> $DIR/static-impl-obligation.rs:4:10
   |
LL |     impl dyn Foo {
   |          ^^^^^^^ `dyn Trait` introduces an implicit `'static` lifetime requirement
LL |         fn hello(&self) {}
   |
```
```
note: `'static` lifetime requirement from `<(dyn e::Foo + 'static)>::hello` introduced here
  --> $DIR/static-impl-obligation.rs:48:20
   |
LL |     impl dyn Foo + 'static {
   |                    ^^^^^^^ lifetime requirement introduced here
LL |         fn hello(&self) {}
   |
```
@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Sep 22, 2026
Comment on lines +7 to +18
error[E0478]: lifetime bound not satisfied
--> $DIR/static-impl-obligation.rs:221:10
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^^^^^^
|
note: lifetime parameter instantiated with the anonymous lifetime as defined here
--> $DIR/static-impl-obligation.rs:221:40
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^
= note: but lifetime parameter must outlive the static lifetime

@estebank estebank Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Future work: This diagnostic should be pointing at 214 trait MyTrait where Self: 'static { as the reason why it must outlive 'static.

View changes since the review

Comment on lines +20 to +43
error[E0803]: cannot infer an appropriate lifetime for lifetime parameter `'_` due to conflicting requirements
--> $DIR/static-impl-obligation.rs:221:22
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime as defined here...
--> $DIR/static-impl-obligation.rs:221:40
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^
note: ...so that the types are compatible
--> $DIR/static-impl-obligation.rs:221:22
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^^^^^^^^^^^^^^^^^^^
= note: expected `<dyn t::ObjectTrait as t::MyTrait>`
found `<dyn t::ObjectTrait as t::MyTrait>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the declared lifetime parameter bounds are satisfied
--> $DIR/static-impl-obligation.rs:221:22
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^^^^^^^^^^^^^^^^^^^

@estebank estebank Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The second to last note should be pointing at 214.

View changes since the review

@davidtwco

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a91d256 has been approved by davidtwco

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 23, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Sep 23, 2026
…l, r=davidtwco

Point at `'static` obligations from an fn being called

Add this note to lifetime errors:

```text
error[E0521]: borrowed data escapes outside of function
  --> $DIR/static-impl-obligation.rs:163:9
   |
LL |     fn bar<'a>(x: &'a &'a u32) {
   |            --  - `x` is only valid in the function body
   |            |
   |            lifetime `'a` defined here
LL |         let y: &dyn Foo = x;
LL |         y.hello();
   |         ^^^^^^^^^
   |         |
   |         `x` escapes the function body here
   |         argument requires that `'a` must outlive `'static`
   |
note: `'static` lifetime requirement from `<(dyn o::Foo + 'static)>::hello` introduced here
  --> $DIR/static-impl-obligation.rs:158:20
   |
LL |     impl dyn Foo + 'static where Self: 'static {
   |                    ^^^^^^^             ^^^^^^^ lifetime requirement introduced here
   |                    |
   |                    lifetime requirement introduced here
LL |         fn hello(&'static self) where Self: 'static {}
   |                  ^^^^^^^^^^^^^              ^^^^^^^ lifetime requirement introduced here
   |                  |
   |                  lifetime requirement introduced here

```

Follow up to rust-lang#121274, taking only a subset of that unmerged PR's functionality.

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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-clippy Relevant to the Clippy team. 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.

5 participants