aptos-labs / aptos-core

Aptos is a layer 1 blockchain built to support the widespread use of blockchain through better technology and user experience.
https://aptosfoundation.org
Other
5.85k stars 3.54k forks source link

[Feature Request][move-compiler-v2] Lambda Lifting needs to handle modified free variables #13221

Open brmataptos opened 1 week ago

brmataptos commented 1 week ago

🚀 Feature Request

We can't enable lambda lifting because some of our tests have free variables in lambda functions.

To handle mutated free variables in lambdas, we do an additional prepass that transforms turns modified free variables into use of a &mut free variable. A minimal transformation is as follows:

|x| { c = x; e }
===>
{ let ac = &mut c; |x| { let c = move *ac; let res = { c = x; e }; *ac = move c; res } }

after which, the lambda has no mutated free variables and can be replaced by Closure(lifted, ac).

Now, what borrow analysis can do with that remains to be seen. We may want to do something special if the lambda already uses &mut c for free variable c.