Skip to content

MIR-borrowck: augmented assignment causes duplicate errors #45697

Description

@arielb1

Augmented assignments include both a read and a write, so naturally when there's a conflict they cause 2 borrowck errors:

For example, this test (borrowck-assign-to-andmut-in-borrowed-loc)

struct S<'a> {
    pointer: &'a mut isize
}

fn copy_borrowed_ptr<'a>(p: &'a mut S<'a>) -> S<'a> {
    S { pointer: &mut *p.pointer }
}

fn main() {
    let mut x = 1;

    {
        let mut y = S { pointer: &mut x };
        let z = copy_borrowed_ptr(&mut y);
        *y.pointer += 1; //~ ERROR cannot assign
        *z.pointer += 1;
    }
}

Triggers a duplicate error:

error[E0506]: cannot assign to `*y.pointer` because it is borrowed (Ast)
  --> ../src/test/compile-fail/borrowck/borrowck-assign-to-andmut-in-borrowed-loc.rs:28:9
   |
27 |         let z = copy_borrowed_ptr(&mut y);
   |                                        - borrow of `*y.pointer` occurs here
28 |         *y.pointer += 1; //~ ERROR cannot assign
   |         ^^^^^^^^^^^^^^^ assignment to borrowed `*y.pointer` occurs here

error[E0503]: cannot use `(*y.pointer)` because it was mutably borrowed (Mir)
  --> ../src/test/compile-fail/borrowck/borrowck-assign-to-andmut-in-borrowed-loc.rs:28:9
   |
27 |         let z = copy_borrowed_ptr(&mut y);
   |                                   ------ borrow of `y` occurs here
28 |         *y.pointer += 1; //~ ERROR cannot assign
   |         ^^^^^^^^^^^^^^^ use of borrowed `y`

error[E0506]: cannot assign to `(*y.pointer)` because it is borrowed (Mir)
  --> ../src/test/compile-fail/borrowck/borrowck-assign-to-andmut-in-borrowed-loc.rs:28:9
   |
27 |         let z = copy_borrowed_ptr(&mut y);
   |                                   ------ borrow of `(*y.pointer)` occurs here
28 |         *y.pointer += 1; //~ ERROR cannot assign
   |         ^^^^^^^^^^^^^^^ assignment to borrowed `(*y.pointer)` occurs here

error: aborting due to 3 previous errors

Not sure what's the best way to solve this, but this issue alone is fairly low priority.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lints

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions