News for hackers. Stuff that matters.
Re:Flattening ASTs (and Other Compiler Data Structures) by verdagon on Sunday July 02, 2023 @06:35AM (#36560152)
Ironically, we could say that we're losing the true ownership relationships between the objects, and we're making medium-term memory leaks more likely; drop() can free a Box pointing to a child, but can't free an index. In other words, we lose the guarantee that an element is released for reuse.

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

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.