flan/test/programs/bytes-copy.flan
Joseph Ferano a64bee6d96 Review follow-ups: a new def's image, the keyword that cannot change, and the sweep
Three defects, all from lifting every def initialiser, none of which the
suite caught:

A def typed fresh into a live session came up zero and stayed zero. The
image flan_dev_global copies on the allocation is the only value a new
global ever gets — the host's .init-globals never calls its initialiser —
and both backends chose that image with Tast.const_init, which a def's
lifted Call fails by construction. Emit.initial_image reads the constant
back out of the lifted body; the x86 twin had the same bug.

Changing a global between def and defonce was silently ineffective: the
guard lives in the startup function compiled into the host, which a reload
cannot republish. Session.compatible refuses both directions and says to
restart; editing the value stays allowed.

And global/<n> no longer leaks into the signature refusal when a def is
retyped — the global loop names the same fact in words a reader can act on.

flan check prints def, defonce or defconst off grerun; (defvar) with no
arguments names the shapes rather than offering (defonce ); the docs,
plan.org, runtime comments and valgrind.supp are swept; BUILT.md states
the release-build cost and the uninit caveat.
2026-09-21 07:19:33 +07:00

39 lines
1.6 KiB
Plaintext

;;;; (bytes s) copies, (bytes-view s) aliases — the ruling from the
;;;; INSERTIONSORT dogfooding session. The pin is the exact inverse of the
;;;; old aliasing: a write through the copy leaves the original string
;;;; printing unchanged, where the old (bytes s) either showed the write
;;;; through or trapped, depending on where the string's storage was.
(defonce frame Allocator)
(defn main [] i32
;; 1. The copy is writable and independent. Under the old reinterpret this
;; second line printed ZNSERTIONSORT too (or the whole program died in
;; .rodata) — the original staying itself is the whole of the change.
(let [s "INSERTIONSORT"
b (bytes s)]
(set (at b 0) \Z)
(println (string b)) ; ZNSERTIONSORT
(println s)) ; INSERTIONSORT
;; 2. A literal's copy is writable — the exact form that used to segfault
;; at -O0 and silently do nothing at -O2.
(let [b (bytes "hi")]
(set (at b 0) \H)
(println (string b))) ; Hi
;; 3. The view still costs nothing and reads the string's own storage.
(let [v (bytes-view "abc")]
(println (len v)) ; 3
(println (at v 2))) ; 99
;; 4. (bytes s a) names the allocator, like (vec-new T a) and (clone v a):
;; the copy's block comes from the arena and free-all reclaims it.
(set frame (arena-new 4096))
(let [b (bytes "arena" frame)]
(set (at b 4) \A)
(println (string b))) ; arenA
(free-all frame)
(arena-destroy frame)
0)