-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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: wrong error for move into closure #46599
Comments
Hi, I was little busy work stuff. I am taking a stab at this issue again today. I am using simpler case that generates less MIR code struct FancyNum {
num: u8,
}
fn main() {
let mut fancy_num = FancyNum { num: 5 };
// let fancy_ref = &fancy_num;
let mut x = move || {
fancy_num.num += 1;
};
x();
} Generated MIR here When I uncomment the line StorageLive(_2); // bb0[2]: scope 3 at src/main.rs:9:9: 9:14
StorageLive(_3); // bb0[3]: scope 3 at src/main.rs:9:17: 11:6
_3 = move _1; // bb0[4]: scope 3 at src/main.rs:9:17: 11:6
+ The line that causes the error.
_2 = [closure@src/main.rs:9:17: 11:6] { fancy_num: move _3 }; // bb0[5]: scope 3 at src/main.rs:9:17: 11:6
// closure
// └ def_id: DefId(0/1:10 ~ playground[72d2]::main[0]::{{closure}}[0]) Note: _3 will be _4 in the debug log because of the extra variable fancy_ref (_2) So the error occurs way before we enter the closure. rust/src/librustc_mir/borrow_check/mod.rs Lines 349 to 354 in 1699293
rust/src/librustc_mir/borrow_check/mod.rs Lines 1016 to 1023 in 1699293
rust/src/librustc_mir/borrow_check/mod.rs Lines 850 to 851 in 1699293
This call generates the error. The information we have is the place (_1) we access and the span ( |
You need to find uses of |
I don't know how to implement this in code probably because I am not too familiar with API. But I will give it a try. |
@arielb1 There is no |
Sorry for the delay. |
I wasn't able to observe this error, at least not with @vramana's minimized example: https://play.rust-lang.org/?gist=6af331fc0c3dd1f455a2ecda1308991c&version=nightly (I also tried removing the comment) |
I guess this example gets an error: #![feature(nll)]
struct FancyNum {
num: u8,
}
fn main() {
let mut fancy_num = FancyNum { num: 5 };
let fancy_ref = &fancy_num;
let mut x = move || {
fancy_num.num += 1;
};
x();
drop(fancy_ref);
} prints:
|
I'm having trouble deciding how to categorize this -- I guess it's ultimately a "minor missing suggestion" and so belongs in the Release Candidate camp. Still, let me leave a few mentoring notes here. I'll leave a few notes on strategy, at least: First, in the MIR borrowck, we have a move of some value and we need to see whether that value is being moved into a closure. This would be right around here, in the code:
We need to examine the MIR statement that performs the move (the location of which is identified by In that case, you can find the index of the move amongst the arguments of the aggregate. That will be the upvar index. We can check the result of the Lines 2220 to 2221 in 860d169
So we should extract that value and add the label on that span instead. |
…elix [NLL] Use smaller spans for errors involving closure captures Closes rust-lang#51170 Closes rust-lang#46599 Error messages involving closures now point to the captured variable/closure args. r? @pnkfelix
cc @arielb1
I am working on this.
The text was updated successfully, but these errors were encountered: