aver-cert 0.1.4

Independent artifact certificate engine and verifier for Aver WebAssembly
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// Recognised proof faces of expression-fragment plans. These are pure plan
// pattern-matchers (no byte analysis), shared between the producer's MIR
// adapter — which gates plan emission on a face existing — and the engine's
// classifier, so they live in the `plans` layer.

/// The straight-line integer face of a host-call expr fragment: exactly
/// `add(param0, box(k))` over one Int parameter. This is the only host-call
/// fragment shape with a rendered proof face today; any other host-call plan
/// fail-closes classification.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragIntAddFace {
    pub k: i64,
    pub box_idx: u32,
    pub add_idx: u32,
}

pub fn expr_fragment_int_add_face(plan: &ExprFragmentPlan) -> Option<FragIntAddFace> {
    if plan.params.as_slice() != [FragTy::IntCarrier] || plan.result != FragTy::IntCarrier {
        return None;
    }
    let [n0, n1, n2, n3] = plan.body.nodes.as_slice() else {
        return None;
    };
    if plan.body.result != FragValueId(3) {
        return None;
    }
    let (FragNodeKind::Local { index: 0 }, FragNodeKind::ConstI64(k)) = (&n0.kind, &n1.kind) else {
        return None;
    };
    let FragNodeKind::HostCall {
        role: FragHostRole::Box,
        func_idx: box_idx,
        args: box_args,
    } = &n2.kind
    else {
        return None;
    };
    let FragNodeKind::HostCall {
        role: FragHostRole::Add,
        func_idx: add_idx,
        args: add_args,
    } = &n3.kind
    else {
        return None;
    };
    if box_args.as_slice() != [FragValueId(1)]
        || add_args.as_slice() != [FragValueId(0), FragValueId(2)]
    {
        return None;
    }
    if n0.ty != FragTy::IntCarrier
        || n1.ty != FragTy::I64
        || n2.ty != FragTy::IntCarrier
        || n3.ty != FragTy::IntCarrier
    {
        return None;
    }
    Some(FragIntAddFace {
        k: *k,
        box_idx: *box_idx,
        add_idx: *add_idx,
    })
}

/// The verbatim field-projection face of an ADT-ref expr fragment: exactly
/// `struct.get ty field∈{0,1}` of the single reference parameter, returned
/// unchanged. This is the only fragment shape admitting `AdtRef` values today;
/// any other ADT-ref plan fail-closes on producer and verifier alike.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragProjectFace {
    pub struct_idx: u32,
    pub field_idx: u32,
}

pub fn expr_fragment_project_face(plan: &ExprFragmentPlan) -> Option<FragProjectFace> {
    if plan.params.as_slice() != [FragTy::AdtRef] || plan.result != FragTy::AdtRef {
        return None;
    }
    let [n0, n1] = plan.body.nodes.as_slice() else {
        return None;
    };
    if plan.body.result != FragValueId(1) {
        return None;
    }
    let FragNodeKind::Local { index: 0 } = n0.kind else {
        return None;
    };
    let FragNodeKind::StructGetUser {
        ty_idx,
        field,
        value,
    } = n1.kind
    else {
        return None;
    };
    if value != FragValueId(0) || field > 1 {
        return None;
    }
    if n0.ty != FragTy::AdtRef || n1.ty != FragTy::AdtRef {
        return None;
    }
    Some(FragProjectFace {
        struct_idx: ty_idx,
        field_idx: field,
    })
}

/// The stage-1 record field-read result types — exactly the range of the wall's
/// `SchemaCore.scalarLeafFragTy?` (`PlanCheck.fragTyIsRecordScalar`): the boxed
/// Int carrier, the Boolean i32, or the raw f64. Broadening this breaks the
/// producer/wall encode agreement (`encode… = some {name}Plan := rfl`).
pub fn frag_ty_is_record_scalar(ty: FragTy) -> bool {
    matches!(ty, FragTy::BoolI32 | FragTy::IntCarrier | FragTy::F64)
}

/// The scalar record-projection face of an ADT-ref expr fragment: exactly
/// `struct.get structIdx field` of the single opaque record reference, yielding
/// a stage-1 scalar leaf. Unlike `expr_fragment_project_face` the projected
/// result is a SCALAR (`frag_ty_is_record_scalar`) and the field index is NOT
/// capped — the wall record face's type-section equality pin fixes the whole
/// ordered field list. Exact Rust twin of `WasmSlice.exprRecordProjFace?`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragRecordProjFace {
    pub struct_idx: u32,
    pub field_idx: u32,
}

pub fn expr_fragment_record_proj_face(plan: &ExprFragmentPlan) -> Option<FragRecordProjFace> {
    if plan.params.as_slice() != [FragTy::AdtRef]
        || !frag_ty_is_record_scalar(plan.result)
        || plan.body.result != FragValueId(1)
    {
        return None;
    }
    let [n0, n1] = plan.body.nodes.as_slice() else {
        return None;
    };
    if n0.id != FragValueId(0) || n1.id != FragValueId(1) {
        return None;
    }
    let FragNodeKind::Local { index: 0 } = n0.kind else {
        return None;
    };
    let FragNodeKind::StructGetUser {
        ty_idx,
        field,
        value,
    } = n1.kind
    else {
        return None;
    };
    if value != FragValueId(0) || n0.ty != FragTy::AdtRef || n1.ty != plan.result {
        return None;
    }
    Some(FragRecordProjFace {
        struct_idx: ty_idx,
        field_idx: field,
    })
}

/// Exact Rust twin of `StandardFace.classifyTagDispatch` in the frozen wall.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragTagDispatchFace {
    pub opt_idx: u32,
    pub box_idx: u32,
    pub tag: i64,
    pub then_c: i64,
    pub else_c: i64,
}

fn expr_fragment_tag_dispatch_arm(block: &FragBlock) -> Option<(u32, i64)> {
    let [n0, n1] = block.nodes.as_slice() else {
        return None;
    };
    if block.result != FragValueId(1)
        || n0.id != FragValueId(0)
        || n1.id != FragValueId(1)
        || n0.ty != FragTy::I64
        || n1.ty != FragTy::IntCarrier
    {
        return None;
    }
    let FragNodeKind::ConstI64(constant) = n0.kind else {
        return None;
    };
    let FragNodeKind::HostCall {
        role: FragHostRole::Box,
        func_idx,
        ref args,
    } = n1.kind
    else {
        return None;
    };
    (args.as_slice() == [FragValueId(0)]).then_some((func_idx, constant))
}

pub fn expr_fragment_tag_dispatch_face(plan: &ExprFragmentPlan) -> Option<FragTagDispatchFace> {
    if plan.params.as_slice() != [FragTy::AdtRef]
        || plan.result != FragTy::IntCarrier
        || plan.body.result != FragValueId(4)
    {
        return None;
    }
    let [n0, n1, n2, n3, n4] = plan.body.nodes.as_slice() else {
        return None;
    };
    if [n0.id.0, n1.id.0, n2.id.0, n3.id.0, n4.id.0] != [0, 1, 2, 3, 4]
        || n0.ty != FragTy::AdtRef
        || n1.ty != FragTy::RawI32
        || n2.ty != FragTy::RawI32
        || n3.ty != FragTy::BoolI32
        || n4.ty != FragTy::IntCarrier
    {
        return None;
    }
    let FragNodeKind::Local { index: 0 } = n0.kind else {
        return None;
    };
    let FragNodeKind::StructGetUser {
        ty_idx: opt_idx,
        field: 0,
        value: FragValueId(0),
    } = n1.kind
    else {
        return None;
    };
    let FragNodeKind::ConstI32(tag) = n2.kind else {
        return None;
    };
    let FragNodeKind::Prim {
        op: FragPrim::I32Eq,
        ref args,
    } = n3.kind
    else {
        return None;
    };
    let FragNodeKind::If {
        cond: FragValueId(3),
        ref then_block,
        ref else_block,
    } = n4.kind
    else {
        return None;
    };
    if args.as_slice() != [FragValueId(1), FragValueId(2)] {
        return None;
    }
    let (box_idx, then_c) = expr_fragment_tag_dispatch_arm(then_block)?;
    let (else_box_idx, else_c) = expr_fragment_tag_dispatch_arm(else_block)?;
    (box_idx == else_box_idx).then_some(FragTagDispatchFace {
        opt_idx,
        box_idx,
        tag: i64::from(tag),
        then_c,
        else_c,
    })
}

/// Exact Rust twin of `StandardFace.classifyVectorGetOrDefault` in the wall:
/// the plan is the single monolithic fused vector-read node over the pinned
/// `(vector, index)` params, with distinct helper indices.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragVectorGetOrDefaultFace {
    pub arr_ty: u32,
    pub to_index_idx: u32,
    pub box_idx: u32,
    pub default: i64,
}

pub fn expr_fragment_vector_get_face(
    plan: &ExprFragmentPlan,
) -> Option<FragVectorGetOrDefaultFace> {
    if plan.params.as_slice() != [FragTy::AdtRef, FragTy::IntCarrier]
        || plan.result != FragTy::IntCarrier
        || plan.body.result != FragValueId(0)
    {
        return None;
    }
    let [n0] = plan.body.nodes.as_slice() else {
        return None;
    };
    if n0.id != FragValueId(0) || n0.ty != FragTy::IntCarrier {
        return None;
    }
    let FragNodeKind::VectorGetOrDefault {
        arr_ty,
        to_index_idx,
        box_idx,
        default,
    } = n0.kind
    else {
        return None;
    };
    (to_index_idx != box_idx).then_some(FragVectorGetOrDefaultFace {
        arr_ty,
        to_index_idx,
        box_idx,
        default,
    })
}

/// The comparison operator of an admitted Int value-versus-value face. Exact
/// Rust twin of the wall's `StandardFace.IntCmpOp`: `le` is absent by
/// construction, because the plan grammar has no `i32.le_s` to lower it to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FragIntCmpOp {
    Lt,
    Gt,
    Ge,
    Eq,
}

impl FragIntCmpOp {
    /// The wall constructor naming this operator (`StandardFace.IntCmpOp`).
    pub fn lean_ctor(self) -> &'static str {
        match self {
            FragIntCmpOp::Lt => ".lt",
            FragIntCmpOp::Gt => ".gt",
            FragIntCmpOp::Ge => ".ge",
            FragIntCmpOp::Eq => ".eq",
        }
    }
}

/// Face data of both Int comparison shapes: which operator, and the resolved
/// index of the single runtime helper it reads (`__aint_cmp` for the three
/// relational operators, `__aint_eq` for equality). Exact Rust twin of
/// `StandardFace.IntCmpFace`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragIntCmpFace {
    pub op: FragIntCmpOp,
    pub helper_idx: u32,
}

/// Twin of `StandardFace.intCmpOfPrim?`: the signed relational primitive each
/// operator's tail uses. Every other primitive declines, which keeps an
/// `i32.and`- or `i32.eq`-tailed body out of these faces.
fn frag_int_cmp_of_prim(op: FragPrim) -> Option<FragIntCmpOp> {
    match op {
        FragPrim::I32LtS => Some(FragIntCmpOp::Lt),
        FragPrim::I32GtS => Some(FragIntCmpOp::Gt),
        FragPrim::I32GeS => Some(FragIntCmpOp::Ge),
        _ => None,
    }
}

/// The pinned comparison prefix shared by both faces: `local 0`, `local 1`, and
/// either the `__aint_eq` call alone (3 nodes) or the `__aint_cmp` call plus
/// `i32.const 0` plus a signed relational operator (5 nodes). Returns the face
/// and the number of nodes the prefix occupies.
fn frag_int_cmp_prefix(nodes: &[FragNode]) -> Option<(FragIntCmpFace, usize)> {
    let [n0, n1, rest @ ..] = nodes else {
        return None;
    };
    if n0.id != FragValueId(0)
        || n1.id != FragValueId(1)
        || n0.ty != FragTy::IntCarrier
        || n1.ty != FragTy::IntCarrier
    {
        return None;
    }
    let (FragNodeKind::Local { index: 0 }, FragNodeKind::Local { index: 1 }) = (&n0.kind, &n1.kind)
    else {
        return None;
    };
    let [n2, tail @ ..] = rest else {
        return None;
    };
    if n2.id != FragValueId(2) {
        return None;
    }
    match &n2.kind {
        FragNodeKind::HostCall {
            role: FragHostRole::Eq,
            func_idx,
            args,
        } if n2.ty == FragTy::BoolI32
            && args.as_slice() == [FragValueId(0), FragValueId(1)] =>
        {
            Some((
                FragIntCmpFace {
                    op: FragIntCmpOp::Eq,
                    helper_idx: *func_idx,
                },
                3,
            ))
        }
        FragNodeKind::HostCall {
            role: FragHostRole::Cmp,
            func_idx,
            args,
        } if n2.ty == FragTy::RawI32
            && args.as_slice() == [FragValueId(0), FragValueId(1)] =>
        {
            let [n3, n4, ..] = tail else {
                return None;
            };
            if n3.id != FragValueId(3)
                || n4.id != FragValueId(4)
                || n3.ty != FragTy::RawI32
                || n4.ty != FragTy::BoolI32
            {
                return None;
            }
            let FragNodeKind::ConstI32(0) = n3.kind else {
                return None;
            };
            let FragNodeKind::Prim { op, args } = &n4.kind else {
                return None;
            };
            if args.as_slice() != [FragValueId(2), FragValueId(3)] {
                return None;
            }
            Some((
                FragIntCmpFace {
                    op: frag_int_cmp_of_prim(*op)?,
                    helper_idx: *func_idx,
                },
                5,
            ))
        }
        _ => None,
    }
}

/// Exact Rust twin of `StandardFace.classifyIntCmpBool`: two Int-carrier
/// parameters, a Boolean result, and exactly the pinned comparison nodes.
pub fn expr_fragment_int_cmp_bool_face(plan: &ExprFragmentPlan) -> Option<FragIntCmpFace> {
    if plan.params.as_slice() != [FragTy::IntCarrier, FragTy::IntCarrier]
        || plan.result != FragTy::BoolI32
    {
        return None;
    }
    let (face, len) = frag_int_cmp_prefix(&plan.body.nodes)?;
    (plan.body.nodes.len() == len && plan.body.result == FragValueId(len - 1)).then_some(face)
}

/// One arm of the selection: a bare argument read, no box and no host call.
/// Twin of `StandardFace.intSelectArm`.
fn frag_int_select_arm(block: &FragBlock, local: u32) -> bool {
    let [node] = block.nodes.as_slice() else {
        return false;
    };
    block.result == FragValueId(0)
        && node.id == FragValueId(0)
        && node.ty == FragTy::IntCarrier
        && node.kind == FragNodeKind::Local { index: local }
}

/// Exact Rust twin of `StandardFace.classifyIntSelect`: the comparison above
/// followed by an `if` whose two arms are the bare reads of parameter 0 and
/// parameter 1 in that order — so the result is a passthrough of an input, never
/// a freshly boxed value.
pub fn expr_fragment_int_select_face(plan: &ExprFragmentPlan) -> Option<FragIntCmpFace> {
    if plan.params.as_slice() != [FragTy::IntCarrier, FragTy::IntCarrier]
        || plan.result != FragTy::IntCarrier
    {
        return None;
    }
    let (face, len) = frag_int_cmp_prefix(&plan.body.nodes)?;
    if plan.body.nodes.len() != len + 1 || plan.body.result != FragValueId(len) {
        return None;
    }
    let node = &plan.body.nodes[len];
    if node.id != FragValueId(len) || node.ty != FragTy::IntCarrier {
        return None;
    }
    let FragNodeKind::If {
        cond,
        then_block,
        else_block,
    } = &node.kind
    else {
        return None;
    };
    (*cond == FragValueId(len - 1)
        && frag_int_select_arm(then_block, 0)
        && frag_int_select_arm(else_block, 1))
    .then_some(face)
}

