Lines
97.44 %
Functions
66.67 %
Branches
100 %
//! Tests for `emit_nil_default` (moved out of `atoms.rs` per CLAUDE.md's
//! file-size guideline).
use super::*;
use crate::ast::{EntityKind, PairElement, WasmType};
/// `emit_nil_default` produces a typed nil for every wasm type `nil_repr`
/// classifies as `I32Zero` / `Null`, and errors for every `Never` type
/// (ADR-0033: an Index, Scalar, Money or Closure has no valid nil — the
/// nil-punning the Common Lisp boolean model retires). Rewritten from the
/// pre-ADR-0033 `emit_nil_default_errors_only_for_commodity`, which pinned
/// the old (incorrect) "only Commodity errors" behaviour.
#[test]
fn emit_nil_default_errors_for_every_never_type() {
let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
let sig = ctx
.intern_closure_signature(&[], WasmType::Ratio)
.expect("intern closure sig");
let with_nil = [
WasmType::Bool,
WasmType::StringRef,
WasmType::SymbolRef,
WasmType::PairRef(PairElement::I32),
WasmType::AnyRef,
WasmType::EntityRef(EntityKind::Split),
WasmType::StructRef(crate::ast::StructId(0)),
];
for ty in with_nil {
let mut emit = FunctionEmitter::new();
emit_nil_default(&ctx, &mut emit, ty)
.unwrap_or_else(|e| panic!("emit_nil_default({ty:?}) should succeed: {e}"));
}
let never = [
WasmType::I32,
WasmType::Ratio,
WasmType::Commodity,
WasmType::Closure(sig),
for ty in never {
let err = emit_nil_default(&ctx, &mut emit, ty)
.expect_err(&format!("emit_nil_default({ty:?}) should fail"));
assert_eq!(
err.to_string(),
"Compile error: nil is not a scalar; initialise with 0"
);
fn push_symbol_builds_a_symbol_struct_over_its_name() {
let ty = push_symbol(&mut ctx, &mut emit, ":K").expect("push_symbol");
assert_eq!(ty, WasmType::SymbolRef);
emit.finish(&[]).expect("finish");