Borrow scopes


#1

I think one of the priority documentation item is borrow scopes, i.e. how long something is considered to be borrowed. All examples in ownership chapter of TRPL are let bindings.

The following issue is relevant, and I think the issue should get higher priority than P-low.

https://github.com/rust-lang/rust/issues/12032

Here is my understanding of current rules:

  1. Things are borrowed for the innermost enclosing statement.
  2. let bindings are borrowed for the innermost enclosing block.
  3. Tail expression of block is special and is borrowed for the innermost enclosing block of block, not expression. This rule is recursive.
  4. Implication of 1 is that something borrowed in true branch of if expression is also borrowed in false branch, likewise for different arms of match expression. This will be fixed.
  5. Implication of 2 is that let x = y; x is not equivalent to y and you may need to introduce or eliminate let bindings to pass the borrow checker. Will this be fixed?
  6. Another implication of 2 is that destructuring let is not equivalent to destructuring match, because match bindings are borrowed for the innermost enclosing statement, not block. This is probably working as designed, as long as we have rule 2.
  7. Another implication of 1 and 2 is that since things are not borrowed for something less than a statement, x is borrowed until f returns in f(g(&x)), not until g returns. So you may need to introduce or eliminate temporaries to pass the borrow checker. Will this be fixed?

#2

I beleive the P-low is because it’s backwards compatible to improve it.

This keeps getting discussed, IIRC the intention is to make borrowck smarter here, but it’s not been done yet. I certainly support “fixing” this behavior.