Memory
Xu uses ownership inference and deterministic scope cleanup, without a garbage collector. An owning value has one owner; a borrow grants temporary access without transferring that ownership. These rules do not prove that foreign code or native host resources are leak-free.
The examples below were checked with the official SDK v0.10.12. Save each block as a separate .xu file. Use xu check for complete programs and xu check --lib for function-only examples. Diagnostic text below is a substring of actual compiler output. Run examples with fn main; the file example requires the local SDK.
| Form | Permission / lifetime |
|---|---|
Owning string / []T by value | Moves ownership; the old binding becomes unavailable. |
&x | Shared, read-only access; several shared borrows may coexist. |
$x, then $&x | Writable binding, then exclusive writable access. |
*r | Read a scalar through a reference; writing requires an exclusive reference. |
*raw T raw pointer | Low-level hardware contract; dereferencing requires an unsafe block, bypassing borrow checks. |
Scalar values such as int copy. Channel values are shared handles to the same runtime channel; passing a channel does not consume its sender's handle. See Concurrency for the boundary between borrowing and sending.
Move: use the new owner
Assignment, by-value arguments, returns and stores into owning slots transfer an owning value. Use the destination after the move. Clone only through an API that explicitly provides a copy.
@main
fn main() int {
text := "owned"
next := text
println(next)
<< 0
}
Expected xu check: pass. xu run prints owned.
@main
fn bad() {
text := "owned"
next := text
println(text)
println(next)
}
Expected xu check --lib: fail; diagnostic containing use of moved value 'text'.
Share: read without changing the owner
Shared references may coexist. While a shared loan is live, the owner cannot be changed, moved or exclusively borrowed. A loan can end at its last use, before the enclosing scope ends: the assignment below follows both reads.
@main
fn main() int {
$value := 1
first := &value
second := &value
println(*first + *second)
value = 2
println(value)
<< 0
}
Expected xu check: pass. xu run prints 2, then 2.
@main
fn bad() {
$value := 1
view := &value
value = 2
println(*view)
}
Expected xu check --lib: fail; diagnostic containing cannot assign to 'value' because it is borrowed.
Exclusive: one writer at a time
Declare a writable owner with $value, then take $&value. A live exclusive loan excludes other access to the owner. After the writer's last use, a shared read is allowed. Two overlapping writers are rejected.
@main
fn main() int {
$value := 1
writer := $&value
*writer = 2
println(*writer)
view := &value
println(*view)
<< 0
}
Expected xu check: pass. xu run prints 2, then 2.
@main
fn bad() {
$value := 1
first := $&value
second := $&value
println(*first)
println(*second)
}
Expected xu check --lib: fail; diagnostic containing cannot borrow 'value' as mutable because it is also borrowed.
Scope release: the owner ends, its borrows cannot escape
Ordinary owned values are released on scope exit, including normal returns. Moving transfers that cleanup responsibility. Resource types with a compiler-managed drop hook, such as os.File, close their resource as a fallback; call File.close() explicitly when you need to observe a close error. The fallback suppresses close errors.
Local SDK only. This program creates scope-release.txt in the current directory. write_once borrows the file for writing; its local file owner is cleaned up when the function returns.
@main
#os
fn write_once() int {
file := os.create("scope-release.txt") >>>
file.write("scope") >>>
<< 0
}
fn main() int {
write_once() >>> (err) {
println(err.msg())
<< 1
}
println("scope ended")
<< 0
}
Expected xu check: pass. xu run prints scope ended.
A returned reference must be tied to a borrowed input. It cannot refer to a local value that ends on return, even if the function also has a reference parameter.
@main
fn dangling(seed &int) &int {
value := 1
<< &value
}
Expected xu check --lib: fail; diagnostic containing cannot return a reference to local data.
Native handles: follow the explicit disposal contract
Scope cleanup of Xu values does not replace every native resource API. For example, the caller must explicitly dispose a ui/draw.Surface when it is no longer needed, including error and cancellation paths. Accepted frames retain their immutable pixels across disposal; retiring the caller's handle does not erase an accepted frame.
Native integration fragment; no browser Run. Given a live surface from the host, propagate disposal errors. The helper needs exclusive access because disposal updates the handle.
@main
#ui/draw
fn retire(surface $&draw.Surface) int {
<< surface.dispose() >>>
}
Expected xu check --lib: pass.
A shared reference cannot call this writable method:
@main
#ui/draw
fn retire(surface &draw.Surface) int {
<< surface.dispose() >>>
}
Expected xu check --lib: fail; diagnostic containing through a shared reference.
Omitting disposal is not necessarily a compile error: xu check is not a native leak detector. To be verified: this helper is type checked, but allocation and disposal against a live native renderer, including error/cancellation cleanup, have not been run here.
The hosted scheduler's coroutine stacks and channel metadata have a separate process-lifetime policy. Scope exit does not promise immediate reclamation of every runtime allocation. FFI callers must also follow each foreign API's ownership contract.
Low-Level Hardware Contracts: Raw Pointers (*raw T) & unsafe Isolation
Application code strictly adheres to implicit ownership inference and borrow checks, achieving zero GC without manual lifetime annotations. In the bottom 5% of systems code—operating system kernels, device drivers, and MMIO register interactions—Xu provides uncompromised micro-architectural control through explicitly isolated hardware contracts:
- Raw pointers
*raw T: May be constructed via integer address casts (e.g.base as *raw u32), supporting pointer arithmetic and comparison operators (==,!=,<,>,<=,>=); unsafe { … }isolation blocks: Dereferencing raw pointers (reading*por writing*p = val) must occur inside an explicitunsafeblock; accessing raw pointers outsideunsafetriggers a compile-time rejection. Borrow inference skips raw pointers entirely;- Volatile MMIO barriers: Built-in
volatile_read(ptr)andvolatile_write(ptr, val)intrinsics strictly prevent dead-store elimination (DSE) and loop-invariant code motion (LICM), ensuring register reads and writes hit the bus deterministically; - Precise alignment & packed layouts:
@repr("C", align: 4096)guarantees page directory alignment, while@repr("C", packed)eliminates struct padding for network packet headers and hardware descriptors.
@main
fn mmio_write(reg_addr usize, val u32) {
p := reg_addr as *raw u32
unsafe {
*p = val
}
}
fn main() int {
$buf: [4]u8
ptr := (&buf[0]) as *raw u8
unsafe {
*ptr = 42
}
println(buf[0])
<< 0
}
Expected xu check: pass. xu run prints 42.
OS API · I/O API · Drawing API · Concurrency