The inconceivable types of Rust: How to make self-borrows safe (2024)
7 days ago
- #Async
- #Rust
- #Borrow Checker
- Rust's borrow checker traditionally prevents passing an object and a reference to it simultaneously, seen as an inherent limitation.
- The post argues that self-borrows can be made safe in a hypothetical future version of Rust by adopting a more ambitious goal: safe async functions.
- Rust's type system lacks support for naming the types of local variables, which is essential for desugaring async functions into safe Rust code.
- Unnameable types, like closures and async functions, pose a challenge as they exist in Rust's type system but cannot be explicitly named in code.
- Named lifetimes are proposed to allow explicit lifetime tokens, enabling more precise control over borrows and lifetimes within functions.
- Partial moves and borrowed types are identified as 'inconceivable types' that don't exist in Rust's formal type system but are crucial for async functions.
- The post introduces the concept of 'owned references' (&own T) to address issues with Drop and non-movable types, allowing for safe destruction and initialization.
- Safe transmute and enum updates are proposed to handle in-place conversions between different states in async functions without violating type invariants.
- The limitations of Rust's current design, including the conflation of ownership with representation and the lack of support for non-movable types, are highlighted.
- The conclusion suggests that while implementing these changes in Rust is unlikely due to backward compatibility, the ideas could inspire future languages with borrow checking.