Closed
Description
https://doc.rust-lang.org/reference/types/closure.html
https://doc.rust-lang.org/rust-by-example/fn/closures.html
https://doc.rust-lang.org/nomicon/hrtb.html
I think there is stuff in GCC we can take inspiration from the C++ implementation of closures for example:
int test() {
int capture = 456;
auto a = [&](int a) -> int {
return a + capture;
};
return a(123);
}
will give us
int test ()
{
int D.2400;
int capture;
struct ._anon_0 a;
typedef struct ._anon_0 ._anon_0;
try
{
capture = 456;
a.__capture = &capture;
D.2400 = test()::<lambda(int)>::operator() (&a, 123);
return D.2400;
}
finally
{
capture = {CLOBBER};
a = {CLOBBER};
}
}
int test()::<lambda(int)>::operator() (const struct ._anon_0 * const __closure, int a)
{
int D.2403;
int & capture [value-expr: __closure->__capture];
_1 = __closure->__capture;
_2 = *_1;
D.2403 = a + _2;
return D.2403;
}
The task list breaks down into:
- Implement ClosureType for the type system
- name resolution to mark captured variables
- backend code generation to generate implicit struct for this closure type and take references, all with must me TREE_ADDRESSABLE
- generate new function body and take code address of it into the lamba implicit struct
- Support function lang-items https://github.com/rust-lang/rust/blob/7807a694c2f079fd3f395821bcc357eee8650071/library/core/src/ops/function.rs#L54-L71
- Investigate blocking unstable stuff in rustc Tracking issue for Fn traits (
unboxed_closures
&fn_traits
feature) rust-lang/rust#29625 - rust-abi will convert variable number of arguments into a tuple
I am currently blocked on this until we get lang-item support into the compiler from my operator overloading branch,
Metadata
Metadata
Assignees
Type
Projects
Status
Done