Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis
Created June 21, 2018 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikomatsakis/0eb5ba344e939b678fcad5452c82366c to your computer and use it in GitHub Desktop.
Save nikomatsakis/0eb5ba344e939b678fcad5452c82366c to your computer and use it in GitHub Desktop.
fn main() {
let mut x: i32 = 17;
let y: i32 = 21;
let mut u: Vec<&/* '0 */ i32> = vec![];
let mut v: Vec<&/* '1 */ i32> = vec![];
/* { // NLL */
let p: &/* '3 */ i32 = &/* '2 */x; // Hence: '3: '5
let q: &/* '5 */ i32 = &/* '4 */y; // Hence: '4: '5
let mut r: &/* '6 */ mut Vec<&/* '7 */ i32>;
r = &/* '8 */ mut u; // Hence: {'8: '6, '0 = '7}
r.push(p); // Hence: '3: '7
// Here, the value of `r` is dead. The analysis as I specified
// it would therefore discard relations that directly involve
// '6 and '7 but keep transitive relations.
//
// In this case ,that means ('3: '0) is retained.
r = &/* '9 */ mut v; // Hence: {'9: '6, '1 = '7}
r.push(q); // Hence: '5: '7
// Again, the value of `r` is dead, but ('5: '0) is retained.
/* } // NLL */
// Here: we could conclude that the borrows of `p` and `q` are in scope,
// yes, and hence prohibit mutation of `x` (and `y`).
x += 1; // <-- Error! can't mutate `x` while borrowed by `u` through `p`
// If `'0 == '1 == '7`, could the analysis (wrongly) conclude
// that the borrowing by `v` is also problematic?
take(v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment