Skip to content

MIR borrowck: weird error message for closures #45638

Closed
@arielb1

Description

@arielb1

When compiling the test E0501, I get the following result:

$ ./build-debug-assertions/build/x86_64-unknown-linux-gnu/stage1/bin/rustc ./src/test/compile-fail/E0501.rs -Z borrowck-mir
warning: unused variable: `x`
  --> ./src/test/compile-fail/E0501.rs:11:19
   |
11 | fn inside_closure(x: &mut i32) {
   |                   ^
   |
   = note: #[warn(unused_variables)] on by default
   = note: to avoid this warning, consider using `_x` instead

warning: unused variable: `x`
  --> ./src/test/compile-fail/E0501.rs:14:20
   |
14 | fn outside_closure(x: &mut i32) {
   |                    ^
   |
   = note: to avoid this warning, consider using `_x` instead

warning: unused variable: `bar`
  --> ./src/test/compile-fail/E0501.rs:18:9
   |
18 |     let bar = || {
   |         ^^^
   |
   = note: to avoid this warning, consider using `_bar` instead

error[E0501]: cannot borrow `*a` as mutable because previous closure requires unique access (Ast)
  --> ./src/test/compile-fail/E0501.rs:21:21
   |
18 |     let bar = || {
   |               -- closure construction occurs here
19 |         inside_closure(a)
   |                        - previous borrow occurs due to use of `a` in closure
20 |     };
21 |     outside_closure(a); //~ ERROR E0501
   |                     ^ borrow occurs here                                                     
22 | }                                                                                            
   | - borrow from closure ends here                                                              
                                                                                                  
error[E0501]: cannot borrow `(*a)`it as  because previous closure requires unique access (Mir)    
  --> ./src/test/compile-fail/E0501.rs:21:21                                                      
   |                                                                                              
18 |       let bar = || {                                                                         
   |  _______________-                                                                            
19 | |         inside_closure(a)                                                                  
20 | |     };                                                                                     
   | |_____- closure construction occurs here                                                     
21 |       outside_closure(a); //~ ERROR E0501                                                    
   |                       ^ borrow occurs hereit                                                 
22 |   }                                                                                          
   |   - borrow from closure ends here                                                            
                                                                                                  
error: aborting due to 2 previous errors                                                          

The message "cannot borrow (*a)it as because previous closure requires unique access" looks wrong - I think the arguments to cannot_reborrow_already_uniquely_borrowed got mixed up a bit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions