flan/test/programs/widening.flan
Joseph Ferano 57fe91f303 Five records become one, and every citation lands somewhere
FIX.org, NEXT.md, DISCUSS.org, docs/DISCUSS.md and the session handoff at the
root are one TODO.org now: 293 entries under seven subsystem headings, each
carrying an org keyword that says where it stands. A DONE entry is a few lines
saying what was decided and what that rules out; the reasoning that would not
compress — the embedding spike and the four reports the hand-written x86
backend was built from — moved into docs/BUILT.md instead, and its entries
point there in one line.

Every entry was checked against the tree before it got a keyword, and the
prose was wrong in both directions. Things the deleted files called open were
built: the first-evaluation stall, main being redefinable, macro parameter
lists, the type-limit constants, the array constructors, the byte fills,
inc/dec, the discard's fontification, the Emacs buffers, rt_die's _exit, the
backtrace surface, and the acceptance failure that could print and still exit
zero. Things they called done were not: the backend reports' no-plan buckets
had gone stale in the other direction, the value-dependent defvar was
superseded rather than built, and macro-expansion source locations are on an
unmerged lane, so that entry is NEXT and names the branch.

Every comment that cited one of the five by name now cites a heading that
exists, in TODO.org or in docs/BUILT.md. The session reports under
docs/handoffs/ keep naming the files they worked on, because rewriting them
would falsify what those sessions did; each carries a note saying where the
content went.
2026-09-21 21:05:48 +07:00

110 lines
5.6 KiB
Plaintext

;;;; Implicit widening, and the only thing worth pinning about it: the bits.
;;;; TODO.org, "Implicit numeric widening is legal; narrowing stays a hard error".
;;;;
;;;; A widening conversion is admitted exactly when it cannot change the
;;;; number, so every row here has an answer that is also the answer the source
;;;; type had. Which means the test is entirely about the *emitted* cast being
;;;; the right one: a sign-extension where the source is signed, a
;;;; zero-extension where it is not, and the float conversions picking sitofp
;;;; against uitofp by the same rule. Each of those is a separate instruction
;;;; on both backends and choosing the wrong one gives a wrong number rather
;;;; than a wrong type, which no type test would catch.
;;;;
;;;; The rows are picked so that a mistake is visible in the printed value:
;;;;
;;;; -5 i8 to i64 sext; a zext prints 251
;;;; -1 i32 to i64 sext; a zext prints 4294967295
;;;; 255 u8 to i16 zext; a sext prints -1
;;;; 4000000000 u32 zext to i64; a sext prints -294967296
;;;; 4294967295 u32 to f64, exact; a signed conversion prints -1
;;;; -2000000000 i32 to f64; a uitofp prints 2294967296
;;;;
;;;; Everything goes through a global rather than a literal, because a literal
;;;; is built at the wanted width by the literal rule and would never reach a
;;;; cast at all.
(defonce w-i8neg i8 -5)
(defonce w-i8pos i8 127)
(defonce w-i16neg i16 -300)
(defonce w-i32neg i32 -2000000000)
(defonce w-i32one i32 1)
(defonce w-i32all i32 -1)
(defonce w-u8max u8 255)
(defonce w-u16max u16 65535)
(defonce w-u32big u32 4000000000)
(defonce w-u32max u32 4294967295)
(defonce w-i64big i64 5000000000)
(defonce w-f32half f32 0.5)
;; Widening at a parameter. Each of these is a plain typed function and the
;; call sites below hand it a narrower type with no cast written anywhere.
(defn w-take-i64 [x i64] i64 x)
(defn w-take-i16 [x i16] i16 x)
(defn w-take-u64 [x u64] u64 x)
(defn w-take-f64 [x f64] f64 x)
(defn w-take-f32 [x f32] f32 x)
;; Widening at a return position: the body is an i32 and the signature is i64.
(defn w-ret-widened [] i64 w-i32neg)
;; Widening in a binary operator, both orders. The first is the direction
;; [expect] already served; the second is the one the join rule added.
(defn w-add-wide-first [] i64 (+ w-i64big w-i32one))
(defn w-add-narrow-first [] i64 (+ w-i32one w-i64big))
(defn main [args [string]] i32
;; ── integer to integer ──────────────────────────────────────────
(println (w-take-i64 w-i8neg)) ;; -5
(println (w-take-i64 w-i8pos)) ;; 127
(println (w-take-i64 w-i16neg)) ;; -300
(println (w-take-i64 w-i32all)) ;; -1
(println (w-take-i64 w-i32neg)) ;; -2000000000
(println (w-take-i16 w-u8max)) ;; 255
(println (w-take-i64 w-u8max)) ;; 255
(println (w-take-i64 w-u16max)) ;; 65535
(println (w-take-i64 w-u32big)) ;; 4000000000
(println (w-take-i64 w-u32max)) ;; 4294967295
(println (w-take-u64 w-u32big)) ;; 4000000000
(println (w-take-u64 w-u8max)) ;; 255
;; ── a widened return ────────────────────────────────────────────
(println (w-ret-widened)) ;; -2000000000
;; ── integer to float, exact only ────────────────────────────────
(println (w-take-f64 w-i32neg)) ;; -2000000000.0
(println (w-take-f64 w-u32max)) ;; 4294967295.0
(println (w-take-f64 w-i8neg)) ;; -5.0
(println (w-take-f32 w-i16neg)) ;; -300.0
(println (w-take-f32 w-u16max)) ;; 65535.0
;; The printer answers %g, which rounds an f64 long before the bits it is
;; carrying run out, so the exactness the int-to-float boundary is chosen for
;; is asserted by subtraction rather than by reading the digits. Each of
;; these is the difference between the widened value and the number it is
;; supposed to be, and a conversion that lost anything answers something
;; other than the last unit.
(println (- (w-take-f64 w-u32max) 4294967294.0)) ;; 1
(println (- (w-take-f64 w-i32neg) -1999999999.0)) ;; -1
(println (- (w-take-f32 w-u16max) 65534.0)) ;; 1
;; ── float to float ──────────────────────────────────────────────
(println (w-take-f64 w-f32half)) ;; 0.5
;; ── the binary join, both operand orders ────────────────────────
(println (w-add-wide-first)) ;; 5000000001
(println (w-add-narrow-first)) ;; 5000000001
;; The narrower operand is the first one, and the sum is an i64 even though
;; nothing on this line is annotated.
(println (+ w-i32neg w-i64big)) ;; 3000000000
;; A comparison joins the same way, and the widened -1 must still be -1.
(println (< w-i32all w-i64big)) ;; true
;; min and max over two widths answer at the wider one.
(println (max w-i8neg w-i16neg)) ;; -5
(println (min w-i8neg w-i32neg)) ;; -2000000000
;; A count narrower than the value widens to it; the value's width decides.
(println (<< w-i64big w-i8pos)) ;; 0 -- masked to 127 mod 64 = 63
;; An expectation reaches the operands, so this adds at i64 rather than
;; wrapping at i32 and widening the sum afterwards.
(println (w-take-i64 (+ w-i32neg w-i32neg))) ;; -4000000000
0)