Skip to content

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

Closed
@pnkfelix

Description

@pnkfelix

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions