1
//! Tests for `emit_nil_default` (moved out of `atoms.rs` per CLAUDE.md's
2
//! file-size guideline).
3

            
4
use super::*;
5
use crate::ast::{EntityKind, PairElement, WasmType};
6

            
7
/// `emit_nil_default` produces a typed nil for every wasm type `nil_repr`
8
/// classifies as `I32Zero` / `Null`, and errors for every `Never` type
9
/// (ADR-0033: an Index, Scalar, Money or Closure has no valid nil — the
10
/// nil-punning the Common Lisp boolean model retires). Rewritten from the
11
/// pre-ADR-0033 `emit_nil_default_errors_only_for_commodity`, which pinned
12
/// the old (incorrect) "only Commodity errors" behaviour.
13
#[test]
14
1
fn emit_nil_default_errors_for_every_never_type() {
15
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
16
1
    let sig = ctx
17
1
        .intern_closure_signature(&[], WasmType::Ratio)
18
1
        .expect("intern closure sig");
19
1
    let with_nil = [
20
1
        WasmType::Bool,
21
1
        WasmType::StringRef,
22
1
        WasmType::SymbolRef,
23
1
        WasmType::PairRef(PairElement::I32),
24
1
        WasmType::AnyRef,
25
1
        WasmType::EntityRef(EntityKind::Split),
26
1
        WasmType::StructRef(crate::ast::StructId(0)),
27
1
    ];
28
1
    for ty in with_nil {
29
7
        let mut emit = FunctionEmitter::new();
30
7
        emit_nil_default(&ctx, &mut emit, ty)
31
            .unwrap_or_else(|e| panic!("emit_nil_default({ty:?}) should succeed: {e}"));
32
    }
33
1
    let never = [
34
1
        WasmType::I32,
35
1
        WasmType::Ratio,
36
1
        WasmType::Commodity,
37
1
        WasmType::Closure(sig),
38
1
    ];
39
1
    for ty in never {
40
4
        let mut emit = FunctionEmitter::new();
41
4
        let err = emit_nil_default(&ctx, &mut emit, ty)
42
4
            .expect_err(&format!("emit_nil_default({ty:?}) should fail"));
43
4
        assert_eq!(
44
4
            err.to_string(),
45
4
            "Compile error: nil is not a scalar; initialise with 0"
46
        );
47
    }
48
1
}
49

            
50
#[test]
51
1
fn push_symbol_builds_a_symbol_struct_over_its_name() {
52
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
53
1
    let mut emit = FunctionEmitter::new();
54
1
    let ty = push_symbol(&mut ctx, &mut emit, ":K").expect("push_symbol");
55
1
    assert_eq!(ty, WasmType::SymbolRef);
56
1
    emit.finish(&[]).expect("finish");
57
1
}