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

Fix argument indices in MIR for closures. #30630

Merged
merged 2 commits into from
Dec 31, 2015
Merged

Fix argument indices in MIR for closures. #30630

merged 2 commits into from
Dec 31, 2015

Conversation

solson
Copy link
Member

@solson solson commented Dec 30, 2015

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:

let a = 2i32;
let _f = |b: i32| -> i32 { a + b }:
--- 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 #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 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.
@rust-highfive
Copy link
Collaborator

r? @jroesch

(rust_highfive has picked a reviewer for you, use r? to override)

@nagisa
Copy link
Member

nagisa commented Dec 30, 2015

I would instead map both implicit and explicit argument iterator chains into something like (Ty, Option<Pattern>) and chain them both before enumeration.

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 }
                    });

@solson
Copy link
Member Author

solson commented Dec 30, 2015

@nagisa Done.

@nagisa
Copy link
Member

nagisa commented Dec 30, 2015

LGTM.

@eddyb
Copy link
Member

eddyb commented Dec 30, 2015

@bors r=nagisa

@bors
Copy link
Contributor

bors commented Dec 30, 2015

📌 Commit f8b6134 has been approved by nagisa

@bors
Copy link
Contributor

bors commented Dec 31, 2015

⌛ Testing commit f8b6134 with merge c99f8cd...

@bors
Copy link
Contributor

bors commented Dec 31, 2015

💔 Test failed - auto-linux-64-nopt-t

@eddyb
Copy link
Member

eddyb commented Dec 31, 2015

@bors retry

@bors
Copy link
Contributor

bors commented Dec 31, 2015

📌 Commit f8b6134 has been approved by nagisa

nagisa added a commit to nagisa/rust that referenced this pull request Dec 31, 2015
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.)
bors added a commit that referenced this pull request Dec 31, 2015
@bors bors merged commit f8b6134 into rust-lang:master Dec 31, 2015
@solson solson deleted the mir-closure-args branch January 1, 2016 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants