Re:Flattening ASTs (and Other Compiler Data Structures)
by PartiallyTyped on Sunday July 02, 2023 @08:56AM
(#36561052)
Re linear types, Rust actually has an affine typesystem, and the compiler complains when you move a variable and try to access it, so instead you need to provide a reference if you intend to do that multiple times.
A reference is a new object that references an existing memory value. You can not store a reference unless the borrow-checker can prove that the object that stores it has a shorter lifetime than the referenced object.
That is also why you can't just pass that reference around willy-nilly, because the reference is consumed due to affine types.
I may misunderstanding something though so feel free to correct me.
AFAICT, a language would need something like higher RAII [0] or linear types [1] for that. I'd love to see Rust adopt these features too one day, though it may be difficult to do backwards compatibly.
[0] https://verdagon.dev/blog/higher-raii-7drl
[1] https://austral-lang.org/linear-types