You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"this closure does not fulfill the lifetime requirements" could be more helpful #102877
fnfoo(_:&mutimplFnMut(&str)){}fnmain(){letmut fun = |_arg| {};foo(&mut fun);}
The current output is:
Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:5:5
|
5 | foo(&mut fun);
| ^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected trait `for<'r> FnMut<(&'r str,)>`
found trait `FnMut<(&str,)>`
note: this closure does not fulfill the lifetime requirements
--> src/main.rs:4:19
|
4 | let mut fun = |_arg| {};
| ^^^^^^
note: the lifetime requirement is introduced here
--> src/main.rs:1:21
|
1 | fn foo(_: &mut impl FnMut(&str)) {}
| ^^^^^^^^^^^
error: implementation of `FnOnce` is not general enough
--> src/main.rs:5:5
|
5 | foo(&mut fun);
| ^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
= note: closure with signature `fn(&'2 str)` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to 2 previous errors
Ideally it should suggest naming the closure argument type:
let mut fun = |_arg: &str| {};
++++++
Or it could say something else? I'm not sure what would make more sense, but this error isn't great.
[Outdated] Since #105888, rustc provides you with a working suggestion:
Outdated output (2024-03-01)
error[E0308]: mismatched types
--> src/main.rs:5:5
|
5 | foo(&mut fun);
| ^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected trait `for<'a> FnMut(&'a str)`
found trait `FnMut(&str)`
note: this closure does not fulfill the lifetime requirements
--> src/main.rs:4:19
|
4 | let mut fun = |_arg| {};
| ^^^^^^
note: the lifetime requirement is introduced here
--> src/main.rs:1:21
|
1 | fn foo(_: &mut impl FnMut(&str)) {}
| ^^^^^^^^^^^
help: consider specifying the type of the closure parameters
|
4 | let mut fun = |_arg: &_| {};
| ~~~~~~~~~~
error: implementation of `FnOnce` is not general enough
--> src/main.rs:5:5
|
5 | foo(&mut fun);
| ^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
= note: closure with signature `fn(&'2 str)` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
Update: The suggestion was removed again in #119849.
Updated, not so helpful output (2024-09-06):
error: implementation of `FnMut` is not general enough
--> src/main.rs:5:5
|
5 | foo(&mut fun);
| ^^^^^^^^^^^^^ implementation of `FnMut` is not general enough
|
= note: closure with signature `fn(&'2 str)` must implement `FnMut<(&'1 str,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnMut<(&'2 str,)>`, for some specific lifetime `'2`
error: implementation of `FnOnce` is not general enough
--> src/main.rs:5:5
|
5 | foo(&mut fun);
| ^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
= note: closure with signature `fn(&'2 str)` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
error: could not compile `playground` (bin "playground") due to 2 previous errors
Activity
kadiwa4 commentedon Mar 1, 2024
[Outdated] Since #105888, rustc provides you with a working suggestion:
Outdated output (2024-03-01)
Test is in tests/ui/lifetimes/issue-79187.
Update: The suggestion was removed again in #119849.
Updated, not so helpful output (2024-09-06):