Skip to content

Non-lexical lifetimes has different behavior for Vec<&i32> and Vec<&String> #46953

Closed
@shepmaster

Description

@shepmaster

With 1.24.0-nightly (2017-12-21 250b492):

This compiles:

#![feature(nll)]

fn main() {
    let mut vec = Vec::new();

    let a = 1;
    vec.push(&a);

    let b = 2;
    vec.push(&b);

    println!("{:?}", vec);
}

But this does not:

#![feature(nll)]

fn f() {
    let mut vec = Vec::new();

    let a = String::from("");
    vec.push(&a);

    let b = String::from("");
    vec.push(&b);

    println!("{:?}", vec);
}

fn main() {}
error[E0597]: `a` does not live long enough
  --> src/main.rs:7:14
   |
7  |     vec.push(&a);
   |              ^^ borrowed value does not live long enough
...
13 | }
   |  - borrowed value only lives until here
   |
   = note: borrowed value must be valid for lifetime '_#11r...

/cc @arielb1 @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions