Lines
100 %
Functions
90 %
Branches
//! Arm reconciliation and the single nil classifier (ADR-0033), shared by the
//! multi-arm forms.
use crate::ast::{PairElement, WasmType};
/// Whether — and how — a value of static type `ty` can be `nil` (ADR-0033).
/// The single source of truth every nil-facing consumer routes through. One
/// row per [`WasmType`] variant, so a new variant without a row is a Rust
/// compile error.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum NilRepr {
/// `nil` is the unboxed i32 zero (`t` is one) — only `Bool`.
I32Zero,
/// This type has no valid `nil`; a nil arm beside it widens to `AnyRef`
/// (or is a compile error, at the call site's discretion).
Never,
/// `nil` is `ref.null` in this type's reference slot.
Null,
}
#[must_use]
pub(crate) fn nil_repr(ty: &WasmType) -> NilRepr {
match ty {
WasmType::Bool => NilRepr::I32Zero,
WasmType::I32 | WasmType::Ratio | WasmType::Commodity | WasmType::Closure(_) => {
NilRepr::Never
WasmType::PairRef(_)
| WasmType::StringRef
| WasmType::SymbolRef
| WasmType::EntityRef(_)
| WasmType::StructRef(_)
| WasmType::AnyRef => NilRepr::Null,
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) enum Arm {
/// Compiled: a value of this type is already on the arm's stack.
Value(WasmType),
/// A numeric literal of this default stratum, not yet emitted.
NumberLiteral(WasmType),
/// A form resolving to `nil`, not yet emitted.
Nil,
/// A form resolving to a constant list of this chain element, materialized
/// at the reconciled element once the result is known.
ConstantList(PairElement),
/// A BLOCK exit or tail as one pass compiled it: its own arm, the same in
/// every pass, and the type its bytes left on the stack.
pub(crate) struct Exit {
pub(crate) arm: Arm,
pub(crate) emitted: WasmType,
/// The type a form that never returns reports, as `(error …)` does: its
/// bytes end `unreachable`, so the type is never observed at runtime.
pub(crate) const NEVER_RETURNS: WasmType = WasmType::I32;
/// Unifies reachable arms: compiled values join (pair elements widen), a
/// numeric literal adopts an Index/Scalar value's stratum, `nil` rides a
/// list / anyref / bool result, and anything else widens to `AnyRef`. With no
/// reachable arm the form never returns.
pub(crate) fn reconcile_arms(arms: &[Arm]) -> WasmType {
let mut value: Option<WasmType> = None;
let mut literal: Option<WasmType> = None;
let mut nil = false;
for arm in arms {
match *arm {
Arm::Value(ty) => value = Some(joined(value, ty)),
Arm::ConstantList(elem) => value = Some(joined(value, WasmType::PairRef(elem))),
Arm::NumberLiteral(ty) => {
literal = Some(match literal {
Some(prev) if prev != ty => WasmType::Ratio,
_ => ty,
});
Arm::Nil => nil = true,
let typed = match (value, literal) {
(base, None) => base,
(None, lit) => lit,
(Some(base), Some(lit)) if literal_fits(base, lit) => Some(base),
(Some(base), Some(lit)) => Some(join_types(base, lit)),
};
match (typed, nil) {
(None, false) => NEVER_RETURNS,
(None, true) => WasmType::Bool,
(Some(ty), false) => ty,
(Some(ty), true) => match nil_repr(&ty) {
NilRepr::I32Zero | NilRepr::Null => ty,
NilRepr::Never => WasmType::AnyRef,
},
/// [`reconcile_arms`] for a consumer that expects `expected`: when every
/// reachable arm is a numeric literal fitting it (or none is reachable),
/// nothing else fixes the type, so the consumer's does.
pub(crate) fn reconcile_arms_expecting(arms: &[Arm], expected: Option<WasmType>) -> WasmType {
let adopts = |ty: &WasmType| {
arms.iter()
.all(|arm| matches!(arm, Arm::NumberLiteral(lit) if literal_fits(*ty, *lit)))
match expected.filter(adopts) {
Some(ty) => ty,
None => reconcile_arms(arms),
/// Whether a value of `ty` already stands as `result` with no instruction: the
/// same type, a pair under a widened element, or a reference read as
/// `AnyRef`. Only an i32-repr value needs boxing (`emit_box_anyref`).
pub(crate) fn rides(ty: WasmType, result: WasmType) -> bool {
match (ty, result) {
_ if ty == result => true,
(WasmType::PairRef(_), WasmType::PairRef(_)) => true,
(WasmType::I32 | WasmType::Bool, _) => false,
(_, result) => result == WasmType::AnyRef,
fn join_types(a: WasmType, b: WasmType) -> WasmType {
match (a, b) {
_ if a == b => a,
(WasmType::PairRef(x), WasmType::PairRef(y)) => WasmType::PairRef(x.widen(y)),
_ => WasmType::AnyRef,
fn joined(prev: Option<WasmType>, ty: WasmType) -> WasmType {
match prev {
Some(prev) => join_types(prev, ty),
None => ty,
/// Whether a literal of stratum `lit` can adopt `base`: an integer literal
/// becomes an index or a scalar, a fractional one only a scalar, and no literal
/// becomes Money (it has no currency).
pub(crate) fn literal_fits(base: WasmType, lit: WasmType) -> bool {
matches!(
(base, lit),
(WasmType::I32 | WasmType::Ratio, WasmType::I32) | (WasmType::Ratio, WasmType::Ratio)
)
/// A fact never holds a closure type, so no signature id outlives its attempt.
pub(crate) fn fact(ty: WasmType) -> WasmType {
WasmType::Closure(_) => WasmType::AnyRef,
WasmType::PairRef(PairElement::Closure(_)) => WasmType::PairRef(PairElement::AnyRef),
#[cfg(test)]
#[path = "join_tests.rs"]
mod tests;