as-slice was a warning, not an operation. The input type already decides which of the two things happens — a Vec can only be borrowed, an array or a string can only be viewed, and no call site picks between them — so the second name expressed no choice a reader could make. And it warned at the moment the view is taken, which is the one moment nothing is wrong; the danger arrives later, at the push. slice now takes a Vec at all three arities and as-slice is gone. (slice v lo) was free, and is the arity the Vec never had: the runtime already reads a hi of -1 as "to the end", so the tail form passes the caller's lo and the same -1 — no slot, no length read, no second evaluation. The merge is entirely in the checker; the Vec path builds the flan_vec_as_slice call it always built and neither backend has a line about any of it. A Vec a call returned is refused at every arity, and not for the array's reason. (slice (mk)) over an array dangles. (slice (make-vec)) does not — the storage outlives the expression — but the header is a temporary, so nothing can ever free the block. The refusal says that and names the let. The name's own refusal sits in ordinary_call after every table, so a program that defines an as-slice still reaches its own. It reads for somebody who has never heard of the old name and writes the call back out, spelling each argument that is a name or a number. The warning moved to where it bites: BUILT.md gains a section beside the Vec table and the push row points at it, spec-memory.md's Borrowing says the same. Investigated and deliberately not built — a diagnostic for a live view at the push. (reserve v 100) then a slice, a push and a read is correct code under the contract the spec chose, so any flag on it is a false positive by the language's own semantics rather than by an approximation. FIX.org has the finding and the syntactic sketch that does not work.
104 lines
4.4 KiB
Plaintext
104 lines
4.4 KiB
Plaintext
;;;; slurp and barf — NEXT.md decisions 2 and 5.
|
|
;;;;
|
|
;;;; slurp reads a whole file and answers a (Vec u8). It allocates, which is
|
|
;;;; why it waited for Vec, and it follows spec-memory.md's rule to the letter:
|
|
;;;; no allocating operation returns an error, so there is no Result here and
|
|
;;;; no out-parameter anywhere.
|
|
;;;;
|
|
;;;; Failure signals a condition under a restart, which is the pattern
|
|
;;;; StorageExhausted set this session. Two restarts, and they are the pair
|
|
;;;; Common Lisp establishes for a file-error:
|
|
;;;;
|
|
;;;; retry the file may be there now
|
|
;;;; use-value [p string] try this other path instead
|
|
;;;;
|
|
;;;; use-value is a *typed* restart — the second thing this session bought —
|
|
;;;; and it is the first one the compiler itself emits. Its parameter is the
|
|
;;;; path slot the attempt reads, so the clause body is empty: the invoker's
|
|
;;;; argument lands in the slot, the clause falls through, and the loop
|
|
;;;; re-attempts against the new path.
|
|
|
|
;; Handlers cannot see the locals of the function that established them, so the
|
|
;; observations are globals — the same shape exhausted.flan uses.
|
|
(defonce seen i64)
|
|
(defonce last-reason i32)
|
|
(defonce last-op i32)
|
|
(defonce last-path string)
|
|
|
|
(defn main [] i32
|
|
;; ── The happy path ────────────────────────────────────────────────
|
|
(let [v (slurp "programs/assets/a.txt")]
|
|
(println (len v)) ; 13
|
|
(print (string (slice v))) ; hello from a
|
|
(free v))
|
|
|
|
;; Byte-exact, the same as an embed: nothing here decodes anything.
|
|
(let [v (slurp "programs/assets/raw.bin")]
|
|
(println (len v)) ; 4
|
|
(println (at v 0)) ; 0
|
|
(println (at v 2)) ; 255
|
|
(free v))
|
|
|
|
;; ── use-value: a missing file, answered with another path ─────────
|
|
;; The textbook case. The handler does not know what slurp was going to do
|
|
;; with the bytes and does not have to: it names a file that is there and
|
|
;; the read resumes as if that had been asked for all along.
|
|
(handler-bind
|
|
[(FileError [c]
|
|
(set seen (+ seen 1))
|
|
(set last-reason (.reason c))
|
|
(set last-op (.op c))
|
|
(set last-path (.path c))
|
|
(invoke-restart 'use-value "programs/assets/b.bin"))]
|
|
(let [v (slurp "programs/assets/does-not-exist")]
|
|
(println (len v)) ; 3
|
|
(println (string (slice v))) ; BBB
|
|
(free v)))
|
|
(println seen) ; 1
|
|
(println (= last-reason file-missing)) ; true
|
|
(println (= last-op file-op-read)) ; true
|
|
;; The condition carries the path that actually failed, not the one that
|
|
;; eventually worked — the handler is told what it is answering about.
|
|
(println last-path) ; programs/assets/does-not-exist
|
|
|
|
;; ── barf, and reading back what it wrote ──────────────────────────
|
|
(barf "slurp-out.txt" (bytes-view "round trip\n"))
|
|
(let [v (slurp "slurp-out.txt")]
|
|
(println (len v)) ; 11
|
|
(print (string (slice v))) ; round trip
|
|
(free v))
|
|
|
|
;; barf's own failure signals the same condition with op = write. A directory
|
|
;; that does not exist is the reachable case on every platform.
|
|
(set seen 0)
|
|
(handler-bind
|
|
[(FileError [c]
|
|
(set seen (+ seen 1))
|
|
(set last-op (.op c))
|
|
(invoke-restart 'use-value "slurp-out.txt"))]
|
|
(barf "no-such-dir/x.txt" (bytes-view "second\n")))
|
|
(println seen) ; 1
|
|
(println (= last-op file-op-write)) ; true
|
|
(let [v (slurp "slurp-out.txt")]
|
|
(print (string (slice v))) ; second
|
|
(free v))
|
|
|
|
;; ── retry: the file was not there, so the handler makes it ────────
|
|
;; The other restart, and the one use-value cannot stand in for: here the
|
|
;; path is right and the world is wrong. The handler fixes the world and
|
|
;; re-attempts the *same* request, which is exactly what retry means for a
|
|
;; failed allocation too.
|
|
(set seen 0)
|
|
(handler-bind
|
|
[(FileError [c]
|
|
(set seen (+ seen 1))
|
|
(set last-reason (.reason c))
|
|
(barf "slurp-made.txt" (bytes-view "made by the handler\n"))
|
|
(invoke-restart 'retry))]
|
|
(let [v (slurp "slurp-made.txt")]
|
|
(print (string (slice v))) ; made by the handler
|
|
(free v)))
|
|
(println seen) ; 1
|
|
(println (= last-reason file-missing)) ; true
|
|
0)
|