Lines
98.81 %
Functions
100 %
Branches
//! Tests for `compile_body_into_helper` (moved out of `emit.rs` per
//! CLAUDE.md's file-size guideline).
use super::*;
use crate::compiler::context::CompileContext;
use crate::runtime::SymbolTable;
/// Carried-in fix (ADR-0033 Phase 3): `bind_user_params` failing between
/// `take_local_pool` and `restore_local_pool` used to leak the trial
/// allocation (the `HostAnyref` downcast for a `Closure` param allocates
/// a local via `ctx.alloc_local` BEFORE `emit_anyref_to_typed_downcast`
/// fails on the type), leaving the local pool permanently shifted for
/// every lambda compiled afterward. `compile_body_into_helper` must
/// restore the snapshot on that error path too.
#[test]
fn bind_user_params_failure_restores_the_local_pool_snapshot() {
let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
let idx_before = ctx.alloc_local(WasmType::I32).expect("alloc");
let symbols = SymbolTable::with_builtins_for_wasm();
let unknown = crate::ast::ClosureSigId(99);
let result = compile_body_into_helper(
&mut ctx,
&symbols,
&["x".to_string()],
&[WasmType::Closure(unknown)],
&Expr::Nil,
&[],
CallingConvention::HostAnyref,
);
assert!(
result.is_err(),
"a Closure param of an unknown signature cannot be downcast"
let idx_after = ctx.alloc_local(WasmType::I32).expect("alloc");
assert_eq!(
idx_after,
idx_before + 1,
"the local pool must be restored to its pre-call snapshot after \
bind_user_params fails, not left shifted by the leaked trial local"
}
fn compile_error(source: &str) -> Error {
let program = crate::reader::Reader::parse(source).expect("parses");
let hosts = vec![
crate::host_fn::HostFnSpec::new("rpc-protocol-version", "nomi", "rpc_protocol_version")
.returns(WasmType::I32),
];
let mut symbols = SymbolTable::with_builtins_for_wasm();
symbols.register_host_fns(&hosts);
match crate::compiler::Compiler::with_host_fns(hosts).compile(&program, &mut symbols) {
Err(err) => err,
Ok(_) => panic!("{source} compiles"),
fn mix() -> Error {
compile_error("(+ (rpc-protocol-version) 1/2)")
fn every_dimension_producer_builds_the_dimension_variant() {
for source in [
"(+ (rpc-protocol-version) 1/2)",
"(+ 1/2 (rpc-protocol-version))",
"(the index 1/2)",
"(index->scalar 1/2)",
"(scalar->index (rpc-protocol-version))",
"(* (rpc-protocol-version) 1/2)",
"(let ((a (rpc-protocol-version))) (+ a 1/2))",
] {
let err = compile_error(source);
assert!(matches!(err, Error::Dimension(_)), "{source}: {err:?}");
fn an_error_that_is_no_dimension_mix_stays_a_plain_compile_error() {
for source in ["(list if)", "(funcall #'if 1)"] {
assert!(matches!(err, Error::Compile(_)), "{source}: {err:?}");
fn a_dimension_error_names_the_ambiguous_parameter_the_body_uses() {
let params = LambdaParams::simple(vec!["X".to_string(), "Y".to_string()]);
let body = Expr::List(vec![Expr::symbol("LIST"), Expr::symbol("Y")]);
let err = name_unpinned_param(mix(), (¶ms, &body), &[false, true]);
err.to_string(),
format!(
"{}; cannot infer the type of parameter Y; pin it with THE",
mix()
)
fn a_genuine_mix_names_no_parameter() {
"(defvar *f* (lambda (a s) (list a (+ (the scalar s) (length (list 1 a)))))) 1",
"(defvar *f* (lambda (a b s) (list a (+ b 1 (the scalar s))))) 1",
let message = compile_error(source).to_string();
assert!(message.contains("cannot mix"), "{source}: {message}");
assert!(!message.contains("cannot infer"), "{source}: {message}");
fn a_dimension_error_stays_when_every_used_parameter_is_pinned_or_unused() {
let body = Expr::List(vec![Expr::symbol("+"), Expr::symbol("X"), Expr::Nil]);
name_unpinned_param(mix(), (¶ms, &body), &[false, true]).to_string(),
mix().to_string()
fn an_error_of_another_family_is_not_blamed_on_a_parameter() {
let params = LambdaParams::simple(vec!["X".to_string()]);
let other = Error::Compile("boom".to_string());
let err = name_unpinned_param(other, (¶ms, &Expr::symbol("X")), &[true]);
assert_eq!(err.to_string(), "Compile error: boom");