Skip to content

NLL regression regarding AddAssign #48175

Closed
@Manishearth

Description

@Manishearth
#![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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions