borrowscope-runtime 0.1.2

Runtime tracking system for BorrowScope
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
//! Comprehensive tests for lifetime tracking and inference

use borrowscope_runtime::*;

lazy_static::lazy_static! {
    static ref TEST_LOCK: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
}

#[test]
fn test_simple_lifetime_relation() {
    let _lock = TEST_LOCK.lock();
    reset();

    // Simulate: let x = 42; let r = &x;
    let events = vec![
        Event::New {
            timestamp: 0,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "i32".to_string(),
        },
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r".to_string(),
            borrower_id: "r_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 20,
            var_id: "r_0".to_string(),
        },
        Event::Drop {
            timestamp: 30,
            var_id: "x_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    assert_eq!(timeline.relations.len(), 1);
    assert_eq!(timeline.min_time, 0);
    assert_eq!(timeline.max_time, 30);

    let relation = &timeline.relations[0];
    assert_eq!(relation.borrower_id, "r_0");
    assert_eq!(relation.borrowed_id, "x_0");
    assert_eq!(relation.start_time, 10);
    assert_eq!(relation.end_time, Some(20));
    assert!(!relation.is_mutable);
}

#[test]
fn test_multiple_immutable_borrows() {
    let _lock = TEST_LOCK.lock();
    reset();

    // Simulate: let x = 42; let r1 = &x; let r2 = &x;
    let events = vec![
        Event::New {
            timestamp: 0,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "i32".to_string(),
        },
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r1".to_string(),
            borrower_id: "r1_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 15,
            borrower_name: "r2".to_string(),
            borrower_id: "r2_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 25,
            var_id: "r2_0".to_string(),
        },
        Event::Drop {
            timestamp: 30,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 40,
            var_id: "x_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    assert_eq!(timeline.relations.len(), 2);

    // Both borrows should overlap
    assert!(timeline.lifetimes_overlap("r1_0", "r2_0"));

    // Check that both borrow from x
    let r1_relations = timeline.relations_for("r1_0");
    let r2_relations = timeline.relations_for("r2_0");

    assert_eq!(r1_relations.len(), 1);
    assert_eq!(r2_relations.len(), 1);
    assert_eq!(r1_relations[0].borrowed_id, "x_0");
    assert_eq!(r2_relations[0].borrowed_id, "x_0");
}

#[test]
fn test_nested_scopes() {
    let _lock = TEST_LOCK.lock();
    reset();

    // Simulate:
    // let x = 42;
    // let r1 = &x;
    // { let r2 = &x; } // r2 dropped
    // // r1 still valid
    let events = vec![
        Event::New {
            timestamp: 0,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "i32".to_string(),
        },
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r1".to_string(),
            borrower_id: "r1_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 20,
            borrower_name: "r2".to_string(),
            borrower_id: "r2_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 30,
            var_id: "r2_0".to_string(),
        },
        Event::Drop {
            timestamp: 50,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 60,
            var_id: "x_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    // r2's lifetime should be nested within r1's
    let r1 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r1_0")
        .unwrap();
    let r2 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r2_0")
        .unwrap();

    assert!(r1.start_time < r2.start_time);
    assert!(r1.end_time.unwrap() > r2.end_time.unwrap());

    // At time 25, both should be active
    let active_at_25 = timeline.active_at(25);
    assert_eq!(active_at_25.len(), 2);

    // At time 40, only r1 should be active
    let active_at_40 = timeline.active_at(40);
    assert_eq!(active_at_40.len(), 1);
    assert_eq!(active_at_40[0].borrower_id, "r1_0");
}

#[test]
fn test_mutable_borrow() {
    let _lock = TEST_LOCK.lock();
    reset();

    // Simulate: let mut x = 42; let r = &mut x;
    let events = vec![
        Event::New {
            timestamp: 0,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "i32".to_string(),
        },
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r".to_string(),
            borrower_id: "r_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: true,
        },
        Event::Drop {
            timestamp: 20,
            var_id: "r_0".to_string(),
        },
        Event::Drop {
            timestamp: 30,
            var_id: "x_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    assert_eq!(timeline.relations.len(), 1);

    let relation = &timeline.relations[0];
    assert!(relation.is_mutable);
    assert_eq!(relation.duration(), Some(10));
}

#[test]
fn test_lifetime_duration_calculation() {
    let _lock = TEST_LOCK.lock();

    let mut relation = LifetimeRelation::new("r".to_string(), "x".to_string(), 100, false);

    // Active lifetime has no duration
    assert_eq!(relation.duration(), None);
    assert!(relation.is_active());

    // Set end time
    relation.end_time = Some(250);
    assert_eq!(relation.duration(), Some(150));
    assert!(!relation.is_active());
}

#[test]
fn test_lifetime_overlap_detection() {
    let _lock = TEST_LOCK.lock();

    let r1 = LifetimeRelation {
        borrower_id: "r1".to_string(),
        borrowed_id: "x".to_string(),
        start_time: 100,
        end_time: Some(200),
        is_mutable: false,
    };

    let r2 = LifetimeRelation {
        borrower_id: "r2".to_string(),
        borrowed_id: "x".to_string(),
        start_time: 150,
        end_time: Some(250),
        is_mutable: false,
    };

    let r3 = LifetimeRelation {
        borrower_id: "r3".to_string(),
        borrowed_id: "x".to_string(),
        start_time: 300,
        end_time: Some(400),
        is_mutable: false,
    };

    // r1 and r2 overlap
    assert!(r1.overlaps_with(&r2));
    assert!(r2.overlaps_with(&r1));

    // r1 and r3 don't overlap
    assert!(!r1.overlaps_with(&r3));
    assert!(!r3.overlaps_with(&r1));

    // r2 and r3 don't overlap
    assert!(!r2.overlaps_with(&r3));
}

#[test]
fn test_active_borrows_at_timestamp() {
    let _lock = TEST_LOCK.lock();

    let events = vec![
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r1".to_string(),
            borrower_id: "r1_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 20,
            borrower_name: "r2".to_string(),
            borrower_id: "r2_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 30,
            borrower_name: "r3".to_string(),
            borrower_id: "r3_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 25,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 35,
            var_id: "r2_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    // At time 15: only r1
    let active_15 = timeline.active_at(15);
    assert_eq!(active_15.len(), 1);
    assert_eq!(active_15[0].borrower_id, "r1_0");

    // At time 22: r1 and r2
    let active_22 = timeline.active_at(22);
    assert_eq!(active_22.len(), 2);

    // At time 32: r2 and r3
    let active_32 = timeline.active_at(32);
    assert_eq!(active_32.len(), 2);

    // At time 40: only r3 (still active, no drop event)
    let active_40 = timeline.active_at(40);
    assert_eq!(active_40.len(), 1);
    assert_eq!(active_40[0].borrower_id, "r3_0");
}

#[test]
fn test_timeline_total_duration() {
    let _lock = TEST_LOCK.lock();

    let events = vec![
        Event::New {
            timestamp: 100,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "i32".to_string(),
        },
        Event::Drop {
            timestamp: 500,
            var_id: "x_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    assert_eq!(timeline.min_time, 100);
    assert_eq!(timeline.max_time, 500);
    assert_eq!(timeline.total_duration(), 400);
}

#[test]
fn test_empty_timeline() {
    let _lock = TEST_LOCK.lock();

    let timeline = Timeline::from_events(&[]);

    assert_eq!(timeline.relations.len(), 0);
    assert_eq!(timeline.min_time, 0);
    assert_eq!(timeline.max_time, 0);
    assert_eq!(timeline.total_duration(), 0);
}

#[test]
fn test_relations_for_variable() {
    let _lock = TEST_LOCK.lock();

    let events = vec![
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r1".to_string(),
            borrower_id: "r1_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 20,
            borrower_name: "r2".to_string(),
            borrower_id: "r2_0".to_string(),
            owner_id: "y_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 30,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 40,
            var_id: "r2_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    // Get relations for x_0
    let x_relations = timeline.relations_for("x_0");
    assert_eq!(x_relations.len(), 1);
    assert_eq!(x_relations[0].borrowed_id, "x_0");

    // Get relations for y_0
    let y_relations = timeline.relations_for("y_0");
    assert_eq!(y_relations.len(), 1);
    assert_eq!(y_relations[0].borrowed_id, "y_0");

    // Get relations for r1_0
    let r1_relations = timeline.relations_for("r1_0");
    assert_eq!(r1_relations.len(), 1);
    assert_eq!(r1_relations[0].borrower_id, "r1_0");
}

#[test]
fn test_complex_lifetime_scenario() {
    let _lock = TEST_LOCK.lock();

    // Simulate complex scenario with multiple variables and borrows
    let events = vec![
        Event::New {
            timestamp: 0,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "String".to_string(),
        },
        Event::New {
            timestamp: 5,
            var_name: "y".to_string(),
            var_id: "y_0".to_string(),
            type_name: "String".to_string(),
        },
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r1".to_string(),
            borrower_id: "r1_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 15,
            borrower_name: "r2".to_string(),
            borrower_id: "r2_0".to_string(),
            owner_id: "y_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 20,
            borrower_name: "r3".to_string(),
            borrower_id: "r3_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 25,
            var_id: "r3_0".to_string(),
        },
        Event::Drop {
            timestamp: 30,
            var_id: "r2_0".to_string(),
        },
        Event::Drop {
            timestamp: 35,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 40,
            var_id: "y_0".to_string(),
        },
        Event::Drop {
            timestamp: 45,
            var_id: "x_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    // Should have 3 borrow relations
    assert_eq!(timeline.relations.len(), 3);

    // r1 and r3 both borrow from x, so they should overlap
    assert!(timeline.lifetimes_overlap("r1_0", "r3_0"));

    // r1 and r2 borrow from different variables but overlap in time
    let r1 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r1_0")
        .unwrap();
    let r2 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r2_0")
        .unwrap();
    assert!(r1.overlaps_with(r2));

    // At time 22, r1, r2, and r3 should all be active
    let active_22 = timeline.active_at(22);
    assert_eq!(active_22.len(), 3);
}

#[test]
fn test_elision_rules() {
    let _lock = TEST_LOCK.lock();

    // Test that elision rules have descriptions
    assert!(!ElisionRule::EachInputOwn.description().is_empty());
    assert!(!ElisionRule::SingleInputToOutput.description().is_empty());
    assert!(!ElisionRule::SelfToOutput.description().is_empty());

    // Test equality
    assert_eq!(ElisionRule::EachInputOwn, ElisionRule::EachInputOwn);
    assert_ne!(ElisionRule::EachInputOwn, ElisionRule::SingleInputToOutput);
}

#[test]
fn test_still_active_borrows() {
    let _lock = TEST_LOCK.lock();

    // Simulate borrows that are never dropped (still active)
    let events = vec![
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r1".to_string(),
            borrower_id: "r1_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 20,
            borrower_name: "r2".to_string(),
            borrower_id: "r2_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
    ];

    let timeline = Timeline::from_events(&events);

    // Both borrows should be in the timeline
    assert_eq!(timeline.relations.len(), 2);

    // Both should be active (no end_time)
    for relation in &timeline.relations {
        assert!(relation.is_active());
        assert_eq!(relation.end_time, None);
        assert_eq!(relation.duration(), None);
    }
}

#[test]
fn test_serialization() {
    let _lock = TEST_LOCK.lock();

    let relation = LifetimeRelation::new("r".to_string(), "x".to_string(), 100, false);

    // Test that it can be serialized
    let json = serde_json::to_string(&relation).unwrap();
    assert!(!json.is_empty());

    // Test that it can be deserialized
    let deserialized: LifetimeRelation = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, relation);
}

#[test]
fn test_timeline_serialization() {
    let _lock = TEST_LOCK.lock();

    let events = vec![
        Event::Borrow {
            timestamp: 10,
            borrower_name: "r".to_string(),
            borrower_id: "r_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 20,
            var_id: "r_0".to_string(),
        },
    ];

    let timeline = Timeline::from_events(&events);

    // Test serialization
    let json = serde_json::to_string(&timeline).unwrap();
    assert!(!json.is_empty());

    // Test deserialization
    let deserialized: Timeline = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.relations.len(), timeline.relations.len());
    assert_eq!(deserialized.min_time, timeline.min_time);
    assert_eq!(deserialized.max_time, timeline.max_time);
}