1
//! Regression rows for plan 0011 Phase 0's `FunctionEmitter::splice`
2
//! (Fable review, `doc/ops/0011-typed-elaboration-ir.org` Phase 0): before
3
//! the fix, `splice_checked`'s `declared` argument at several call sites
4
//! compared a syntactic CFG-divergence belief (`compile_arm`'s
5
//! `form_diverges`, which doesn't see through a nested AND/OR/GO hand-off)
6
//! against the simulator's own VALIDATOR-reachability notion — a different
7
//! thing — and failed these forms with an internal "declared … but it
8
//! simulated as …" compile error even though they compile at HEAD (the
9
//! legacy pipeline, with no splice check at all). `splice` now merges the
10
//! scratch's own simulated reachability unconditionally, with no declared
11
//! belief to disagree with, so all of these must compile and run exactly as
12
//! they do without Phase 0 in the picture. `v` stands for
13
//! `(rpc-protocol-version)`, which is 1, so `(> v 5)` is always false here.
14
//!
15
//! A second review round found the two-signal `value_compiled &&
16
//! is_reachable()` gate this file's first pass landed with was itself
17
//! incomplete in two ways (both now fixed at the root, in
18
//! `arms.rs::compile_arm`, by sealing every diverging arm with
19
//! `scratch.unreachable()` right after compiling it — matching the seal
20
//! `compile_handler_case_for_stack`'s body-diverges path and
21
//! `unwind_protect.rs` already used):
22
//! - a leading clause form that diverges only in the VALIDATOR's eyes (GO
23
//!   again, but this time in LEADING position rather than value position)
24
//!   still had its value form compiled into dead code, stranding a value;
25
//! - the two-signal gate assumed a diverging arm's residual nominal type
26
//!   was always `i32` (true for every row the first pass covered), so it
27
//!   silently depended on handler-case's own result type happening to be
28
//!   `i32` too — false once the result type is e.g. `StringRef`.
29

            
30
use super::*;
31

            
32
#[tokio::test]
33
1
async fn handler_case_clause_body_if_arms_both_raising_is_not_an_internal_error() {
34
1
    let form = "(handler-case (error 'x \"m\") (x (e) (if (> (rpc-protocol-version) 5) (error 'a \"a\") (error 'b \"b\"))))";
35
1
    assert_error_code(form, "B").await;
36
1
}
37

            
38
#[tokio::test]
39
1
async fn handler_case_t_clause_body_if_arms_both_raising_is_not_an_internal_error() {
40
1
    let form = "(handler-case (error 'x \"m\") (t (e) (if (> (rpc-protocol-version) 5) (error 'a \"a\") (error 'b \"b\"))))";
41
1
    assert_error_code(form, "B").await;
42
1
}
43

            
44
#[tokio::test]
45
1
async fn handler_case_clause_body_unwind_protect_around_error_is_not_an_internal_error() {
46
1
    let form = "(handler-case (error 'x \"m\") (x (e) (unwind-protect (error 'a \"a\") (+ 1 2))))";
47
1
    assert_error_code(form, "A").await;
48
1
}
49

            
50
#[tokio::test]
51
1
async fn handler_case_clause_body_if_arms_both_returning_from_block_is_not_an_internal_error() {
52
1
    let form = "(block b (handler-case (error 'x \"m\") (x (e) (if (> (rpc-protocol-version) 5) (return-from b 1) (return-from b 2)))))";
53
1
    assert_values(&[(form, "2")]).await;
54
1
}
55

            
56
#[tokio::test]
57
1
async fn handler_case_clause_body_if_one_arm_returns_from_block_other_raises() {
58
1
    let form = "(block b (handler-case (error 'x \"m\") (x (e) (if (> (rpc-protocol-version) 5) (return-from b 1) (error 'q \"q\")))))";
59
1
    assert_error_code(form, "Q").await;
60
1
}
61

            
62
#[tokio::test]
63
1
async fn handler_case_clause_leading_if_form_both_arms_raising_then_a_value_form() {
64
1
    let form = "(handler-case (error 'x \"m\") (x (e) (if (> (rpc-protocol-version) 5) (error 'a \"a\") (error 'b \"b\")) 5))";
65
1
    assert_error_code(form, "B").await;
66
1
}
67

            
68
#[tokio::test]
69
1
async fn handler_case_clause_body_go_back_into_the_enclosing_tagbody_is_not_an_internal_error() {
70
    // The opposite direction: `form_diverges` doesn't classify GO at all, so
71
    // the clause was declared "reachable" while the compiled GO actually
72
    // ends the scratch unreachable — the mirror-image mismatch of the other
73
    // rows here.
74
1
    let form = "(tagbody start (handler-case (error 'x \"m\") (x (e) (go done))) done)";
75
1
    let response = eval_value_of(form).await;
76
1
    assert!(
77
1
        !response.contains("internal:"),
78
1
        "must not be an internal splice-check error, got: {response}"
79
1
    );
80
1
}
81

            
82
#[tokio::test]
83
1
async fn handler_case_clause_leading_go_then_a_value_form_is_not_an_internal_error() {
84
    // Second review round, finding 1: GO in LEADING position (not value
85
    // position) — `compile_clause_body` must stop compiling the clause
86
    // right after the GO, reading `emit.is_reachable()`, never reaching the
87
    // trailing `5` at all.
88
1
    let form = "(tagbody start (handler-case (error 'x \"m\") (x (e) (go done) 5)) done)";
89
1
    let response = eval_value_of(form).await;
90
1
    assert!(
91
1
        !response.contains("internal:"),
92
1
        "must not be an internal splice-check error, got: {response}"
93
1
    );
94
1
}
95

            
96
#[tokio::test]
97
1
async fn handler_case_non_i32_result_with_both_arms_returning_from_block() {
98
    // Second review round, finding 2: the diverging clause's residual
99
    // nominal type is `i32` (an empty-arms `reconcile_arms` default), which
100
    // must never leak into a handler-case whose OWN result type is
101
    // something else — here `StringRef`. The body never actually raises,
102
    // so the clause is dead at runtime; it still has to compile correctly
103
    // and stay `StringRef`-typed at $outer with no stray i32 on the stack.
104
1
    let form = "(block b (handler-case \"s\" (x (e) (if (> (rpc-protocol-version) 5) (return-from b \"p\") (return-from b \"q\")))))";
105
1
    assert_values(&[(form, "\"s\"")]).await;
106
1
}
107

            
108
#[tokio::test]
109
1
async fn handler_case_non_i32_result_with_one_arm_returning_from_block_other_raising() {
110
1
    let form = "(block b (handler-case \"s\" (x (e) (if (> (rpc-protocol-version) 5) (return-from b \"p\") (error 'q \"q\")))))";
111
1
    assert_values(&[(form, "\"s\"")]).await;
112
1
}
113

            
114
#[tokio::test]
115
1
async fn handler_case_non_i32_result_with_unwind_protect_around_error() {
116
1
    let form = "(handler-case \"s\" (x (e) (unwind-protect (error 'a \"a\") (+ 1 2))))";
117
1
    assert_values(&[(form, "\"s\"")]).await;
118
1
}
119

            
120
#[tokio::test]
121
1
async fn handler_case_non_i32_result_with_leading_effect_form_then_both_arms_returning_from_block()
122
1
{
123
1
    let form = "(block b (handler-case \"s\" (x (e) (progn 1 (if (> (rpc-protocol-version) 5) (return-from b \"p\") (return-from b \"q\"))))))";
124
1
    assert_values(&[(form, "\"s\"")]).await;
125
1
}
126

            
127
#[tokio::test]
128
1
async fn handler_case_non_i32_result_with_unwind_protect_body_returning_from_block() {
129
1
    let form = "(block b (handler-case \"s\" (x (e) (unwind-protect 7 (return-from b \"c\")))))";
130
1
    assert_values(&[(form, "\"s\"")]).await;
131
1
}
132

            
133
#[tokio::test]
134
1
async fn handler_case_non_i32_result_t_clause_variant() {
135
1
    let form = "(block b (handler-case \"s\" (t (e) (if (> (rpc-protocol-version) 5) (return-from b \"p\") (return-from b \"q\")))))";
136
1
    assert_values(&[(form, "\"s\"")]).await;
137
1
}