1
//! Tests for `FunctionEmitter::splice` (plan 0011 Phase 0).
2

            
3
use super::*;
4

            
5
#[test]
6
1
fn splice_of_a_reachable_scratch_leaves_the_parent_reachable() {
7
1
    let mut emit = FunctionEmitter::new();
8
1
    let mut scratch = FunctionEmitter::new();
9
1
    scratch.i32_const(1);
10
1
    emit.splice(&scratch).expect("a normal scratch must splice");
11
1
    assert!(emit.is_reachable());
12
1
}
13

            
14
#[test]
15
1
fn splice_of_a_diverging_scratch_marks_the_parent_unreachable() {
16
1
    let mut emit = FunctionEmitter::new();
17
1
    let mut scratch = FunctionEmitter::new();
18
1
    scratch.unreachable();
19
1
    emit.splice(&scratch)
20
1
        .expect("a diverging scratch must splice");
21
1
    assert!(!emit.is_reachable());
22
1
}
23

            
24
#[test]
25
1
fn splice_reads_the_scratchs_own_state_not_any_external_belief() {
26
    // No `declared` parameter exists any more: whatever the scratch itself
27
    // simulated is what gets merged, regardless of what a caller might
28
    // otherwise have believed about it (e.g. a syntactic `form_diverges`
29
    // guess that disagrees with the real control flow).
30
1
    let mut emit = FunctionEmitter::new();
31
1
    let mut scratch = FunctionEmitter::new();
32
1
    scratch.unreachable();
33
1
    emit.splice(&scratch)
34
1
        .expect("splice never consults a caller belief");
35
1
    assert!(!emit.is_reachable());
36
1
}
37

            
38
#[test]
39
1
fn splice_of_an_unbalanced_scratch_is_a_structured_error() {
40
    // The scratch opened a `block` and never closed it — an emitter bug
41
    // splice must catch independent of reachability.
42
1
    let mut emit = FunctionEmitter::new();
43
1
    let mut scratch = FunctionEmitter::new();
44
1
    scratch.block_start();
45
1
    let err = emit
46
1
        .splice(&scratch)
47
1
        .expect_err("an unclosed control frame must be rejected");
48
1
    assert!(matches!(err, crate::error::Error::Compile(_)));
49
1
    assert!(format!("{err}").contains("unclosed structured-control frame"));
50
1
}
51

            
52
#[test]
53
1
fn splice_of_an_unreachable_scratch_marks_the_parent_unreachable_inside_a_block() {
54
1
    let mut emit = FunctionEmitter::new();
55
1
    emit.block_start();
56
1
    let mut scratch = FunctionEmitter::new();
57
1
    scratch.unreachable();
58
1
    emit.splice(&scratch)
59
1
        .expect("balanced, diverging scratch splices");
60
1
    assert!(!emit.is_reachable());
61
1
}
62

            
63
#[test]
64
1
fn splice_through_a_diverging_arm_inside_an_if_stays_reachable_after_end() {
65
    // Mirrors `compile_runtime_if`'s shape: one arm diverges (`br`), the
66
    // other falls through — after `block_end`, the parent must be
67
    // reachable regardless of which arm diverged (see the
68
    // `end_without_incoming_branches_stays_reachable` /
69
    // `br_then_unreachable_then_end_leaves_the_enclosing_frame_reachable`
70
    // pair in `frame_tests.rs`, exercised here through the real emitter).
71
1
    let mut emit = FunctionEmitter::new();
72
1
    emit.if_block(BlockType::Empty);
73
1
    let mut then_scratch = FunctionEmitter::new();
74
1
    then_scratch.unreachable();
75
1
    emit.splice(&then_scratch).expect("then diverges");
76
1
    emit.else_block();
77
1
    let mut else_scratch = FunctionEmitter::new();
78
1
    else_scratch.i32_const(0);
79
1
    emit.splice(&else_scratch).expect("else falls through");
80
1
    emit.block_end().expect("close the if");
81
1
    assert!(emit.is_reachable());
82
1
}
83

            
84
#[test]
85
1
fn splice_of_a_seeded_scratch_that_never_closed_its_opened_frame_is_rejected() {
86
    // `new_seeded` mirrors the seeded depth in both `block_depth` and the
87
    // simulator's own balance bookkeeping; opening one frame inside it and
88
    // never closing it must still be caught.
89
1
    let mut emit = FunctionEmitter::new();
90
1
    let mut scratch = FunctionEmitter::new_seeded(1);
91
1
    scratch.block_start();
92
1
    let err = emit
93
1
        .splice(&scratch)
94
1
        .expect_err("an unclosed frame inside a seeded scratch must be rejected");
95
1
    assert!(format!("{err}").contains("unclosed structured-control frame"));
96
1
}
97

            
98
#[test]
99
1
fn on_live_scratch_splices_and_reports_whether_the_scratch_returns() {
100
1
    let mut emit = FunctionEmitter::new();
101
1
    emit.unreachable();
102
1
    let (value, returns) = emit
103
1
        .on_live_scratch(|scratch| {
104
1
            scratch.i32_const(1);
105
1
            Ok(7)
106
1
        })
107
1
        .expect("a live scratch must splice");
108
1
    assert_eq!(value, 7);
109
1
    assert!(returns);
110
1
    assert!(!emit.is_reachable());
111
1
    let (_, returns) = emit
112
1
        .on_live_scratch(|scratch| {
113
1
            scratch.unreachable();
114
1
            Ok(())
115
1
        })
116
1
        .expect("a diverging scratch must splice");
117
1
    assert!(!returns);
118
1
}