-
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
Borrow checker is confused by return statements #6613
Comments
http://paste.debian.net/5129/ is the original longer example. (But I'm mostly commenting here just to add myself to the cc list.) |
Closing as a dup of #6393. The borrow checker is not in fact confused by return statements, but rather is defending you against a subtle and non-obvious possible error, albeit in its simple minded sort of way. To demonstrate what I mean, consider this version of your code which works fine:
Now consider this version, which generates an error:
The reason that an error is generated is because the scope of the pointer (Note: it is also true that the borrow checker does not know that the |
Hey @nikomatsakis, there is one thing I don't quite understand about your explanation, which is why the first code fragment you posted does compile. After all, it is performing a borrow with lifetime |
It is not assigning to a variable that makes a difference but rather the control flow. In the first fragment, there is a |
Unless I'm misunderstanding something, the following code should work (it's a minimized example based on code from cscottnet on irc):
Instead it results in this error:
I don't know about the underlying code, but it looks like the borrow checker is confused by the return statement and assumes that the argument to return (the
j
) is borrowed for the rest of the function, thus making the new immutable borrow fail.The following workaround compiles without problems:
The text was updated successfully, but these errors were encountered: