Lines
100 %
Functions
Branches
//! Tests for `FunctionEmitter::block_end` (moved out of `emit.rs` per
//! CLAUDE.md's file-size guideline).
use super::*;
/// Fable review (Phase 3): `block_end` used to hide a mismatched
/// `block_start` / `block_end` pair with `saturating_sub`, silently leaving
/// `block_depth` at 0 instead of surfacing the misuse. It is now a checked
/// subtraction returning a structured `Error::Compile` — the `Result` IS the
/// contract, so no `#[spec(requires: ...)]` shadows it with a panic under
/// `--cfg anodized_panic`.
#[test]
fn block_end_at_depth_zero_is_a_compile_error_not_a_panic() {
let mut emit = FunctionEmitter::new();
assert_eq!(emit.block_depth(), 0);
let err = emit
.block_end()
.expect_err("block_end() with no open block frame must error");
assert!(
err.to_string()
.contains("block_end() called with no open block frame")
);
}
/// A balanced `block_start` / `block_end` pair still succeeds and leaves the
/// depth back at 0.
fn block_end_after_block_start_succeeds() {
emit.block_start();
assert_eq!(emit.block_depth(), 1);
emit.block_end().expect("balanced block_end must succeed");
/// Second review round, item 5: there is no public raw `end()` any more —
/// `finish()` is the only way to close a `FunctionEmitter`'s outermost
/// scope, and it structurally refuses an unclosed frame instead of trusting
/// every call site to check balance first (the class of bug
/// `create_tag.rs`'s stray `emit.end()` produced: valid bytes, but a
/// `block_depth` / simulator desync).
fn finish_with_an_unclosed_block_is_a_structured_error() {
.finish(&[])
.expect_err("finish() with an open block frame must error");
.contains("finish() called with an unclosed structured-control frame")
/// A fully balanced emitter finishes normally.
fn finish_with_a_balanced_emitter_succeeds() {
emit.block_end().expect("close the block");
emit.finish(&[]).expect("balanced emitter must finish");
fn locals_addressed_is_one_past_the_highest_local_named() {
assert_eq!(emit.locals_addressed().ok(), Some(0));
emit.local_get(2);
emit.local_set(5);
emit.local_tee(3);
assert_eq!(emit.locals_addressed().ok(), Some(6));