fn frag_block_has_user_struct_get(block: &FragBlock) -> bool {
    block.nodes.iter().any(|node| match &node.kind {
        FragNodeKind::StructGetUser { .. } => true,
        FragNodeKind::If {
            then_block,
            else_block,
            ..
        } => {
            frag_block_has_user_struct_get(then_block)
                || frag_block_has_user_struct_get(else_block)
        }
        _ => false,
    })
}

/// Rust twin of the wall's broad `exprFragmentIsTagDispatch` discriminator:
/// an ADT-ref argument, an Int-carrier result, and at least one user-struct
/// field read in the encoded plan. The wall's classifier later checks the
/// exact canonical tag-dispatch node shape.
pub fn expr_fragment_is_tag_dispatch(plan: &ExprFragmentPlan) -> bool {
    plan.params.as_slice() == [FragTy::AdtRef]
        && plan.result == FragTy::IntCarrier
        && frag_block_has_user_struct_get(&plan.body)
}

fn frag_block_touches_adt_ref(block: &FragBlock) -> bool {
    block.nodes.iter().any(|node| {
        node.ty == FragTy::AdtRef
            || match &node.kind {
                FragNodeKind::StructGetUser { .. } => true,
                FragNodeKind::If {
                    then_block,
                    else_block,
                    ..
                } => {
                    frag_block_touches_adt_ref(then_block) || frag_block_touches_adt_ref(else_block)
                }
                _ => false,
            }
    })
}

/// Whether a plan involves opaque user-ADT references anywhere (params, result
/// or body). Such plans are admitted ONLY through the field-projection face.
pub fn expr_fragment_plan_touches_adt_ref(plan: &ExprFragmentPlan) -> bool {
    plan.params.contains(&FragTy::AdtRef)
        || plan.result == FragTy::AdtRef
        || frag_block_touches_adt_ref(&plan.body)
}

fn frag_block_has_host_call_where(block: &FragBlock, want: &dyn Fn(FragHostRole) -> bool) -> bool {
    block.nodes.iter().any(|node| match &node.kind {
        FragNodeKind::HostCall { role, .. } => want(*role),
        FragNodeKind::If {
            then_block,
            else_block,
            ..
        } => {
            frag_block_has_host_call_where(then_block, want)
                || frag_block_has_host_call_where(else_block, want)
        }
        _ => false,
    })
}

/// Whether a plan calls a runtime host helper anywhere in its body. Such plans
/// are admitted ONLY through an exact recognised face: a host call is a claim
/// about a runtime contract, and the generic expression-fragment gate in the
/// wall (`genericFragmentAllowedFuel`) rejects every `.hostCall` node outright.
/// Producer and verifier read this same predicate so the two gates cannot drift.
pub fn expr_fragment_plan_has_host_calls(plan: &ExprFragmentPlan) -> bool {
    frag_block_has_host_call_where(&plan.body, &|_| true)
}

/// Whether a plan calls one PARTICULAR host role. The producer reads this to
/// decide whether the module it is about to emit really calls a helper, which
/// is what gates the helper's named export (and with it the role's certificate
/// binding) — a plan is the exact body its canonical lowering emits, so the
/// roles it names are the calls the bytes will carry.
pub fn expr_fragment_plan_calls_host_role(plan: &ExprFragmentPlan, role: FragHostRole) -> bool {
    frag_block_has_host_call_where(&plan.body, &|candidate| candidate == role)
}

#[cfg(all(test, feature = "engine"))]
mod record_proj_face_tests {
    use super::*;

