The "while var/const" construct - design (1b) in #13639, implemented in #17304 - is currently unusable for traversing data structures like linked list by advancing the borrowed pointer. The following generates a compilation error:
var curr: borrowed Node?;
while const currNN = curr {
writeln(currNN);
curr = currNN.next; // error: curr would outlive the value it is set to
}
a small complete reproducer
class Node {
var next: owned Node?;
}
var curr: borrowed Node?;
curr = (new Node()).borrow(); // error comes up regardless of this line
while const currNN = curr {
writeln(currNN);
curr = currNN.next; // error: curr would outlive the value it is set to
}
Whereas the following variant that does not use const works:
while curr {
writeln(curr!);
curr = curr!.next;
}
While in general this lifetime error may be appropriate, this issue requests to make it work in codes like the ones above.