flan/test/programs/bounds.flan
Joseph Ferano 96ab4c9cf0 Retire the per-type printers, since print says all of it
print-str, print-i64, print-f64, print-bytes, print-line and newline leave
the prelude. print and println are the whole printing surface now, and print
is the better call at every one of the sites that used them: it is the same
structural walk without the newline, so the no-newline case the family was
kept for is covered, and it takes the value as it is. The old print-i64
forced an explicit (i64 x) at every call site, because this language widens
nothing implicitly; that cast is gone from 127 places.

Dropping it moves one answer. hash-grid returns u64, and the cast through
the signed printer showed sand-headless's hash as -2851001042534928384.
print routes a u64 through flan_u64_to_bytes, so it now prints
15595743031174623232 — the same 64 bits, read as the unsigned number they
are. The pinned expectation follows the correction.

test-flan-dev.el and test_session.ml both reached for print-line as "a name
the prelude has"; they reach for rand-seed instead.
2026-09-12 05:32:25 +07:00

32 lines
1.5 KiB
Plaintext

;;;; Bounds checks, NEXT.md item 2. One program, one case per argument, so a
;;;; trap is observable: the checked build exits 134 with the source location
;;;; on stderr, the unchecked one runs off the end and is not asserted on.
;;;;
;;;; The selector is also the index wherever it can be, which is what keeps the
;;;; index dynamic — a literal would let the checker reject it outright one day
;;;; (that is a separate job) and lets LLVM fold the branch away here.
(defvar arr [3 i32])
(defn main [args [string]] i32
(let [n (i32 (bytes->i64 (bytes (at args 1))))
s (bytes "hello")] ; len 5
(cond
;; In bounds, including both edges: the last index, and a slice that
;; ends exactly at len. Neither may trap.
(= n 0) (do (print (at arr 2))
(print (slice s 1 5))
(print (slice s 5 5)) ; empty at len is legal
(println ""))
(= n 3) (print (at arr n)) ; past the end of a fixed array
(= n -1) (print (at arr n)) ; negative index
(= n 9) (print (at s n)) ; past the end of a slice
;; The write path lowers through place/Pindex rather than through At, so
;; it is checked separately even though the message is the same.
(= n 7) (set (at arr n) 1) ; write past the end
(= n 4) (print (slice s n 9)) ; hi past the end
(= n 2) (print (slice s n 1)) ; reversed range
:else (println "?"))
0))