Skip to content

Provide suggestion from old trait solver in next trait solver on closure lifetime mismatch - #165670

Open
estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-158393
Open

estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-158393

Conversation

@estebank

@estebank estebank commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Suggest type annotation for closure params on HRTB FnOnce mismatch in next trait solver. Follow up to #158701 (which provided the suggestion in the old trait solver).

help: consider adding an explicit type annotation to the closure's argument
   |
LL |     let outer_closure = |buf: &mut [u8]| {
   |                             +++++++++++

in

error[E0277]: expected `FnOnce(&mut [u8])` signature, found `{closure@$DIR/hrtb-closure-suggest-type-annotation.rs:22:25: 22:30}`
  --> $DIR/hrtb-closure-suggest-type-annotation.rs:35:16
   |
LL |     inner(buf, outer_closure)
   |     -----      ^^^^^^^^^^^^^ expected `FnOnce(&mut [u8])` signature, found `{closure@$DIR/hrtb-closure-suggest-type-annotation.rs:22:25: 22:30}`
   |     |
   |     required by a bound introduced by this call
   |
help: the trait `for<'a> FnOnce(&'a mut [u8])` is not implemented for closure `{closure@$DIR/hrtb-closure-suggest-type-annotation.rs:22:25: 22:30}`
  --> $DIR/hrtb-closure-suggest-type-annotation.rs:22:25
   |
LL |     let outer_closure = |buf| {
   |                         ^^^^^
   = note: expected type with signature `for<'a> fn(&'a mut _)`
           found closure with signature `fn(&mut _)`
note: required by a bound in `inner`
  --> $DIR/hrtb-closure-suggest-type-annotation.rs:10:8
   |
LL | fn inner<F>(buf: &mut [u8], func: F) -> usize
LL | where
LL |     F: FnOnce(&mut [u8]) -> Result<usize, isize>,
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `inner`
help: consider adding an explicit type annotation to the closure's argument
   |
LL |     let outer_closure = |buf: &mut [u8]| {
   |                             +++++++++++

Fix #158393.

…ure lifetime mismatch

Suggest type annotation for closure params on HRTB FnOnce mismatch in next trait solver.

```
help: consider adding an explicit type annotation to the closure's argument
   |
LL |     let outer_closure = |buf: &mut [u8]| {
   |                             +++++++++++
```

in

```
error[E0277]: expected `FnOnce(&mut [u8])` signature, found `{closure@$DIR/hrtb-closure-suggest-type-annotation.rs:22:25: 22:30}`
  --> $DIR/hrtb-closure-suggest-type-annotation.rs:35:16
   |
LL |     inner(buf, outer_closure)
   |     -----      ^^^^^^^^^^^^^ expected `FnOnce(&mut [u8])` signature, found `{closure@$DIR/hrtb-closure-suggest-type-annotation.rs:22:25: 22:30}`
   |     |
   |     required by a bound introduced by this call
   |
help: the trait `for<'a> FnOnce(&'a mut [u8])` is not implemented for closure `{closure@$DIR/hrtb-closure-suggest-type-annotation.rs:22:25: 22:30}`
  --> $DIR/hrtb-closure-suggest-type-annotation.rs:22:25
   |
LL |     let outer_closure = |buf| {
   |                         ^^^^^
   = note: expected type with signature `for<'a> fn(&'a mut _)`
           found closure with signature `fn(&mut _)`
note: required by a bound in `inner`
  --> $DIR/hrtb-closure-suggest-type-annotation.rs:10:8
   |
LL | fn inner<F>(buf: &mut [u8], func: F) -> usize
LL | where
LL |     F: FnOnce(&mut [u8]) -> Result<usize, isize>,
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `inner`
help: consider adding an explicit type annotation to the closure's argument
   |
LL |     let outer_closure = |buf: &mut [u8]| {
   |                             +++++++++++
```
@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 Nov 9, 2026
@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

r? @tmiasko

rustbot has assigned @tmiasko.
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 79 candidates
  • Random selection from 21 candidates

@estebank estebank Nov 9, 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.

Making this one run-rustfix causes additional lifetime errors that don't have suggestions when they likely should.

error[E0271]: type mismatch resolving `<{closure@main.rs:8:14} as FnOnce<(&i32,)>>::Output == &i32`
 --> src/main.rs:8:24
  |
8 |     take_foo(|a: &i32| a);
  |     -------- --------- ^ types differ
  |     |        |
  |     |        this closure
  |     required by a bound introduced by this call
  |
note: required for `{closure@src/main.rs:8:14: 8:23}` to implement `Foo`
 --> src/main.rs:3:9
  |
3 | impl<F> Foo for F where F: Fn(&i32) -> &i32 {}
  |         ^^^     ^                      ---- unsatisfied trait bound introduced here
note: required by a bound in `take_foo`
 --> src/main.rs:5:21
  |
5 | fn take_foo(_: impl Foo) {}
  |                     ^^^ required by this bound in `take_foo`

error[E0271]: type mismatch resolving `<{closure@main.rs:9:14} as FnOnce<(&i32,)>>::Output == &i32`
 --> src/main.rs:9:34
  |
9 |     take_foo(|a: &i32| -> &i32 { a });
  |     -------- -----------------   ^ types differ
  |     |        |
  |     |        this closure
  |     required by a bound introduced by this call
  |
note: required for `{closure@src/main.rs:9:14: 9:31}` to implement `Foo`
 --> src/main.rs:3:9
  |
3 | impl<F> Foo for F where F: Fn(&i32) -> &i32 {}
  |         ^^^     ^                      ---- unsatisfied trait bound introduced here
note: required by a bound in `take_foo`
 --> src/main.rs:5:21
  |
5 | fn take_foo(_: impl Foo) {}
  |                     ^^^ required by this bound in `take_foo`

and on old trait solver

error[E0308]: mismatched types
 --> src/main.rs:8:5
  |
8 |     take_foo(|a: &i32| a);
  |     ^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected reference `&_`
             found reference `&_`
note: the lifetime requirement is introduced here
 --> src/main.rs:5:21
  |
5 | fn take_foo(_: impl Foo) {}
  |                     ^^^

error: lifetime may not live long enough
 --> src/main.rs:8:24
  |
8 |     take_foo(|a: &i32| a);
  |                  -   - ^ returning this value requires that `'1` must outlive `'2`
  |                  |   |
  |                  |   return type of closure is &'2 i32
  |                  let's call the lifetime of this reference `'1`

error[E0308]: mismatched types
 --> src/main.rs:9:5
  |
9 |     take_foo(|a: &i32| -> &i32 { a });
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected reference `&_`
             found reference `&_`
note: the lifetime requirement is introduced here
 --> src/main.rs:5:21
  |
5 | fn take_foo(_: impl Foo) {}
  |                     ^^^

error: lifetime may not live long enough
 --> src/main.rs:9:34
  |
9 |     take_foo(|a: &i32| -> &i32 { a });
  |                  -        -      ^ returning this value requires that `'1` must outlive `'2`
  |                  |        |
  |                  |        let's call the lifetime of this reference `'2`
  |                  let's call the lifetime of this reference `'1`

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=891df37a2740c6b6fd600c17dcf70666

View changes since the review

@estebank estebank changed the title Provide suggestion from old trait solver in next trait solver on clos… Provide suggestion from old trait solver in next trait solver on closure lifetime mismatch Nov 9, 2026
@tmiasko

tmiasko commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

@rustbot reroll

@rustbot rustbot assigned chenyukang and unassigned tmiasko Nov 10, 2026

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.

Compiler suggests HRTB for closure when only type annotation needed

4 participants