Lines
95.71 %
Functions
100 %
Branches
//! An observation must mean the same thing after the pass that made it is
//! rolled back, whatever scope encloses that pass.
use super::{Leaf, Observations, ObservedBlock, StructuralType};
use crate::ast::{PairElement, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::recompile::Trial;
use crate::compiler::recompile::recompile;
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
fn setup() -> (CompileContext, SymbolTable) {
let ctx = CompileContext::new_with_host_fns(&[]).expect("context");
let symbols = SymbolTable::with_builtins_for_wasm();
(ctx, symbols)
}
/// `ratio -> (ratio -> pair<i32>)`
fn expected() -> StructuralType {
StructuralType {
arrows: vec![vec![Leaf::Ratio], vec![Leaf::Ratio]],
result: Leaf::PairRef(PairElement::I32),
fn nested_block(join: StructuralType) -> ObservedBlock {
ObservedBlock {
name: "INNER".to_string(),
body_len: 2,
join,
/// Mints the closure type and a local holding it, as a pass binding one
/// would, and observes it, both as the result and as a nested block's join.
fn observe(ctx: &mut CompileContext) -> Result<Observations> {
let inner =
ctx.intern_closure_signature(&[WasmType::Ratio], WasmType::PairRef(PairElement::I32))?;
let outer = ctx.intern_closure_signature(&[WasmType::Ratio], WasmType::Closure(inner))?;
ctx.alloc_local(WasmType::Closure(outer))?;
let result = StructuralType::of(WasmType::Closure(outer), ctx)?;
let start = ctx.block_join_start();
ctx.record_block_join(start, nested_block(result.clone()));
let blocks = ctx.take_block_joins(start);
Ok(Observations { result, blocks })
fn discarded_pass(ctx: &mut CompileContext, symbols: &mut SymbolTable) -> Result<Observations> {
match recompile(ctx, symbols, |ctx, _| {
Ok(Trial::<(), _>::Discard(observe(ctx)?))
})? {
Trial::Discard(observations) => Ok(observations),
Trial::Commit(()) => panic!("the pass discards"),
fn in_committed_pass(ctx: &mut CompileContext, symbols: &mut SymbolTable) -> Result<Observations> {
match recompile(ctx, symbols, |ctx, symbols| {
Ok(Trial::<Observations, Observations>::Commit(discarded_pass(
ctx, symbols,
)?))
Trial::Commit(observations) => Ok(observations),
Trial::Discard(_) => panic!("the pass commits"),
/// Signature ids and local indices a context has already handed out.
fn shift_ids(ctx: &mut CompileContext) {
ctx.intern_closure_signature(&[WasmType::I32], WasmType::I32)
.expect("intern");
ctx.intern_closure_signature(&[], WasmType::StringRef)
for _ in 0..3 {
ctx.alloc_local(WasmType::I32).expect("local");
type Scope = fn(&mut CompileContext, &mut SymbolTable) -> Result<Observations>;
#[test]
fn observations_hold_no_local_index_or_signature_id_under_any_scope() {
let scopes: [Scope; 2] = [discarded_pass, in_committed_pass];
for scope in scopes {
for shifted in [false, true] {
let (mut ctx, mut symbols) = setup();
if shifted {
shift_ids(&mut ctx);
let observations = scope(&mut ctx, &mut symbols).expect("scope");
assert_eq!(observations.result, expected(), "shifted: {shifted}");
assert_eq!(
observations.blocks,
vec![nested_block(expected())],
"shifted: {shifted}"
);
fn an_observed_closure_reinterns_to_its_signature_after_the_rollback() {
let observations = discarded_pass(&mut ctx, &mut symbols).expect("pass");
let ty = observations.result.intern(&mut ctx).expect("intern");
let WasmType::Closure(outer) = ty else {
panic!("a closure observation interns to a closure: {ty:?}");
};
let outer = ctx.closure_sig(outer).unwrap();
assert_eq!(outer.params, vec![WasmType::Ratio]);
let WasmType::Closure(inner) = outer.result else {
panic!("the outer closure returns a closure: {:?}", outer.result);
ctx.closure_sig(inner).unwrap().params,
vec![WasmType::Ratio]
ctx.closure_sig(inner).unwrap().result,
WasmType::PairRef(PairElement::I32)
assert_eq!(StructuralType::of(ty, &ctx).expect("of"), expected());
fn a_nested_block_join_reinterns_after_the_rollback_under_a_shifted_registry() {
let [block] = observations.blocks.as_slice() else {
panic!("one nested block: {:?}", observations.blocks);
let ty = block.join.intern(&mut ctx).expect("intern");
let WasmType::Closure(sig) = ty else {
panic!("a closure join interns to a closure: {ty:?}");
assert_eq!(ctx.closure_sig(sig).unwrap().params, vec![WasmType::Ratio]);
const CHAIN_DEPTH: usize = 12;
fn chain_params(depth: usize) -> Vec<Leaf> {
let leaves = [
Leaf::Ratio,
Leaf::StringRef,
Leaf::PairRef(PairElement::I32),
];
leaves[..depth % leaves.len()].to_vec()
/// A curried chain whose `depth`-th link takes `chain_params(depth)`.
fn chain() -> StructuralType {
arrows: (0..CHAIN_DEPTH).map(chain_params).collect(),
result: Leaf::Bool,
fn observe_chain(ctx: &mut CompileContext) -> Result<Observations> {
let mut ty = WasmType::Bool;
for depth in (0..CHAIN_DEPTH).rev() {
let params: Vec<WasmType> = chain_params(depth).into_iter().map(Into::into).collect();
ty = WasmType::Closure(ctx.intern_closure_signature(¶ms, ty)?);
ctx.alloc_local(ty)?;
let result = StructuralType::of(ty, ctx)?;
Ok(Observations {
result,
blocks: Vec::new(),
})
fn a_deep_closure_chain_round_trips_across_the_rollback() {
let observations = match recompile(&mut ctx, &mut symbols, |ctx, _| {
Ok(Trial::<(), _>::Discard(observe_chain(ctx)?))
.expect("pass")
{
Trial::Discard(observations) => observations,
assert_eq!(observations.result, chain());
assert_eq!(StructuralType::of(ty, &ctx).expect("of"), chain());
fn a_closure_typed_parameter_is_refused() {
let (mut ctx, _) = setup();
let inner = ctx
.intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
let outer = ctx
.intern_closure_signature(&[WasmType::Closure(inner)], WasmType::Ratio)
let refused = StructuralType::of(WasmType::Closure(outer), &ctx);
assert!(
matches!(refused, Err(Error::Compile(ref msg)) if msg.contains("closure-typed")),
"{refused:?}"
fn every_plain_type_round_trips() {
let plain = [
WasmType::I32,
WasmType::Bool,
WasmType::Ratio,
WasmType::StringRef,
WasmType::Commodity,
WasmType::PairRef(PairElement::AnyRef),
WasmType::AnyRef,
WasmType::EntityRef(crate::ast::EntityKind::Account),
for ty in plain {
let structural = StructuralType::of(ty, &ctx).expect("of");
assert_eq!(structural.intern(&mut ctx).expect("intern"), ty);