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
//! Tests for lifetime tracking challenges and edge cases

use borrowscope_runtime::*;

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

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

    // Simulate: let x = 42; let r1 = &x; let r2 = &r1;
    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: "r1_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 30,
            var_id: "r2_0".to_string(),
        },
        Event::Drop {
            timestamp: 40,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 50,
            var_id: "x_0".to_string(),
        },
    ];

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

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

    // r2 borrows r1
    let r2_relation = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r2_0")
        .unwrap();
    assert_eq!(r2_relation.borrowed_id, "r1_0");

    // r1 borrows x
    let r1_relation = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r1_0")
        .unwrap();
    assert_eq!(r1_relation.borrowed_id, "x_0");

    // r2's lifetime is nested within r1's
    assert!(r2_relation.start_time > r1_relation.start_time);
    assert!(r2_relation.end_time.unwrap() < r1_relation.end_time.unwrap());
}

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

    // 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);

    // Both r1 and r2 borrow x
    assert_eq!(timeline.relations.len(), 2);

    for relation in &timeline.relations {
        assert_eq!(relation.borrowed_id, "x_0");
    }

    // They overlap in time
    assert!(timeline.lifetimes_overlap("r1_0", "r2_0"));
}

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

    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 graph = build_graph(&events);

    // Get lifetime relations through graph
    let relations = graph.lifetime_relations(&events);
    assert_eq!(relations.len(), 1);
    assert_eq!(relations[0].borrower_id, "r_0");
    assert_eq!(relations[0].borrowed_id, "x_0");

    // Create timeline through graph
    let timeline = graph.create_timeline(&events);
    assert_eq!(timeline.relations.len(), 1);
    assert_eq!(timeline.min_time, 0);
    assert_eq!(timeline.max_time, 30);
}

#[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::Drop {
            timestamp: 30,
            var_id: "r1_0".to_string(),
        },
        Event::Borrow {
            timestamp: 35,
            borrower_name: "r3".to_string(),
            borrower_id: "r3_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 40,
            var_id: "r2_0".to_string(),
        },
    ];

    let graph = build_graph(&events);

    // At time 25: r1 and r2 are active
    let active_25 = graph.active_borrows_at(&events, 25);
    assert_eq!(active_25.len(), 2);

    // At time 32: only r2 is active (r1 dropped at 30)
    let active_32 = graph.active_borrows_at(&events, 32);
    assert_eq!(active_32.len(), 1);
    assert_eq!(active_32[0].borrower_id, "r2_0");

    // At time 37: r2 and r3 are active
    let active_37 = graph.active_borrows_at(&events, 37);
    assert_eq!(active_37.len(), 2);
}

#[test]
fn test_lifetimes_overlap_through_graph() {
    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::Drop {
            timestamp: 30,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 40,
            var_id: "r2_0".to_string(),
        },
    ];

    let graph = build_graph(&events);

    // r1 and r2 overlap (r1: 10-30, r2: 20-40)
    assert!(graph.lifetimes_overlap(&events, "r1_0", "r2_0"));

    // Add a non-overlapping borrow
    let events_with_r3 = 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::Drop {
            timestamp: 20,
            var_id: "r1_0".to_string(),
        },
        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: 40,
            var_id: "r3_0".to_string(),
        },
    ];

    let graph2 = build_graph(&events_with_r3);

    // r1 and r3 don't overlap (r1: 10-20, r3: 30-40)
    assert!(!graph2.lifetimes_overlap(&events_with_r3, "r1_0", "r3_0"));
}

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

    // Simulate complex nesting: x -> r1 -> r2 -> r3
    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: "r1_0".to_string(),
            mutable: false,
        },
        Event::Borrow {
            timestamp: 30,
            borrower_name: "r3".to_string(),
            borrower_id: "r3_0".to_string(),
            owner_id: "r2_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 40,
            var_id: "r3_0".to_string(),
        },
        Event::Drop {
            timestamp: 50,
            var_id: "r2_0".to_string(),
        },
        Event::Drop {
            timestamp: 60,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 70,
            var_id: "x_0".to_string(),
        },
    ];

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

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

    // Verify the chain: r3 -> r2 -> r1 -> x
    let r3 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r3_0")
        .unwrap();
    assert_eq!(r3.borrowed_id, "r2_0");

    let r2 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r2_0")
        .unwrap();
    assert_eq!(r2.borrowed_id, "r1_0");

    let r1 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r1_0")
        .unwrap();
    assert_eq!(r1.borrowed_id, "x_0");

    // Verify nesting: r3 < r2 < r1
    assert!(r3.start_time > r2.start_time);
    assert!(r2.start_time > r1.start_time);
    assert!(r3.end_time.unwrap() < r2.end_time.unwrap());
    assert!(r2.end_time.unwrap() < r1.end_time.unwrap());
}

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

    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::Drop {
            timestamp: 20,
            var_id: "r1_0".to_string(),
        },
        Event::Borrow {
            timestamp: 30,
            borrower_name: "r2".to_string(),
            borrower_id: "r2_0".to_string(),
            owner_id: "x_0".to_string(),
            mutable: true,
        },
        Event::Drop {
            timestamp: 40,
            var_id: "r2_0".to_string(),
        },
        Event::Drop {
            timestamp: 50,
            var_id: "x_0".to_string(),
        },
    ];

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

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

    // Check immutable borrow
    let r1 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r1_0")
        .unwrap();
    assert!(!r1.is_mutable);

    // Check mutable borrow
    let r2 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r2_0")
        .unwrap();
    assert!(r2.is_mutable);

    // They don't overlap (sequential)
    assert!(!timeline.lifetimes_overlap("r1_0", "r2_0"));
}

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

    // Simulate explicit drop
    let events = vec![
        Event::New {
            timestamp: 0,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "String".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: 15,
            var_id: "r_0".to_string(),
        },
        Event::Drop {
            timestamp: 50,
            var_id: "x_0".to_string(),
        },
    ];

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

    let r_relation = &timeline.relations[0];
    assert_eq!(r_relation.duration(), Some(5)); // 15 - 10 = 5

    // r's lifetime is much shorter than x's
    assert!(r_relation.end_time.unwrap() < 50);
}

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

    let events = vec![
        Event::New {
            timestamp: 0,
            var_name: "x".to_string(),
            var_id: "x_0".to_string(),
            type_name: "i32".to_string(),
        },
        Event::New {
            timestamp: 5,
            var_name: "y".to_string(),
            var_id: "y_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: "y_0".to_string(),
            mutable: false,
        },
        Event::Drop {
            timestamp: 30,
            var_id: "r1_0".to_string(),
        },
        Event::Drop {
            timestamp: 35,
            var_id: "r2_0".to_string(),
        },
        Event::Drop {
            timestamp: 40,
            var_id: "x_0".to_string(),
        },
        Event::Drop {
            timestamp: 45,
            var_id: "y_0".to_string(),
        },
    ];

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

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

    // r1 borrows x
    let r1 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r1_0")
        .unwrap();
    assert_eq!(r1.borrowed_id, "x_0");

    // r2 borrows y
    let r2 = timeline
        .relations
        .iter()
        .find(|r| r.borrower_id == "r2_0")
        .unwrap();
    assert_eq!(r2.borrowed_id, "y_0");

    // They overlap in time but borrow different variables
    assert!(r1.overlaps_with(r2));
}

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

    let events: Vec<Event> = vec![];
    let graph = build_graph(&events);

    let relations = graph.lifetime_relations(&events);
    assert_eq!(relations.len(), 0);

    let timeline = graph.create_timeline(&events);
    assert_eq!(timeline.relations.len(), 0);
    assert_eq!(timeline.total_duration(), 0);
}

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

    // Borrow that never gets dropped (still active)
    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,
        },
    ];

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

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

    let relation = &timeline.relations[0];
    assert!(relation.is_active());
    assert_eq!(relation.end_time, None);
    assert_eq!(relation.duration(), None);
}