Lines
91.3 %
Functions
100 %
Branches
//! Pair element types and struct layout ids.
use core::fmt;
use crate::ast::{ClosureSigId, EntityKind, WasmType};
/// Element type of a homogeneous `$pair` chain. Tracked at compile time so
/// CAR/CDR emit the right downcast and CONS refuses heterogeneous mixing
/// with a structured type error. Kept as a `Copy` enum so `WasmType` stays
/// `Copy` — and so adding a new element type stays a one-line variant
/// addition. Nesting (pairs-of-pairs) waits for a follow-up sub-slice
/// once flat lists are stable across the consumers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum PairElement {
I32,
/// Truth-value cell: shares `I32`'s i31-boxed car representation (CAR/CDR
/// emit the same i31 downcast), but distinct so a bool extracted from a
/// list serializes as `Nil` / `Bool`, not `Number` — the list-element
/// analogue of [`WasmType::Bool`] vs `I32`.
Bool,
Ratio,
Commodity,
StringRef,
/// Runtime symbol or keyword: a `$symbol` struct carrying its printed name.
Symbol,
Entity(EntityKind),
/// A `DEFSTRUCT` instance of this layout: a `$struct` reference.
Struct(StructId),
/// A closure of this signature: a closure struct reference.
Closure(ClosureSigId),
/// Heterogeneous escape hatch (ADR-0025). Cars stay as raw
/// `anyref`; CAR returns `WasmType::AnyRef` and the script must
/// downcast at the use site (or consume via type-test natives like
/// `ok?` / `err-code`). CONS widens to this variant when the two
/// arms disagree on element type, and host fns that produce
/// fundamentally heterogeneous lists (e.g. =catch-each= result
/// cells) construct directly into `PairRef(AnyRef)`.
AnyRef,
}
impl fmt::Display for PairElement {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::I32 => write!(f, "i32"),
Self::Bool => write!(f, "bool"),
Self::Ratio => write!(f, "ratio"),
Self::Commodity => write!(f, "commodity"),
Self::StringRef => write!(f, "string"),
Self::Symbol => write!(f, "symbol"),
Self::Entity(kind) => write!(f, "{kind}"),
Self::Struct(id) => write!(f, "{id}"),
Self::Closure(sig) => write!(f, "{}", WasmType::Closure(*sig)),
Self::AnyRef => write!(f, "any"),
impl PairElement {
/// The matching `WasmType` for a pair's car when extracted.
#[must_use]
pub fn as_wasm_type(self) -> WasmType {
Self::I32 => WasmType::I32,
Self::Bool => WasmType::Bool,
Self::Ratio => WasmType::Ratio,
Self::Commodity => WasmType::Commodity,
Self::StringRef => WasmType::StringRef,
Self::Symbol => WasmType::SymbolRef,
Self::Entity(kind) => WasmType::EntityRef(kind),
Self::Struct(id) => WasmType::StructRef(id),
Self::Closure(sig) => WasmType::Closure(sig),
Self::AnyRef => WasmType::AnyRef,
/// Inverse of `as_wasm_type`: `None` for a nested list, which has no
/// slot of its own. `WasmType::AnyRef` round-trips to `PairElement::AnyRef`.
pub fn from_wasm_type(ty: WasmType) -> Option<Self> {
match ty {
WasmType::I32 => Some(Self::I32),
// `Bool` keeps its own slot — it shares `I32`'s i31-boxed car
// representation (the CAR/CDR downcast is identical) but stays
// distinct so a bool extracted from a list serializes as Nil/Bool,
// not Number.
WasmType::Bool => Some(Self::Bool),
WasmType::Ratio => Some(Self::Ratio),
WasmType::Commodity => Some(Self::Commodity),
WasmType::StringRef => Some(Self::StringRef),
WasmType::SymbolRef => Some(Self::Symbol),
WasmType::EntityRef(kind) => Some(Self::Entity(kind)),
WasmType::StructRef(id) => Some(Self::Struct(id)),
WasmType::Closure(sig) => Some(Self::Closure(sig)),
WasmType::AnyRef => Some(Self::AnyRef),
WasmType::PairRef(_) => None,
/// Widen `self` against `other` to the most-specific common
/// element. Identical types stay; everything else widens to
/// `AnyRef` so heterogeneous CONS no longer errors. Symmetric.
pub fn widen(self, other: Self) -> Self {
if self == other { self } else { Self::AnyRef }
/// Index of a `DEFSTRUCT` layout in the session's struct registry. Rides
/// inside `WasmType` like `ClosureSigId`, so it stays small and `Copy`.
pub struct StructId(pub u32);
impl fmt::Display for StructId {
write!(f, "struct#{}", self.0)