[rust-dev] Question about lifetime analysis (a 0.5 transition question)

Niko Matsakis niko at alum.mit.edu
Thu Dec 27 15:16:46 PST 2012

I'm sorry for not replying more quickly, I was not checking e-mail over 
xmas break.  I do not know of any general problems with @ and 
borrowing.  In any event, this is a misdiagnosis.

The problem is this: the declaration of iter looks like this:

     impl<K: Copy Eq Ord, V: Copy> RBMap<K, V>: iter::BaseIter<(&K, &V)>

In the context of a type declaration, an & means `&self`, where `self` 
is the lifetime parameter associated with the type/impl/whatever (just 
as the impl has two type parameters K and V, it also has an implicit 
lifetime parameter `self`).  So, this declaration written out more 
explicitly would be:

impl<K: Copy Eq Ord, V: Copy> RBMap<K, V>: iter::BaseIter<(&self/K, 

However, your method declaration is:

     pure fn each(&self, f: fn(&(&K, &V)) -> bool) { ... }

In the context of a function declaration, & means "a fresh lifetime".  
So this winds up being short for a declaration life this:

     pure fn each(&self, f: fn(&a/(&a/K, &a/V)) -> bool) { ... }

However, the trait declares that `each` should have this type:

     pure fn each(&self, f: fn(&a/(&self/K, &self/V)) -> bool) { ... }

So I think that if you change your declaraiton of `each()` to:

     pure fn each(&self, f: fn(&(&self/K, &self/V)) -> bool) { ... }

It will work just fine.  I apologize for the cryptic error message.  
Working on it.

I think the system of defaults regarding types like `&T` needs to 
change.  I hope that a more explicit notation would make the problem 
more obvious.  But that's a subject for another e-mail.


Lucian Branescu wrote:
> I think the problem is the compiler can't guarantee the managed box 
> will survive, so it won't allow a borrowed pointer.
> I think there are problems in general with @ and borrowing.
> I've converted the red-black tree I wrote to use iter::BaseIter but am 
> now fighting with lifetime analysis with the switch to 0.5.
> https://github.com/stevej/rustled/blob/master/red_black_tree.rs#L91
> And the error I'm getting with 0.5 is:
> http://pastebin.com/YK8v7EdA
> I've read the docs on lifetimes several times now but it's not quite 
> enough to get me over this hurdle.
> Thanks!
> Steve
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org <mailto:Rust-dev at mozilla.org>
> https://mail.mozilla.org/listinfo/rust-dev
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20121227/c883afaf/attachment.html>

More information about the Rust-dev mailing list