Skip to content

MIR-borrowck: diagnostics for closures are much worse compared to AST-borrowck #45362

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

Closed
pnkfelix opened this issue Oct 18, 2017 · 0 comments
Closed

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Oct 18, 2017

When compiling with MIR-borrowck via -Z borrowck-mir, the diagnostics printed when errors involves borrows/moves into the bodies of closures (where the origin path is from the outside of that closure) are pretty bad.

For example, for the test src/test/compile-fail/borrowck/borrowck-closures-two-mut.rs, the AST-borrowck error messages tend to highlight the particular uses within the closure body, while the MIR-borrowck error messages just treat the whole closure as a black box.

Specifically, look at this AST-borrowck error:

error[E0499]: cannot borrow `x` as mutable more than once at a time (Ast)
  --> ../src/test/compile-fail/borrowck/borrowck-closures-two-mut.rs:55:24
   |
54 |     let c1 = to_fn_mut(|| set(&mut *x.f));
   |                        --           - previous borrow occurs due to use of `x` in closure
   |                        |
   |                        first mutable borrow occurs here
55 |     let c2 = to_fn_mut(|| set(&mut *x.f));
   |                        ^^           - borrow occurs due to use of `x` in closure
   |                        |
   |                        second mutable borrow occurs here
56 |     //~^ ERROR cannot borrow `x` as mutable more than once
57 | }
   | - first borrow ends here

and compare that to the corresponding MIR-borrowck error:

error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
  --> ../src/test/compile-fail/borrowck/borrowck-closures-two-mut.rs:55:24
   |
54 |     let c1 = to_fn_mut(|| set(&mut *x.f));
   |                        ----------------- first mutable borrow occurs here
55 |     let c2 = to_fn_mut(|| set(&mut *x.f));
   |                        ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
56 |     //~^ ERROR cannot borrow `x` as mutable more than once
57 | }
   | - first borrow ends here

What MIR-borrowck needs, if we can somehow get it, is the equivalent to the "previous borrow occurs due to use of x in closure" and "borrow occurs due to use of x in closure" messages, with the corresponding (very) precise spans.

kennytm added a commit to kennytm/rust that referenced this issue Nov 13, 2017
…bank

MIR-borrowck: fix diagnostics for closures

Emit notes for captured variables in the same manner as AST borrowck.

```
error[E0499]: cannot borrow `x` as mutable more than once at a time (Ast)
  --> $DIR/borrowck-closures-two-mut.rs:24:24
   |
23 |     let c1 = to_fn_mut(|| x = 4);
   |                        -- - previous borrow occurs due to use of `x` in closure
   |                        |
   |                        first mutable borrow occurs here
24 |     let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
   |                        ^^ - borrow occurs due to use of `x` in closure
   |                        |
   |                        second mutable borrow occurs here
25 | }
   | - first borrow ends here

error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
  --> $DIR/borrowck-closures-two-mut.rs:24:24
   |
23 |     let c1 = to_fn_mut(|| x = 4);
   |                        -- - previous borrow occurs due to use of `x` in closure
   |                        |
   |                        first mutable borrow occurs here
24 |     let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
   |                        ^^ - borrow occurs due to use of `x` in closure
   |                        |
   |                        second mutable borrow occurs here
25 | }
   | - first borrow ends here
```

Fixes rust-lang#45362.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant