-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[nll] borrows that must be valid for a free lifetime should explain why #52534
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
Comments
I'm repurposing this issue slightly. I think the general problem here is this note:
in particular, it would be great if we could explain why this is the case. We have some of the machinery that we need now, which we use as part of region errors. To start though it'd be good to brainstorm just what this would look like. |
Nightly emits
It could be (the text would need more effort):
Maybe the following?
|
I think in my ideal world we would recognize, in this case, that the
|
To start, we can take the closure out of the equation and think instead of this example: fn foo(x: &u32) -> &u32 {
let x = 22;
&x
} This currently gives (playground):
|
I think I would like potentially a similar error:
maybe something like that? |
This suggests our original could've been:
but somehow this seems kind of "meh". In both cases, there is maybe something even clearer, where we talk about "data local to the closure" or "data local to the function", but I can't quite see how to say it right now. |
Talking about where |
[nll] borrows that must be valid for a free lifetime should explain why Fixes #52534. r? @nikomatsakis
Consider this example:
This fails to compile -- and rightly so. The closure is supposed to return something with the same lifetime as its argument, but it returns
&x
. However, the error we get is quite confusing:this error arises because we infer (again, correctly) that if the
&x
expression returned a&'static u32
, it would be suitable -- and hence it infers that the borrow must last for'static
(but it cannot).The text was updated successfully, but these errors were encountered: