Skip to content

NLL regression regarding AddAssign #48175

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Manishearth opened this issue Feb 13, 2018 · 4 comments
Closed

NLL regression regarding AddAssign #48175

Manishearth opened this issue Feb 13, 2018 · 4 comments
Labels
A-NLL Area: Non-lexical lifetimes (NLL)

Comments

@Manishearth
Copy link
Member

#![feature(nll)]
use std::ops::*;

#[derive(Copy, Clone)]
struct Au(u32);

impl Add<Au> for Au {
    type Output = Au;
    fn add(self, other: Au) -> Au {
        Au(self.0 + other.0)
    }
}

impl AddAssign<Au> for Au {
    fn add_assign(&mut self, other: Au) {
        *self = Au(self.0 + other.0)
    }
}

fn main() {
    let mut foo = vec![Au(4), Au(5), Au(6)];
    foo[2] += foo[2];
}

(playpen)

fails to compile with the following error:

error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
  --> src/main.rs:22:15
   |
22 |     foo[2] += foo[2];
   |     ---       ^^^ immutable borrow occurs here
   |     |
   |     mutable borrow occurs here

However, it works fine without NLL.

cc @nikomatsakis

@Manishearth Manishearth added the A-NLL Area: Non-lexical lifetimes (NLL) label Feb 13, 2018
@Manishearth
Copy link
Member Author

Interestingly, the same issue does not happen for primitives, it has to be through this wrapper Au type.

@mbrubeck
Copy link
Contributor

This error happens without feature(nll) if -Z borrowck=mir is passed.

@Aaron1011
Copy link
Member

I believe this is a duplicate of #48129.

@Manishearth
Copy link
Member Author

dupe

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL)
Projects
None yet
Development

No branches or pull requests

3 participants