pbt 0.5.10

High-throughput property-based testing with `derive`, swarm-testing, precise sizing, and full graph-theoretic type analysis over mutually inductive and uninstantiable types.
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
//! Logic for generating and/or storing
//! fields to be used on a given constructor.

use {
    crate::{
        Pbt,
        hash::map,
        multiset::Multiset,
        reflection::{BucketOps, Erased, bucket_ops_of, is_literal, register_globally},
        size::{self, Size},
        swarm::Swarm,
    },
    ahash::HashMap,
    core::{any::TypeId, mem, ptr},
    std::collections::hash_map,
    wyrand::WyRand,
};

/// Logic for generating and/or storing
/// fields to be used on a given constructor.
///
/// Note that this unifies two cases:
/// generation, in which we want fields on demand with maximum throughput,
/// and shrinking, in which we want to reuse existing fields.
pub trait Fields {
    /// Retrieve and/or generate a term of type T.
    fn field<T>(&mut self) -> T
    where
        T: Pbt;
}

/// Fields are not stored ahead of time;
/// instead, their sizes are stored in an iterator,
/// and all fields are produced just in time.
#[non_exhaustive]
pub(crate) struct Lazy<'prng, 'swarm> {
    /// Pseudorandom number generator.
    ///
    /// This is inside `Lazy` and not a function argument
    /// because shrinking (existing fields) doesn't need a PRNG.
    pub(crate) prng: &'prng mut WyRand,
    /// A lazy partition over sizes, tuned to match
    /// the number of inductive types among the fields to generate.
    pub(crate) sizes: size::Partition,
    /// A masked view into this type's constructors,
    /// partitioned into potential leaves and loops.
    pub(crate) swarm: &'swarm Swarm,
}

/// Iterate over all possible subsets and orderings
/// using these stored fields to create a sub-store
/// containing a requested multiset of types.
#[non_exhaustive]
struct Sections {
    /// One type at a time, index over all stored terms of that type.
    index: usize,
    /// One type at a time, index over all stored terms of that type.
    maybe_ty: Option<TypeId>,
    /// After removing one term of the currently focused type,
    /// recurse with that term removed from both the store and the multiset.
    recurse: Option<(ptr::NonNull<Erased>, Box<Self>)>,
    /// The desired output multiset of types.
    requirements: Option<Multiset<TypeId>>,
    /// The store of which we're iterating over sections.
    store: Store,
}

/// A collection of fields of arbitrary/mixed types.
/// Fields are known and returned if present;
/// unknown fields are newly generated leaves.
#[non_exhaustive]
pub struct Store {
    /// A map from type IDs to erased vectors
    /// whose elements match the associated type.
    store: HashMap<TypeId, Vec<Erased>>,
}

/// Visit all sub-terms of an arbitrary type within a `Store`.
#[non_exhaustive]
struct Visitor<T> {
    /// Function pointers performing operations on vectors of `self.ty`.
    bucket_ops: BucketOps<Erased>,
    /// All immediate sub-terms of type `T`.
    matches: Vec<T>,
    /// All immediate sub-terms of type `self.ty`.
    queue: Option<Vec<Erased>>,
    /// Recurse on each field.
    recurse: Option<Box<Self>>,
    /// The original store we're visiting.
    store: Store,
    /// The type on which we're currently recursing.
    ty: TypeId,
}

impl Fields for Lazy<'_, '_> {
    #[inline]
    #[expect(
        clippy::expect_used,
        reason = "Internal invariants: violations should fail loudly."
    )]
    fn field<T>(&mut self) -> T
    where
        T: Pbt,
    {
        let size = if self.swarm.is_inductive::<T>() {
            self.sizes
                .next()
                .expect("INTERNAL ERROR (`pbt`): overdrawn size partition")
        } else {
            Size::zero()
        };
        self.swarm.arbitrary(size, self.prng)
    }
}

impl Sections {
    /// Iterate over all possible subsets and orderings
    /// using these stored fields to create a sub-store
    /// containing a requested multiset of types.
    #[inline]
    fn new(store: Store, mut requirements: Multiset<TypeId>) -> Self {
        Self {
            index: 0,
            maybe_ty: requirements.pop(),
            recurse: None,
            requirements: Some(requirements),
            store,
        }
    }
}

impl Drop for Sections {
    #[inline]
    #[expect(
        clippy::expect_used,
        reason = "Internal invariants: violations should fail loudly."
    )]
    fn drop(&mut self) {
        let () = self.store.drop_unused();
        if let Some((head, _)) = self.recurse.take() {
            let ty = self
                .maybe_ty
                .expect("INTERNAL ERROR (`pbt`): unused `Sections` head without a type");
            let () = (bucket_ops_of(ty).drop)(head);
        }
    }
}

impl Iterator for Sections {
    type Item = Store;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let requirements = self.requirements.as_ref()?;
            let Some(ty) = self.maybe_ty else {
                // No requirements, so we should return the empty store:
                self.requirements = None; // <-- "don't return another after this" (see above)
                return Some(Store::new());
            };
            let bucket_ops = bucket_ops_of(ty);

            if let Some((ref head, ref mut recurse)) = self.recurse {
                if let Some(mut tail) = recurse.next() {
                    let v: &mut Vec<Erased> = tail.store.entry(ty).or_insert_with(bucket_ops.empty);
                    let cloned = (bucket_ops.clone)(*head);
                    let () = (bucket_ops.push)(v, cloned);
                    return Some(tail);
                }
                if let Some((drop_head, _)) = self.recurse.take() {
                    let () = (bucket_ops.drop)(drop_head);
                }
            }

            if self.index >= self.store.store.get(&ty)?.len() {
                return None;
            }
            let mut ablated = self.store.clone();
            let v = ablated.store.get_mut(&ty)?;
            let head = (bucket_ops.swap_remove)(v, self.index);
            #[expect(
                clippy::arithmetic_side_effects,
                reason = "hardware can't support `usize::MAX` elements in a vector"
            )]
            let () = self.index += 1;

            self.recurse = Some((head, Box::new(Self::new(ablated, requirements.clone()))));
        }
    }
}

impl Fields for Store {
    #[inline]
    #[expect(
        clippy::expect_used,
        reason = "Internal invariants: violations should fail loudly."
    )]
    fn field<T>(&mut self) -> T
    where
        T: Pbt,
    {
        self.pop().expect("INTERNAL ERROR (`pbt`): missing field")
    }
}

impl Store {
    /// Deserialize from JSON.
    #[inline]
    pub(crate) fn deserialize(
        json: &serde_json::Value,
        field_types: &Multiset<TypeId>,
    ) -> Option<Self> {
        let mut store = map();
        let serde_json::Value::Object(ref map) = *json else {
            return None;
        };
        #[expect(clippy::iter_over_hash_type, reason = "order doesn't matter")]
        for &ty in field_types.iter_dedup() {
            let bucket_ops = bucket_ops_of(ty);
            let serde_json::Value::Array(ref values) = *map.get((bucket_ops.name)())? else {
                return None;
            };
            let _: Option<_> = store.insert(ty, (bucket_ops.deserialize)(values)?);
        }
        Some(Self { store })
    }

    /// Drop all unused fields of this store.
    /// If this is not called, stores must
    /// use all their stored fields before being dropped.
    #[inline]
    pub(crate) fn drop_unused(&mut self) {
        #[expect(clippy::iter_over_hash_type, reason = "order doesn't matter")]
        for (k, v) in self.store.drain() {
            let bucket_ops = bucket_ops_of(k);
            let () = (bucket_ops.drop_vec)(v);
        }
    }

