Commit 8dfe43dd authored by Gustav Hansson's avatar Gustav Hansson

added a bit about borrow checking

parent d993f34e
......@@ -116,6 +116,37 @@ The Type Checker works as intended and rejects ill-typed programs. It does not u
## Your borrrow checker
### Borrow Checker Rules
The first major thing the borrow checker should keep track of is the moving of variables using a Move Path.
``` Rust
let a: (Vec<u32>, Vec<u32>) = (vec![22], vec![44]);
let b = a.0; // <- Acceptable borrow is done here, move path: a.b
let c = b; // Another acceptable borrow, move path: a.b.c
let d = b; // Ill formed borrow, variable already moved,
// cant borrow from anything
// but the end of a move path
#### Illegal moves
- Moving a element of an array. If you have an array A a move of element A[n] can't be made.
- Move of static variables can't be made.
- Move away from a borrowed refrenece can't be done.
Moving of variables as references can be done but it has some restrictions. When moving a nonmutable reference can be done multiple times:
``` Rust
let a: (Vec<u32>, Vec<u32>) = (vec![22], vec![44]);
let b = &a;
let c = &a; // This move is completely legal due to b and c being nonmutable.
Moving a mutable variable as a reference can also be done but only to one variable at a time:
``` Rust
let mut a: (Vec<u32>, Vec<u32>) = (vec![22], vec![44]);
let b = &mut a;
let c = &mut a; // Ill move, Can't move mutable references more then once.
This error is there to prevent race conditions that can happen when a value can be changed in different places at the same time.
### Comparision
Not implemented.
## Your LLVM backend
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment