Lines
100 %
Functions
Branches
use super::{
Arm, NEVER_RETURNS, NilRepr, literal_fits, nil_repr, reconcile_arms, reconcile_arms_expecting,
rides,
};
use crate::ast::{PairElement, StructId, WasmType};
#[test]
fn a_literal_adopts_a_scalar_value() {
let arms = [
Arm::Value(WasmType::Ratio),
Arm::NumberLiteral(WasmType::I32),
];
assert_eq!(reconcile_arms(&arms), WasmType::Ratio);
}
fn a_fractional_literal_beside_an_index_value_widens() {
Arm::Value(WasmType::I32),
Arm::NumberLiteral(WasmType::Ratio),
assert_eq!(reconcile_arms(&arms), WasmType::AnyRef);
fn a_nil_beside_an_index_widens() {
assert_eq!(
reconcile_arms(&[Arm::Value(WasmType::I32), Arm::Nil]),
WasmType::AnyRef
);
reconcile_arms(&[Arm::NumberLiteral(WasmType::I32), Arm::Nil]),
fn a_nil_beside_a_list_is_the_null_list() {
let pair = WasmType::PairRef(PairElement::I32);
assert_eq!(reconcile_arms(&[Arm::Value(pair), Arm::Nil]), pair);
reconcile_arms(&[Arm::Nil, Arm::ConstantList(PairElement::I32)]),
pair
fn pair_elements_widen_and_other_types_box_to_anyref() {
let pairs = [
Arm::ConstantList(PairElement::I32),
Arm::Value(WasmType::PairRef(PairElement::StringRef)),
reconcile_arms(&pairs),
WasmType::PairRef(PairElement::AnyRef)
reconcile_arms(&[Arm::Value(WasmType::I32), Arm::Value(WasmType::StringRef)]),
reconcile_arms(&[
Arm::Value(WasmType::StringRef),
Arm::NumberLiteral(WasmType::I32)
]),
fn no_reachable_arm_never_returns_and_a_lone_nil_is_bool() {
assert_eq!(reconcile_arms(&[]), NEVER_RETURNS);
assert_eq!(reconcile_arms(&[Arm::Nil]), WasmType::Bool);
fn only_an_i32_repr_value_needs_a_box_to_stand_as_anyref() {
assert!(rides(WasmType::Ratio, WasmType::Ratio));
assert!(rides(pair, WasmType::PairRef(PairElement::AnyRef)));
assert!(rides(WasmType::StringRef, WasmType::AnyRef));
assert!(rides(WasmType::Ratio, WasmType::AnyRef));
assert!(!rides(WasmType::I32, WasmType::AnyRef));
assert!(!rides(WasmType::Bool, WasmType::AnyRef));
assert!(!rides(WasmType::Bool, pair));
assert!(!rides(WasmType::I32, WasmType::Ratio));
assert!(!rides(WasmType::StringRef, pair));
fn a_structure_is_nullable_and_widens_against_anything_else() {
let (pt, other) = (
WasmType::StructRef(StructId(0)),
WasmType::StructRef(StructId(1)),
assert_eq!(nil_repr(&pt), NilRepr::Null);
assert_eq!(reconcile_arms(&[Arm::Value(pt), Arm::Nil]), pt);
assert_eq!(reconcile_arms(&[Arm::Value(pt), Arm::Value(pt)]), pt);
reconcile_arms(&[Arm::Value(pt), Arm::Value(other)]),
reconcile_arms(&[Arm::Value(pt), Arm::Value(WasmType::I32)]),
assert!(rides(pt, WasmType::AnyRef));
assert!(!rides(pt, other));
fn nil_repr_and_literal_fitting() {
assert_eq!(nil_repr(&WasmType::StringRef), NilRepr::Null);
assert_eq!(nil_repr(&WasmType::Bool), NilRepr::I32Zero);
assert_eq!(nil_repr(&WasmType::I32), NilRepr::Never);
assert!(literal_fits(WasmType::Ratio, WasmType::I32));
assert!(!literal_fits(WasmType::I32, WasmType::Ratio));
assert!(!literal_fits(WasmType::Commodity, WasmType::I32));
fn arms_order_canonically() {
let mut arms = [
Arm::Nil,
arms.sort();
assert_eq!(arms[0], Arm::Value(WasmType::I32));
assert_eq!(arms[2], Arm::Nil);
fn literal_arms_adopt_an_expected_type_they_fit() {
let literals = [
reconcile_arms_expecting(&literals, Some(WasmType::Ratio)),
WasmType::Ratio
assert_eq!(reconcile_arms_expecting(&literals, None), WasmType::I32);
reconcile_arms_expecting(&[], Some(WasmType::Ratio)),
reconcile_arms_expecting(&[Arm::NumberLiteral(WasmType::Ratio)], Some(WasmType::I32)),
fn a_computed_or_nil_arm_keeps_its_own_join_under_an_expected_type() {
let expected = Some(WasmType::Ratio);
let computed = [Arm::Value(WasmType::I32), Arm::NumberLiteral(WasmType::I32)];
assert_eq!(reconcile_arms_expecting(&computed, expected), WasmType::I32);
let nil = [Arm::NumberLiteral(WasmType::I32), Arm::Nil];
assert_eq!(reconcile_arms_expecting(&nil, expected), WasmType::AnyRef);