"Cannot root managed value long enough" with recursion and returning borrowed pointers
I wonder if this shouldn't work?
struct Node<T> {
val: T,
next: Option<@Node<T>>
}
impl<T> Node<T> {
pure fn get(&self) -> &self/T {
match self.next {
Some(next) => next.get(),
None => &self.val
}
}
}
fn main() {}
The failure is:
19:20: 19:24 error: illegal borrow: cannot root managed value long enough
19 Some(next) => next.get(),
^~~~
17:32: 22:3 note: managed value would have to be rooted for the lifetime &self as defined on the block at 17:32...
17 pure fn get(&self) -> &self/T {
18 match self.next {
19 Some(next) => next.get(),
20 None => &self.val
21 }
22 }
17:32: 22:3 note: ...but can only be rooted for the block at 17:32
17 pure fn get(&self) -> &self/T {
18 match self.next {
19 Some(next) => next.get(),
20 None => &self.val
21 }
22 }
https://github.com/mozilla/rust/blob/incoming/src/librustc/middle/borrowck/preserve.rs#L333
attempt_root() expects a ty::re_scope but self.scope_region is a ty::re_free. self.scope_region's id is the same as self.root_ub (root_region's id).
CC'ing @nikomatsakis because he's the borrowed pointer specialist.
I think this never should have worked, since the Node struct is declared to contain a managed box. The error message isn't the clearest, but is saying the compiler doesn't know that the lifetime of the contents of a node's next field is the same as the lifetime of the contents of the node itself.
I'll check in a modified run-pass test case (where the next field contains a pointer with a lifetime annotation).
Pending #5620
Doesn't look critical for 0.6
Yeah, this is working-as-intended. The associated test (which landed) shows how this needs to be written if you want to use borrowed pointers here. Closing.