moonproof-runtime 0.2.1

Wasm-friendly Compact contract runtime core — IR walker, IR-driven circuit executor, contract state, runtime values
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
//! Pre-built O(1) lookup tables and pre-compiled VM / FAB caches over a
//! [`MoonIr`].
//!
//! Constructed once in [`IrExecutable::new`](crate::IrExecutable::new) (and
//! when binding a cross-contract callee) so hot-path circuit execution avoids
//! linear scans, `BTreeMap` VM-op parsing, serde `TypeDescriptor`
//! deserialization, and repeated `Alignment` construction on every call.

use std::sync::OnceLock;

use moonproof_runtime_ir::ir::{
    CircuitBody, Expression, MoonIr, Native, PathElement, Statement, TypeDescriptor, VmInstr,
    Witness,
};
use rustc_hash::{FxBuildHasher, FxHashMap};

use super::bytecode::{compile_circuit, compile_constructor, CompiledCircuit};
use super::descriptor::{CircuitInputPlan, FabPlan};
use super::vm_decode::{compile_vm_ops, CompiledLedgerQuery};

/// Upper cap for initial `public_transcript` `Vec::with_capacity` hint so
/// pathological/unbounded estimates never over-allocate.
const MAX_TRANSCRIPT_CAPACITY_HINT: usize = 4096;

/// Number of VM ops emitted by `claim_contract_call_ops` per `ContractCall`.
const CLAIM_CONTRACT_CALL_OP_COUNT: usize = 9;

/// Pre-compiled input and output [`FabPlan`]s for one [`CircuitBody`].
#[derive(Debug, Clone)]
pub struct CircuitIoPlan {
    pub input: Option<CircuitInputPlan>,
    pub output: Option<FabPlan>,
}

/// Pre-compiled argument and result [`FabPlan`]s for one [`Native`].
#[derive(Debug, Clone)]
pub struct NativeFabPlans {
    pub args: Vec<Option<FabPlan>>,
    pub result: Option<FabPlan>,
}

/// O(1) indices and lazily-compiled query / FAB caches for a [`MoonIr`].
///
/// All symbol entries store indices into the owning `MoonIr` so the index
/// carries no AST clones and stays cheap to share via `Arc`. Query and
/// circuit-I/O caches are keyed by stable heap addresses of slices/elements
/// owned by the `Arc<MoonIr>` (zero `unsafe`) and compile on first use via
/// wait-free [`OnceLock`] slots so `IrIndex::build` performs zero upfront
/// MPT construction or SHA-256 hashing for unexecuted circuits.
#[derive(Debug, Default)]
pub struct IrIndex {
    ir_ptr: usize,
    witnesses_by_name: FxHashMap<String, usize>,
    natives_by_uniq: FxHashMap<u64, usize>,
    natives_by_name: FxHashMap<String, Vec<usize>>,
    circuits_by_uniq: FxHashMap<u64, usize>,
    circuits_by_name: FxHashMap<String, Vec<usize>>,
    exported_circuits_by_name: FxHashMap<String, usize>,
    compiled_queries: FxHashMap<(usize, usize), OnceLock<Option<CompiledLedgerQuery>>>,
    circuit_idx_by_ptr: FxHashMap<usize, usize>,
    circuit_io_plans: Vec<OnceLock<CircuitIoPlan>>,
    estimated_transcript_ops: Vec<usize>,
    witness_result_plans: Vec<OnceLock<Option<FabPlan>>>,
    native_idx_by_ptr: FxHashMap<usize, usize>,
    native_plans: Vec<OnceLock<NativeFabPlans>>,
    compiled_circuits: Vec<OnceLock<Option<CompiledCircuit>>>,
    compiled_constructor: OnceLock<Option<CompiledCircuit>>,
}

impl IrIndex {
    /// Build O(1) symbol lookup tables, lazy query/FAB cache slots, and
    /// per-circuit transcript capacity hints for `ir`.
    #[must_use]
    pub fn build(ir: &MoonIr) -> Self {
        let ir_ptr = std::ptr::from_ref(ir) as usize;
        let mut witnesses_by_name =
            FxHashMap::with_capacity_and_hasher(ir.witnesses.len(), FxBuildHasher);
        let mut witness_result_plans = Vec::with_capacity(ir.witnesses.len());
        for (idx, w) in ir.witnesses.iter().enumerate() {
            witnesses_by_name.entry(w.name.clone()).or_insert(idx);
            witness_result_plans.push(OnceLock::new());
        }

        let mut natives_by_uniq =
            FxHashMap::with_capacity_and_hasher(ir.natives.len(), FxBuildHasher);
        let mut natives_by_name: FxHashMap<String, Vec<usize>> =
            FxHashMap::with_capacity_and_hasher(ir.natives.len(), FxBuildHasher);
        let mut native_idx_by_ptr =
            FxHashMap::with_capacity_and_hasher(ir.natives.len(), FxBuildHasher);
        let mut native_plans = Vec::with_capacity(ir.natives.len());
        for (idx, n) in ir.natives.iter().enumerate() {
            if let Some(u) = n.internal_uniq {
                natives_by_uniq.insert(u, idx);
            }
            natives_by_name.entry(n.name.clone()).or_default().push(idx);
            native_idx_by_ptr.insert(std::ptr::from_ref(n) as usize, idx);
            native_plans.push(OnceLock::new());
        }

        let mut circuits_by_uniq =
            FxHashMap::with_capacity_and_hasher(ir.circuits.len(), FxBuildHasher);
        let mut circuits_by_name: FxHashMap<String, Vec<usize>> =
            FxHashMap::with_capacity_and_hasher(ir.circuits.len(), FxBuildHasher);
        let mut exported_circuits_by_name =
            FxHashMap::with_capacity_and_hasher(ir.circuits.len(), FxBuildHasher);
        let mut circuit_idx_by_ptr =
            FxHashMap::with_capacity_and_hasher(ir.circuits.len(), FxBuildHasher);
        let mut circuit_io_plans = Vec::with_capacity(ir.circuits.len());

        for (idx, c) in ir.circuits.iter().enumerate() {
            if let Some(u) = c.internal_uniq {
                circuits_by_uniq.insert(u, idx);
            }
            circuits_by_name
                .entry(c.name.clone())
                .or_default()
                .push(idx);
            if c.exported {
                exported_circuits_by_name
                    .entry(c.name.clone())
                    .or_insert(idx);
            }
            circuit_idx_by_ptr.insert(std::ptr::from_ref(c) as usize, idx);
            circuit_io_plans.push(OnceLock::new());
        }

        let mut compiled_queries = FxHashMap::default();
        let mut ctor_acc = QueryScanAcc::default();
        for init_op in &ir.constructor.init_ops {
            collect_expr_queries(init_op, &mut compiled_queries, &mut ctor_acc);
        }
        collect_stmt_queries(&ir.constructor.body, &mut compiled_queries, &mut ctor_acc);

        let mut direct_ops = Vec::with_capacity(ir.circuits.len());
        let mut call_refs: Vec<Vec<(Option<u64>, &str)>> = Vec::with_capacity(ir.circuits.len());
        for c in &ir.circuits {
            let mut acc = QueryScanAcc::default();
            collect_stmt_queries(&c.body, &mut compiled_queries, &mut acc);
            direct_ops.push(acc.direct_transcript_ops);
            call_refs.push(acc.callees);
        }

        let mut estimated_transcript_ops = Vec::with_capacity(ir.circuits.len());
        let mut visited = vec![false; ir.circuits.len()];
        for idx in 0..ir.circuits.len() {
            visited.fill(false);
            let total = estimate_circuit_ops(
                idx,
                &direct_ops,
                &call_refs,
                &circuits_by_uniq,
                &circuits_by_name,
                &mut visited,
            );
            estimated_transcript_ops.push(total.min(MAX_TRANSCRIPT_CAPACITY_HINT));
        }

        let mut compiled_circuits = Vec::with_capacity(ir.circuits.len());
        for _ in 0..ir.circuits.len() {
            compiled_circuits.push(OnceLock::new());
        }

        Self {
            ir_ptr,
            witnesses_by_name,
            natives_by_uniq,
            natives_by_name,
            circuits_by_uniq,
            circuits_by_name,
            exported_circuits_by_name,
            compiled_queries,
            circuit_idx_by_ptr,
            circuit_io_plans,
            estimated_transcript_ops,
            witness_result_plans,
            native_idx_by_ptr,
            native_plans,
            compiled_circuits,
            compiled_constructor: OnceLock::new(),
        }
    }

    /// Return `true` when this index was built for the exact `MoonIr`
    /// allocation at `ir`. Guards every pointer- and index-based lookup
    /// against accidental reuse with a moved or distinct IR instance.
    #[inline]
    #[must_use]
    pub fn matches_ir(&self, ir: &MoonIr) -> bool {
        self.ir_ptr != 0 && self.ir_ptr == std::ptr::from_ref(ir) as usize
    }

    #[inline]
    #[must_use]
    pub fn witness<'ir>(&self, ir: &'ir MoonIr, name: &str) -> Option<&'ir Witness> {
        if !self.matches_ir(ir) {
            return None;
        }
        let &idx = self.witnesses_by_name.get(name)?;
        ir.witnesses.get(idx)
    }

    #[inline]
    #[must_use]
    pub fn witness_with_plan<'a, 'ir>(
        &'a self,
        ir: &'ir MoonIr,
        name: &str,
    ) -> Option<(&'ir Witness, Option<&'a FabPlan>)> {
        if !self.matches_ir(ir) {
            return None;
        }
        let &idx = self.witnesses_by_name.get(name)?;
        let witness = ir.witnesses.get(idx)?;
        let plan = self
            .witness_result_plans
            .get(idx)?
            .get_or_init(|| FabPlan::compile(&witness.result_type))
            .as_ref();
        Some((witness, plan))
    }

    #[inline]
    #[must_use]
    pub fn native_by_uniq<'ir>(&self, ir: &'ir MoonIr, uniq: u64) -> Option<&'ir Native> {
        if !self.matches_ir(ir) {
            return None;
        }
        let &idx = self.natives_by_uniq.get(&uniq)?;
        ir.natives.get(idx)
    }

    #[inline]
    #[must_use]
    pub fn native_indices_by_name(&self, ir: &MoonIr, name: &str) -> Option<&[usize]> {
        if !self.matches_ir(ir) {
            return None;
        }
        self.natives_by_name.get(name).map(Vec::as_slice)
    }

    #[inline]
    #[must_use]
    pub fn native_plans(&self, ir: &MoonIr, native: &Native) -> Option<&NativeFabPlans> {
        if !self.matches_ir(ir) {
            return None;
        }
        let &idx = self
            .native_idx_by_ptr
            .get(&(std::ptr::from_ref(native) as usize))?;
        let slot = self.native_plans.get(idx)?;
        Some(slot.get_or_init(|| {
            NativeFabPlans {
                args: native
                    .arguments
                    .iter()
                    .map(|a| FabPlan::compile(&a.arg_type))
                    .collect(),
                result: FabPlan::compile(&native.result_type),
            }
        }))
    }

    #[inline]
    #[must_use]
    pub fn circuit_by_uniq<'ir>(&self, ir: &'ir MoonIr, uniq: u64) -> Option<&'ir CircuitBody> {
        if !self.matches_ir(ir) {
            return None;
        }
        let &idx = self.circuits_by_uniq.get(&uniq)?;
        ir.circuits.get(idx)
    }

    #[inline]
    #[must_use]
    pub fn circuit_indices_by_name(&self, ir: &MoonIr, name: &str) -> Option<&[usize]> {
        if !self.matches_ir(ir) {
            return None;
        }
        self.circuits_by_name.get(name).map(Vec::as_slice)
    }

    #[inline]
    #[must_use]
    pub fn first_circuit<'ir>(&self, ir: &'ir MoonIr, name: &str) -> Option<&'ir CircuitBody> {
        if !self.matches_ir(ir) {
            return None;
        }
        let indices = self.circuits_by_name.get(name)?;
        let &idx = indices.first()?;
        ir.circuits.get(idx)
    }

    #[inline]
    #[must_use]
    pub fn exported_circuit<'ir>(&self, ir: &'ir MoonIr, name: &str) -> Option<&'ir CircuitBody> {
        if !self.matches_ir(ir) {
            return None;
        }
        let &idx = self.exported_circuits_by_name.get(name)?;
        ir.circuits.get(idx)
    }

    #[inline]
    #[must_use]
    pub fn compiled_query(&self, ir: &MoonIr, vm_ops: &[VmInstr]) -> Option<&CompiledLedgerQuery> {
        if !self.matches_ir(ir) || vm_ops.is_empty() {
            return None;
        }
        let slot = self
            .compiled_queries
            .get(&(vm_ops.as_ptr() as usize, vm_ops.len()))?;
        slot.get_or_init(|| compile_vm_ops(vm_ops)).as_ref()
    }

    #[inline]
    #[must_use]
    pub fn circuit_io_plan(&self, ir: &MoonIr, circuit: &CircuitBody) -> Option<&CircuitIoPlan> {
        if !self.matches_ir(ir) {
            return None;
        }
        let &idx = self
            .circuit_idx_by_ptr
            .get(&(std::ptr::from_ref(circuit) as usize))?;
        let slot = self.circuit_io_plans.get(idx)?;
        Some(slot.get_or_init(|| {
            let input = CircuitInputPlan::compile(&circuit.arguments);
            let output = if matches!(&circuit.result_type, TypeDescriptor::Tuple { types } if types.is_empty())
            {
                None
            } else {
                FabPlan::compile(&circuit.result_type)
            };
            CircuitIoPlan { input, output }
        }))
    }

    /// Pre-computed initial capacity hint for `public_transcript` when
    /// executing `circuit`.
    #[inline]
    #[must_use]
    pub fn estimated_transcript_ops(&self, ir: &MoonIr, circuit: &CircuitBody) -> usize {
        if !self.matches_ir(ir) {
            return 0;
        }
        self.circuit_idx_by_ptr
            .get(&(std::ptr::from_ref(circuit) as usize))
            .and_then(|&idx| self.estimated_transcript_ops.get(idx).copied())
            .unwrap_or(0)
    }

    /// Number of VM-op query sites indexed in this table.
    #[must_use]
    pub fn compiled_query_count(&self) -> usize {
        self.compiled_queries.len()
    }

    #[inline]
    #[must_use]
    pub(crate) fn witness_index(&self, ir: &MoonIr, name: &str) -> Option<usize> {
        if !self.matches_ir(ir) {
            return None;
        }
        self.witnesses_by_name.get(name).copied()
    }

    #[inline]
    #[must_use]
    pub(crate) fn witness_result_plan_by_index(&self, ir: &MoonIr, idx: usize) -> Option<&FabPlan> {
        if !self.matches_ir(ir) {
            return None;
        }
        let witness = ir.witnesses.get(idx)?;
        self.witness_result_plans
            .get(idx)?
            .get_or_init(|| FabPlan::compile(&witness.result_type))
            .as_ref()
    }

    #[inline]
    #[must_use]
    pub(crate) fn native_index_by_uniq(&self, ir: &MoonIr, uniq: u64) -> Option<usize> {
        if !self.matches_ir(ir) {
            return None;
        }
        self.natives_by_uniq.get(&uniq).copied()
    }

    #[inline]
    #[must_use]
    pub(crate) fn circuit_index_by_uniq(&self, ir: &MoonIr, uniq: u64) -> Option<usize> {
        if !self.matches_ir(ir) {
            return None;
        }
        self.circuits_by_uniq.get(&uniq).copied()
    }

    #[inline]
    #[must_use]
    pub(crate) fn first_circuit_index(&self, ir: &MoonIr, name: &str) -> Option<usize> {
        if !self.matches_ir(ir) {
            return None;
        }
        self.circuits_by_name.get(name)?.first().copied()
    }

    /// Lazily compile and return the flat register bytecode for `ir.circuits[idx]`.
    #[inline]
    #[must_use]
    pub(crate) fn compiled_circuit(&self, ir: &MoonIr, idx: usize) -> Option<&CompiledCircuit> {
        if !self.matches_ir(ir) {
            return None;
        }
        let circuit = ir.circuits.get(idx)?;
        let slot = self.compiled_circuits.get(idx)?;
        slot.get_or_init(|| compile_circuit(ir, self, circuit))
            .as_ref()
    }

    /// Lazily compile and return the flat register bytecode for `ir.constructor`.
    #[inline]
    #[must_use]
    pub(crate) fn compiled_constructor(&self, ir: &MoonIr) -> Option<&CompiledCircuit> {
        if !self.matches_ir(ir) {
            return None;
        }
        self.compiled_constructor
            .get_or_init(|| compile_constructor(ir, self, &ir.constructor))
            .as_ref()
    }
}

#[derive(Default)]
struct QueryScanAcc<'ir> {
    direct_transcript_ops: usize,
    callees: Vec<(Option<u64>, &'ir str)>,
}

fn estimate_circuit_ops(
    idx: usize,
    direct_ops: &[usize],
    call_refs: &[Vec<(Option<u64>, &str)>],
    circuits_by_uniq: &FxHashMap<u64, usize>,
    circuits_by_name: &FxHashMap<String, Vec<usize>>,
    visited: &mut [bool],
) -> usize {
    if visited[idx] {
        return 0;
    }
    visited[idx] = true;
    let mut total = direct_ops[idx];
    for &(uniq, name) in &call_refs[idx] {
        let callee_idx = uniq
            .and_then(|u| circuits_by_uniq.get(&u).copied())
            .or_else(|| circuits_by_name.get(name).and_then(|v| v.first().copied()));
        if let Some(cidx) = callee_idx {
            total = total.saturating_add(estimate_circuit_ops(
                cidx,
                direct_ops,
                call_refs,
                circuits_by_uniq,
                circuits_by_name,
                visited,
            ));
        }
    }
    total
}

fn register_vm_ops(
    vm_ops: &[VmInstr],
    out: &mut FxHashMap<(usize, usize), OnceLock<Option<CompiledLedgerQuery>>>,
    acc: &mut QueryScanAcc<'_>,
) {
    if vm_ops.is_empty() {
        return;
    }
    acc.direct_transcript_ops = acc.direct_transcript_ops.saturating_add(vm_ops.len());
    out.entry((vm_ops.as_ptr() as usize, vm_ops.len()))
        .or_default();
}

