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

MIR borrowck: closure borrow errors mention the closure instead of the upvar #46626

Closed
arielb1 opened this issue Dec 10, 2017 · 1 comment
Closed
Labels
A-borrow-checker Area: The borrow checker A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@arielb1
Copy link
Contributor

arielb1 commented Dec 10, 2017

e.g. the test for E0504:

struct FancyNum {
    num: u8,
}

fn main() {
    let fancy_num = FancyNum { num: 5 };
    let fancy_ref = &fancy_num;

    let x = move || {
        println!("child function: {}", fancy_num.num); //~ ERROR E0504
    };

    x();
    println!("main function: {}", fancy_ref.num);
}

Gives the following errors with -Z borrowck=compare:

error[E0504]: cannot move `fancy_num` into closure because it is borrowed (Ast)
  --> /home/ariel/Rust/rust-master/src/test/compile-fail/E0504.rs:20:40
   |
17 |     let fancy_ref = &fancy_num;
   |                      --------- borrow of `fancy_num` occurs here
...
20 |         println!("child function: {}", fancy_num.num); //~ ERROR E0504
   |                                        ^^^^^^^^^ move into closure occurs here

error[E0505]: cannot move out of `fancy_num` because it is borrowed (Mir)
  --> /home/ariel/Rust/rust-master/src/test/compile-fail/E0504.rs:19:13
   |
17 |       let fancy_ref = &fancy_num;
   |                       ---------- borrow of `fancy_num` occurs here
18 | 
19 |       let x = move || {
   |  _____________^
20 | |         println!("child function: {}", fancy_num.num); //~ ERROR E0504
21 | |     };
   | |_____^ move out of `fancy_num` occurs here

error: aborting due to 2 previous errors

If the closure is big, it can be pretty annoying to find the offending upvar reference, so the AST error message is better.

@arielb1 arielb1 added A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints WG-compiler-nll labels Dec 10, 2017
@arielb1 arielb1 changed the title MIR borrowck: Closure borrow errors mention the closure instead of the upvar MIR borrowck: closure borrow errors mention the closure instead of the upvar Dec 10, 2017
@arielb1 arielb1 added the A-closures Area: Closures (`|…| { … }`) label Dec 10, 2017
@arielb1
Copy link
Contributor Author

arielb1 commented Dec 10, 2017

Dupe of #46599.

@arielb1 arielb1 closed this as completed Dec 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

1 participant