Lines
100 %
Functions
Branches
use crate::ast::Fraction;
use crate::ast::{Expr, Number, PairElement, WasmType};
use super::infer::{chain_pair_element, infer_pair_element, literal_pair_element};
fn int(n: i64) -> Expr {
Expr::Number(Number::Index(n))
}
#[test]
fn chain_of_integer_literals_is_an_index_chain() {
assert_eq!(
chain_pair_element(&[int(1), int(2)]).unwrap(),
PairElement::I32
);
fn chain_integer_literals_cross_to_a_ratio_neighbor() {
let half = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
chain_pair_element(&[half, int(3)]).unwrap(),
PairElement::Ratio
chain_pair_element(&[Expr::WasmRuntime(WasmType::Ratio), int(3)]).unwrap(),
fn chain_integer_literals_at_the_index_bounds_are_an_index_chain() {
chain_pair_element(&[int((1 << 30) - 1), int(-(1 << 30))]).unwrap(),
fn chain_of_bool_and_nil_uses_bool() {
chain_pair_element(&[Expr::T, Expr::Nil]).unwrap(),
PairElement::Bool
fn chain_integer_literal_adopts_runtime_index_on_either_side() {
let index = Expr::WasmRuntime(WasmType::I32);
chain_pair_element(&[index.clone(), int(2)]).unwrap(),
chain_pair_element(&[int(2), index]).unwrap(),
fn chain_literal_tail_disagreeing_with_runtime_head_widens_to_anyref() {
let scalar = Expr::WasmRuntime(WasmType::Ratio);
chain_pair_element(&[scalar, Expr::String("a".to_string())]).unwrap(),
PairElement::AnyRef
fn chain_integer_literal_against_non_numeric_slot_widens_to_anyref() {
chain_pair_element(&[int(1), Expr::Nil]).unwrap(),
chain_pair_element(&[Expr::String("a".to_string()), int(1)]).unwrap(),
fn chain_fractional_literal_against_runtime_index_widens_to_anyref() {
chain_pair_element(&[Expr::WasmRuntime(WasmType::I32), half]).unwrap(),
fn chain_of_nothing_uses_anyref_and_of_symbols_the_symbol_slot() {
let symbol = |name: &str| Expr::Symbol(name.to_string());
assert_eq!(chain_pair_element(&[]).unwrap(), PairElement::AnyRef);
chain_pair_element(&[symbol("A"), Expr::Keyword("K".to_string())]).unwrap(),
PairElement::Symbol
chain_pair_element(&[symbol("A"), Expr::String("s".to_string())]).unwrap(),
fn a_runtime_nested_list_rides_an_anyref_cell() {
let nested = Expr::WasmRuntime(WasmType::PairRef(PairElement::I32));
chain_pair_element(&[nested.clone(), int(1)]).unwrap(),
infer_pair_element(&nested, &Expr::Nil).unwrap(),
fn chain_of_a_runtime_closure_rides_its_own_cell() {
let sig = crate::ast::ClosureSigId(0);
let closure = Expr::WasmRuntime(WasmType::Closure(sig));
chain_pair_element(&[closure.clone(), closure]).unwrap(),
PairElement::Closure(sig)
/// `infer_pair_element` decides the static PairElement from a
/// `(cons car cdr)` form, balancing the car's compile-time type
/// against the cdr's. The legacy integration test reached this
/// via `Symbol::new("BAL").with_value(Expr::WasmRuntime(Commodity))`
/// in the symbol table; the unit form exercises the function
/// directly so the contract is locked in without needing a
/// script-mode Commodity producer.
fn commodity_car_with_nil_cdr_is_commodity_pair() {
let car = Expr::WasmRuntime(WasmType::Commodity);
let elem = infer_pair_element(&car, &Expr::Nil).unwrap();
assert_eq!(elem, PairElement::Commodity);
fn commodity_car_with_pair_of_i32_cdr_widens_to_anyref() {
// ADR-0025 escape hatch: when the car and cdr disagree on element
// type, CONS widens to `PairElement::AnyRef` (canonical Common Lisp
// heterogeneous `cons`) rather than rejecting. The strict typed path
// still applies when both arms agree.
let cdr = Expr::WasmRuntime(WasmType::PairRef(PairElement::I32));
let elem = infer_pair_element(&car, &cdr).expect("heterogeneous CONS widens to AnyRef");
assert_eq!(elem, PairElement::AnyRef);
fn ratio_car_with_pair_of_ratio_cdr_resolves_to_ratio_pair() {
let car = Expr::WasmRuntime(WasmType::Ratio);
let cdr = Expr::WasmRuntime(WasmType::PairRef(PairElement::Ratio));
let elem = infer_pair_element(&car, &cdr).unwrap();
assert_eq!(elem, PairElement::Ratio);
fn integer_number_car_with_nil_cdr_is_an_index_cell() {
let elem = infer_pair_element(&int(1), &Expr::Nil).unwrap();
assert_eq!(elem, PairElement::I32);
fn integer_number_car_adopts_a_ratio_cdr() {
infer_pair_element(&int(1), &cdr).unwrap(),
fn nil_car_takes_the_bool_slot_and_widens() {
let index_cdr = Expr::WasmRuntime(WasmType::PairRef(PairElement::I32));
infer_pair_element(&Expr::Nil, &index_cdr).unwrap(),
let bool_cdr = Expr::WasmRuntime(WasmType::PairRef(PairElement::Bool));
infer_pair_element(&Expr::Nil, &bool_cdr).unwrap(),
fn runtime_car_unifies_with_quoted_list_cdr_members() {
let quoted = |members: Vec<Expr>| Expr::Datum(Box::new(Expr::List(members)));
infer_pair_element(&index, "ed(vec![int(1), int(2)])).unwrap(),
infer_pair_element(&index, "ed(vec![Expr::Nil])).unwrap(),
fn constant_car_without_slot_adopts_typed_cdr_or_is_rejected() {
let list = Expr::Datum(Box::new(Expr::List(vec![int(1)])));
let cdr = Expr::WasmRuntime(WasmType::PairRef(PairElement::StringRef));
infer_pair_element(&list, &cdr).unwrap(),
PairElement::StringRef
assert!(infer_pair_element(&list, &Expr::Nil).is_err());
fn a_symbol_car_rides_the_symbol_slot_and_widens_beside_a_string() {
let symbol = Expr::Datum(Box::new(Expr::Symbol("A".to_string())));
let strings = Expr::WasmRuntime(WasmType::PairRef(PairElement::StringRef));
let symbols = Expr::WasmRuntime(WasmType::PairRef(PairElement::Symbol));
infer_pair_element(&symbol, &Expr::Nil).unwrap(),
infer_pair_element(&symbol, &symbols).unwrap(),
infer_pair_element(&symbol, &strings).unwrap(),
fn literal_pair_element_of_a_symbol_or_keyword_is_the_symbol_slot() {
for datum in [
Expr::Symbol("A".to_string()),
Expr::Keyword("K".to_string()),
Expr::Quote(Box::new(Expr::Symbol("A".to_string()))),
Expr::Quote(Box::new(Expr::Keyword("K".to_string()))),
Expr::Datum(Box::new(Expr::Symbol("A".to_string()))),
Expr::Datum(Box::new(Expr::Keyword("K".to_string()))),
] {
literal_pair_element(&datum),
Some(PairElement::Symbol),
"{datum:?}"
fn fractional_number_car_with_nil_cdr_uses_ratio_default() {
let car = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
fn bool_car_with_nil_cdr_uses_bool_slot() {
// A bool literal car rides PairElement::Bool (not I32), so CAR recovers a
// truth value that serializes as Nil/Bool rather than Number.
let car = Expr::T;
assert_eq!(elem, PairElement::Bool);
fn string_car_with_nil_cdr_uses_stringref() {
let car = Expr::String("hi".to_string());
assert_eq!(elem, PairElement::StringRef);
fn anyref_cdr_propagates_anyref_pair_element() {
let cdr = Expr::WasmRuntime(WasmType::PairRef(PairElement::AnyRef));
let elem = infer_pair_element(&car, &cdr).expect("AnyRef cdr accepts any car");
fn ratio_car_with_pair_of_string_cdr_widens_to_anyref() {
fn anyref_widen_is_symmetric_and_idempotent() {
PairElement::I32.widen(PairElement::Ratio),
PairElement::Ratio.widen(PairElement::I32),
PairElement::AnyRef.widen(PairElement::I32),
assert_eq!(PairElement::I32.widen(PairElement::I32), PairElement::I32);
fn literal_string_car_with_pair_of_ratio_cdr_widens_to_anyref() {
// Regression: before the literal-shape branch ran ahead of the
// cdr-element fallback, a literal String car with a typed Ratio
// cdr was silently classified as Ratio and erroried at codegen
// ("expected ratio, got string"). The widening contract says a
// disagreeing arm widens to AnyRef instead.
let car = Expr::String("label".to_string());
let elem = infer_pair_element(&car, &cdr).expect("literal car widens against typed cdr");
fn pair_element_anyref_round_trips_through_wasm_type() {
assert_eq!(PairElement::AnyRef.as_wasm_type(), WasmType::AnyRef);
PairElement::from_wasm_type(WasmType::AnyRef),
Some(PairElement::AnyRef)
fn cons_charges_the_datum_it_builds_to_the_work_budget() {
let mut symbols = crate::runtime::SymbolTable::new();
let mut list = Expr::Nil;
for _ in 0..50 {
list = super::cons::cons(&mut symbols, &[int(1), list]).unwrap();
assert!(symbols.measurements().charged() >= 50 * 51 / 2);
fn append_charges_the_datum_it_builds_to_the_work_budget() {
let items = Expr::quoted_list((0..40).map(int).collect());
let before = symbols.measurements().charged();
super::append::append(&mut symbols, &[items.clone(), items]).unwrap();
assert!(symbols.measurements().charged() - before >= 80);
fn a_list_growing_cons_loop_exhausts_the_work_budget() {
let mut steps = 0_u64;
let outcome = loop {
assert!(steps < 2_000, "no work-budget error after {steps} conses");
match super::cons::cons(&mut symbols, &[int(1), list.clone()]) {
Ok(next) => list = next,
Err(err) => break err,
steps += 1;
};
assert!(matches!(outcome, crate::error::Error::Resource(_)));
fn a_chain_with_an_integer_literal_beyond_the_index_range_is_refused() {
for literal in [1 << 30, i64::from(i32::MAX), -(1 << 30) - 1] {
let message = chain_pair_element(&[int(1), int(literal)])
.expect_err("no index holds it")
.to_string();
assert!(message.contains("outside the index range"), "{message}");
assert!(infer_pair_element(&int(literal), &Expr::Nil).is_err());
assert!(
infer_pair_element(
&int(1),
&Expr::Datum(Box::new(Expr::List(vec![int(literal)])))
)
.is_err()
fn a_map_function_refusing_the_element_dimension_is_an_error_not_a_guess() {
use super::map_runtime::predict_map_result_element;
let symbols = crate::runtime::SymbolTable::with_builtins();
let function = crate::reader::Reader::parse_expr("(lambda (z) (index->scalar z))")
.expect("function parses");
let refused = predict_map_result_element(&symbols, &function, PairElement::Ratio);
matches!(refused, Err(crate::error::Error::Dimension(_))),
"{refused:?}"
let accepted = predict_map_result_element(&symbols, &function, PairElement::I32);
assert_eq!(accepted.expect("an index element fits"), PairElement::Ratio);