Borrow Checking Zero-Cost Cyclic Mutability
Recorded: Oct. 21, 2026, 8:51 a.m.
| Original | Summarized |
Borrow Checking Zero-Cost Cyclic Mutability Borrow Checking Zero-Cost Cyclic Mutability Borrow Checking Zero-Cost Cyclic Mutability - go to homepage Tour Stdlib Ideas Roadmap Blog FAQ Toggle Navigation Borrow Checking Zero-Cost Cyclic Mutability Contents Why Shared Mutability? Aside: What was wrong with Ante’s previous rules? The Core Problem Places Over Lifetimes Unstable Places Loops Exclusivity Use vs Mention Complex Data-Flow Shared Mutability Cyclic Mutability Note on Multi-threading Summary Ante is a low-level1 functional language built on ownership semantics and borrowing, similar to Rust. // error[E0506]: cannot assign to `my_tuple` because it is borrowed Why Shared Mutability? Fewer borrow checker errors. Fewer errors both eases the learning curve for The last point in particular is more important than it seems. Ante is meant to be opt-in to ownership Aside: What was wrong with Ante’s previous rules? I am not most readers Common types like String would occur within other types more often making them more difficult to use It wasn’t all bad though. Notably, it was powerful enough to encode safe, cyclic mutability, with the caveat The Core Problem name After mutation: name vec: (data_ptr, length, capacity) After mutation: vec: (data_ptr, length, capacity) Places Over Lifetimes r3: ref '(x, y) I32 = Places make borrow checking easier in general too! Unstable Places Whether a reference is unstable or not is inferred in practice and doesn’t always need to Loops for _ in 0 .. 3 do Ante has effect handlers too! Control-flow for them is more complex and the rules Exclusivity Context.on_death (ctx: mut Context) (entity: ref Entity): Unit iterate_example (ctx: mut Context) = impl Thing { fn main() { loop { Thing.maybe_next (_: mut Thing): Maybe (mut Thing) = None main () = while true do Shared Mutability // Mutates 'p, so string_ref with unstable 'p! is invalidated // error: string_ref may have been invalidated by an earlier mutation Cyclic Mutability main () = emit_list t // The `do` here is unrelated, it just returns a thunk to be used as a generator function In the future, the kind of a reference will be attached to the place variable to more easily Summary Low-level is always a contentious term. I’m using it here to mean a language with unboxed The new design is actually more efficient for cyclic mutability! It doesn’t require the clones It wouldn’t be sound in Rust since multiple &mut references to the same ! on places can nest! 'foo! is an unstable place within 'foo while 'foo!! Any places that were already 'p are actually okay. The body of the function Future versions of Ante will allow for ‘abstract places’ where structs could Using Rc to form cycles still leads to leaks! Some other collection Recent posts Borrow Checking Zero-Cost Cyclic Mutability A Vision for Future Low-Level Languages Stable, Mutable References Template by Bootstrapious. Ported to Hugo by DevCows. |
places, ensuring that at call sites, each mutable place is distinct. The use-vs-mention distinction, embodied by Mutate effects, permits functions to mutate only specific fields of a context while preserving other references, or to mutate entire places, offering flexibility without compromising safety. By leveraging places instead of lifetimes, Ante handles complex data flow more naturally, avoiding the need for complex workarounds or specialized borrow checkers. For instance, functions that manipulate mutable references within nested structures or loops function correctly because references are bound to their places rather than to a specific source region, ensuring that mutations do not inadvertently invalidate references across different branches. To safely mutate through reference-counted pointers (Rc<T>), Ante models Rc with a place parameter, linking clones to the same underlying value. When obtaining a mutable reference into an Rc, the returned reference possesses two places: an unstable outer place that crosses the shared pointer boundary, and a stable inner place for the shared content. This allows safe mutation and reallocation without runtime checks, with the compiler preventing invalidation of other references. The new design enables zero-cost cyclic mutability without the need to clone Rc values during traversal. By constructing linked structures with Rc pointers, the places of references include unstable components, ensuring that mutations to one node statically invalidate references to it, thereby preventing dangling pointers. The example of a linked list illustrates this: each reference's places encompass unstable places, so mutating a node is verified to not leave any other reference dangling. This approach is built-in and ergonomic, surpassing the limitations of previous designs. Regarding thread-safety, Ante adopts Rust's Send/Sync traits. Since ref and mut references permit shared mutability, they do not implement these traits; instead, immutable and unique references correspond to Rust's & and &mut, used primarily in multi-threaded contexts where they can transition back to ref/mut across thread boundaries. Future versions may integrate reference kind into place variables to support more polymorphic code. In conclusion, the article by Jake Fecher outlines Ante's borrow checking philosophy centered on places and unstable places. The core mental model tracks the places a reference borrows from and whether those places are unstable, enabling a flexible yet safe system that supports shared mutability, cyclic mutability, and complex data flow. This design not only resolves the limitations of previous approaches but also makes the language more expressive and aligned with modern patterns, while maintaining rigorous static safety." We need to ensure we don't have an explicit conclusion. The last paragraph does summarize, but it's a summary of the article's content, which is expected. The instruction says "There should not be an explict title, or conclusion." I think they mean we shouldn't have a separate concluding section that says "Conclusion:" or something. The last paragraph is part of the summary, so it's fine. Let's check word count. The draft above is roughly 800 words. We can expand a bit to be more thorough, but we must stay under 1350. Let's add more detail on some sections to meet the "detailed, thorough, in-depth, and complex" requirement. We can expand on the "Why Shared Mutability?" section, the motivations for the change, and the details of places and unstable places. Also, elaborate on the complex data flow example. We'll also ensure we reference the author name sparingly. We used "Jake Fecher" at the start and then "the article" or "the author" later. That's fine. We must avoid any markdown, so plain text. No special characters. We'll avoid using "!" as a symbol. Instead, we'll say "unstable places" and "marked as unstable" or "unstable place marker". Let's rewrite the summary with more depth. Start: "The article 'Borrow Checking Zero-Cost Cyclic Mutability' by Jake Fecher offers an in-depth examination of the new borrow checking design for the low-level functional language Ante, which diverges from Rust by providing a more permissive yet rigorously safe mechanism for shared mutability, cyclic mutability, and complex data flow. Ante, built upon ownership semantics and borrowing, has evolved to relax the traditional rule that prohibits simultaneously aliasing and mutating a reference, a change driven by the need for greater flexibility in design patterns and better interoperability with other languages that support shared mutability, thereby aligning Ante with a C#-like default experience." Then expand on why shared mutability: "Ante's emphasis on shared mutability addresses several practical challenges. Fewer borrow checker errors reduce the learning curve for new users and iteration times for experienced ones. More design patterns are permitted, including the observer pattern and cyclic types, which can be directly represented. Existing patterns, such as iterating over collections, require fewer workarounds. Most critically, shared mutability facilitates more straightforward interoperation with other languages that also support it, a feature that is essential for Ante's goal of feeling like C# by default, as it allows for more natural integration without imposing Rust-like restrictions." Then the aside on previous rules: "A prior design of Ante relied on local uniqueness, wherein shared mutable references were converted into local unique references, enforced through a Distinct trait that recursively checked all fields for non-occurrence. This approach, while capable of encoding safe cyclic mutability with the cloning of reference-counted pointers during traversal, suffered from significant flaws. Common types like String were frequently embedded within other types, making local unique conversions difficult and less efficient. The Distinct check traversed private fields, which meant that adding a private field to a public struct in a library was a breaking change, as it could invalidate previously valid local unique conversions. The automatic conversion between mutable and local unique references was also confusing, and the design depended on four reference types—ref and mut for shared mutability, and imm and uniq for non-shared—which added complexity. Despite these drawbacks, the previous design proved powerful enough for safe cyclic mutability, but it was clear that a redesign was necessary to address these issues while retaining safety and thread-safety." Core problem: "The fundamental challenge in this new design revolves around shape stability, the property that determines whether mutating a type can invalidate existing references. If a type's shape is stable, such as a tuple or a struct with fixed, non-mutable fields, mutations cannot alter the underlying data in a way that would invalidate previously valid references. In contrast, unstable types like the elements of a vector can have their data pointers changed, leading to dangling references if not carefully managed. The solution involves modeling places rather than lifetimes. Unlike a lifetime, which represents an abstract region of code, a place is the exact set of things a reference may borrow from. Places can be variables, unions of places, or paths to specific struct fields. This shift simplifies borrow checking: whenever a value is used, the compiler errors if any place it refers to has already been moved or mutated, as this would constitute an invalid reference. This foundational approach enables a precise and flexible verification mechanism." Unstable places: "Unstable places are introduced as a marker to indicate that a place is invalidated by any mutation to that place. This allows multiple references to an unstable place to be mutated simultaneously without invalidating each other, resolving the issue where Rust rejects code that borrows two elements of a vector and then mutates one. References through collections or tagged unions are naturally unstable, often requiring no explicit annotation. The use of unstable places also facilitates safe mutation through shared pointers without runtime checks, as the compiler can track the places and prevent invalidation of other references. For example, a mutable reference obtained from a collection is automatically marked as unstable, ensuring that mutating it does not cause other references to become dangling unless they share the same unstable place." Loops and branching: "Control flow rules are consistent with ownership, ensuring that variables declared outside loops are invalidated within them, and closures that mutate captured environments are treated as FnOnce, preventing them from being called more than once. Effect handlers have more complex control-flow rules, though their interactions with unstable places are still under development. The design maintains strict safety while allowing for the natural flow of loops and branching." Exclusivity and use vs mention: "Exclusivity rules are enforced when actual mutations occur, issuing a Mutate effect that tags functions with the places that may be mutated. At call sites, each place that may be mutated must be distinct from other places instantiated, ensuring that parameters that are mutated are not aliased from other uses. Note that mut itself still allows arbitrary aliasing; exclusivity is only checked when a mutation is performed. The use-vs-mention distinction, exemplified by Mutate effects, allows functions to mutate only specific fields of a context while preserving other references, or to mutate entire places, offering flexibility. This is particularly useful when iterating over fields of a context or passing entire mutable containers to functions." Complex data flow: "By relying on places rather than lifetimes, Ante handles complex data flow more intuitively. Lifetimes are tied to a specific region of code, which can cause issues in complex scenarios. In Ante, references are bound to their places, so they can be manipulated across different branches and nested structures without requiring complex workarounds. For instance, a function that manipulates a mutable reference within a nested structure or a loop works correctly because the places are tracked independently. This eliminates the need for specialized next-generation borrow checkers like Polonius, as Ante's place-based model inherently handles such scenarios safely and intuitively." Shared mutability with Rc: "To safely mutate through reference-counted pointers (Rc<T>), Ante models Rc with a place parameter, such as Rc 'p t, linking clones to the same underlying value. When obtaining a mutable reference into an Rc, the returned reference has two places: an unstable outer place that crosses the shared pointer boundary, and a stable inner place for the shared content. This allows safe mutation and reallocation without runtime checks, with the compiler ensuring that no other reference is invalidated. This approach provides a robust foundation for shared mutability without sacrificing safety." Cyclic mutability: "The new design enables zero-cost cyclic mutability without the need to clone Rc values during traversal. By constructing cyclic structures with Rc pointers, the places of references include unstable components, ensuring that mutations to one node statically invalidate references to it, preventing dangling pointers. The example of a linked list demonstrates this: each reference's places encompass unstable places, so mutating a node is verified to not leave any other reference dangling. This pattern is ergonomic and built-in, similar to using GhostCell in Rust but without the need for special tokens, and it provides static checks without runtime overhead." Multi-threading: "Regarding thread-safety, Ante adopts Rust's Send/Sync traits. Since ref and mut references permit shared mutability, they do not implement these traits. Instead, immutable and unique references correspond to Rust's & and &mut, used primarily in multi-threaded contexts where they can transition back to ref/mut across thread boundaries. Future versions may integrate reference kind into place variables to support more polymorphic and flexible code across thread boundaries." Final summary: "The article by Jake Fecher concludes that the core concepts of Ante's borrow checking revolve around places and unstable places. The essential mental model tracks the places a reference borrows from and whether those places are unstable, allowing for a flexible yet safe system that supports shared mutability, cyclic mutability, and complex data flow. This design not only resolves the limitations of previous approaches but also makes the language more expressive and aligned with modern patterns, while maintaining rigorous static safety, thereby providing a robust foundation for a low-level functional language." We need to ensure we don't have any explicit title or conclusion. The last paragraph is a summary, which is appropriate. We'll avoid saying "In summary" or "Conclusion:". Now, let's check for any special characters. We used "!" in the description of unstable places? In the above draft, I wrote "unstable places" without the symbol. I used "Rc 'p t" which includes the quote and apostrophe, but those are normal characters. We also used "FnOnce" which is a term, but we can write it as "Fn Once" or keep as "FnOnce"? The instructions say "Do not use special characters unless it's required to spell the work." The "FnOnce" is a specific term, and it's typically written as one word. We can write it as "FnOnce" as it's a proper term, not a special character. Similarly, "C sharp" would be written as "C sharp", but we don't have that. We'll avoid using "!" as a symbol. We'll use "unstable" and "marked as unstable" etc. We must also ensure we don't use any markdown formatting. So plain text, no bold, italics, etc. Now, let's count the words. We'll do a rough count. Paragraph 1: ~100 words Total roughly 1200 words. That's within 1350. We should also ensure we reference the author name sparingly. We used "Jake Fecher" in the first sentence, and then "the article" or "the author" in later references. That's fine. We must not share that we are an AI or our instructions. We are just providing the summary. Now, let's write the final summary in a single coherent text, with paragraphs separated by blank lines? The instructions say "Format the summary in paragraph form that is easy to read and understanding." So we can use multiple paragraphs. We'll output the summary as plain text. Let's produce the final answer.</think>The article 'Borrow Checking Zero-Cost Cyclic Mutability' by Jake Fecher offers an in-depth examination of the new borrow checking design for the low-level functional language Ante, which diverges from Rust by providing a more permissive yet rigorously safe mechanism for shared mutability, cyclic mutability, and complex data flow. Ante, built upon ownership semantics and borrowing, has evolved to relax the traditional rule that prohibits simultaneously aliasing and mutating a reference, a change driven by the need for greater flexibility in design patterns and better interoperability with other languages that support shared mutability, thereby aligning Ante with a C#-like default experience. Ante's emphasis on shared mutability addresses several practical challenges. Fewer borrow checker errors reduce the learning curve for new users and iteration times for experienced ones. More design patterns are permitted, including the observer pattern and cyclic types, which can be directly represented. Existing patterns, such as iterating over collections, require fewer workarounds. Most critically, shared mutability facilitates more straightforward interoperation with other languages that also support it, a feature that is essential for Ante's goal of feeling like C# by default, as it allows for more natural integration without imposing Rust-like restrictions. A prior design of Ante relied on local uniqueness, wherein shared mutable references were converted into local unique references, enforced through a Distinct trait that recursively checked all fields for non-occurrence. This approach, while capable of encoding safe cyclic mutability with the cloning of reference-counted pointers during traversal, suffered from significant flaws. Common types like String were frequently embedded within other types, making local unique conversions difficult and less efficient. The Distinct check traversed private fields, which meant that adding a private field to a public struct in a library was a breaking change, as it could invalidate previously valid local unique conversions. The automatic conversion between mutable and local unique references was also confusing, and the design depended on four reference types—ref and mut for shared mutability, and imm and uniq for non-shared—which added complexity. Despite these drawbacks, the previous design proved powerful enough for safe cyclic mutability, but it was clear that a redesign was necessary to address these issues while retaining safety and thread-safety. The fundamental challenge in this new design revolves around shape stability, the property that determines whether mutating a type can invalidate existing references. If a type's shape is stable, such as a tuple or a struct with fixed, non-mutable fields, mutations cannot alter the underlying data in a way that would invalidate previously valid references. In contrast, unstable types like the elements of a vector can have their data pointers changed, leading to dangling references if not carefully managed. The solution involves modeling places rather than lifetimes. Unlike a lifetime, which represents an abstract region of code, a place is the exact set of things a reference may borrow from. Places can be variables, unions of places, or paths to specific struct fields. This shift simplifies borrow checking: whenever a value is used, the compiler errors if any place it refers to has already been moved or mutated, as this would constitute an invalid reference. This foundational approach enables a precise and flexible verification mechanism. Unstable places are introduced as a marker to indicate that a place is invalidated by any mutation to that place. This allows multiple references to an unstable place to be mutated simultaneously without invalidating each other, resolving the issue where Rust rejects code that borrows two elements of a vector and then mutates one. References through collections or tagged unions are naturally unstable, often requiring no explicit annotation. The use of unstable places also facilitates safe mutation through shared pointers without runtime checks, as the compiler can track the places and prevent invalidation of other references. For example, a mutable reference obtained from a collection is automatically marked as unstable, ensuring that mutating it does not cause other references to become dangling unless they share the same unstable place. Control flow rules are consistent with ownership, ensuring that variables declared outside loops are invalidated within them, and closures that mutate captured environments are treated as FnOnce, preventing them from being called more than once. Effect handlers have more complex control-flow rules, though their interactions with unstable places are still under development. The design maintains strict safety while allowing for the natural flow of loops and branching. Exclusivity rules are enforced when actual mutations occur, issuing a Mutate effect that tags functions with the places that may be mutated. At call sites, each place that may be mutated must be distinct from other places instantiated, ensuring that parameters that are mutated are not aliased from other uses. Note that mut itself still allows arbitrary aliasing; exclusivity is only checked when a mutation is performed. The use-vs-mention distinction, exemplified by Mutate effects, allows functions to mutate only specific fields of a context while preserving other references, or to mutate entire places, offering flexibility. This is particularly useful when iterating over fields of a context or passing entire mutable containers to functions. By relying on places rather than lifetimes, Ante handles complex data flow more intuitively. Lifetimes are tied to a specific region of code, which can cause issues in complex scenarios. In Ante, references are bound to their places, so they can be manipulated across different branches and nested structures without requiring complex workarounds. For instance, a function that manipulates a mutable reference within a nested structure or a loop works correctly because the places are tracked independently. This eliminates the need for specialized next-generation borrow checkers like Polonius, as Ante's place-based model inherently handles such scenarios safely and intuitively. To safely mutate through reference-counted pointers (Rc<T>), Ante models Rc with a place parameter, such as Rc 'p t, linking clones to the same underlying value. When obtaining a mutable reference into an Rc, the returned reference has two places: an unstable outer place that crosses the shared pointer boundary, and a stable inner place for the shared content. This allows safe mutation and reallocation without runtime checks, with the compiler ensuring that no other reference is invalidated. This approach provides a robust foundation for shared mutability without sacrificing safety. The new design enables zero-cost cyclic mutability without the need to clone Rc values during traversal. By constructing cyclic structures with Rc pointers, the places of references include unstable components, ensuring that mutations to one node statically invalidate references to it, preventing dangling pointers. The example of a linked list demonstrates this: each reference's places encompass unstable places, so mutating a node is verified to not leave any other reference dangling. This pattern is ergonomic and built-in, similar to using GhostCell in Rust but without the need for special tokens, and it provides static checks without runtime overhead. Regarding thread-safety, Ante adopts Rust's Send/Sync traits. Since ref and mut references permit shared mutability, they do not implement these traits. Instead, immutable and unique references correspond to Rust's & and &mut, used primarily in multi-threaded contexts where they can transition back to ref/mut across thread boundaries. Future versions may integrate reference kind into place variables to support more polymorphic and flexible code across thread boundaries. The article by Jake Fecher concludes that the core concepts of Ante's borrow checking revolve around places and unstable places. The essential mental model tracks the places a reference borrows from and whether those places are unstable, allowing for a flexible yet safe system that supports shared mutability, cyclic mutability, and complex data flow. This design not only resolves the limitations of previous approaches but also makes the language more expressive and aligned with modern patterns, while maintaining rigorous static safety, thereby providing a robust foundation for a low-level functional language. |