Lines
100 %
Functions
Branches
//! End-reachability (`is_live`) edge rules of `FunctionEmitter` (plan 0014 P4).
use wasm_encoder::{BlockType, Catch};
use super::FunctionEmitter;
const EMPTY: BlockType = BlockType::Empty;
fn closed(emit: &mut FunctionEmitter) -> bool {
emit.block_end().expect("matching end");
emit.frame.is_live()
}
#[test]
fn br_marks_its_target_and_kills_the_rest() {
let mut emit = FunctionEmitter::new();
emit.block_start();
emit.br(1);
assert!(!emit.frame.is_live());
assert!(!closed(&mut emit), "inner end is not targeted");
assert!(closed(&mut emit), "outer end is the br target");
fn br_if_marks_its_target_and_stays_live() {
emit.br_if(0);
assert!(emit.frame.is_live());
emit.unreachable();
assert!(closed(&mut emit));
fn br_table_marks_every_target_and_the_default() {
emit.br_table(&[1, 2], 3);
assert!(!closed(&mut emit), "depth 0 is not a target");
assert!(closed(&mut emit), "depth 1 is the first listed target");
assert!(closed(&mut emit), "depth 2 is the second listed target");
assert!(closed(&mut emit), "depth 3 is the default");
fn throw_kills_live_and_marks_nothing() {
emit.throw(0);
assert!(!closed(&mut emit));
fn unreachable_kills_live_and_marks_nothing() {
fn an_edge_emitted_while_dead_marks_nothing() {
emit.try_table(EMPTY, &[]);
assert!(!closed(&mut emit), "sealed try_table is not end-reachable");
emit.br_if(1);
emit.br_table(&[1], 1);
assert!(!closed(&mut emit), "dead branches target nothing");
assert!(!closed(&mut emit), "the outer end stays unreachable");
fn block_and_loop_inherit_live_when_opened() {
emit.loop_start();
fn block_is_end_reachable_by_fallthrough() {
emit.i32_const(1);
fn block_is_end_reachable_by_a_branch_alone() {
emit.block_start_typed(EMPTY);
emit.br(0);
fn a_branch_to_a_loop_does_not_make_its_end_reachable() {
fn if_with_else_is_reachable_when_the_then_arm_falls_through() {
emit.if_block(EMPTY);
emit.else_block();
assert!(emit.frame.is_live(), "else restarts at the entry live");
fn else_restarts_live_after_a_diverging_then_arm() {
assert!(closed(&mut emit), "the else arm falls through");
fn if_with_else_is_dead_when_both_arms_diverge() {
fn if_without_else_is_reachable_through_the_implicit_false_path() {
fn an_if_opened_dead_stays_dead() {
fn try_table_marks_its_catch_labels_outside_itself() {
let mut live = FunctionEmitter::new();
live.block_start();
live.try_table(EMPTY, &[Catch::All { label: 0 }]);
live.throw(0);
assert!(!closed(&mut live));
assert!(closed(&mut live), "the catch label names the block's end");
let mut dead = FunctionEmitter::new();
dead.block_start();
dead.unreachable();
dead.try_table(EMPTY, &[Catch::All { label: 0 }]);
dead.block_end().expect("end try_table");
assert!(!closed(&mut dead));
fn try_table_marks_every_catch_label() {
emit.try_table(
EMPTY,
&[Catch::One { tag: 0, label: 1 }, Catch::All { label: 2 }],
);
assert!(!closed(&mut emit), "the try_table itself");
assert!(!closed(&mut emit), "label 0 is not a catch target");
assert!(closed(&mut emit), "label 1 is the first catch");
assert!(closed(&mut emit), "label 2 is the second catch");
fn try_table_whose_body_throws_is_not_end_reachable() {
fn live_after_end_is_the_closed_frames_answer_not_reset() {
assert!(!emit.is_live(), "semantic state is the closed frame's");
assert!(!emit.is_reachable(), "the dead fallthrough is sealed");
fn splicing_a_dead_scratch_makes_the_parent_dead() {
let mut scratch = FunctionEmitter::new();
scratch.block_start();
scratch.unreachable();
scratch.block_end().expect("matching end");
assert!(!scratch.frame.is_live());
emit.splice(&scratch).expect("balanced scratch");
assert!(!emit.is_reachable(), "the scratch's seal carries over");
fn splicing_a_live_scratch_keeps_a_dead_parent_dead() {
let scratch = FunctionEmitter::new();
assert!(scratch.frame.is_live());
fn live_never_outlives_reachability() {
let check = |emit: &FunctionEmitter| {
assert!(emit.is_reachable() || !emit.frame.is_live());
};
check(&emit);
emit.block_end().expect("end if");
emit.block_end().expect("end loop");
emit.block_end().expect("end block");