-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for argument capture of closures
When we have a closure expression that captures a parent function's variable we must setup the closure data to contain this. Ignoring moveability and mutability requires for now, this patch creates the closure structure with fields for each of the captured variables. When it comes to compilation of the closure expression in order to support nested closures we must setup a context of implicit mappings so that for all path resolution we hit this implicit closure mappings lookups code before any lookup_var_decl as this decl will not exist so the order here is important during path resolution which is a similar problem to match expression destructuring. Fixes #195
- Loading branch information
Showing
6 changed files
with
172 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// { dg-output "3\n" } | ||
extern "C" { | ||
fn printf(s: *const i8, ...); | ||
} | ||
|
||
#[lang = "fn_once"] | ||
pub trait FnOnce<Args> { | ||
#[lang = "fn_once_output"] | ||
type Output; | ||
|
||
extern "rust-call" fn call_once(self, args: Args) -> Self::Output; | ||
} | ||
|
||
fn f<F: FnOnce(i32) -> i32>(g: F) { | ||
let call = g(1); | ||
unsafe { | ||
let a = "%i\n\0"; | ||
let b = a as *const str; | ||
let c = b as *const i8; | ||
|
||
printf(c, call); | ||
} | ||
} | ||
|
||
pub fn main() -> i32 { | ||
let capture = 2; | ||
let a = |i: i32| { | ||
let b = i + capture; | ||
b | ||
}; | ||
f(a); | ||
0 | ||
} |