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:
2026-06-05 21:56:47 +03:00
parent ce3b4c99f0
commit 636c49e9df
8 changed files with 145 additions and 27 deletions
+1
View File
@@ -0,0 +1 @@
[Package]
+38
View File
@@ -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;
}