aprender-contracts 0.31.1

Papers to Math to Contracts in Code — YAML contract parsing, validation, scaffold generation, and Kani harness codegen for provable Rust kernels
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
#![allow(clippy::all)]
use super::*;

// ---------------------------------------------------------------------------
// extract_feature_gate
// ---------------------------------------------------------------------------

#[test]
fn test_extract_feature_gate_simple() {
    let gate = extract_feature_gate(r#"#[cfg(feature = "gpu")]"#);
    assert_eq!(gate.as_deref(), Some("gpu"));
}

#[test]
fn test_extract_feature_gate_all_test() {
    let gate = extract_feature_gate(r#"#[cfg(all(test, feature = "gpu"))]"#);
    assert_eq!(gate.as_deref(), Some("gpu"));
}

#[test]
fn test_extract_feature_gate_not() {
    let gate = extract_feature_gate(r#"#[cfg(not(feature = "x"))]"#);
    assert_eq!(gate.as_deref(), Some("x"));
}

#[test]
fn test_extract_feature_gate_no_feature() {
    let gate = extract_feature_gate(r"#[cfg(test)]");
    assert_eq!(gate, None);
}

#[test]
fn test_extract_feature_gate_target_os() {
    let gate = extract_feature_gate(r#"#[cfg(target_os = "linux")]"#);
    assert_eq!(gate, None);
}

#[test]
fn test_extract_feature_gate_complex_all() {
    let gate =
        extract_feature_gate(r#"#[cfg(all(not(miri), feature = "simd", target_arch = "x86"))]"#);
    assert_eq!(gate.as_deref(), Some("simd"));
}

// ---------------------------------------------------------------------------
// extract_bound_functions
// ---------------------------------------------------------------------------

#[test]
fn test_extract_bound_functions() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("binding.yaml");
    std::fs::write(
        &path,
        "bindings:\n  - function: \"Foo::bar\"\n    status: implemented\n  - function: baz\n    status: implemented\n",
    )
    .unwrap();
    let names = extract_bound_functions(&path);
    assert!(names.contains("bar"), "Expected 'bar' in {names:?}");
    assert!(names.contains("baz"), "Expected 'baz' in {names:?}");
}

#[test]
fn test_extract_bound_functions_missing_file() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("nonexistent.yaml");
    let names = extract_bound_functions(&path);
    assert!(names.is_empty());
}

#[test]
fn test_extract_bound_functions_no_function_lines() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("binding.yaml");
    std::fs::write(
        &path,
        "bindings:\n  - contract: foo-v1\n    status: implemented\n",
    )
    .unwrap();
    let names = extract_bound_functions(&path);
    assert!(names.is_empty());
}

#[test]
fn test_extract_bound_functions_quoted_names() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("binding.yaml");
    std::fs::write(
        &path,
        "bindings:\n  - function: 'Module::compute_attention'\n  - function: \"train_step\"\n",
    )
    .unwrap();
    let names = extract_bound_functions(&path);
    assert!(
        names.contains("compute_attention"),
        "Expected 'compute_attention' in {names:?}"
    );
    assert!(
        names.contains("train_step"),
        "Expected 'train_step' in {names:?}"
    );
}

#[test]
fn test_extract_bound_functions_lowercased() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("binding.yaml");
    std::fs::write(&path, "  - function: \"Module::FooBar\"\n").unwrap();
    let names = extract_bound_functions(&path);
    assert!(
        names.contains("foobar"),
        "Expected lowercased 'foobar' in {names:?}"
    );
}

// ---------------------------------------------------------------------------
// scan_file
// ---------------------------------------------------------------------------

#[test]
fn test_scan_file() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        "pub fn hello() {}\n#[contract(\"test\", equation = \"eq\")]\npub fn world() {}\nfn private() {}\n",
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 2);
    assert!(!results[0].has_contract_macro);
    assert!(results[1].has_contract_macro);
}

#[test]
fn test_scan_file_skips_main_and_new() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        "pub fn main() {}\npub fn new() {}\npub fn real_fn() {}\n",
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, "real_fn");
}

#[test]
fn test_scan_file_pub_async_fn() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(tmp.path(), "pub async fn serve() {}\n").unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, "serve");
}

#[test]
fn test_scan_file_generic_fn() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(tmp.path(), "pub fn process<T: Clone>(x: T) -> T {}\n").unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, "process");
}

#[test]
fn test_scan_file_cfg_feature_gate_on_function() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        "#[cfg(feature = \"gpu\")]\npub fn gpu_dispatch() {}\npub fn cpu_dispatch() {}\n",
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 2);
    assert_eq!(results[0].path, "gpu_dispatch");
    assert_eq!(results[0].feature_gate.as_deref(), Some("gpu"));
    assert_eq!(results[1].path, "cpu_dispatch");
    assert!(results[1].feature_gate.is_none());
}

