-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Fix argument indices in MIR for closures. #30630
Conversation
Previously, all references to closure arguments went to the argument before the one they should (e.g. to arg1 when it was supposed to be arg2). This was because the MIR builder did not account for the implicit arguments that come before the explicit arguments, and closures have one implicit argument - the struct containing the captures.
r? @jroesch (rust_highfive has picked a reviewer for you, use r? to override) |
I would instead map both implicit and explicit argument iterator chains into something like EDIT: i.e.: something along the lines of (untested, might not compile without changes) // to start, translate the argument patterns and collect the
// argument types.
let explicit_arg_decls =
explicit_arguments
.into_iter()
.map(|(ty, pat)| (ty, Some(pat)))
.chain(implicit_arguments.into_iter().map(|ty| (ty, None)))
.enumerate()
.map(|(index, (ty, pattern))| {
if let Some(pattern) = pattern {
let lvalue = Lvalue::Arg(index as u32);
let pattern = this.hir.irrefutable_pat(pattern);
unpack!(block = this.lvalue_into_pattern(block,
argument_extent,
pattern,
&lvalue));
}
ArgDecl { ty: ty }
}); |
@nagisa Done. |
LGTM. |
@bors r=nagisa |
📌 Commit f8b6134 has been approved by |
⌛ Testing commit f8b6134 with merge c99f8cd... |
💔 Test failed - auto-linux-64-nopt-t |
@bors retry |
📌 Commit f8b6134 has been approved by |
Previously, all references to closure arguments went to the argument before the one they should (e.g. to `arg1` when it was supposed to go to `arg2`). This was because the MIR builder did not account for the implicit arguments that come before the explicit arguments, and closures have one implicit argument - the struct containing the captures. This is my test code and a diff of the MIR generated for the closure: ```rust let a = 2i32; let _f = |b: i32| -> i32 { a + b }: ``` ```diff --- old 2015-12-29 23:16:32.027926372 -0600 +++ new 2015-12-29 23:16:42.975400757 -0600 @@ -1,22 +1,22 @@ fn(arg0: &[closure@closure-args.rs:8:14: 8:39 a:&i32], arg1: i32) -> i32 { let var0: i32; // b let tmp0: (); let tmp1: i32; let tmp2: i32; bb0: { - var0 = arg0; + var0 = arg1; tmp1 = (*(*arg0).0); tmp2 = var0; ReturnPointer = Add(tmp1, tmp2); goto -> bb1; } bb1: { return; } bb2: { diverge; } } ``` (If you're wondering where this text MIR output comes from, it's from another branch of mine waiting on rust-lang#30602 to get merged.)
Previously, all references to closure arguments went to the argument before the one they should (e.g. to
arg1
when it was supposed to go toarg2
). This was because the MIR builder did not account for the implicit arguments that come before the explicit arguments, and closures have one implicit argument - the struct containing the captures.This is my test code and a diff of the MIR generated for the closure:
(If you're wondering where this text MIR output comes from, it's from another branch of mine waiting on #30602 to get merged.)