Skip to content

Non-lexical lifetimes do not work on a field of structure #65156

Open
@alkbt

Description

@alkbt

I noticed that the non-lexical lifetimes do not work if the reference is used in the structure field.

rustc 1.38.0 (625451e37 2019-09-23)
#[derive(Debug)]
struct Foo<'a, T> {
    x: &'a T,
}

fn main() {
    let str_1 = "str1".to_string();
    let str_2 = "str2".to_string();

    let mut foo = Foo { x: &str_1 };

    foo.x = &str_2;

    // move out
    let str_3 = str_1;

    println!("{:?} {}", foo, str_3);
}
 error[E0505]: cannot move out of `str_1` because it is borrowed
  --> src/main.rs:15:17
   |
10 |     let mut foo = Foo { x: &str_1 };
   |                            ------ borrow of `str_1` occurs here
...
15 |     let str_3 = str_1;
   |                 ^^^^^ move out of `str_1` occurs here
16 | 
17 |     println!("{:?} {}", foo, str_3);
   |                         --- borrow later used here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0505`.
error: Could not compile `testapp2`.

To learn more, run the command again with --verbose.

Process finished with exit code 101

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-enhancementCategory: An issue proposing an enhancement or a PR with one.NLL-completeWorking towards the "valid code works" goalT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions