r/ProgrammingLanguages • u/verdagon Vale • Jul 20 '26
Any languages using "shared mutable borrow refs", or things like group borrowing?
Hey all, I'm trying to collect a list of languages that are using (or experimenting with!) shared mutable borrow refs.
A shared-mutable borrow reference is a memory-safe reference that has no run-time overhead (no GC, refcounting, nor generations). It lets you do something like this (using Rust-ish syntax):
func main() {
let ship: Ship =
Ship{ fuel: 100 };
// Two refs pointing at the same ship
let ref_a = &shmut ship;
let ref_b = &shmut ship;
// You can use both references to modify the ship
ref_a.fuel += 20;
ref_b.fuel -= 8;
print(ref_a.fuel); // Prints 112
}
Of course, they have limitations; they can't reach into Vec elements, through pointers, or into unions safely. For example:
func main() {
let fleet: Vec<Ship> =
vec![
Ship{fuel: 100},
Ship{fuel: 100}
];
let ref_a = &shmut fleet[0];
fleet.clear();
ref_a.fuel += 20; // UH OH, crash
}
To address this, some languages add unique references in (like Rust's &mut), like:
func main() {
let fleet: Vec<Ship> =
vec![
Ship{fuel: 100},
Ship{fuel: 100}
];
let ref_a = &uniq fleet[0];
ref_a.fuel += 20;
print(fleet[0].fuel); // 120
}
With some special rules to prevent us from accessing anything else that might alias it while we're doing things with the unique reference. Ante does this with its temporary unique conversions.
Even that has limitations though (we can only borrow one element at a time), and so Ante and a few other languages are looking at group borrowing to support "invalidation" like below:
func main() {
let fleet: Vec<Ship> =
vec![
Ship{fuel: 100},
Ship{fuel: 100}
];
// Can have &shmut refs pointing at elements of Vec
let ref_a = &shmut fleet[0];
let ref_b = &shmut fleet[1];
// Can modify through them
ref_a.fuel += 20;
ref_b.fuel -= 8;
fleet.clear(); // Erase all ships
// ref_a.fuel += 50;
// Compile error: ref_a was invalidated by `clear` call.
// ref_b.fuel -= 3;
// Compile error: ref_b was invalidated by `clear` call.
}
Basically, group borrowing:
- Tracks that
ref_a's type is actually&Ship in fleet[]; it tracks where the pointer is pointing to. - Requires functions to express what they're modifying;
clear's signature says that it's modifyingself's contents. - Invalidates any references (like ref_a and ref_b) that are pointing inside an object that a function said it modified.
As far as I know, these are all the languages investigating some form of group borrowing:
- Carbon (talk, slides)
- Mojo (roadmap, some recent work)
- Ante
- Conjure
- Fang
- Plecra/Vi is working on something called "Exclusion typing"/the exclusion calculus"
- (My own stuff, but thats a whole 'nother story)
- Anyone else?
(Note I'm not really counting things like Pony and Verona since they're GC'd)
I want to write a followup article about group borrowing soon, and I'd love to hear about any of you that are exploring it or anything similar.
Cheers!
6
u/disruptek Jul 21 '26
It's not group borrowing, but basically the flip side of the coin, so maybe it's a good villain for your article. ;-)
Elle (v2) has aliased mutation with no GC, no scan, and no annotations. It's currently based on Tofte-Talpin region assignment, with extensions:
The article-worthy counterpoint is that Elle doesn't reject fleet.clear():
The crash and compilation error examples Just Work.
9
u/TheChief275 Jul 21 '26
How about smut instead? Or call signed longs schlongs to be consistent
3
u/leosmi_ajutar Jul 21 '26 edited Jul 21 '26
I tried something along these lines some time ago, though I dropped it in favor of singular borrowing. Now, I did take what I learned and improved semantic analysis to be per-place, not per-object.
So, while your first example is a compile time error in my programming language, the final one is not only semantically accurate, but you can take it a step further and give Ship multiple properties which in turn are provably disjoint from one another.
3
u/onlyrealcuzzo Jul 20 '26 edited Jul 21 '26
Is everything mutable by default in this language? Can you have a `&shmut` to an immutable list and then mutate what's inside it?
What is the limit supposed to be of this? If you mutate fleet at all, do all group borrows lose access permissions?
What happens if I do:
Is ref_b still valid?
11
u/fdwr Jul 21 '26
(total random aside) I love you spelling out
funcin your language, even if it's slightly longer to type, which gels more with {enum,impl,struct}, which aren't {en,im,st} (perhaps I'm weird, caring less about the historical inherited legacy of thefnkey from ancient calculators 🧮, and more about linguistic harmony).1
u/UberAtlas Jul 21 '26
What would be the benefit to allowing this? Is there a particular use case this enables?
I worry it leaves a lot of room for footguns.
3
u/LPTK Jul 23 '26
You may be interested in a paper we published last year on a somewhat related topic: A Lightweight Type-and-Effect System for Invalidation Safety: Tracking Permanent and Temporary Invalidation With Constraint-Based Subtype Inference.
It was mentioned in a Mojo design discussion (I just noticed I forgot to respond to it).
2
u/Natural_Builder_3170 Jul 20 '26
Im doing so in my language github.com/git-i/yoyo-lang Im currently figuring out aliasing of function parameters, but everything seems to work correctly locally, it uses the path stability concept from ante (structs have stable paths, unions/arrays/pointers don’t ) to allow mutable aliasing
It does a full points to analysis (i used the staged flow sensitive points to anlaysis algorithm)
1
u/vmcrash Jul 22 '26
Did you already look at Hylo?
1
u/rjmarten Jul 27 '26
Mismo uses a combination of second-class references (ala Hylo) and group borrowing to maintain memory safety in the presence of shared mutable references. In Mismo we can do this:
The
clearfunction signature looks like this:The
mut!parameter-passing mode gives the function permission to mutate the value in any way. It also signifies to the compiler that the "children" (potential pointees) of the "place" passed as an argument to that function must be invalidated — the compiler knows thatref_ais a "child" (potential pointee) offleet.ships.*.If the mode was
mutinstead ofmut!then the function could only do shape-stable mutation (basically just changing ints and floats; a trick I learned from Ante) and in that case would not invalidate references.1
u/asdfa2342543 Jul 20 '26
I guess what I’m doing is along these lines but I’m not an expert in this stuff