Skip to content

Instantly share code, notes, and snippets.

View nmsmith's full-sized avatar

Nick Smith nmsmith

  • Melbourne, Australia
View GitHub Profile

Update March 2025: I am working on a newer version of this proposal, that aims to simplify the model substantially. It's still based on "regions", but I am no longer tying a region to a specific path in the ownership tree. When I have finished that proposal, I will put a link here.

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 currently uses.

The major advancement is to eliminate the "aliasing XOR mutability" restriction amongst references, and replace it with a similar restriction applied to lifetime parameters. This model is significantly more expressive, allowing many of the memory-safe programs that Rust rejects to compile successfully, without resorting to escape hatches.

This document is meant to be a high-level overview, not a specification. Some details are omitted for the sake of brevity, others are omitted because the design is not fully finished. The design has been under development

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