assura-codegen 0.2.0

Rust code generation from type-checked Assura contracts
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
//! Codec registry, generic block, and Rust formatting code generation.

use super::*;

// Codec registry (FMT.4)
// ---------------------------------------------------------------------------

pub(crate) fn generate_codec_registry(cr: &CodecRegistryDecl, code: &mut String) {
    use crate::hir::*;

    let output_ty = if cr.output_type.is_empty() {
        "()".to_string()
    } else {
        cr.output_type.join(" ")
    };

    let mut body: Vec<RustStmt> = Vec::new();

    for codec in &cr.codecs {
        match &codec.magic {
            MagicPattern::Bytes { bytes, prefix: _ } => {
                let len = bytes.len();
                let byte_checks: Vec<String> = bytes
                    .iter()
                    .enumerate()
                    .map(|(i, b)| format!("data[{i}] == 0x{b:02X}"))
                    .collect();
                let cond = byte_checks.join(" && ");
                body.push(RustStmt::Raw(format!(
                    "if data.len() >= {len} && {cond} {{\n    return Some({}(data));\n}}",
                    codec.decoder
                )));
            }
            MagicPattern::Extension(exts) => {
                body.push(RustStmt::Comment(format!(
                    "Extension-based detection: {exts:?}"
                )));
            }
            MagicPattern::Probe(fn_name) => {
                body.push(RustStmt::Raw(format!(
                    "if {fn_name}(data) {{\n    return Some({}(data));\n}}",
                    codec.decoder
                )));
            }
        }
    }

    body.push(RustStmt::Expr(RustExpr::Ident("None".into())));

    let item = RustItem::Fn(RustFn {
        name: format!("dispatch_{}", cr.name.to_lowercase()),
        params: vec![RustParam {
            name: "data".into(),
            ty: RustType::Raw("&[u8]".into()),
        }],
        ret: Some(RustType::Raw(format!("Option<{output_ty}>"))),
        body,
        doc: vec![format!("Codec registry `{}` dispatch function.", cr.name)],
        ..RustFn::default()
    });
    code.push_str(&render_item_raw(&item));
}

// ---------------------------------------------------------------------------
// Generic blocks (feature, incremental, etc.)
// ---------------------------------------------------------------------------

pub(crate) fn generate_block(kind: &BlockKind, name: &str, body: &[Clause], code: &mut String) {
    use crate::hir::*;

    // Interface blocks generate Rust traits
    if *kind == BlockKind::Interface {
        generate_interface_trait(name, body, code);
        return;
    }

    // Feature blocks with only Other clauses: compile-time only, no Rust codegen needed.
    if *kind == BlockKind::Feature
        && body
            .iter()
            .all(|c| matches!(c.kind, ClauseKind::Other(_) | ClauseKind::Requires))
    {
        let item = RustItem::Comment(format!("{kind} {name}: compile-time feature flag"));
        code.push_str(&render_item_raw(&item));
        code.push('\n');
        return;
    }

    // Table blocks: compile-time verified by SMT, no Rust codegen.
    if *kind == BlockKind::Table {
        let item = RustItem::Comment(format!("{kind} {name}: compile-time verified by SMT"));
        code.push_str(&render_item_raw(&item));
        code.push('\n');
        return;
    }

    // MISC.1 incremental state machines: verified by SMT. Emitting a mod with
    // typestate `self.state` asserts and trailing `///` metadata for `on` /
    // `transition` clauses produced invalid Rust (doc comments with no
    // following item; free-fn `self`). Keep a single compile-time marker.
    if *kind == BlockKind::Incremental {
        let item = RustItem::Comment(format!(
            "incremental {name}: state machine verified by SMT (MISC.1); no runtime scaffolding"
        ));
        code.push_str(&render_item_raw(&item));
        code.push('\n');
        return;
    }

    // Other blocks: generate as a module with documented constants/assertions
    let lower_name = name.to_lowercase();
    let mut items: Vec<RustItem> = Vec::new();

    for clause in body {
        let expr = expr_to_rust(&clause.body);
        match clause.kind {
            ClauseKind::Ensures | ClauseKind::Invariant => {
                items.push(RustItem::Fn(RustFn {
                    name: format!("check_{lower_name}"),
                    body: vec![RustStmt::Raw(format!("debug_assert!({expr});"))],
                    is_pub: true,
                    doc: vec![format!("Invariant: {expr}")],
                    ..RustFn::default()
                }));
            }
            ClauseKind::Requires => {
                items.push(RustItem::Const(RustConst {
                    name: "PRECONDITION".into(),
                    ty: RustType::Raw("&str".into()),
                    value: format!("\"{}\"", expr.replace('"', "\\\"")),
                    is_pub: true,
                    doc: vec![format!("Precondition: {expr}")],
                }));
            }
            ClauseKind::Rule => {
                items.push(RustItem::Fn(RustFn {
                    name: format!("check_rule_{lower_name}"),
                    body: vec![RustStmt::Raw(format!("debug_assert!({expr});"))],
                    is_pub: true,
                    doc: vec![format!("Rule: {expr}")],
                    ..RustFn::default()
                }));
            }
            ClauseKind::MustNot => {
                items.push(RustItem::Fn(RustFn {
                    name: format!("check_must_not_{lower_name}"),
                    body: vec![RustStmt::Raw(format!("debug_assert!(!({expr}));"))],
                    is_pub: true,
                    doc: vec![format!("MustNot: {expr}")],
                    ..RustFn::default()
                }));
            }
            ClauseKind::Ordering => {
                // Use // not /// so trailing metadata does not leave a doc
                // comment with no following item (invalid Rust).
                items.push(RustItem::Raw(format!("// Ordering: {expr}\n")));
                if let Some(ord) = resolve_ordering_variant(&clause.body) {
                    items.push(RustItem::Raw(format!(
                        "const ORDERING: std::sync::atomic::Ordering = std::sync::atomic::Ordering::{ord};\n"
                    )));
                }
            }
            ClauseKind::Effects => {
                items.push(RustItem::Raw(format!("// Effects: {expr}\n")));
            }
            ClauseKind::Modifies => {
                items.push(RustItem::Raw(format!("// Modifies: {expr}\n")));
            }
            ClauseKind::Input => {
                items.push(RustItem::Raw(format!("// Input: {expr}\n")));
            }
            ClauseKind::Output => {
                items.push(RustItem::Raw(format!("// Output: {expr}\n")));
            }
            ClauseKind::Errors => {
                items.push(RustItem::Raw(format!("// Errors: {expr}\n")));
            }
            ClauseKind::DataFlow => {
                items.push(RustItem::Raw(format!("// DataFlow: {expr}\n")));
            }
            ClauseKind::Decreases => {
                items.push(RustItem::Raw(format!("// Decreases: {expr}\n")));
            }
            ClauseKind::Other(ref kind_name) => {
                items.push(RustItem::Raw(format!("// {kind_name}: {expr}\n")));
            }
        }
    }

    let m = RustItem::Mod(RustMod {
        name: format!("block_{lower_name}"),
        items,
        is_pub: true,
        doc: vec![format!("{kind}: {name}")],
    });
    code.push_str(&render_item_raw(&m));
}

// ---------------------------------------------------------------------------
// Rust formatting via prettyplease
// ---------------------------------------------------------------------------

/// Format a Rust source string via prettyplease.
///
/// If parsing fails (the generated code is not valid Rust syntax),
/// returns the input unchanged with a comment noting the failure.
pub(crate) fn format_rust(code: &str) -> String {
    match syn::parse_file(code) {
        Ok(syntax_tree) => prettyplease::unparse(&syntax_tree),
        Err(e) => {
            eprintln!("warning: generated Rust has syntax errors, skipping formatting: {e}");
            format!("// WARNING: prettyplease formatting skipped (parse error: {e})\n\n{code}")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assura_ast::Spanned;

    fn mk_clause(kind: ClauseKind, body: SpExpr) -> Clause {
        Clause {
            kind,
            body,
            effect_variables: vec![],
        }
    }

    // ---- generate_block ----

    #[test]
    fn block_interface_delegates_to_trait() {
        let clauses = vec![mk_clause(
            ClauseKind::Other("method".into()),
            Spanned::no_span(Expr::Ident("compute".into())),
        )];
        let mut code = String::new();
        generate_block(&BlockKind::Interface, "Computable", &clauses, &mut code);
        assert!(code.contains("pub trait Computable"));
    }

    #[test]
    fn block_feature_compile_time_only() {
        let clauses = vec![mk_clause(
            ClauseKind::Other("flag".into()),
            Spanned::no_span(Expr::Literal(Literal::Bool(true))),
        )];
        let mut code = String::new();
        generate_block(&BlockKind::Feature, "my_flag", &clauses, &mut code);
        assert!(code.contains("compile-time feature flag"));
        assert!(!code.contains("pub mod"));
    }

    #[test]
    fn block_table_compile_time_smt() {
        let mut code = String::new();
        generate_block(&BlockKind::Table, "lookup", &[], &mut code);
        assert!(code.contains("compile-time verified by SMT"));
    }

    #[test]
    fn block_incremental_is_smt_marker_only() {
        // #833 / PR #834: must not emit mod with trailing /// or free-fn `self`.
        let clauses = vec![
            mk_clause(
                ClauseKind::Invariant,
                Spanned::no_span(Expr::Field(
                    Box::new(Spanned::no_span(Expr::Ident("self".into()))),
                    "state".into(),
                )),
            ),
            mk_clause(
                ClauseKind::Other("on".into()),
                Spanned::no_span(Expr::Raw(vec![
                    "step".into(),
                    "requires".into(),
                    "true".into(),
                ])),
            ),
        ];
        let mut code = String::new();
        generate_block(
            &BlockKind::Incremental,
            "InflateDecoder",
            &clauses,
            &mut code,
        );
        assert!(
            code.contains("MISC.1") || code.contains("incremental InflateDecoder"),
            "expected SMT marker, got: {code}"
        );
        assert!(
            !code.contains("pub mod block_"),
            "incremental must not emit a mod: {code}"
        );
        assert!(
            !code.contains("debug_assert!(self"),
            "must not emit free-fn self assert: {code}"
        );
        // Generated snippet must parse as valid Rust when embedded in a file.
        let wrapped = format!("#![allow(dead_code)]\n{code}\n");
        syn::parse_file(&wrapped).unwrap_or_else(|e| panic!("invalid Rust: {e}\n{wrapped}"));
    }

    #[test]
    fn block_generic_with_ensures() {
        let clauses = vec![mk_clause(
            ClauseKind::Ensures,
            Spanned::no_span(Expr::BinOp {
                lhs: Box::new(Spanned::no_span(Expr::Ident("x".into()))),
                op: BinOp::Gt,
                rhs: Box::new(Spanned::no_span(Expr::Literal(Literal::Int("0".into())))),
            }),
        )];
        let mut code = String::new();
        generate_block(
            &BlockKind::Other("verification".into()),
            "positive",
            &clauses,
            &mut code,
        );
        assert!(code.contains("pub mod block_positive"));
        assert!(code.contains("debug_assert!"));
    }

    #[test]
    fn block_with_requires() {
        let clauses = vec![mk_clause(
            ClauseKind::Requires,
            Spanned::no_span(Expr::Literal(Literal::Bool(true))),
        )];
        let mut code = String::new();
        generate_block(
            &BlockKind::Other("verification".into()),
            "precond",
            &clauses,
            &mut code,
        );
        assert!(code.contains("PRECONDITION"));
    }

    #[test]
    fn block_with_must_not() {
        let clauses = vec![mk_clause(
            ClauseKind::MustNot,
            Spanned::no_span(Expr::Ident("overflow".into())),
        )];
        let mut code = String::new();
        generate_block(
            &BlockKind::Other("verification".into()),
            "safe",
            &clauses,
            &mut code,
        );
        assert!(code.contains("check_must_not_safe"));
        assert!(code.contains("!(overflow)"));
    }

    #[test]
    fn block_with_rule() {
        let clauses = vec![mk_clause(
            ClauseKind::Rule,
            Spanned::no_span(Expr::Literal(Literal::Bool(true))),
        )];
        let mut code = String::new();
        generate_block(
            &BlockKind::Other("verification".into()),
            "r1",
            &clauses,
            &mut code,
        );
        assert!(code.contains("check_rule_r1"));
    }

    // ---- generate_codec_registry ----

    #[test]
    fn codec_registry_bytes() {
        let cr = CodecRegistryDecl {
            name: "images".into(),
            output_type: vec!["Image".into()],
            codecs: vec![assura_ast::CodecEntry {
                name: "png".into(),
                decoder: "decode_png".into(),
                magic: MagicPattern::Bytes {
                    bytes: vec![0x89, 0x50, 0x4E, 0x47],
                    prefix: true,
                },
                contracts: vec![],
            }],
        };
        let mut code = String::new();
        generate_codec_registry(&cr, &mut code);
        assert!(code.contains("dispatch_images"));
        assert!(code.contains("0x89"));
        assert!(code.contains("decode_png"));
    }

    #[test]
    fn codec_registry_probe() {
        let cr = CodecRegistryDecl {
            name: "formats".into(),
            output_type: vec![],
            codecs: vec![assura_ast::CodecEntry {
                name: "custom".into(),
                decoder: "decode_custom".into(),
                magic: MagicPattern::Probe("is_custom".into()),
                contracts: vec![],
            }],
        };
        let mut code = String::new();
        generate_codec_registry(&cr, &mut code);
        assert!(code.contains("is_custom(data)"));
        assert!(code.contains("decode_custom"));
    }

    // ---- format_rust ----

    #[test]
    fn format_valid_rust() {
        let code = "fn main() { let x = 1; }";
        let formatted = format_rust(code);
        assert!(formatted.contains("fn main()"));
    }

    #[test]
    fn format_invalid_rust_returns_original() {
        let code = "this is not valid rust {{{";
        let result = format_rust(code);
        assert!(result.contains("WARNING"));
        assert!(result.contains(code));
    }
}