Skip to content

Peculiar lifetime bound required error avoidable via &ref x #17418

Closed
@huonw

Description

@huonw
#[allow(dead_code)]
fn make<'a, A: Clone, I: Iterator<&'a A>>(mut it: I) -> Vec<A> {
    let mut v = vec![];
    for a in it {
        v.push(a.clone())
    }
    v
}

fn main() {}

fails to compile with

<anon>:4:9: 4:10 error: the parameter type `A` may not live long enough; consider adding an explicit lifetime bound `A:'a`...
<anon>:4     for a in it {
                 ^
<anon>:4:9: 4:10 note: ...so that the reference type `&'a A` does not outlive the data it points at
<anon>:4     for a in it {
                 ^
<anon>:5:16: 5:17 error: the parameter type `A` may not live long enough; consider adding an explicit lifetime bound `A:'a`...
<anon>:5         v.push(a.clone())
                        ^
<anon>:5:16: 5:17 note: ...so that the reference type `&'a A` does not outlive the data it points at
<anon>:5         v.push(a.clone())
                        ^

changing the declaration of A to include 'a as a bound makes it compile, as does changing the for loop to for &ref a in it { ... }. It seems somewhat peculiar that this noop re-referencing pattern silences the error (filing a bug because I'm concerned by the possibility that the pattern isn't being handled correctly, it may be all alright).

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions