Skip to content

Lifetime inference issue when destructuring in argument list #13167

Closed
@sfackler

Description

@sfackler

I would expect that the sadface and non-sadface versions of next would be equivalent, but the sadface one is rejected and the other is not:

use std::slice;

pub struct PhfMapEntries<'a, T> {
    priv iter: slice::Items<'a, (&'static str, T)>,
}

impl<'a, T> Iterator<(&'static str, &'a T)> for PhfMapEntries<'a, T> {
    #[cfg(sadface)]
    fn next(&mut self) -> Option<(&'static str, &'a T)> {
        self.iter.by_ref().map(|&(key, ref value)| (key, value)).next()
    }

    #[cfg(not(sadface))]
    fn next(&mut self) -> Option<(&'static str, &'a T)> {
        self.iter.by_ref().map(|e| {
            let &(key, ref value) = e;
            (key, value)
        }).next()
    }

    fn size_hint(&self) -> (uint, Option<uint>) {
        self.iter.size_hint()
    }
}

fn main() {}
~ ❯ rustc --cfg sadface test.rs
test.rs:10:40: 10:49 error: borrowed value does not live long enough
test.rs:10         self.iter.by_ref().map(|&(key, ref value)| (key, value)).next()
                                                  ^~~~~~~~~
test.rs:9:57: 11:6 note: reference must be valid for the lifetime &'a  as defined on the block at 9:56...
test.rs:9     fn next(&mut self) -> Option<(&'static str, &'a T)> {
test.rs:10         self.iter.by_ref().map(|&(key, ref value)| (key, value)).next()
test.rs:11     }
test.rs:10:9: 10:72 note: ...but borrowed value is only valid for the method call at 10:8
test.rs:10         self.iter.by_ref().map(|&(key, ref value)| (key, value)).next()
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions