tpt-telos-codegen 0.1.0

Rust/Go dual-backend code generation and FFI bridge for tpt-telos
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
//! Rust backend code generation for tpt-telos (Phase 2 milestone).
//!
//! [`generate_program`] lowers a set of parsed modules into a single,
//! self-contained Rust library source string. The generated code:
//!   * declares a `struct` for every user type that is accessed by field,
//!   * attaches an `invariants()` method to each such struct for every
//!     `invariant` block of the same name,
//!   * emits one `fn` per `func`, threading `&mut` only to parameters that are
//!     actually mutated,
//!   * carries the original `requires` / `ensures` contracts as doc-comments.
//!
//! The function bodies come from the agentic transpiler's final candidates
//! (see [`tpt_telos_agent`]), so the emitted code is mathematically verified before
//! it is written out.

use std::collections::{BTreeSet, HashMap, HashSet};

use tpt_telos_agent::FuncOutcome;
use tpt_telos_parser::ast::*;

pub mod eject;
pub mod ffi;
pub mod go;
pub mod project;

pub use project::{generate_project, GeneratedFile, Project};

/// Maps a type name to the set of fields that must appear on its generated
/// struct.
pub(crate) type TypeFields = HashMap<String, BTreeSet<String>>;

/// Classification of a single (post-analysis) function parameter, shared by the
/// Rust backend, the Go backend, and the automatic FFI bridge so all three
/// agree on calling conventions.
#[derive(Debug, Clone)]
pub(crate) enum InputParam {
    /// A scalar (`i64`) input parameter.
    Scalar { name: String },
    /// A parameter of a struct type. `mutated` is true when the function body
    /// writes to any of its fields (so it is passed `&mut` in Rust and by
    /// pointer across the FFI boundary).
    Struct {
        name: String,
        ty: String,
        mutated: bool,
        fields: Vec<String>,
    },
}

/// The result of analysing a function body: its effective input parameters and
/// whether it produces a scalar return value (an assignment to a bare
/// parameter/variable name).
#[derive(Debug, Clone)]
pub(crate) struct FuncAnalysis {
    pub inputs: Vec<InputParam>,
    pub scalar_out: Option<String>,
}

/// Analyse a function's parameters and body once, deriving the mutation set and
/// scalar-return output so every backend lowers the same signature.
pub(crate) fn analyze_func(f: &Func, stmts: &[Stmt], types: &TypeFields) -> FuncAnalysis {
    let mut mutated: HashSet<String> = HashSet::new();
    let mut scalar_out: Option<String> = None;
    for stmt in stmts {
        let assigns = match stmt {
            Stmt::MutateState(a) => a,
            Stmt::Assign(a) => std::slice::from_ref(a),
        };
        for a in assigns {
            match &a.target {
                Expr::Field { base, .. } => {
                    mutated.insert(base.clone());
                }
                Expr::Var(v) if scalar_out.is_none() => {
                    scalar_out = Some(v.clone());
                }
                _ => {}
            }
        }
    }

    let mut inputs = Vec::new();
    for p in &f.params {
        if Some(&p.name) == scalar_out.as_ref() {
            continue; // produced as the return value, not an input
        }
        let ty = p.ty.name();
        let struct_fields = types.get(ty).cloned().unwrap_or_default();
        if struct_fields.is_empty() {
            inputs.push(InputParam::Scalar {
                name: p.name.clone(),
            });
        } else {
            inputs.push(InputParam::Struct {
                name: p.name.clone(),
                ty: ty.to_string(),
                mutated: mutated.contains(&p.name),
                fields: struct_fields.into_iter().collect(),
            });
        }
    }

    FuncAnalysis { inputs, scalar_out }
}

/// Generate a full Rust library source for all modules.
///
/// `bodies` supplies the final (verified) body for each function, keyed by
/// `"<module>.<func>"`. A function absent from the map falls back to its body
/// in the parsed source.
///
/// # Examples
///
/// ```
/// use tpt_telos_parser::parse;
/// use tpt_telos_agent::{StaticAgent, transpile_module};
/// use tpt_telos_codegen::generate_program;
///
/// let src = r#"
///     module Bank {
///         invariant Wallet { balance >= 0 }
///         func deposit(w: Wallet, amount: PositiveInt)
///             ensures w.balance == old(w.balance) + amount
///         ;
///     }
/// "#;
///
/// let modules = parse(src).unwrap();
/// let agent = StaticAgent::new();
/// let outcomes: Vec<_> = modules.iter()
///     .flat_map(|m| transpile_module(m, &agent).unwrap())
///     .collect();
///
/// let rust_src = generate_program(&modules, &outcomes);
///
/// assert!(rust_src.contains("pub struct Wallet"));
/// assert!(rust_src.contains("pub fn deposit"));
/// ```
pub fn generate_program(modules: &[Module], outcomes: &[FuncOutcome]) -> String {
    let bodies = collect_bodies(outcomes);
    let refs: Vec<&Module> = modules.iter().collect();
    render_rust(&refs, &bodies)
}

/// Render a Rust library for the given modules using the supplied verified
/// bodies. Shared by [`generate_program`] and the project assembler so both
/// emit identical Rust.
pub(crate) fn render_rust(modules: &[&Module], bodies: &HashMap<String, Vec<Stmt>>) -> String {
    let mut out = String::new();
    out.push_str("// Generated by tpt-telos. Do not edit by hand.\n");
    out.push_str("// Mathematical contracts were verified by the tpt-telos SMT core.\n");
    out.push_str("#![allow(dead_code)]\n\n");

    let mut struct_types: TypeFields = HashMap::new();
    for m in modules {
        collect_types(m, &mut struct_types);
    }

    for m in modules {
        out.push_str(&generate_module(m, bodies, &struct_types));
        out.push('\n');
    }

    out
}

/// Collect the final (verified) body for each function, keyed by function name.
pub(crate) fn collect_bodies(outcomes: &[FuncOutcome]) -> HashMap<String, Vec<Stmt>> {
    let mut bodies: HashMap<String, Vec<Stmt>> = HashMap::new();
    for o in outcomes {
        bodies.insert(o.func_name.clone(), o.final_candidate.stmts.clone());
    }
    bodies
}

pub(crate) fn collect_types(module: &Module, types: &mut TypeFields) {
    // Field accesses keyed by the *base name* they were written with (usually a
    // parameter name). We re-point these onto the parameter's type afterwards.
    let mut raw_fields: HashMap<String, BTreeSet<String>> = HashMap::new();
    let mut field_users: HashSet<String> = HashSet::new();

    let scan_expr =
        |e: &Expr, users: &mut HashSet<String>, raw: &mut HashMap<String, BTreeSet<String>>| {
            let mut fields: Vec<(String, String)> = Vec::new();
            collect_fields(e, &mut fields);
            for (base, field) in fields {
                users.insert(base.clone());
                raw.entry(base.clone()).or_default().insert(field);
            }
        };

    for item in &module.items {
        if let Item::Func(f) = item {
            for r in &f.requires {
                scan_expr(r, &mut field_users, &mut raw_fields);
            }
            for e in &f.ensures {
                scan_expr(e, &mut field_users, &mut raw_fields);
            }
            for stmt in &f.body {
                let assigns = match stmt {
                    Stmt::MutateState(a) => a,
                    Stmt::Assign(a) => std::slice::from_ref(a),
                };
                for a in assigns {
                    if let Expr::Field { base, field } = &a.target {
                        field_users.insert(base.clone());
                        raw_fields
                            .entry(base.clone())
                            .or_default()
                            .insert(field.clone());
                    }
                    scan_expr(&a.value, &mut field_users, &mut raw_fields);
                }
            }
        }
    }

    // Re-point: a base that is a parameter of a named type is really that
    // *type's* field.
    for item in &module.items {
        if let Item::Func(f) = item {
            for p in &f.params {
                if let Some(fields) = raw_fields.remove(&p.name) {
                    types
                        .entry(p.ty.name().to_string())
                        .or_default()
                        .extend(fields);
                }
            }
        }
    }
    // Any bases that were never parameters keep their raw (base-named) fields.
    for (base, fields) in raw_fields {
        types.entry(base).or_default().extend(fields);
    }
}

fn collect_fields(expr: &Expr, out: &mut Vec<(String, String)>) {
    match expr {
        Expr::Field { base, field } => out.push((base.clone(), field.clone())),
        Expr::Old(e) => collect_fields(e, out),
        Expr::Unary { expr, .. } => collect_fields(expr, out),
        Expr::Bin { lhs, rhs, .. } => {
            collect_fields(lhs, out);
            collect_fields(rhs, out);
        }
        _ => {}
    }
}

fn generate_module(
    module: &Module,
    bodies: &HashMap<String, Vec<Stmt>>,
    types: &TypeFields,
) -> String {
    let route = tpt_telos_router::route(&module.attributes);
    let mut out = String::new();
    out.push_str(&format!(
        "// ===== module {} (target: {}) =====\n",
        module.name,
        route.target.as_str()
    ));

    // Structs.
    for (ty, fields) in types {
        if fields.is_empty() {
            continue;
        }
        out.push_str(&format!("pub struct {} {{\n", ty));
        for f in fields {
            out.push_str(&format!("    pub {}: i64,\n", f));
        }
        out.push_str("}\n\n");
    }

    // Invariants as `impl` methods.
    for item in &module.items {
        if let Item::Invariant(inv) = item {
            let conds = inv
                .constraints
                .iter()
                .map(render_inv)
                .collect::<Vec<_>>()
                .join(" && ");
            out.push_str(&format!(
                "impl {} {{\n    pub fn satisfies_invariants(&self) -> bool {{\n        {}\n    }}\n}}\n\n",
                inv.name, conds
            ));
        }
    }

    // Functions.
    for item in &module.items {
        if let Item::Func(f) = item {
            let key = f.name.clone();
            let stmts = bodies.get(&key).cloned().unwrap_or_else(|| f.body.clone());
            if f.is_ejected() {
                out.push_str(&eject::render_rust_ejected(f, &stmts, types));
            } else {
                out.push_str(&render_func(f, &stmts, types));
            }
            out.push('\n');
        }
    }

    out
}

pub(crate) fn render_func(f: &Func, stmts: &[Stmt], types: &TypeFields) -> String {
    let mut out = String::new();

    // Doc-comment carrying the contract.
    out.push_str(&format!("/// {}({})\n", f.name, render_params_sig(f)));
    for r in &f.requires {
        out.push_str(&format!("/// requires: {}\n", render_expr_doc(r)));
    }
    for e in &f.ensures {
        out.push_str(&format!("/// ensures:  {}\n", render_expr_doc(e)));
    }

    // Determine mutated struct params and scalar-return outputs via the shared
    // analysis so the Rust, Go, and FFI backends agree on the signature.
    let analysis = analyze_func(f, stmts, types);
    let scalar_out = analysis.scalar_out.clone();

    // Build parameter list + return type.
    let mut params = Vec::new();
    for input in &analysis.inputs {
        match input {
            InputParam::Scalar { name } => {
                params.push(format!("{}: i64", name));
            }
            InputParam::Struct {
                name, ty, mutated, ..
            } => {
                let borrow = if *mutated { "&mut " } else { "&" };
                params.push(format!("{}: {}{}", name, borrow, ty));
            }
        }
    }

    let ret = if scalar_out.is_some() { " -> i64" } else { "" };

    out.push_str(&format!(
        "pub fn {}({}){} {{\n",
        f.name,
        params.join(", "),
        ret
    ));

    // Body.
    for stmt in stmts {
        match stmt {
            Stmt::MutateState(assigns) => {
                for a in assigns {
                    out.push_str("    ");
                    out.push_str(&render_assign(a));
                    out.push_str(";\n");
                }
            }
            Stmt::Assign(a) => {
                let value = render_expr(&a.value);
                if scalar_out.as_deref() == Some(target_var(&a.target)) {
                    out.push_str(&format!("    let {} = {};\n", target_var(&a.target), value));
                } else {
                    out.push_str("    ");
                    out.push_str(&render_assign(a));
                    out.push_str(";\n");
                }
            }
        }
    }

    if let Some(outvar) = &scalar_out {
        out.push_str(&format!("    {}\n", outvar));
    }

    out.push_str("}\n");
    out
}

pub(crate) fn render_params_sig(f: &Func) -> String {
    f.params
        .iter()
        .map(|p| format!("{}: {}", p.name, p.ty.name()))
        .collect::<Vec<_>>()
        .join(", ")
}

pub(crate) fn target_var(e: &Expr) -> &str {
    match e {
        Expr::Var(v) => v,
        _ => "",
    }
}

pub(crate) fn render_assign(a: &Assign) -> String {
    let lhs = render_expr(&a.target);
    let rhs = render_expr(&a.value);
    let op = match a.op {
        AssignOp::Set => "=",
        AssignOp::Add => "+=",
        AssignOp::Sub => "-=",
    };
    format!("{} {} {}", lhs, op, rhs)
}

