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

Non-Lexical Lifetimes only work with some types #46935

Closed
CryZe opened this issue Dec 22, 2017 · 2 comments
Closed

Non-Lexical Lifetimes only work with some types #46935

CryZe opened this issue Dec 22, 2017 · 2 comments
Labels
A-NLL Area: Non-lexical lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CryZe
Copy link
Contributor

CryZe commented Dec 22, 2017

This one is really weird. It's the same code, but one is a Vec and one is a i32. The Vec one compiles, the i32 one doesn't:

fn vec() {
    let mut _x = vec!['c'];
    let _y = &_x;
    _x = Vec::new();
}

fn int()  {
    let mut _x = 5;
    let _y = &_x;
    _x = 7;
}

Error:

error[E0506]: cannot assign to `_x` because it is borrowed
  --> src\lib.rs:49:5
   |
48 |     let _y = &_x;
   |              --- borrow of `_x` occurs here
49 |     _x = 7;
   |     ^^^^^^ assignment to borrowed `_x` occurs here

@CryZe
Copy link
Contributor Author

CryZe commented Dec 22, 2017

It looks like if the type of _x is either &T, &mut T, or T: Drop (or rather T that contains a Drop type somewhere) then NLL behaves just fine, but if you have a type that is not a reference and doesn't cause any calls to Drop, then it breaks.

@pnkfelix pnkfelix added A-NLL Area: Non-lexical lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-compiler-nll labels Dec 22, 2017
@matthewjasper
Copy link
Contributor

I think this is #46875. Delaying the assignment to _x makes this compile:

fn int1()  {
    let mut _x = 5;
    let _y = &_x;
    ();
    _x = 7;
}

fn int2()  {
    let mut _x = 5;
    let _y = &_x;
    _x = 1 + 2 + 4;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants