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

Pattern reborrows get assigned the wrong lifetime in for loops' LHS. #17068

Closed
eddyb opened this issue Sep 7, 2014 · 0 comments
Closed

Pattern reborrows get assigned the wrong lifetime in for loops' LHS. #17068

eddyb opened this issue Sep 7, 2014 · 0 comments

Comments

@eddyb
Copy link
Member

eddyb commented Sep 7, 2014

These two work:

fn foo<'a>(v: &'a [uint], f: |&'a uint|) {
    for x in v.iter() { f(x); }
}
fn foo<'a>(v: &'a [uint], f: |&'a uint|) {
    for x in v.iter() {
        let  &ref x = x;
        f(x);
    }
}

This doesn't:

fn foo<'a>(v: &'a [uint], f: |&'a uint|) {
    for &ref x in v.iter() { f(x); }
}
error: borrowed value does not live long enough
fn foo<'a>(v: &'a Vec<uint>, f: |&'a uint|) {for &ref x in v.iter() {f(x);}}
                                                           ^~~~~~~~
note: reference must be valid for the lifetime 'a as defined on the block...
fn foo<'a>(v: &'a Vec<uint>, f: |&'a uint|) {for &ref x in v.iter() {f(x);}}
                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: ...but borrowed value is only valid for the block
fn foo<'a>(v: &'a Vec<uint>, f: |&'a uint|) {for &ref x in v.iter() {f(x);}}
                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The patterns may seem redundant, but while the original code had &(_, ref x), that is not necessary to demonstrate the issue.
The error suggests the lifetime used for x is the lifetime of the v.iter() rvalue, instead of 'a from slice::Items<'a, uint>.

cc @pcwalton

bkoropoff added a commit to bkoropoff/rust that referenced this issue Nov 8, 2014
bors added a commit that referenced this issue Nov 9, 2014
Use the mem-cat of the iterator element type rather than the iterator itself when processing the for loop pattern.

Closes #17068
Closes #18767
@bors bors closed this as completed in fbc2e92 Nov 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant