-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing lifetime error with closure #42758
Comments
This is a really weird error. It seems the compiler is failing to infer the lifetimes for the closure correctly. My naive intuition is something like:
which matches what is normally done for elision in ordinary functions. However that does not seem to be the case here. It appears to be inferring something like:
for some pre-chosen lifetime
Yeah, those error messages are very frustrating to read. It would be nice if the compiler could actually assign names to these lifetimes. |
As an experiment, if you use a function rather than a closure, even if you rely on lifetime inference for the inner lifetime of |
I don't think lifetime elision in closures works (or is even present?). Generally, closures work best when no type ascriptions are provided and the compiler works out everything itself (the type and associated lifetimes). But, you could define this closure like this: |
Thanks, interesting. In that example the compiler still infers some concrete lifetime, IIUC, in contrast with what happens in the function. But this time it manages to find a lifetime that actually works. However you look at it, this is all very confusing! I still don't know what the actual rules are beyond: experiment and hope for the best, and if you can't make it work then don't use a closure. So:
|
I agree, it's confusing and seemingly inconsistent with how inference/lifetimes work in other contexts. There have been various proposals to enhance closure syntax to allow specifying lifetimes (and also have inference), but I don't know what the latest/current thinking is. |
Intuitively I don't quite see why this shouldn't compile, but:
Especially bewildering is that
expected Foo<'_>, found Foo<'_>
. Those look the same to me!The text was updated successfully, but these errors were encountered: