Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible defect in region checker #13665

Closed
chridou opened this issue Apr 21, 2014 · 3 comments
Closed

Possible defect in region checker #13665

chridou opened this issue Apr 21, 2014 · 3 comments
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@chridou
Copy link

chridou commented Apr 21, 2014

Tonight I was told on the IRC channel that all of the lines with comments should be valid rust code but two fail. I was told it might be an issue with the borrow checker.

fn find<'r, T: Eq>(data: &'r Vec<T>, key: &T) -> Option<&'r T> {
  data
    .iter()
    .find(|& x| x == key )
    //.map(|& ref v| v) // Does NOT compile -> Borrowed value does not live long enough
    //.map(|& ref v| -> &'r T {v}) // does NOT compile -> Borrowed value does not live long enough
    //.map(|& ref v: &'r T| -> &'r T {v}) // compiles
     .map(|& ref v: &'r T| {v}) // compiles
}

I'm sorry I can't tell you anymore since I'm still new to rust.

I used the following rustc version: 0.11-pre-nightly (d35804e 2014-04-18 00:01:22 -0700)

@eddyb
Copy link
Member

eddyb commented Apr 21, 2014

cc @nikomatsakis @pnkfelix
I've seen similar behavior around iterators and closures, is this related in any way to method lookup improvements?

@chridou TBQH, few people actually know the distinction, but the borrow checker is doing its job perfectly here - the issue is in the region checker, which does lifetime inference, and in this case the lifetimes it assigns are shorter than they could be.

This doesn't show up in regular functions as they require a complete signature, but closures can omit types (and lifetimes), making them vulnerable to inaccurate inference.

Even worse, you might not be able to name the input/output lifetimes (if they're temporaries in the outer function, for example) to work around such an issue, and can end up needing a function generic over the lifetime - there's syntax for generic lifetime types, but not expressions, AFAIK:

<'a> |&'a T| -> &'a U // returns a reference with the same lifetime as its input

@chridou chridou changed the title Possible defect in borrow checker Possible defect in region checker Apr 22, 2014
@chridou
Copy link
Author

chridou commented Apr 22, 2014

@eddyb
I changed the title "borrow" -> "region"

Thanks for the hint. I think that's also what you said in the IRC channel and I mixed things up.

Many thanks for the explanation!

@ftxqxd
Copy link
Contributor

ftxqxd commented Dec 20, 2014

All of these compile for me now on the playpen, so I suppose this can be closed.

@sfackler sfackler added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 20, 2014
@bors bors closed this as completed in 885d7de Dec 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

4 participants