lineiform icon indicating copy to clipboard operation
lineiform copied to clipboard

Compile-time closure freeze checking

Open chc4 opened this issue 2 years ago • 1 comments

There's a section in the README about the hypothetical freezing allocator we can use to make sure closure environments are safe to inline by forcing them to be immutable (at the cost of runtime segfaults...)

We can actually do slightly better, and put off implementing that for a bit: Rust has automatic marker traits like Send and Sync that are implemented for any type that only contains fields already marked with that trait. The Rust compiler has an internal marker trait Freeze that's used for marking interior immutability - which is the exact same thing we care about. Magically, marker traits are also implemented for Fn trait objects automatically too, so we can 1) verbatim copy Freeze out of the stdlib (or maybe use https://crates.io/crates/freeze-macros which seems to do exactly that??) 2) change Jit::speedup<F> to be Jit::speedup<F: Freeze> to only accept closures that are interior immutable. This gives us compile-time errors instead of runtime segfaults, which is a bonus.

chc4 avatar Sep 12 '21 21:09 chc4

I removed the freezing part from the readme, since we'd probably implement the marker trait solution along with whatever initial implementation, and there's no reason to lie about the freezing design if it isn't even implemented yet.

https://github.com/chc4/lineiform/commit/4a104c994561282318179ec50a3a6b00b3f6e8c4#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L56 is a hotlink to it, for posterity.

chc4 avatar Feb 17 '22 03:02 chc4