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, Number, PairElement, WasmType};
use super::infer::infer_runtime_type;
use crate::reader::Reader;
/// `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(Number::Index(0))),
WasmType::I32
);
infer_runtime_type(&Expr::Number(Number::Scalar(Fraction::new(1, 2)))),
WasmType::Ratio
}
fn infer_runtime_type_lambda_is_anyref() {
let lambda = Expr::Lambda(
crate::ast::LambdaParams::simple(vec!["X".to_string()]),
Box::new(Expr::Symbol("X".to_string())),
assert_eq!(infer_runtime_type(&lambda), WasmType::AnyRef);
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::T), 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,
fn dolist_accumulator_type(list: &str) -> Option<WasmType> {
let body = crate::Reader::parse_expr(&format!("(dolist (y {list}) (setf acc (cons y acc)))"))
.expect("parse");
let symbols = crate::runtime::SymbolTable::with_builtins_for_wasm();
super::infer::assigned_var_runtime_type(&[body], "ACC", &Expr::Nil, &symbols).expect("infer")
fn a_dolist_over_a_constant_list_types_the_loop_var_for_the_accumulator() {
dolist_accumulator_type("'(1 2 3)"),
Some(WasmType::PairRef(PairElement::I32))
dolist_accumulator_type("'(1/2 3/4)"),
Some(WasmType::PairRef(PairElement::Ratio))
fn accumulator_type_of(source: &str, runtime_n: bool) -> Option<WasmType> {
accumulator_result_of(source, runtime_n).expect("infer")
fn accumulator_result_of(source: &str, runtime_n: bool) -> crate::error::Result<Option<WasmType>> {
let body = crate::Reader::parse_expr(source).expect("parse");
let mut symbols = crate::runtime::SymbolTable::with_builtins_for_wasm();
if runtime_n {
symbols.define(
crate::runtime::Symbol::new("N", crate::runtime::SymbolKind::Variable)
.with_value(Expr::WasmRuntime(WasmType::I32)),
super::infer::assigned_var_runtime_type(&[body], "ACC", &Expr::Nil, &symbols)
fn an_unrolled_loop_variable_is_bound_as_its_datum() {
let ratio = Some(WasmType::PairRef(PairElement::Ratio));
accumulator_type_of(
"(do ((i 1/1 (+ i 1/1))) ((= i 4/1)) (setf acc (cons (* i 1/2) acc)))",
false
),
ratio
"(dolist (y '(1/1 2/1 3/1)) (setf acc (cons (* y 1/2) acc)))",
fn an_unrolled_index_variable_beside_a_fraction_is_a_mix() {
for source in [
"(do ((i 1 (+ i 1))) ((= i 4)) (setf acc (cons (* i 1/2) acc)))",
"(dolist (y '(1 2 3)) (setf acc (cons (* y 1/2) acc)))",
] {
let err = accumulator_result_of(source, false).expect_err("a mix, CL a list of 1/2 steps");
assert!(
err.to_string()
.contains("* cannot mix an index (count) with a scalar"),
"{source}: {err}"
fn a_runtime_loop_variable_is_bound_as_a_runtime_value() {
"(do ((i 1 (+ i 1))) ((= i n)) (setf acc (cons (* (index->scalar i) 1/2) acc)))",
true
fn a_runtime_index_loop_variable_beside_a_fraction_is_a_mix() {
let err = accumulator_result_of(
"(do ((i 1 (+ i 1))) ((= i n)) (setf acc (cons (* i 1/2) acc)))",
true,
)
.expect_err("a mix");
"{err}"
fn a_list_store_of_the_accumulator_itself_is_a_nested_list() {
accumulator_type_of("(dolist (x (list 1 2)) (setf acc (list acc x)))", false),
Some(WasmType::PairRef(PairElement::AnyRef))
fn an_inner_binding_of_the_target_shadows_the_outer_one() {
let body = crate::Reader::parse_expr("(dolist (x (list 1 2)) (setf acc (list acc x)))")
crate::runtime::Symbol::new("ACC", crate::runtime::SymbolKind::Variable)
.with_value(Expr::Number(Number::Index(5.into()))),
.expect("infer"),
fn only_a_nil_initialised_accumulator_is_seen_as_a_list() {
let infer = |init: &Expr| {
super::infer::assigned_var_runtime_type(std::slice::from_ref(&body), "ACC", init, &symbols)
.expect("infer")
};
infer(&Expr::Nil),
infer(&Expr::Number(Number::Index(0.into()))),
Some(WasmType::I32)
fn differing_stores_widen_the_accumulator_and_agreeing_ones_do_not() {
let any = Some(WasmType::PairRef(PairElement::AnyRef));
"(dolist (y '(1 2 3)) (setf acc (cons y acc)) (setf acc (cons 1/2 acc)))",
any
"(dolist (y '(1 2 3)) (if (> y 2) (setf acc (cons 1/2 acc)) (setf acc (cons y acc))))",
"(dolist (y '(1 2)) (setf acc (cons y acc)) (setf acc (cons y acc)))",
fn spec_name_reads_the_variable_of_every_spec_shape() {
let read = |text: &str| {
let form = Reader::parse(text).expect("parses").exprs.remove(0);
super::spec_name(&form).map(str::to_string)
assert_eq!(read("x").as_deref(), Some("X"));
assert_eq!(read("(x 1)").as_deref(), Some("X"));
assert_eq!(read("((:a x) 1)").as_deref(), Some("X"));
assert_eq!(read("((:a x))").as_deref(), Some("X"));
assert_eq!(read("((a x) 1)"), None);
assert_eq!(read("()"), None);
assert_eq!(read("1"), None);
fn a_nil_binder_stored_a_constant_list_loop_variable_takes_its_element_type() {
let store = |list: &str| {
let body = crate::Reader::parse_expr(&format!("(dolist (x {list}) (setf best x))"))
super::infer::assigned_var_runtime_type(&[body], "BEST", &Expr::Nil, &symbols)
assert_eq!(store("'(3 1 2)"), Some(WasmType::I32));
assert_eq!(store("(list 3 1 2)"), Some(WasmType::I32));
assert_eq!(store("'(3 \"a\")"), Some(WasmType::AnyRef));
fn a_nil_binder_stored_a_compound_form_takes_the_forms_type() {
let store = |rhs: &str| {
let body = crate::Reader::parse_expr(&format!("(dolist (x '(3 1 2)) (setf best {rhs}))"))
assert_eq!(store("(+ x 0)"), Some(WasmType::I32));
assert_eq!(store("(* (index->scalar x) 1/2)"), Some(WasmType::Ratio));
assert_eq!(store("(car (list x))"), Some(WasmType::I32));
assert_eq!(store("nil"), Some(WasmType::Bool));