feat: improve borrow checker — reinitialization, assignment move, return move
- Add isOwn field to Symbol for tracking own T declarations - Reinitialization after move: assigning to a moved variable removes it from movedVars - Move tracking in let/var initialization: var b: own Box = a moves a - Move tracking in assignment: x = y moves y if y is own T - Move tracking in return: return x moves x if x is own T - Expand borrow_test.nim to 10 tests (all passing)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
[Package]
|
||||
@@ -0,0 +1,38 @@
|
||||
import Std::Io::{PrintLine};
|
||||
|
||||
@[Checked]
|
||||
func Take(x: own String) {
|
||||
PrintLine(x);
|
||||
}
|
||||
|
||||
@[Checked]
|
||||
func UseAfterMoveReinit() {
|
||||
var s: own String = "hello";
|
||||
Take(s);
|
||||
/* s is moved here */
|
||||
/* PrintLine(s); */ /* would error */
|
||||
s = "reinitialized";
|
||||
/* s is valid again after reinitialization */
|
||||
PrintLine(s);
|
||||
}
|
||||
|
||||
@[Checked]
|
||||
func MoveInAssign() {
|
||||
var a: own String = "A";
|
||||
var b: own String = a; /* a is moved to b */
|
||||
PrintLine(b);
|
||||
/* PrintLine(a); */ /* would error */
|
||||
}
|
||||
|
||||
@[Checked]
|
||||
func MoveInReturn() -> own String {
|
||||
var x: own String = "returned";
|
||||
return x; /* x is moved */
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
UseAfterMoveReinit();
|
||||
MoveInAssign();
|
||||
PrintLine(MoveInReturn());
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user