Can we make `@defer` inferrable?
It seems that context passing and cleanup has a similar structure to reverse mode AD: we accumulate a bunch of actions wrapped inside closures which must be run at the exit of a scope.
Currently ResourceContexts generates kind of bad code for the cleanup because everything becomes boxed. It's not a disaster if you're cleaning up heavy weight resources, but perhaps there's some way to use opaque closures to give the compiler insight into the list of cleanup actions?
A difficulty here is that @defer may be called within a loop to defer cleanup of a collection of objects, so the cleanup list is currently of dynamic length. It's unclear whether this is a good pattern, but if it is, maybe we should try to encapsulate the list of closures as a concrete list of zero-argument thunks which can be called with minimal overhead.