Memory Safety & Type System
Relevant source files
The following files were used as context for generating this wiki page:
Sploosh is a safety-first, AI-native programming language that enforces Rust-grade memory safety and strong typing to eliminate common classes of programming errors. The system is designed to provide high-level abstractions like Elixir-style actors while maintaining low-level control and performance across multiple targets, including native, WASM, EVM, and SVM.
The core philosophy centers on "One way to do everything," ensuring that both human developers and AI agents can reason about memory and types without ambiguity. This is achieved through explicit ownership rules, the absence of null and exceptions, and the use of structural concurrency to prevent shared mutable state bugs.
Sources: README.md:3-12, VISION.md:88-105, AGENTS.md:34-40
Type System Fundamentals
Sploosh utilizes a static type system where types are resolved at compile time. The language supports primitive types, compound types (tuples, arrays, slices), and user-defined types (structs, enums, actors).
Primitive Types
The language defines a specific set of primitive types representing integers, floats, booleans, and characters.
| Category | Types | Description |
|---|---|---|
| Signed Integers | i8, i16, i32, i64, i128 | Fixed-width signed integers. |
| Unsigned Integers | u8, u16, u32, u64, u128, u256 | Fixed-width unsigned integers; u256 is standard for Web3. |
| Floating Point | f32, f64 | IEEE-754 floating point numbers. Prohibited in onchain mod. |
| Textual | char, str, String | Unicode characters and strings. |
| Web3 | Address | Native type for blockchain addresses. |
| Boolean | bool | Logical true or false. |
Sources: crates/sploosh-parser/src/lib.rs:1072-1092, crates/sploosh-lexer/src/lib.rs:160-163, VISION.md:100-102
Compound and Reference Types
Types can be composed into complex structures or referenced using specific syntax:
- References: Indicated by
&or&mutfor mutable references. - Arrays/Slices: Fixed-size arrays
[T; N]and dynamically sized slices[T]. - Tuples: Heterogeneous groupings like
(i64, bool). - Function Types: Represented as
fn(Args) -> Ret. - Dynamic Traits:
dyn Traitfor polymorphism.
Sources: crates/sploosh-parser/src/lib.rs:400-449
Ownership and Memory Safety
Memory safety in Sploosh is governed by an ownership model that prevents data races and undefined behavior.
Shared and Isolated State
Sploosh distinguishes between local memory, shared immutable memory, and isolated mutable state.
This diagram illustrates the three primary ways memory is managed to ensure safety.
Sources: VISION.md:73-86, README.md:20-30
- No Shared Mutable State: Mutability is confined within actors or local scopes. Global shared mutable state is prohibited to prevent race conditions.
Shared<T>: An immutable ref-counted primitive used to opt into sharing data without the overhead of message passing.- Errors as Values: All fallible operations return
Result<T, E>. The language has no panics in safe code and no exceptions.
Sources: VISION.md:73-86, README.md:58-60
Pointers and Unsafe Code
Sploosh intentionally excludes features that lead to memory corruption:
- No
unsafeblock: The language does not provide a mechanism for manual memory manipulation or pointer arithmetic. - No Raw Pointers: All references are checked by the compiler.
- FFI Safety: Foreign Function Interface (FFI) is only permitted via safe wrappers around
extern "C".
Sources: VISION.md:110-112, crates/sploosh-parser/src/lib.rs:382-398
Type Inference and Generics
The compiler bootstrap includes support for generic parameters and turbofish syntax for explicit type instantiation.
Generic Handling
Generics can be applied to fn, struct, enum, actor, trait, and impl blocks. The parser validates generic arguments and associated type bindings (e.g., Iter<Item = i64>).
The relationship between base types and their generic arguments as represented in the AST.
Sources: crates/sploosh-parser/src/lib.rs:887-937, crates/sploosh-parser/src/lib.rs:1367-1385
The Turbofish Operator
Explicit type application in expressions uses the ::<> (turbofish) operator, ensuring there is no ambiguity between generic instantiation and comparison operations.
Sources: crates/sploosh-parser/src/lib.rs:563-577, crates/sploosh-parser/src/lib.rs:905-911
Web3 and On-chain Safety
Specialized restrictions apply to the type system and memory when targeting blockchain environments (onchain mod).
- Deterministic Execution: The compiler rejects non-deterministic types and operations (e.g., floating-point math, filesystem I/O, or networking) for on-chain targets.
- Reentrancy Guard: Public on-chain functions are guarded by default. Developers must explicitly use the
@reentrantattribute to opt out. - Solidity Compatibility: The memory and storage layout for
onchain modis designed to be compatible with Solidity's storage model.
Sources: VISION.md:73-86, SECURITY.md:65-72, README.md:40-55
Summary
The Sploosh memory safety and type system represent a synthesis of systems-level rigor and high-level safety abstractions. By enforcing strict ownership, removing unsafe pointer arithmetic, and providing specialized targets for Web3, Sploosh ensures that programs are both secure and predictable. The reliance on a "spec-first" approach means these safety guarantees are architecturally defined before being implemented in the compiler frontend.
Sources: VISION.md:10-25, CONTRIBUTING.md:4-9