Skip to content

Instantly share code, notes, and snippets.

View nmsmith's full-sized avatar

Nick Smith nmsmith

  • Biarri Optimisation
  • Brisbane, Australia
View GitHub Profile

Note to readers: This document is an early draft of a model for memory-safe references that I've been working on for the last ~2 years. While I think it is quite promising, the design is unfinished. The "regions" that I present in this document have not been given a formal semantics, and I have not explained where they originate from. My goal is merely to convince you that the design is promising, and that if seen to completion, it would advance the frontier of zero-cost memory safety.

Update March 2025: I am working on a newer version of this proposal, that aims to simplify the model substantially. When I have finished that proposal, I will put a link here.

Update August 2025: Verdagon has written an awesome blog post that explains this proposal at a high level. I recommend you check it out!

Abstract

In this document, I present a novel model for memory-safe references that aims to significantly improve upon the Rust-inspired model that Mojo c

The goal

We need a syntax for declaring "Mojo references", i.e. identifiers that refer to pre-existing variables.

I will refer to a Mojo reference's identifier as an alias. This is a widely-used term; it means "a secondary name for something". Henceforth, every time I talk about an "alias", I am referring to this concept. (I am not talking about Mojo's alias keyword for declaring compile-time variables. I suspect this keyword will eventually be renamed, because it's misleading.)

As discussed in this thread, we want the ability to declare aliases in the following locations:

  • Argument lists and parameter lists (already implemented)
  • Assignment targets
  • i.e. =, :=, and for-loop variables