Skip to content
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

MIR-borrowck: augmented assignment causes duplicate errors #45697

Closed
arielb1 opened this issue Nov 1, 2017 · 1 comment · Fixed by #47607
Closed

MIR-borrowck: augmented assignment causes duplicate errors #45697

arielb1 opened this issue Nov 1, 2017 · 1 comment · Fixed by #47607
Assignees
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@arielb1
Copy link
Contributor

arielb1 commented Nov 1, 2017

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.

@arielb1 arielb1 added A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints WG-compiler-nll labels Nov 1, 2017
@nikomatsakis nikomatsakis added this to the NLL Future Compat Warnings milestone Jan 4, 2018
@davidtwco
Copy link
Member

I'll tackle this one.

@nikomatsakis nikomatsakis modified the milestones: NLL run-pass without ICEs, NLL diagnostic parity Jan 19, 2018
@davidtwco davidtwco self-assigned this Jan 25, 2018
davidtwco added a commit to davidtwco/rust that referenced this issue Feb 5, 2018
bors added a commit that referenced this issue Feb 7, 2018
MIR-borrowck: augmented assignment causes duplicate errors

Fixes #45697. This PR resolves the error duplication. I attempted to replace the existing sets since there were quite a few but only managed to replace two of them.

r? @nikomatsakis
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants