Lines
100 %
Functions
Branches
//! Unit tests for the runtime-type inference used to promote the
//! body's tail value when a binding form has side effects. Each
//! variant must map to the wasm type the codegen path emits;
//! otherwise upstream consumers downcast to the wrong shape and the
//! wasm becomes invalid.
use crate::ast::{Expr, Fraction, WasmType};
use super::infer::infer_runtime_type;
/// `infer_runtime_type` is the fallback for promoting a let body's
/// tail value to a runtime type when the body has side-effects.
#[test]
fn infer_runtime_type_integer_is_index_fraction_is_scalar() {
// ADR-0028: an integer literal defaults to Index (I32); a fractional
// literal is a dimensionless Scalar (Ratio). Mirrors `classify_stack_type`.
assert_eq!(
infer_runtime_type(&Expr::Number(Fraction::from_integer(0))),
WasmType::I32
);
infer_runtime_type(&Expr::Number(Fraction::new(1, 2))),
WasmType::Ratio
}
fn infer_runtime_type_bool_is_bool() {
// Bool/nil literals lower to `WasmType::Bool` on the stack, so the let-body
// tail inference must mirror that (sizing the binding local to match the
// value codegen pushes — see the WasmType::Bool serialization split).
assert_eq!(infer_runtime_type(&Expr::Bool(true)), WasmType::Bool);
assert_eq!(infer_runtime_type(&Expr::Nil), WasmType::Bool);
/// A let body whose tail is a String literal must report
/// `StringRef`. The previous hardcoded `PairRef(I32)` fallback
/// made `(cons (let ...) nil)` see a pair-of-pair and refuse
/// at compile time.
fn infer_runtime_type_string_is_stringref() {
infer_runtime_type(&Expr::String("hi".into())),
WasmType::StringRef,
/// `WasmLocal(idx, ty)` carries its wasm type directly. Promoting
/// must surface that type unchanged.
fn infer_runtime_type_wasm_local_passes_type_through() {
infer_runtime_type(&Expr::WasmLocal(5, WasmType::Ratio)),
WasmType::Ratio,
infer_runtime_type(&Expr::WasmLocal(7, WasmType::StringRef)),
/// `WasmRuntime(ty)` carries its wasm type directly. Same path
/// as `WasmLocal` from the inference perspective.
fn infer_runtime_type_wasm_runtime_passes_type_through() {
infer_runtime_type(&Expr::WasmRuntime(WasmType::Commodity)),
WasmType::Commodity,