unsafe is an expression form exported from #:coalton++ (coalton++:unsafe) that lexically disables generated Common Lisp runtime safety and type checks within its body.
Syntax
(coalton++:unsafe
⟨body-element⟩*
⟨expr⟩)
Semantics
unsafeis exported from the#:coalton++package rather than#:coalton.- Its body is an implicit
progn(supporting multiple expressions and shorthandletbindings) and returns the value(s) of the final⟨expr⟩. - Static typechecking is not disabled: Coalton still typechecks every expression inside
unsafeunder the normal type rules. - In generated Common Lisp,
unsafe:- Wraps the body in
(cl:locally (cl:declare (cl:optimize (cl:safety 0) #+sbcl (sb-c::type-check 0))) ...). - Suppresses the
(sb-c::type-check 1)declaration that Coalton normally emits around embeddedlispforms inside its lexical scope.
- Wraps the body in
- The final expression of an
unsafeblock is in tail position whenever theunsafeblock itself is in tail position (including forrectail calls). - Returning a value from a
lispform insideunsafethat violates its declared Coalton type causes undefined behavior in the host Common Lisp runtime.
Examples
(coalton-toplevel
;; Unchecked 64-bit unsigned addition via inline Lisp
(declare unsafe-u64-add (U64 * U64 -> U64))
(define (unsafe-u64-add a b)
(coalton++:unsafe
(lisp (-> U64) (a b)
(cl:the (cl:unsigned-byte 64) (cl:+ a b))))))
Other notes
unsafeis intended for low-level standard library primitives and verified hot loops where boundary checks onlispforms have already been established by surrounding invariants.- Inspect the effect of
unsafeon generated declarations withpprint-coalton-codegen-types.