Ownership & Move Semantics
Flame achieves predictable, high-performance systems execution and complete memory safety through a compile-time ownership and borrow-checking system inspired by Rust. Flame has zero Garbage Collector (GC)—memory is reclaimed deterministically at compile-time via RAII when owners go out of scope, eliminating memory management pitfalls (such as use-after-free and double-free) with zero GC pauses, zero runtime sweeps, and zero runtime overhead.
The Three Rules of Ownership
Section titled “The Three Rules of Ownership”- Every value in Flame has an owner represented by a variable binding.
- There can only be one owner at any given point in time.
- When the owner goes out of scope, the value is dropped deterministically and its backing memory or resources are freed immediately (RAII).
Value Categories: Copy vs Move
Section titled “Value Categories: Copy vs Move”Values in Flame fall into two distinct semantic categories when bound, assigned, or passed into functions:
1. Copy Semantics (Stack Primitives)
Section titled “1. Copy Semantics (Stack Primitives)”Primitive data types whose sizes are statically fixed and inexpensive to duplicate live entirely on the stack. Copying them creates an independent, bitwise duplicate rather than transferring ownership:
Int(e.g.,42)Float(e.g.,3.14)Bool(true,false)Byte(e.g.,0xFF)Nil- Immutable references (
&T)
let x = 42let y = x // x is COPIED, not moved
// Both x and y remain completely valid and independent!println($"x = {x}, y = {y}")2. Move Semantics (Heap & Resource Types)
Section titled “2. Move Semantics (Heap & Resource Types)”Types that manage heap-allocated buffers or OS resources (such as String, user-defined struct, dynamically sized vectors [T], and file handles) implement Move semantics.
When a non-Copy variable is assigned to another variable or passed by value to a function, ownership transfers to the destination place. The original place becomes invalidated:
fn consume_data(s: String) { println($"Received: {s}")} // `s` is dropped here when the function exits
let original = "Hello Flame"
// Ownership transfers (moves) to consume_dataconsume_data(original)
// ❌ Compile Error: use of moved value: 'original'// println(original)Control-Flow-Aware Move Tracking
Section titled “Control-Flow-Aware Move Tracking”Flame’s compiler performs path-sensitive control-flow analysis to ensure no variable can ever be accessed after it might have been moved.
Branch Isolation (if / else)
Section titled “Branch Isolation (if / else)”When branching logic moves a variable, Flame tracks whether the move occurred unconditionally across all branches or conditionally along only a subset:
let resource = "database_connection"
if condition { let transferred = resource // Moved in 'then' branch println(transferred)} else { println("Resource preserved along this branch")}
// ❌ Compile Error: use of conditionally moved value: 'resource'// println(resource)If a variable is moved across all possible execution paths, it is permanently marked as moved. If it is moved along only one path, the compiler marks it as conditionally moved and statically prevents subsequent reads.
Loop Back-Edge Invariants
Section titled “Loop Back-Edge Invariants”Moving an outer non-Copy variable inside a while or for loop body without restoring it is strictly forbidden at compile time, because subsequent iterations would encounter an invalid, moved place:
let config = "app_config"let mut i = 0
while i < 3 { // ❌ Compile Error: cannot move out of 'config' in a loop let current = config println(current) i = i + 1}Re-initialization & Assignment
Section titled “Re-initialization & Assignment”Assigning a new value to a moved variable restores its ownership. Once re-initialized, the variable is fully valid again for moves and borrows:
let mut token = "initial_token"
// Move token into consumerlet consumed = tokenprintln(consumed)
// Token is currently moved...// Re-assigning re-initializes ownership:token = "refreshed_token"
// ✅ Fully valid!println($"Re-initialized token: {token}")Scope & Deterministic Drops (RAII)
Section titled “Scope & Deterministic Drops (RAII)”Memory and resources are released the exact moment an owner exits its lexical block { ... }:
{ let buffer = [1, 2, 3, 4, 5] println(buffer.len()) // 5} // `buffer` exits scope here; heap buffer is reclaimed immediately!
// `buffer` is inaccessible here