    /// An empty collection of fields of arbitrary/mixed types.
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self { store: map() }
    }

    /// Pop and return a cached field of this type iff one exists.
    #[inline]
    pub(crate) fn pop<T>(&mut self) -> Option<T>
    where
        T: 'static,
    {
        let ty = TypeId::of::<T>();
        let hash_map::Entry::Occupied(mut entry) = self.store.entry(ty) else {
            return None;
        };
        let erased: &mut Vec<Erased> = entry.get_mut();
        // SAFETY: Invariant. Extremely dangerous.
        let typed: &mut Vec<T> =
            unsafe { ptr::from_mut(erased).cast::<Vec<T>>().as_mut_unchecked() };
        let t = typed.pop()?;
        if typed.is_empty() {
            let erased_to_drop: Vec<Erased> = entry.remove();
            // SAFETY: Invariant. Extremely dangerous.
            let typed_to_drop: Vec<T> =
                unsafe { mem::transmute::<Vec<Erased>, Vec<T>>(erased_to_drop) };
            let () = drop(typed_to_drop);
        }
        Some(t)
    }

    /// Pop and return all cached fields of this type iff they exist.
    #[inline]
    #[cfg(test)]
    pub(crate) fn pop_all<T>(&mut self) -> Option<Vec<T>>
    where
        T: 'static,
    {
        // SAFETY: Invariant. Extremely dangerous.
        unsafe {
            mem::transmute::<Option<Vec<Erased>>, Option<Vec<T>>>(
                self.store.remove(&TypeId::of::<T>()),
            )
        }
    }

    /// Pop and return a cached field of any type iff one exists.
    #[inline]
    #[expect(
        clippy::expect_used,
        clippy::panic,
        clippy::unwrap_in_result,
        reason = "Internal invariants: violations should fail loudly."
    )]
    pub(crate) fn pop_erased(&mut self) -> Option<(TypeId, ptr::NonNull<Erased>)> {
        let ty = *self.store.keys().next()?;
        let bucket_ops = bucket_ops_of(ty);
        let hash_map::Entry::Occupied(mut entry) = self.store.entry(ty) else {
            panic!("INTERNAL ERROR (`pbt`): disappearing store items")
        };
        let erased: &mut Vec<Erased> = entry.get_mut();
        let popped =
            (bucket_ops.pop)(erased).expect("INTERNAL ERROR (`pbt`): empty vector in a `Store`");
        if erased.is_empty() {
            let erased_to_drop: Vec<Erased> = entry.remove();
            let () = (bucket_ops.drop_vec)(erased_to_drop);
        }
        Some((ty, popped))
    }

    /// Store a field of this type.
    #[inline]
    pub fn push<T>(&mut self, t: T)
    where
        T: Pbt,
    {
        let () = register_globally::<T>();
        let () = self.push_erased(
            TypeId::of::<T>(),
            ptr::NonNull::from_mut(Box::leak(Box::new(t))).cast(),
        );
    }

    /// Store a field of some type.
    #[inline]
    pub(crate) fn push_erased(&mut self, ty: TypeId, erased_boxed: ptr::NonNull<Erased>) {
        let bucket_ops = bucket_ops_of(ty);
        let v: &mut Vec<Erased> = self.store.entry(ty).or_insert_with(bucket_ops.empty);
        let () = (bucket_ops.push)(v, erased_boxed);
    }

    /// Iterate over all possible subsets and orderings
    /// using these stored fields to create a sub-store
    /// containing a requested multiset of types.
    #[inline]
    pub(crate) fn sections(self, requirements: Multiset<TypeId>) -> impl Iterator<Item = Self> {
        Sections::new(self, requirements)
    }

    /// Serialize into JSON.
    #[inline]
    pub(crate) fn serialize(mut self) -> serde_json::Value {
        serde_json::Value::Object(
            self.store
                .drain()
                .map(|(ty, v)| {
                    let bucket_ops = bucket_ops_of(ty);
                    (
                        (bucket_ops.name)().to_owned(),
                        serde_json::Value::Array((bucket_ops.serialize)(v)),
                    )
                })
                .collect(),
        )
    }

    /// Visit all sub-terms of an arbitrary type within a `Store`.
    #[inline]
    pub(crate) fn visit<T>(self) -> impl Iterator<Item = T>
    where
        T: Pbt,
    {
        Visitor::<T>::new(self)
    }
}

impl Clone for Store {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            store: self
                .store
                .iter()
                .map(|(&k, v)| (k, (bucket_ops_of(k).clone_vec)(v)))
                .collect(),
        }
    }
}

