r/rust•
actuallyzza

Confusion around borrow checker analysis of loops

I've run into a case where borrow checker seems to think that returning from a function mid loop can result in a mutable borrow being held over into another iteration of a loop.

I've put together a simplified example. The curious part is in the lastfn, the rest is just fluff to justify the signature.

How I mentally check borrows in loops is to figure out what borrows survive over a loop iteration after the drops at the end of the scope occur and make sure that no incompatible borrows occur. In this case we can see that the candidate reference to be returned gets dropped before another iteration can occur and that there is no sub-borrow of self crossing iterations. The compiler on the other hand seems to be working backwards from the lifetime of the return type and using that to assert that any borrow created fromself that could be returned should be treated as though it definitely is living as long as a returned type and therefore self is permanently borrowed each iteration.

Is that whats happening? Does anyone have a workarounds to suggest? All I have are the ugly options of Arcs or Clones.

Edit: it looks like the consensus is that this is behaviour as intended, unfortunately. Is there any chance of creating UB if the loop returns a pointer which is then, after the loop, converted to a reference with a lifetime bound by self, like so?

Posted May 20, 2020, 10:33 AM UTC · edited

87
olemni7•

This sadly is a limitations of the current borrow checker. I can't explain this very well myself, but there is a great talk by Niko Matsakis, which explains this problem and how it may be solved in the somewhat near future: https://youtu.be/_agDeiWek8w?t=1501

8
actuallyzzaOP•

Thanks, I'll have a look. Clearly I need to update my understanding of how the current borrowing checks are performed.

Edit: Yep that was exactly the same problem.

2
pretzelhammer•

I think you may find this StackOverflow question & answer to be relevant to your problem.

7
Sabageti•

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3ecf91e5d56c1942105ff78c69d406f7

This works, but I think it's not what you want because it's a workaround.

2
actuallyzzaOP•

Hi Sabageti, that's smart! I didn't realise I'd left such a solution open in my example. Unfortunately my real case is a default method on a trait that cant see the internal state of implements, which is something I should have said. Creating a new trait method that returns a reference recreates my current problem too, so I am still stuck.

2

You can still solve this problem in a similar way. You just need separate functions to iterate the solver and to return a reference to the result:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=734079f9ab4b8ed9aa4d47c866b0bf6d

I've had this problem in similar situations and ended up with a pattern where you have an advance() method that doesn't return anything, and a current() that returns a reference to the current state. As long as the call to current() is done via static dispatch it will certainly be inlined anyway because it's so short, so it's really not a big deal to call it many times.

The only real downside I know of is when advance() is fallible. In that case it is now possible for logic that uses current() to silently use an old value, if it accidentally gets invoked anyway despite advance() returning an error.

3
Sabageti•

You can change the try_solve signature? Is yes you can try to pass MaybeSolution by ref mut intead of returning a ref.

1