pub(crate) fn render_expr(e: &Expr) -> String {
    match e {
        Expr::Int(n) => n.to_string(),
        Expr::Var(v) => v.clone(),
        Expr::Field { base, field } => format!("{}.{}", base, field),
        Expr::Old(inner) => render_expr(inner),
        Expr::Unary { op, expr } => match op {
            UnOp::Neg => format!("-{}", render_expr(expr)),
        },
        Expr::Bin { op, lhs, rhs } => {
            let s = match op {
                BinOp::Add => "+",
                BinOp::Sub => "-",
                BinOp::Mul => "*",
                BinOp::Div => "/",
                BinOp::Eq => "==",
                BinOp::Ne => "!=",
                BinOp::Lt => "<",
                BinOp::Le => "<=",
                BinOp::Gt => ">",
                BinOp::Ge => ">=",
                BinOp::And => "&&",
                BinOp::Or => "||",
            };
            format!("{} {} {}", render_expr(lhs), s, render_expr(rhs))
        }
    }
}

/// Like [`render_expr`] but preserves `old(...)` so contracts read faithfully in
/// doc-comments.
pub(crate) fn render_expr_doc(e: &Expr) -> String {
    match e {
        Expr::Int(n) => n.to_string(),
        Expr::Var(v) => v.clone(),
        Expr::Field { base, field } => format!("{}.{}", base, field),
        Expr::Old(inner) => format!("old({})", render_expr_doc(inner)),
        Expr::Unary { op, expr } => match op {
            UnOp::Neg => format!("-{}", render_expr_doc(expr)),
        },
        Expr::Bin { op, lhs, rhs } => {
            let s = match op {
                BinOp::Add => "+",
                BinOp::Sub => "-",
                BinOp::Mul => "*",
                BinOp::Div => "/",
                BinOp::Eq => "==",
                BinOp::Ne => "!=",
                BinOp::Lt => "<",
                BinOp::Le => "<=",
                BinOp::Gt => ">",
                BinOp::Ge => ">=",
                BinOp::And => "&&",
                BinOp::Or => "||",
            };
            format!("{} {} {}", render_expr_doc(lhs), s, render_expr_doc(rhs))
        }
    }
}

/// Render an expression inside an `impl` method body, prefixing field/var
/// references with `self.` (invariants are written against the struct's own
/// fields).
fn render_inv(e: &Expr) -> String {
    match e {
        Expr::Int(n) => n.to_string(),
        Expr::Var(v) => format!("self.{}", v),
        Expr::Field { field, .. } => format!("self.{}", field),
        Expr::Old(inner) => render_inv(inner),
        Expr::Unary { op, expr } => match op {
            UnOp::Neg => format!("-{}", render_inv(expr)),
        },
        Expr::Bin { op, lhs, rhs } => {
            let s = match op {
                BinOp::Add => "+",
                BinOp::Sub => "-",
                BinOp::Mul => "*",
                BinOp::Div => "/",
                BinOp::Eq => "==",
                BinOp::Ne => "!=",
                BinOp::Lt => "<",
                BinOp::Le => "<=",
                BinOp::Gt => ">",
                BinOp::Ge => ">=",
                BinOp::And => "&&",
                BinOp::Or => "||",
            };
            format!("{} {} {}", render_inv(lhs), s, render_inv(rhs))
        }
    }
}

// ===========================================================================
// Unit tests for the individual codegen pieces (struct mutability, invariant
// `impl` generation, doc-comment emission, the shared `analyze_func`, type
// collection, and the eject hatch). These exercise the generators in isolation
// from the full `generate_program` / `generate_project` pipeline tests in
// `tests/gen.rs`.
// ===========================================================================

#[cfg(test)]
mod tests {
    use std::collections::{BTreeSet, HashMap};

    use tpt_telos_parser::ast::*;

    use crate::{
        analyze_func, collect_types, eject, generate_program, go, render_func, InputParam,
        TypeFields,
    };

