Lines
98.63 %
Functions
100 %
Branches
//! A LET-bound lambda called with arguments its emitted closure signature
//! cannot take is compiled from its source, so each parameter has the type of
//! the argument (0014 LP1, ISS-209). A call that fits keeps the `call_ref`.
use crate::ast::{ClosureSigId, Expr, LambdaParams, Place, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::error::Result;
use crate::runtime::SymbolTable;
use crate::compiler::join::{NilRepr, nil_repr};
use super::call::{compile_call_ref, compile_lambda_call, compile_lambda_call_for_stack};
use super::fold::probe_value;
use super::operand::always_raises;
use super::stack::classify_stack_type;
use crate::compiler::special::{has_type_evidence, is_capture_free};
/// A call of a closure value: compiled from its source when an argument does
/// not fit the emitted signature, else through `call_ref`.
pub(super) fn compile_closure_call(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
(place, sig): (Place, ClosureSigId),
args: &[Expr],
) -> Result<()> {
if let Some((params, body)) =
respecialised(symbols, place, &ctx.closure_sig(sig)?.params, args)?
{
return symbols.within_shared_recursion(|symbols| {
compile_lambda_call(ctx, emit, symbols, ¶ms, &body, args, None)
});
}
let ty = compile_call_ref(ctx, emit, symbols, place, sig, args)?;
crate::compiler::expr::serialize_stack_to_output(ctx, emit, ty)
/// A call of a closure value for its stack value; see `compile_closure_call`.
pub(super) fn compile_closure_call_for_stack(
) -> Result<WasmType> {
compile_lambda_call_for_stack(ctx, emit, symbols, ¶ms, &body, args, None)
compile_call_ref(ctx, emit, symbols, place, sig, args)
/// The source of the closure held in `place`, iff an argument does not fit the
/// parameter the closure was emitted with.
pub(in crate::compiler) fn respecialised(
symbols: &SymbolTable,
place: Place,
params: &[WasmType],
) -> Result<Option<(LambdaParams, Expr)>> {
let Some(source) = symbols
.closure_body(place)
.filter(|(lambda, body)| is_capture_free(symbols, lambda, body))
else {
return Ok(None);
};
if params.len() != args.len() {
let mut probe = symbols.clone();
for (index, (arg, ¶m)) in args.iter().zip(params).enumerate() {
if !keeps_closure_call(&mut probe, arg, param, || {
has_type_evidence(symbols, &source.0, &source.1, index)
})? {
return Ok(Some(source.clone()));
Ok(None)
/// A nil argument into a never-nil parameter the body typed keeps the closure
/// call, so it reports "nil is not a ..."; a parameter typed only by default
/// is recompiled for the argument like any other.
fn keeps_closure_call(
probe: &mut SymbolTable,
arg: &Expr,
param: WasmType,
evidence: impl FnOnce() -> bool,
) -> Result<bool> {
let value = probe_value(probe, arg)?;
if matches!(value, Expr::Nil) && nil_repr(¶m) == NilRepr::Never && evidence() {
return Ok(true);
call_fits(probe, arg, &value, param)
/// Whether `compile_for_stack_as(arg, param)` accepts the argument: nil, a
/// symbol into a string, an equal stack type.
/// Nil into a never-nil parameter does not fit: `compile_for_stack_as` rejects it.
fn call_fits(probe: &mut SymbolTable, arg: &Expr, value: &Expr, param: WasmType) -> Result<bool> {
let nil_fits = matches!(value, Expr::Nil) && nil_repr(¶m) != NilRepr::Never;
if nil_fits || always_raises(probe, arg)? {
Ok(match classify_stack_type(value) {
Some(WasmType::SymbolRef) => matches!(param, WasmType::SymbolRef | WasmType::StringRef),
Some(ty) => ty == param,
None => true,
})
#[cfg(test)]
#[path = "respecialise_tests.rs"]
mod tests;