Skip to main content

Unsafe Blocks

The unsafe keyword allows low-level operations that bypass Morph's safety guarantees.


Syntax

unsafe {
// raw pointer arithmetic
// manual memory management
// platform-specific operations
}

The construct is parsed and lowered on native / LLVM pipelines like other statements (UnsafeBlock in the parser and semantic analyzer).


What is allowed in unsafe?

Documentation here stays behavioral, not exhaustive:

  • Raw pointer manipulation beyond address of / value of
  • Direct memory access
  • Platform-specific intrinsics

The exact legality of a statement inside unsafe still depends on the active target and semantic rules.


Targets and sandboxing

Do not assume unsafe is accepted on every execution target. Bytecode / sandbox-only paths may reject or fail to lower constructs that rely on unchecked host memory. Treat unsafe as native-oriented unless your toolchain explicitly documents otherwise for your target.


When to use

  • Interfacing with hardware at the lowest level
  • Performance-critical hot paths where safety checks are too expensive
  • FFI compatibility with C/C++ code

Next steps

  • Extern — Foreign function interface