#[test]
fn test_scan_file_cfg_feature_gate_on_module() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        r#"#[cfg(feature = "simd")]
mod simd_impl {
    pub fn simd_add() {}
}
pub fn normal_fn() {}
"#,
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 2);
    assert_eq!(results[0].path, "simd_add");
    assert_eq!(results[0].feature_gate.as_deref(), Some("simd"));
    assert_eq!(results[1].path, "normal_fn");
    assert!(results[1].feature_gate.is_none());
}

#[test]
fn test_scan_file_records_line_number() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        "// comment\n// another\npub fn third_line() {}\n",
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].line, 3);
}

#[test]
fn test_scan_file_empty_file() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(tmp.path(), "").unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert!(results.is_empty());
}

#[test]
fn test_scan_file_private_fns_only() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(tmp.path(), "fn private_a() {}\nfn private_b() {}\n").unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert!(results.is_empty());
}

#[test]
fn test_scan_file_contract_annotation_not_sticky() {
    // #[contract] only applies to the immediately following pub fn, not later ones
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        "#[contract(\"test\", equation = \"eq\")]\npub fn annotated() {}\npub fn not_annotated() {}\n",
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 2);
    assert!(results[0].has_contract_macro);
    assert!(!results[1].has_contract_macro);
}

#[test]
fn test_scan_file_contract_reset_by_non_attr_line() {
    // A non-comment, non-attribute line between #[contract] and pub fn resets the flag
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        "#[contract(\"test\", equation = \"eq\")]\nlet x = 1;\npub fn after_reset() {}\n",
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 1);
    assert!(!results[0].has_contract_macro);
}

// ---------------------------------------------------------------------------
// scan_pub_fns
// ---------------------------------------------------------------------------

#[test]
fn test_scan_pub_fns_src_dir() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("lib.rs"), "pub fn alpha() {}\npub fn beta() {}\n").unwrap();

    let results = scan_pub_fns(dir.path());
    assert_eq!(results.len(), 2);
}

#[test]
fn test_scan_pub_fns_skips_target_and_tests_dirs() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("lib.rs"), "pub fn visible() {}\n").unwrap();

    // These should be skipped
    let target = src.join("target");
    std::fs::create_dir_all(&target).unwrap();
    std::fs::write(target.join("hidden.rs"), "pub fn hidden() {}\n").unwrap();

    let tests = src.join("tests");
    std::fs::create_dir_all(&tests).unwrap();
    std::fs::write(tests.join("test.rs"), "pub fn test_hidden() {}\n").unwrap();

    let results = scan_pub_fns(dir.path());
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, "visible");
}

#[test]
fn test_scan_pub_fns_no_src_dir() {
    let dir = tempfile::tempdir().unwrap();
    let results = scan_pub_fns(dir.path());
    assert!(results.is_empty());
}

#[test]
fn test_scan_pub_fns_nested_modules() {
    let dir = tempfile::tempdir().unwrap();
    let nested = dir.path().join("src").join("sub").join("deep");
    std::fs::create_dir_all(&nested).unwrap();
    std::fs::write(nested.join("mod.rs"), "pub fn deep_fn() {}\n").unwrap();

    let results = scan_pub_fns(dir.path());
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, "deep_fn");
}

// ---------------------------------------------------------------------------
// reverse_coverage (integration)
// ---------------------------------------------------------------------------

#[test]
fn test_reverse_coverage_all_bound() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("lib.rs"), "pub fn alpha() {}\npub fn beta() {}\n").unwrap();

    let binding = dir.path().join("binding.yaml");
    std::fs::write(
        &binding,
        "bindings:\n  - function: alpha\n  - function: beta\n",
    )
    .unwrap();

    let report = reverse_coverage(dir.path(), &binding);
    assert_eq!(report.total_pub_fns, 2);
    assert_eq!(report.bound_fns, 2);
    assert!(report.unbound.is_empty());
    assert!((report.coverage_pct - 100.0).abs() < f64::EPSILON);
}

#[test]
fn test_reverse_coverage_some_unbound() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    // Use a name that won't be auto-exempt: must be >4 chars, no underscore, not in exempt list
    std::fs::write(
        src.join("lib.rs"),
        "pub fn nonexempt_function_alpha_zz() {}\npub fn nonexempt_function_beta_zz() {}\n",
    )
    .unwrap();

    let binding = dir.path().join("binding.yaml");
    std::fs::write(
        &binding,
        "bindings:\n  - function: nonexempt_function_alpha_zz\n",
    )
    .unwrap();

    let report = reverse_coverage(dir.path(), &binding);
    assert_eq!(report.total_pub_fns, 2);
    assert_eq!(report.bound_fns, 1);
    assert_eq!(report.unbound.len(), 1);
    assert_eq!(report.unbound[0].path, "nonexempt_function_beta_zz");
}

#[test]
fn test_reverse_coverage_annotated_fn() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(
        src.join("lib.rs"),
        "#[contract(\"x\", equation = \"eq\")]\npub fn annotated_thing() {}\n",
    )
    .unwrap();

    let binding = dir.path().join("binding.yaml");
    std::fs::write(&binding, "bindings: []\n").unwrap();

    let report = reverse_coverage(dir.path(), &binding);
    assert_eq!(report.total_pub_fns, 1);
    assert_eq!(report.annotated_fns, 1);
    assert_eq!(report.bound_fns, 1);
    assert!(report.unbound.is_empty());
}

#[test]
fn test_reverse_coverage_auto_exempt() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    // "default" is in the auto-exempt list as an exact match
    std::fs::write(src.join("lib.rs"), "pub fn default() {}\n").unwrap();

    let binding = dir.path().join("binding.yaml");
    std::fs::write(&binding, "bindings: []\n").unwrap();

    let report = reverse_coverage(dir.path(), &binding);
    assert_eq!(report.total_pub_fns, 1);
    assert_eq!(report.exempt_fns, 1);
    assert!(report.unbound.is_empty());
}

#[test]
fn test_reverse_coverage_empty_crate() {
    let dir = tempfile::tempdir().unwrap();
    let binding = dir.path().join("binding.yaml");
    std::fs::write(&binding, "bindings: []\n").unwrap();

    let report = reverse_coverage(dir.path(), &binding);
    assert_eq!(report.total_pub_fns, 0);
    assert!((report.coverage_pct - 100.0).abs() < f64::EPSILON);
}

#[test]
fn test_reverse_coverage_case_insensitive_matching() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("lib.rs"), "pub fn MyFunction() {}\n").unwrap();

    let binding = dir.path().join("binding.yaml");
    std::fs::write(&binding, "  - function: \"Mod::myfunction\"\n").unwrap();

    let report = reverse_coverage(dir.path(), &binding);
    assert_eq!(report.total_pub_fns, 1);
    assert_eq!(report.bound_fns, 1);
}

// ---------------------------------------------------------------------------
// scan_dir edge cases
// ---------------------------------------------------------------------------

#[test]
fn test_scan_dir_skips_git_dir() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();

    let git = src.join(".git");
    std::fs::create_dir_all(&git).unwrap();
    std::fs::write(git.join("file.rs"), "pub fn hidden() {}\n").unwrap();

    std::fs::write(src.join("lib.rs"), "pub fn visible() {}\n").unwrap();

    let results = scan_pub_fns(dir.path());
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, "visible");
}

#[test]
fn test_scan_dir_handles_non_rs_files() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("lib.rs"), "pub fn real() {}\n").unwrap();
    std::fs::write(src.join("notes.txt"), "pub fn fake() {}\n").unwrap();
    std::fs::write(src.join("data.json"), "{}").unwrap();

    let results = scan_pub_fns(dir.path());
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, "real");
}

// ---------------------------------------------------------------------------
// scan_file: feature gate cleared after module exit
// ---------------------------------------------------------------------------

#[test]
fn test_scan_file_feature_gate_cleared_after_module_close() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        r#"#[cfg(feature = "gpu")]
mod gpu_impl {
    pub fn gpu_only() {}
}
pub fn after_module() {}
"#,
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 2);
    // gpu_only should have the gate
    assert_eq!(results[0].feature_gate.as_deref(), Some("gpu"));
    // after_module should NOT have the gate
    assert!(
        results[1].feature_gate.is_none(),
        "feature gate should be cleared after module closes"
    );
}

// ---------------------------------------------------------------------------
// scan_file: pending_feature_gate cleared by non-attr/non-comment line
// ---------------------------------------------------------------------------

#[test]
fn test_scan_file_pending_gate_cleared_by_code() {
    let tmp = tempfile::NamedTempFile::with_suffix(".rs").unwrap();
    std::fs::write(
        tmp.path(),
        r#"#[cfg(feature = "gpu")]
let x = 42;
pub fn no_gate_here() {}
"#,
    )
    .unwrap();
    let mut results = Vec::new();
    scan_file(tmp.path(), &mut results);
    assert_eq!(results.len(), 1);
    assert!(
        results[0].feature_gate.is_none(),
        "pending gate should be cleared by intervening code line"
    );
}