    fn t_var(v: &str) -> Expr {
        Expr::Var(v.to_string())
    }
    fn t_int(n: i64) -> Expr {
        Expr::Int(n)
    }
    fn t_field(base: &str, f: &str) -> Expr {
        Expr::Field {
            base: base.to_string(),
            field: f.to_string(),
        }
    }
    fn t_old(e: Expr) -> Expr {
        Expr::Old(Box::new(e))
    }
    fn t_bin(op: BinOp, l: Expr, r: Expr) -> Expr {
        Expr::Bin {
            op,
            lhs: Box::new(l),
            rhs: Box::new(r),
        }
    }
    fn t_assign(target: Expr, op: AssignOp, value: Expr) -> Assign {
        Assign { target, op, value }
    }
    fn t_func(
        name: &str,
        params: Vec<Param>,
        requires: Vec<Expr>,
        ensures: Vec<Expr>,
        body: Vec<Stmt>,
    ) -> Func {
        Func {
            attributes: vec![],
            name: name.to_string(),
            params,
            requires,
            ensures,
            body,
            elided: false,
        }
    }

    fn counter_types() -> TypeFields {
        let mut m = HashMap::new();
        m.insert("Counter".to_string(), BTreeSet::from(["v".to_string()]));
        m
    }

    // ---- analyze_func ----

    #[test]
    fn analyze_scalar_param() {
        let f = t_func(
            "f",
            vec![Param {
                name: "x".into(),
                ty: Type::Named("i64".into()),
            }],
            vec![],
            vec![],
            vec![],
        );
        let a = analyze_func(&f, &f.body, &HashMap::new());
        assert!(matches!(a.inputs[0], InputParam::Scalar { .. }));
        assert!(a.scalar_out.is_none());
    }

    #[test]
    fn analyze_struct_param_mutated_is_borrow_mut() {
        let f = t_func(
            "f",
            vec![Param {
                name: "c".into(),
                ty: Type::Named("Counter".into()),
            }],
            vec![],
            vec![],
            vec![Stmt::MutateState(vec![t_assign(
                t_field("c", "v"),
                AssignOp::Set,
                t_int(0),
            )])],
        );
        let a = analyze_func(&f, &f.body, &counter_types());
        match &a.inputs[0] {
            InputParam::Struct { mutated, .. } => assert!(*mutated),
            other => panic!("expected struct param, got {other:?}"),
        }
    }

    #[test]
    fn analyze_struct_param_unmutated_is_borrow_ref() {
        let f = t_func(
            "f",
            vec![Param {
                name: "c".into(),
                ty: Type::Named("Counter".into()),
            }],
            vec![],
            vec![],
            vec![],
        );
        let a = analyze_func(&f, &f.body, &counter_types());
        match &a.inputs[0] {
            InputParam::Struct { mutated, .. } => assert!(!*mutated),
            other => panic!("expected struct param, got {other:?}"),
        }
    }

    #[test]
    fn analyze_scalar_return_value() {
        // A bare assignment to a parameter-named variable becomes the scalar
        // return; that parameter is then excluded from the inputs.
        let f = t_func(
            "f",
            vec![
                Param {
                    name: "c".into(),
                    ty: Type::Named("Counter".into()),
                },
                Param {
                    name: "result".into(),
                    ty: Type::Named("i64".into()),
                },
            ],
            vec![],
            vec![],
            vec![Stmt::Assign(t_assign(
                t_var("result"),
                AssignOp::Set,
                t_bin(BinOp::Add, t_field("c", "v"), t_int(1)),
            ))],
        );
        let a = analyze_func(&f, &f.body, &counter_types());
        // `result` is the return, so only `c` remains as an input.
        assert_eq!(a.inputs.len(), 1);
        assert_eq!(a.scalar_out.as_deref(), Some("result"));
    }

    // ---- render_func ----

    #[test]
    fn render_func_emits_contract_doc_comments() {
        let f = t_func(
            "transfer",
            vec![Param {
                name: "c".into(),
                ty: Type::Named("Counter".into()),
            }],
            vec![t_bin(BinOp::Ge, t_field("c", "v"), t_int(0))],
            vec![t_bin(
                BinOp::Eq,
                t_field("c", "v"),
                t_bin(BinOp::Add, t_old(t_field("c", "v")), t_int(1)),
            )],
            vec![Stmt::MutateState(vec![t_assign(
                t_field("c", "v"),
                AssignOp::Set,
                t_bin(BinOp::Add, t_field("c", "v"), t_int(1)),
            )])],
        );
        let out = render_func(&f, &f.body, &counter_types());
        assert!(out.contains("/// requires:"), "missing requires doc: {out}");
        assert!(out.contains("/// ensures:"), "missing ensures doc: {out}");
        // The `old(...)` must be preserved faithfully in the doc comment.
        assert!(out.contains("old(c.v)"), "old() lost in doc: {out}");
        // The mutated struct param is threaded as `&mut`.
        assert!(
            out.contains("c: &mut Counter"),
            "expected &mut param: {out}"
        );
    }