impl Default for Store {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for Store {
    #[inline]
    fn drop(&mut self) {
        assert!(
            self.store.is_empty(),
            "INTERNAL ERROR (`pbt`): unused fields",
        );
    }
}

impl<T> Visitor<T>
where
    T: Pbt,
{
    /// Visit all sub-terms of an arbitrary type within a `Store`.
    #[inline]
    fn new(store: Store) -> Self {
        let ty = TypeId::of::<T>();
        Self {
            bucket_ops: bucket_ops_of(ty),
            matches: vec![],
            queue: None,
            recurse: None,
            store,
            ty,
        }
    }
}

impl<T> Iterator for Visitor<T>
where
    T: Pbt,
{
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        'restart: loop {
            if let Some(t) = self.matches.pop() {
                return Some(t);
            }

            if let Some(ref mut recurse) = self.recurse {
                if let Some(next) = recurse.next() {
                    return Some(next);
                }
                self.recurse = None;
            }

            while let Some(ref mut queue) = self.queue
                && let Some(boxed_erased) = (self.bucket_ops.pop)(queue)
            {
                let mut fields = (self.bucket_ops.deconstruct)(boxed_erased).fields;
                if !is_literal(self.ty) {
                    self.recurse = Some(Box::new(Self::new(fields)));
                    continue 'restart;
                }
                let () = fields.drop_unused();
            }

            if let Some(queue) = self.queue.take() {
                let () = (self.bucket_ops.drop_vec)(queue);
            }

            self.ty = *self.store.store.keys().next()?;
            self.bucket_ops = bucket_ops_of(self.ty);
            self.queue = self.store.store.remove(&self.ty);

            if self.ty == TypeId::of::<T>()
                && let Some(ref queue) = self.queue
            {
                // SAFETY: Invariant. Extremely dangerous.
                self.matches = unsafe {
                    mem::transmute::<Vec<Erased>, Vec<T>>((self.bucket_ops.clone_vec)(queue))
                };
            }
        }
    }
}

impl<T> Drop for Visitor<T> {
    #[inline]
    fn drop(&mut self) {
        if let Some(queue) = self.queue.take() {
            let () = (self.bucket_ops.drop_vec)(queue);
        }
        let () = self.store.drop_unused();
    }
}

#[cfg(test)]
mod tests {
    #![expect(clippy::unwrap_used, reason = "Failing tests ought to panic.")]

    use {
        super::*,
        crate::{
            arbitrary::arbitrary,
            check_eta_expansion,
            reflection::{Parts, Variant, Variants},
            registration::Registration,
        },
        core::{iter, num::NonZero},
        pretty_assertions::assert_eq,
    };

    #[derive(Clone, Debug, Eq, PartialEq)]
    struct DestructsWithExtraField((), ());

    impl Pbt for DestructsWithExtraField {
        #[inline]
        fn construct<F>(
            Parts {
                mut fields,
                variant_index,
            }: Parts<F>,
        ) -> Self
        where
            F: Fields,
        {
            debug_assert_eq!(variant_index, Some(const { NonZero::new(1).unwrap() }));
            #[expect(clippy::unit_arg, reason = "b/c `fields` checks its size on `drop`")]
            Self(fields.field(), fields.field())
        }

        #[inline]
        fn deconstruct(self) -> Parts<Store> {
            Parts {
                fields: {
                    let mut acc = Store::new();
                    let () = acc.push(());
                    let () = acc.push(());
                    let () = acc.push(()); // <-- extra `()`
                    acc
                },
                variant_index: const { Some(NonZero::new(1).unwrap()) },
            }
        }

        #[inline]
        fn register(registration: &mut Registration<'_>) -> Variants<Self> {
            let () = registration.register::<()>();
            Variants::Algebraic(vec![Variant {
                field_types: [TypeId::of::<()>(), TypeId::of::<()>()]
                    .into_iter()
                    .collect(),
            }])
        }
    }

    // TODO: make this a real PBT when macro are ready
    #[test]
    fn lossless() {
        let mut prng = WyRand::new(42);
        for ints in arbitrary::<Vec<usize>>(&mut prng).unwrap().take(10) {
            let mut store = Store::new();
            for &int in ints.iter().rev() {
                let () = store.push(int);
            }
            let reconstructed: Vec<usize> = iter::from_fn(|| store.pop()).collect();
            assert_eq!(reconstructed, ints);
        }
    }

