← demo0x84de0e…e3ddSucceededblock 90
No position yet0 / 54
FetchingOpeningPositioning
noir
Narrow session: Code, Call Trace and Values only, read-only. The event log and stepping need a wider viewport.
Code
1 low = "-77"
2 high = "88"
1 // TOUR: branches and conditions
2 //
3 // The subject is which arm ran. Every `if` below is written so that BOTH arms
4 // are reachable and the inputs decide, and the program calls each classifier
5 // more than once with values that land in different arms — so the same source
6 // line is marked taken on one visit and not-taken on another.
7 //
8 // A branch that can only go one way is a branch the marking cannot teach
9 // anything about, so there are none of those here.
10
11 struct Reading {
12 sensor: u8,
13 value: i32,
14 }
15
16 unconstrained fn classify(value: i32) -> u8 {
17 // A three-way chain. Exactly one arm runs per call, and the three calls in
18 // `main` between them run all three.
19 if value < -50 {
20 0 // under
21 } else if value > 50 {
22 2 // over
23 } else {
24 1 // nominal
25 }
26 }
27
28 unconstrained fn clamp(value: i32, lo: i32, hi: i32) -> i32 {
29 // Nested conditions: the outer test decides whether the inner one is
30 // reached at all, so a not-taken outer arm hides an inner branch that was
31 // never evaluated rather than one that was evaluated and went the other
32 // way. Those are different facts and the marking distinguishes them.
33 let mut out = value;
34 if value < lo {
35 out = lo;
36 } else {
37 if value > hi {
38 out = hi;
39 }
40 }
41 out
42 }
43
44 unconstrained fn describe(r: Reading) -> u8 {
45 // Boolean operators in the condition itself, so the condition is worth
46 // inspecting and not just its outcome.
47 let known = (r.sensor == 1) | (r.sensor == 2);
48 let extreme = (r.value < -50) | (r.value > 50);
49 if known & extreme {
50 9
51 } else if known {
52 classify(r.value)
53 } else {
54 255 // unknown sensor
55 }
56 }
57
58 unconstrained fn main(low: i32, high: pub i32) -> pub u32 {
59 // Three readings chosen so the three arms of `classify` each run once and
60 // `describe`'s unknown-sensor arm runs exactly once.
61 let a = describe(Reading { sensor: 1, value: low });
62 let b = describe(Reading { sensor: 2, value: 12 });
63 let c = describe(Reading { sensor: 7, value: high });
64
65 let clamped_low = clamp(low, -10, 10);
66 let clamped_mid = clamp(3, -10, 10);
67 let clamped_high = clamp(high, -10, 10);
68
69 assert(clamped_mid == 3);
70 assert(clamped_low == -10);
71 assert(clamped_high == 10);
72
73 // Widened before summing: `describe` answers in a `u8` and three of its
74 // answers do not fit in one.
75 a as u32 + b as u32 + c as u32
76 }
1 [package]
2 name = "tour_branches"
3 type = "bin"
4 authors = ["BlockTracer capability tour"]
5
6 [dependencies]
Event Log

The recorded event stream is in the published recording. Reading it needs the replay engine, which this page has not started.

Call Trace

The call structure is in the published recording. Reading it needs the replay engine, which this page has not started.

Values

The recorded values are in the published recording. Reading them needs the replay engine, which this page has not started.