Lines
100 %
Functions
Branches
//! Tests for `FunctionEmitter::splice` (plan 0011 Phase 0).
use super::*;
#[test]
fn splice_of_a_reachable_scratch_leaves_the_parent_reachable() {
let mut emit = FunctionEmitter::new();
let mut scratch = FunctionEmitter::new();
scratch.i32_const(1);
emit.splice(&scratch).expect("a normal scratch must splice");
assert!(emit.is_reachable());
}
fn splice_of_a_diverging_scratch_marks_the_parent_unreachable() {
scratch.unreachable();
emit.splice(&scratch)
.expect("a diverging scratch must splice");
assert!(!emit.is_reachable());
fn splice_reads_the_scratchs_own_state_not_any_external_belief() {
// No `declared` parameter exists any more: whatever the scratch itself
// simulated is what gets merged, regardless of what a caller might
// otherwise have believed about it (e.g. a syntactic `form_diverges`
// guess that disagrees with the real control flow).
.expect("splice never consults a caller belief");
fn splice_of_an_unbalanced_scratch_is_a_structured_error() {
// The scratch opened a `block` and never closed it — an emitter bug
// splice must catch independent of reachability.
scratch.block_start();
let err = emit
.splice(&scratch)
.expect_err("an unclosed control frame must be rejected");
assert!(matches!(err, crate::error::Error::Compile(_)));
assert!(format!("{err}").contains("unclosed structured-control frame"));
fn splice_of_an_unreachable_scratch_marks_the_parent_unreachable_inside_a_block() {
emit.block_start();
.expect("balanced, diverging scratch splices");
fn splice_through_a_diverging_arm_inside_an_if_stays_reachable_after_end() {
// Mirrors `compile_runtime_if`'s shape: one arm diverges (`br`), the
// other falls through — after `block_end`, the parent must be
// reachable regardless of which arm diverged (see the
// `end_without_incoming_branches_stays_reachable` /
// `br_then_unreachable_then_end_leaves_the_enclosing_frame_reachable`
// pair in `frame_tests.rs`, exercised here through the real emitter).
emit.if_block(BlockType::Empty);
let mut then_scratch = FunctionEmitter::new();
then_scratch.unreachable();
emit.splice(&then_scratch).expect("then diverges");
emit.else_block();
let mut else_scratch = FunctionEmitter::new();
else_scratch.i32_const(0);
emit.splice(&else_scratch).expect("else falls through");
emit.block_end().expect("close the if");
fn splice_of_a_seeded_scratch_that_never_closed_its_opened_frame_is_rejected() {
// `new_seeded` mirrors the seeded depth in both `block_depth` and the
// simulator's own balance bookkeeping; opening one frame inside it and
// never closing it must still be caught.
let mut scratch = FunctionEmitter::new_seeded(1);
.expect_err("an unclosed frame inside a seeded scratch must be rejected");
fn on_live_scratch_splices_and_reports_whether_the_scratch_returns() {
emit.unreachable();
let (value, returns) = emit
.on_live_scratch(|scratch| {
Ok(7)
})
.expect("a live scratch must splice");
assert_eq!(value, 7);
assert!(returns);
let (_, returns) = emit
Ok(())
assert!(!returns);