fn collect_stmt_queries<'ir>(
    stmt: &'ir Statement,
    out: &mut FxHashMap<(usize, usize), OnceLock<Option<CompiledLedgerQuery>>>,
    acc: &mut QueryScanAcc<'ir>,
) {
    match stmt {
        Statement::Seq { items } => {
            for item in items {
                collect_stmt_queries(item, out, acc);
            }
        }
        Statement::If { cond, then, r#else } => {
            collect_expr_queries(cond, out, acc);
            collect_stmt_queries(then, out, acc);
            if let Some(else_branch) = r#else {
                collect_stmt_queries(else_branch, out, acc);
            }
        }
        Statement::Const { value, .. } | Statement::Expr { value } => {
            collect_expr_queries(value, out, acc);
        }
        Statement::ConstDestructure { .. } | Statement::Unimplemented | Statement::Noop => {}
    }
}

#[allow(clippy::too_many_lines)]
fn collect_expr_queries<'ir>(
    expr: &'ir Expression,
    out: &mut FxHashMap<(usize, usize), OnceLock<Option<CompiledLedgerQuery>>>,
    acc: &mut QueryScanAcc<'ir>,
) {
    match expr {
        Expression::AdtOp(inv) => {
            register_vm_ops(&inv.vm_ops, out, acc);
            for elem in &inv.field_path {
                if let PathElement::Expr { value, .. } = elem {
                    collect_expr_queries(value, out, acc);
                }
            }
            for arg in &inv.arguments {
                collect_expr_queries(arg, out, acc);
            }
        }
        Expression::Emit {
            payload, vm_ops, ..
        } => {
            register_vm_ops(vm_ops, out, acc);
            collect_expr_queries(payload, out, acc);
        }
        Expression::If { cond, then, r#else } => {
            collect_expr_queries(cond, out, acc);
            collect_expr_queries(then, out, acc);
            collect_expr_queries(r#else, out, acc);
        }
        Expression::Tuple { items } | Expression::Vector { items } => {
            for item in items {
                collect_expr_queries(&item.value, out, acc);
            }
        }
        Expression::Seq { items }
        | Expression::New {
            arguments: items, ..
        } => {
            for item in items {
                collect_expr_queries(item, out, acc);
            }
        }
        Expression::Call {
            function,
            function_uniq,
            arguments,
        } => {
            acc.callees.push((*function_uniq, function.as_str()));
            for arg in arguments {
                collect_expr_queries(arg, out, acc);
            }
        }
        Expression::ContractCall {
            address, arguments, ..
        } => {
            acc.direct_transcript_ops = acc
                .direct_transcript_ops
                .saturating_add(CLAIM_CONTRACT_CALL_OP_COUNT);
            collect_expr_queries(address, out, acc);
            for arg in arguments {
                collect_expr_queries(arg, out, acc);
            }
        }
        Expression::Binop { left, right, .. }
        | Expression::Eq { left, right, .. }
        | Expression::Neq { left, right, .. }
        | Expression::And { left, right }
        | Expression::Or { left, right } => {
            collect_expr_queries(left, out, acc);
            collect_expr_queries(right, out, acc);
        }
        Expression::VectorRef { value, index, .. } | Expression::BytesRef { value, index, .. } => {
            collect_expr_queries(value, out, acc);
            collect_expr_queries(index, out, acc);
        }
        Expression::VectorSlice { value, start, .. }
        | Expression::BytesSlice { value, start, .. } => {
            collect_expr_queries(value, out, acc);
            collect_expr_queries(start, out, acc);
        }
        Expression::Assert { cond, .. } => collect_expr_queries(cond, out, acc),
        Expression::EltRef { value, .. }
        | Expression::TupleRef { value, .. }
        | Expression::TupleSlice { value, .. }
        | Expression::Not { value }
        | Expression::Assign { value, .. }
        | Expression::Cast { value, .. }
        | Expression::SafeCast { value, .. }
        | Expression::DowncastUnsigned { value, .. }
        | Expression::FieldToBytes { value, .. }
        | Expression::CastToField { value, .. }
        | Expression::CastFromField { value, .. }
        | Expression::CastFromBytes { value, .. }
        | Expression::VectorToBytes { value, .. }
        | Expression::BytesToVector { value, .. }
        | Expression::CastFromEnum { value, .. }
        | Expression::CastToEnum { value, .. } => {
            collect_expr_queries(value, out, acc);
        }
        Expression::Literal { .. }
        | Expression::Var { .. }
        | Expression::Default { .. }
        | Expression::EnumRef { .. }
        | Expression::Map { .. }
        | Expression::Fold { .. }
        | Expression::Unimplemented => {}
    }
}