1
//! A BLOCK's exits join with provenance (0014 P6a-i): a literal exit adopts
2
//! the reconciled result in a second pass, and no block takes more than two.
3

            
4
use crate::ast::WasmType;
5
use crate::compiler::CompileMode;
6
use crate::compiler::behaviour_oracle::{Outcome, observe};
7
use crate::compiler::stats::CompileStats;
8
use crate::host_fn::HostFnSpec;
9

            
10
const LITERAL_EXIT_RATIO_TAIL: &str =
11
    "(block b (if (> (rpc-protocol-version) 0) (return-from b 1)) 1/2)";
12
const LITERAL_EXIT_INDEX_TAIL: &str =
13
    "(block b (if (> (rpc-protocol-version) 0) (return-from b 1)) 2)";
14

            
15
10
fn run(source: &str) -> (Vec<Outcome>, CompileStats) {
16
10
    let hosts = [
17
10
        HostFnSpec::new("rpc-protocol-version", "nomi", "rpc_protocol_version")
18
10
            .returns(WasmType::I32),
19
10
        HostFnSpec::new("ratio-source", "nomi", "ratio_source").returns(WasmType::Ratio),
20
10
    ];
21
10
    let (behaviours, stats) = observe(source, CompileMode::Eval, &hosts);
22
20
    (behaviours.into_iter().map(|b| b.outcome).collect(), stats)
23
10
}
24

            
25
8
fn all_values_of(outcomes: &[Outcome], ty: &str) -> bool {
26
8
    !outcomes.is_empty()
27
8
        && outcomes.iter().all(
28
16
            |outcome| matches!(outcome, Outcome::Value(v) if v.starts_with(&format!("Some({ty})"))),
29
        )
30
8
}
31

            
32
7
fn passes(stats: CompileStats) -> (u64, u64) {
33
7
    (stats.block_passes_max, stats.block_passes_total)
34
7
}
35

            
36
#[test]
37
1
fn a_literal_exit_beside_a_ratio_tail_joins_as_a_ratio_in_two_passes() {
38
1
    let (outcomes, stats) = run(LITERAL_EXIT_RATIO_TAIL);
39
1
    assert!(all_values_of(&outcomes, "Ratio"), "{outcomes:?}");
40
1
    assert_eq!(passes(stats), (2, 2));
41
1
}
42

            
43
#[test]
44
1
fn a_block_whose_arms_already_agree_takes_one_pass() {
45
1
    let (outcomes, stats) = run(LITERAL_EXIT_INDEX_TAIL);
46
1
    assert!(all_values_of(&outcomes, "I32"), "{outcomes:?}");
47
1
    assert_eq!(passes(stats), (1, 1));
48
1
}
49

            
50
#[test]
51
1
fn the_counters_keep_the_most_passes_and_the_sum() {
52
1
    let source = format!("(progn {LITERAL_EXIT_INDEX_TAIL} {LITERAL_EXIT_RATIO_TAIL})");
53
1
    let (outcomes, stats) = run(&source);
54
1
    assert!(all_values_of(&outcomes, "Ratio"), "{outcomes:?}");
55
1
    assert_eq!(passes(stats), (2, 3));
56
1
}
57

            
58
#[test]
59
1
fn a_literal_exit_adopts_a_computed_ratio_tail() {
60
    let (outcomes, stats) =
61
1
        run("(block b (if (> (rpc-protocol-version) 0) (return-from b 1)) (ratio-source))");
62
1
    assert!(all_values_of(&outcomes, "Ratio"), "{outcomes:?}");
63
1
    assert_eq!(passes(stats), (2, 2));
64
1
}
65

            
66
#[test]
67
1
fn a_literal_tail_adopts_a_ratio_exit() {
68
    let (outcomes, stats) =
69
1
        run("(block b (if (> (rpc-protocol-version) 0) (return-from b 1/4)) 1)");
70
1
    assert!(all_values_of(&outcomes, "Ratio"), "{outcomes:?}");
71
1
    assert_eq!(passes(stats), (2, 2));
72
1
}
73

            
74
#[test]
75
1
fn a_computed_index_exit_beside_a_ratio_tail_widens() {
76
1
    let (outcomes, _) = run(
77
1
        "(block b (if (> (rpc-protocol-version) 0) (return-from b (rpc-protocol-version))) 1/2)",
78
1
    );
79
1
    assert!(all_values_of(&outcomes, "AnyRef"), "{outcomes:?}");
80
1
}
81

            
82
#[test]
83
1
fn a_string_exit_beside_an_index_tail_widens() {
84
1
    let (outcomes, _) = run(
85
1
        "(block b (if (> (rpc-protocol-version) 0) (return-from b \"s\")) (rpc-protocol-version))",
86
1
    );
87
1
    assert!(all_values_of(&outcomes, "AnyRef"), "{outcomes:?}");
88
1
}
89

            
90
#[test]
91
1
fn a_literal_exit_is_boxed_beside_an_anyref_tail() {
92
1
    let (outcomes, stats) = run(
93
1
        "(block b (if (> (rpc-protocol-version) 0) (return-from b 1)) \
94
1
         (car (list (rpc-protocol-version) \"a\")))",
95
1
    );
96
1
    assert!(
97
2
        !outcomes.is_empty() && outcomes.iter().all(|o| matches!(o, Outcome::Value(_))),
98
        "{outcomes:?}"
99
    );
100
1
    assert_eq!(passes(stats), (2, 2));
101
1
}
102

            
103
#[test]
104
1
fn an_exit_in_unreachable_code_does_not_join() {
105
1
    let (outcomes, stats) = run(
106
1
        r#"(block b (do ((i 0 (progn (return-from b "dead") (+ i 1)))) ((> i 0) 0) (return-from b (+ 1073741823 (+ 1 (rpc-protocol-version))))))"#,
107
1
    );
108
1
    assert!(all_values_of(&outcomes, "I32"), "{outcomes:?}");
109
1
    assert_eq!(passes(stats), (1, 1));
110
1
}
111

            
112
#[test]
113
1
fn an_exit_whose_value_raises_does_not_join() {
114
    let (outcomes, _) =
115
1
        run(r#"(block b (if (> (rpc-protocol-version) 0) (return-from b (error 'boom "x"))) "s")"#);
116
1
    assert!(
117
1
        matches!(outcomes.as_slice(), [Outcome::RuntimeError(_), Outcome::Value(v)] if v.starts_with("Some(StringRef)")),
118
        "{outcomes:?}"
119
    );
120
1
}