    /// A record scalar field read: `struct.get struct_idx field` of the single
    /// record reference, yielding `result`. This is the shape person's
    /// `readMember`/`readAge` compile to.
    fn record_proj_plan(struct_idx: u32, field: u32, result: FragTy) -> ExprFragmentPlan {
        ExprFragmentPlan {
            params: vec![FragTy::AdtRef],
            result,
            body: FragBlock {
                nodes: vec![
                    FragNode {
                        id: FragValueId(0),
                        ty: FragTy::AdtRef,
                        kind: FragNodeKind::Local { index: 0 },
                    },
                    FragNode {
                        id: FragValueId(1),
                        ty: result,
                        kind: FragNodeKind::StructGetUser {
                            ty_idx: struct_idx,
                            field,
                            value: FragValueId(0),
                        },
                    },
                ],
                result: FragValueId(1),
            },
        }
    }

    #[test]
    fn frag_ty_is_record_scalar_is_exactly_the_three_leaves() {
        assert!(frag_ty_is_record_scalar(FragTy::BoolI32));
        assert!(frag_ty_is_record_scalar(FragTy::IntCarrier));
        assert!(frag_ty_is_record_scalar(FragTy::F64));
        for ty in [FragTy::I64, FragTy::RawI32, FragTy::Ref, FragTy::AdtRef] {
            assert!(!frag_ty_is_record_scalar(ty), "{ty:?} must not be a record leaf");
        }
    }

    #[test]
    fn recognizes_person_field_reads() {
        // `readMember`: Bool field 1 of struct 0. `readAge`: Int field 0.
        assert_eq!(
            expr_fragment_record_proj_face(&record_proj_plan(0, 1, FragTy::BoolI32)),
            Some(FragRecordProjFace { struct_idx: 0, field_idx: 1 })
        );
        assert_eq!(
            expr_fragment_record_proj_face(&record_proj_plan(0, 0, FragTy::IntCarrier)),
            Some(FragRecordProjFace { struct_idx: 0, field_idx: 0 })
        );
        // The field index is not capped (unlike the verbatim projection face).
        assert_eq!(
            expr_fragment_record_proj_face(&record_proj_plan(7, 5, FragTy::F64)),
            Some(FragRecordProjFace { struct_idx: 7, field_idx: 5 })
        );
    }

    #[test]
    fn declines_whole_reference_projection() {
        // An `AdtRef` result is the verbatim field-projection face, NOT a record
        // scalar leaf — it must decline here (and route to `expr_fragment_project_face`).
        assert_eq!(
            expr_fragment_record_proj_face(&record_proj_plan(0, 0, FragTy::AdtRef)),
            None
        );
    }

    #[test]
    fn declines_non_scalar_leaf_result() {
        for ty in [FragTy::I64, FragTy::RawI32, FragTy::Ref] {
            assert_eq!(expr_fragment_record_proj_face(&record_proj_plan(0, 0, ty)), None);
        }
    }

    #[test]
    fn declines_wrong_parameter_shape() {
        // Not a single opaque record reference.
        let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
        plan.params = vec![FragTy::IntCarrier];
        assert_eq!(expr_fragment_record_proj_face(&plan), None);
        let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
        plan.params = vec![FragTy::AdtRef, FragTy::AdtRef];
        assert_eq!(expr_fragment_record_proj_face(&plan), None);
    }

    #[test]
    fn declines_node_type_mismatch_and_wrong_body_shape() {
        // The projection node's declared type must equal the plan result.
        let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
        plan.body.nodes[1].ty = FragTy::IntCarrier;
        assert_eq!(expr_fragment_record_proj_face(&plan), None);

        // The projected value must be the single parameter local, not a re-read.
        let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
        plan.body.nodes[1].kind = FragNodeKind::StructGetUser {
            ty_idx: 0,
            field: 1,
            value: FragValueId(1),
        };
        assert_eq!(expr_fragment_record_proj_face(&plan), None);

        // An extra node breaks the exact two-node shape.
        let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
        plan.body.nodes.push(FragNode {
            id: FragValueId(2),
            ty: FragTy::BoolI32,
            kind: FragNodeKind::ConstBool(true),
        });
        assert_eq!(expr_fragment_record_proj_face(&plan), None);

        // The first node must read parameter local 0.
        let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
        plan.body.nodes[0].kind = FragNodeKind::Local { index: 1 };
        assert_eq!(expr_fragment_record_proj_face(&plan), None);
    }
}