flan/test/programs/enum-compare.flan

28 lines
1.0 KiB
Plaintext

;;;; Comparing enums, which type checked and then died in the backend.
;;;;
;;;; Types.is_comparable admits an enum, so the checker was stating an intent
;;;; emit never honoured: (= k :a) passed check and then raised
;;;; Failure("comparison on K") with no source location. An enum is an i32 at
;;;; run time, so all six operators lower to icmp.
(defenum K [lo -1 mid 0 hi 1])
(defstruct S [k K])
(defn is-eq [k K] bool (= k :mid))
(defn is-below [k K] bool (< k :mid))
(defn main [] i32
;; Equality, both ways round.
(println (if (is-eq :mid) "eq yes" "eq no"))
(println (if (is-eq :hi) "eq yes" "eq no"))
;; Ordering, and signed: lo is -1, so an unsigned compare would call it the
;; largest member and answer the other way.
(println (if (is-below :lo) "lo below mid" "lo not below mid"))
(println (if (is-below :hi) "hi below mid" "hi not below mid"))
;; And through a struct field, which is a different path to the same compare.
(let [s (S {.k :hi})]
(println (if (= (.k s) :hi) "field eq yes" "field eq no")))
0)