    #[test]
    fn render_func_emits_scalar_return() {
        let f = t_func(
            "f",
            vec![
                Param {
                    name: "c".into(),
                    ty: Type::Named("Counter".into()),
                },
                Param {
                    name: "result".into(),
                    ty: Type::Named("i64".into()),
                },
            ],
            vec![],
            vec![],
            vec![Stmt::Assign(t_assign(
                t_var("result"),
                AssignOp::Set,
                t_bin(BinOp::Add, t_field("c", "v"), t_int(1)),
            ))],
        );
        let out = render_func(&f, &f.body, &counter_types());
        assert!(out.contains("-> i64"), "missing return type: {out}");
        assert!(
            out.contains("let result ="),
            "missing return binding: {out}"
        );
        // The trailing bare expression returns the scalar.
        assert!(
            out.lines().any(|l| l.trim() == "result"),
            "missing return expression: {out}"
        );
    }

    // ---- collect_types ----

    #[test]
    fn collect_types_gathers_struct_fields() {
        let module = Module {
            attributes: vec![],
            name: "M".into(),
            items: vec![Item::Func(t_func(
                "f",
                vec![Param {
                    name: "c".into(),
                    ty: Type::Named("Counter".into()),
                }],
                vec![t_bin(BinOp::Ge, t_field("c", "v"), t_int(0))],
                vec![],
                vec![Stmt::MutateState(vec![t_assign(
                    t_field("c", "v"),
                    AssignOp::Set,
                    t_int(0),
                )])],
            ))],
        };
        let mut types = HashMap::new();
        collect_types(&module, &mut types);
        assert_eq!(types.get("Counter").map(|s| s.len()), Some(1));
        assert!(types.get("Counter").unwrap().contains("v"));
    }

    // ---- go::exported ----

    #[test]
    fn go_exported_capitalises() {
        assert_eq!(go::exported("enqueue"), "Enqueue");
        assert_eq!(go::exported("pending"), "Pending");
        assert_eq!(go::exported("Queue"), "Queue");
    }

    // ---- eject hatch ----

    #[test]
    fn eject_renders_opaque_block_and_guard() {
        let mut f = t_func(
            "withdraw",
            vec![Param {
                name: "b".into(),
                ty: Type::Named("Balance".into()),
            }],
            vec![t_bin(BinOp::Ge, t_field("b", "amount"), t_int(0))],
            vec![t_bin(
                BinOp::Eq,
                t_field("b", "amount"),
                t_bin(BinOp::Sub, t_old(t_field("b", "amount")), t_int(1)),
            )],
            vec![Stmt::MutateState(vec![t_assign(
                t_field("b", "amount"),
                AssignOp::Set,
                t_int(0),
            )])],
        );
        f.attributes.push(Attribute {
            name: "eject".into(),
            args: vec![Arg::Flag("rust".into())],
        });

        let mut types = HashMap::new();
        types.insert(
            "Balance".to_string(),
            BTreeSet::from(["amount".to_string()]),
        );
        let out = eject::render_rust_ejected(&f, &f.body, &types);

        // The trusted opaque block and the generated guard are both present.
        assert!(
            out.contains("fn withdraw_impl"),
            "missing opaque block: {out}"
        );
        assert!(out.contains("pub fn withdraw"), "missing guard: {out}");
        // The guard enforces the contracts with runtime assertions.
        assert!(out.contains("assert!("), "missing contract guard: {out}");
        // `old(...)` is captured into a snapshot local in the guard.
        assert!(
            out.contains("__old_b_amount"),
            "missing old snapshot: {out}"
        );
    }

    // ---- generate_program (structural) ----

    #[test]
    fn generate_program_emits_struct_and_invariant_method() {
        // The struct fields come from the function's field accesses (the
        // invariant alone does not populate the struct type).
        let src =
            "module M { invariant Counter { v >= 0 } func f(c: Counter) requires c.v >= 0 { } }";
        let modules = tpt_telos_parser::parse(src).unwrap();
        let rust = generate_program(&modules, &[]);
        assert!(rust.contains("pub struct Counter"));
        assert!(rust.contains("pub fn satisfies_invariants"));
        assert!(rust.contains("pub fn f"));
    }
}