    #[test]
    fn sections_123_1() {
        let mut store = Store::new();
        let () = store.push(1_usize);
        let () = store.push(2_usize);
        let () = store.push(3_usize);
        let mut iter = store
            .sections(iter::once((TypeId::of::<usize>(), 1)).collect())
            .map(|mut s| s.pop_all::<usize>().unwrap());
        assert_eq!(iter.next(), Some(vec![1]));
        assert_eq!(iter.next(), Some(vec![2]));
        assert_eq!(iter.next(), Some(vec![3]));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn sections_123_2() {
        let mut store = Store::new();
        let () = store.push(1_usize);
        let () = store.push(2_usize);
        let () = store.push(3_usize);
        let mut iter = store
            .sections(iter::once((TypeId::of::<usize>(), 2)).collect())
            .map(|mut s| s.pop_all::<usize>().unwrap());
        assert_eq!(iter.next(), Some(vec![3, 1]));
        assert_eq!(iter.next(), Some(vec![2, 1]));
        assert_eq!(iter.next(), Some(vec![1, 2]));
        assert_eq!(iter.next(), Some(vec![3, 2]));
        assert_eq!(iter.next(), Some(vec![1, 3]));
        assert_eq!(iter.next(), Some(vec![2, 3]));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn sections_123_3() {
        let mut store = Store::new();
        let () = store.push(1_usize);
        let () = store.push(2_usize);
        let () = store.push(3_usize);
        let mut iter = store
            .sections(iter::once((TypeId::of::<usize>(), 3)).collect())
            .map(|mut s| s.pop_all::<usize>().unwrap());
        assert_eq!(iter.next(), Some(vec![2, 3, 1]));
        assert_eq!(iter.next(), Some(vec![3, 2, 1]));
        assert_eq!(iter.next(), Some(vec![3, 1, 2]));
        assert_eq!(iter.next(), Some(vec![1, 3, 2]));
        assert_eq!(iter.next(), Some(vec![2, 1, 3]));
        assert_eq!(iter.next(), Some(vec![1, 2, 3]));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn sections_vec_123() {
        let mut store = Store::new();
        let () = store.push(vec![1_usize]);
        let () = store.push(vec![2_usize]);
        let () = store.push(vec![3_usize]);
        let mut iter = store
            .sections(iter::once((TypeId::of::<Vec<usize>>(), 2)).collect())
            .map(|mut s| s.pop_all::<Vec<usize>>().unwrap());
        assert_eq!(iter.next(), Some(vec![vec![3], vec![1]]));
        assert_eq!(iter.next(), Some(vec![vec![2], vec![1]]));
        assert_eq!(iter.next(), Some(vec![vec![1], vec![2]]));
        assert_eq!(iter.next(), Some(vec![vec![3], vec![2]]));
        assert_eq!(iter.next(), Some(vec![vec![1], vec![3]]));
        assert_eq!(iter.next(), Some(vec![vec![2], vec![3]]));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn drop_sections() {
        let mut store = Store::new();
        let () = store.push(vec![1_usize]);
        let () = store.push(vec![2_usize]);
        let () = store.push(vec![3_usize]);
        let mut sections = store.sections(iter::once((TypeId::of::<Vec<usize>>(), 2)).collect());
        let () = sections.next().unwrap().drop_unused();
        // drop
    }

    #[test]
    fn visit_vec_as_linked_list() {
        let mut store = Store::new();
        let () = store.push(vec![1, 2, 3_usize]);

        let vec_store = store.clone();
        let vecs_visited: Vec<Vec<usize>> = vec_store.visit::<Vec<usize>>().collect();
        let vecs_expected: Vec<Vec<usize>> = vec![vec![1, 2, 3], vec![1, 2], vec![1], vec![]];
        assert_eq!(vecs_visited, vecs_expected);

        let usizes_visited: Vec<usize> = store.visit::<usize>().collect();
        let usizes_expected: Vec<usize> = vec![3, 2, 1];
        assert_eq!(usizes_visited, usizes_expected);
    }

    #[test]
    fn drop_visitor() {
        let mut store = Store::new();
        let () = store.push(1_usize);
        let () = store.push(2_usize);
        let () = store.push(3_usize);
        let mut visitor = store.visit::<usize>();
        let _: usize = visitor.next().unwrap();
        // drop
    }

    #[test]
    #[should_panic(expected = "INTERNAL ERROR (`pbt`): unused fields")]
    fn missing_field() {
        check_eta_expansion::<DestructsWithExtraField>();
    }
}