match-lang 0.2.0

Exhaustiveness and usefulness checking for pattern matching, generic over a language's constructors.
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
//! Witnesses: patterns describing values that no arm matches.

use alloc::vec::Vec;
use core::fmt;

use crate::Model;

/// The constructor at one node of a [`Witness`].
///
/// # Examples
///
/// ```
/// use match_lang::Ctor;
///
/// let gap = Ctor::Int { lo: 10, hi: 255 };
/// assert_ne!(gap, Ctor::Wild);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Ctor {
    /// `_`: any value of the type.
    Wild,
    /// A value no pattern can name: an opaque literal that no arm mentions,
    /// or a [`BigInt`](crate::Signature::BigInt) outside the `i128` range.
    /// Printed as `_`.
    Other,
    /// Variant `index` of a sum type.
    Variant(u32),
    /// The constructor of a product type.
    Product,
    /// A boolean.
    Bool(bool),
    /// Any integer in `lo..=hi`.
    Int {
        /// The low end, inclusive.
        lo: i128,
        /// The high end, inclusive.
        hi: i128,
    },
    /// Any character in `lo..=hi`.
    Char {
        /// The low end, inclusive.
        lo: char,
        /// The high end, inclusive.
        hi: char,
    },
    /// An opaque literal, by id.
    Lit(u64),
    /// A sequence of exactly `len` elements; the fields are the elements.
    Slice {
        /// The number of elements.
        len: u32,
    },
    /// A sequence of at least `prefix + suffix` elements; the fields are the
    /// first `prefix` and the last `suffix` elements.
    SliceRest {
        /// The number of leading elements.
        prefix: u32,
        /// The number of trailing elements.
        suffix: u32,
    },
}

/// One node of a witness. Stored with the root last and each node's fields
/// immediately before it, first field nearest: that is the order the
/// algorithm produces them in, so building a witness never moves a node.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct WNode<T> {
    pub(crate) ctor: Ctor,
    pub(crate) ty: T,
    pub(crate) arity: u32,
    /// Nodes in this subtree, including this one.
    pub(crate) size: u32,
}

/// A pattern describing values that no arm matches: proof that a match is
/// not exhaustive.
///
/// Every value matching the witness is matched by no unguarded arm (a
/// [`Wild`](Ctor::Wild) node stands for any value of its type, an
/// [`Other`](Ctor::Other) node for any value no pattern names). Every
/// witness matches at least one value.
///
/// A witness is a tree of [`WitnessRef`] nodes, each with a [`Ctor`], the
/// type of the value at that position, and the fields beneath it. It prints
/// in a neutral syntax with `Display`, or with the language's variant names
/// through [`display`](Witness::display).
///
/// # Examples
///
/// ```
/// use match_lang::{check, Arm, Ctor, Model, Pat, Signature};
///
/// struct Bytes;
///
/// impl Model for Bytes {
///     type Ty = ();
///     fn signature(&self, _: &()) -> Signature<()> {
///         Signature::Int { min: 0, max: 255 }
///     }
///     fn fields(&self, _: &(), _: u32, _: &mut Vec<()>) {}
/// }
///
/// let arms = [Arm::new(Pat::range(0, 9)), Arm::new(Pat::range(20, 255))];
/// let report = check(&Bytes, &(), &arms)?;
/// let witness = &report.missing()[0];
/// assert_eq!(witness.ctor(), Ctor::Int { lo: 10, hi: 19 });
/// assert_eq!(witness.to_string(), "10..=19");
/// # Ok::<(), match_lang::Error>(())
/// ```
#[derive(Clone, PartialEq, Eq)]
pub struct Witness<T> {
    nodes: Vec<WNode<T>>,
}

impl<T> Witness<T> {
    /// Wraps a node sequence holding exactly one subtree.
    pub(crate) fn from_nodes(nodes: Vec<WNode<T>>) -> Witness<T> {
        Witness { nodes }
    }

    /// The root node.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Ctor, Model, Pat, Signature};
    /// # struct Flag;
    /// # impl Model for Flag {
    /// #     type Ty = ();
    /// #     fn signature(&self, _: &()) -> Signature<()> { Signature::Bool }
    /// #     fn fields(&self, _: &(), _: u32, _: &mut Vec<()>) {}
    /// # }
    /// let report = check(&Flag, &(), &[Arm::new(Pat::bool(true))])?;
    /// assert_eq!(report.missing()[0].root().ctor(), Ctor::Bool(false));
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    #[must_use]
    pub fn root(&self) -> WitnessRef<'_, T> {
        WitnessRef {
            nodes: &self.nodes,
            pos: self.nodes.len().saturating_sub(1),
        }
    }

    /// The root's constructor; shorthand for `root().ctor()`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Ctor, Model, Pat, Signature};
    /// # struct Flag;
    /// # impl Model for Flag {
    /// #     type Ty = ();
    /// #     fn signature(&self, _: &()) -> Signature<()> { Signature::Bool }
    /// #     fn fields(&self, _: &(), _: u32, _: &mut Vec<()>) {}
    /// # }
    /// let report = check(&Flag, &(), &[Arm::new(Pat::bool(false))])?;
    /// assert_eq!(report.missing()[0].ctor(), Ctor::Bool(true));
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    #[must_use]
    pub fn ctor(&self) -> Ctor {
        self.root().ctor()
    }

    /// The type of the whole witness: the scrutinee's type.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Model, Pat, Signature};
    /// # struct Flag;
    /// # impl Model for Flag {
    /// #     type Ty = &'static str;
    /// #     fn signature(&self, _: &&'static str) -> Signature<&'static str> { Signature::Bool }
    /// #     fn fields(&self, _: &&'static str, _: u32, _: &mut Vec<&'static str>) {}
    /// # }
    /// let report = check(&Flag, &"bool", &[])?;
    /// assert_eq!(*report.missing()[0].ty(), "bool");
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    #[must_use]
    pub fn ty(&self) -> &T {
        self.root().ty()
    }

    /// The root's fields; shorthand for `root().fields()`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Ctor, Model, Pat, Signature};
    /// # struct Pair;
    /// # impl Model for Pair {
    /// #     type Ty = bool; // true: the pair, false: a bool inside it
    /// #     fn signature(&self, ty: &bool) -> Signature<bool> {
    /// #         if *ty { Signature::Product } else { Signature::Bool }
    /// #     }
    /// #     fn fields(&self, _: &bool, _: u32, out: &mut Vec<bool>) { out.extend([false, false]) }
    /// # }
    /// let arms = [Arm::new(Pat::product([Pat::bool(true), Pat::wild()]))];
    /// let report = check(&Pair, &true, &arms)?;
    /// let fields: Vec<Ctor> = report.missing()[0].fields().map(|f| f.ctor()).collect();
    /// assert_eq!(fields, [Ctor::Bool(false), Ctor::Wild]);
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    pub fn fields(&self) -> WitnessFields<'_, T> {
        self.root().fields()
    }

    /// Prints the witness with the model's variant names (see
    /// [`Model::variant_name`]).
    ///
    /// # Examples
    ///
    /// ```
    /// use match_lang::{check, Arm, Model, Pat, Signature};
    ///
    /// struct Opt;
    ///
    /// impl Model for Opt {
    ///     type Ty = u8; // 0: Option<bool>, 1: bool
    ///     fn signature(&self, ty: &u8) -> Signature<u8> {
    ///         if *ty == 0 { Signature::Sum { count: 2 } } else { Signature::Bool }
    ///     }
    ///     fn fields(&self, _: &u8, variant: u32, out: &mut Vec<u8>) {
    ///         if variant == 1 {
    ///             out.push(1);
    ///         }
    ///     }
    ///     fn variant_name(&self, _: &u8, variant: u32) -> Option<&str> {
    ///         Some(if variant == 0 { "None" } else { "Some" })
    ///     }
    /// }
    ///
    /// let arms = [Arm::new(Pat::variant(0, [])), Arm::new(Pat::variant(1, [Pat::bool(true)]))];
    /// let report = check(&Opt, &0, &arms)?;
    /// assert_eq!(report.missing()[0].to_string(), "#1(false)");
    /// assert_eq!(report.missing()[0].display(&Opt).to_string(), "Some(false)");
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    pub fn display<'a, M: Model<Ty = T>>(&'a self, model: &'a M) -> WitnessDisplay<'a, M> {
        WitnessDisplay {
            witness: self.root(),
            model,
        }
    }
}

impl<T> fmt::Display for Witness<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.root().fmt(f)
    }
}

impl<T> fmt::Debug for Witness<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Witness({})", self.root())
    }
}

/// A node of a [`Witness`]: its constructor, its type, and its fields.
///
/// # Examples
///
/// ```
/// # use match_lang::{check, Arm, Ctor, Model, Pat, Signature};
/// # struct Pair;
/// # impl Model for Pair {
/// #     type Ty = bool;
/// #     fn signature(&self, ty: &bool) -> Signature<bool> {
/// #         if *ty { Signature::Product } else { Signature::Bool }
/// #     }
/// #     fn fields(&self, _: &bool, _: u32, out: &mut Vec<bool>) { out.extend([false, false]) }
/// # }
/// let arms = [Arm::new(Pat::product([Pat::wild(), Pat::bool(true)]))];
/// let report = check(&Pair, &true, &arms)?;
/// let root = report.missing()[0].root();
/// assert_eq!(root.ctor(), Ctor::Product);
/// assert_eq!(root.arity(), 2);
/// let second = root.fields().nth(1).unwrap();
/// assert_eq!((second.ctor(), *second.ty()), (Ctor::Bool(false), false));
/// # Ok::<(), match_lang::Error>(())
/// ```
pub struct WitnessRef<'a, T> {
    nodes: &'a [WNode<T>],
    pos: usize,
}

impl<T> Clone for WitnessRef<'_, T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for WitnessRef<'_, T> {}

impl<'a, T> WitnessRef<'a, T> {
    fn node(&self) -> Option<&'a WNode<T>> {
        self.nodes.get(self.pos)
    }

    /// The constructor at this node.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Ctor, Model, Pat, Signature};
    /// # struct Flag;
    /// # impl Model for Flag {
    /// #     type Ty = ();
    /// #     fn signature(&self, _: &()) -> Signature<()> { Signature::Bool }
    /// #     fn fields(&self, _: &(), _: u32, _: &mut Vec<()>) {}
    /// # }
    /// let report = check(&Flag, &(), &[Arm::new(Pat::bool(true))])?;
    /// assert_eq!(report.missing()[0].root().ctor(), Ctor::Bool(false));
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    #[must_use]
    pub fn ctor(&self) -> Ctor {
        self.node().map_or(Ctor::Wild, |n| n.ctor)
    }

    /// The type of the value at this node.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Model, Pat, Signature};
    /// # struct Flag;
    /// # impl Model for Flag {
    /// #     type Ty = u8;
    /// #     fn signature(&self, _: &u8) -> Signature<u8> { Signature::Bool }
    /// #     fn fields(&self, _: &u8, _: u32, _: &mut Vec<u8>) {}
    /// # }
    /// let report = check(&Flag, &7, &[])?;
    /// assert_eq!(*report.missing()[0].root().ty(), 7);
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    #[must_use]
    pub fn ty(&self) -> &'a T {
        // A `WitnessRef` is only made for positions inside its witness, and a
        // witness always has a root, so the fallback is never taken.
        match self.nodes.get(self.pos) {
            Some(node) => &node.ty,
            None => &self.nodes[0].ty,
        }
    }

    /// The number of fields under this node.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Model, Pat, Signature};
    /// # struct Flag;
    /// # impl Model for Flag {
    /// #     type Ty = ();
    /// #     fn signature(&self, _: &()) -> Signature<()> { Signature::Bool }
    /// #     fn fields(&self, _: &(), _: u32, _: &mut Vec<()>) {}
    /// # }
    /// let report = check(&Flag, &(), &[])?;
    /// assert_eq!(report.missing()[0].root().arity(), 0);
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    #[must_use]
    pub fn arity(&self) -> usize {
        self.node().map_or(0, |n| n.arity as usize)
    }

    /// The fields under this node, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Ctor, Model, Pat, Signature};
    /// # struct Pair;
    /// # impl Model for Pair {
    /// #     type Ty = bool;
    /// #     fn signature(&self, ty: &bool) -> Signature<bool> {
    /// #         if *ty { Signature::Product } else { Signature::Bool }
    /// #     }
    /// #     fn fields(&self, _: &bool, _: u32, out: &mut Vec<bool>) { out.extend([false, false]) }
    /// # }
    /// let arms = [Arm::new(Pat::product([Pat::bool(false), Pat::bool(false)]))];
    /// let report = check(&Pair, &true, &arms)?;
    /// let first = report.missing()[0].root().fields().next().unwrap();
    /// assert_eq!(first.ctor(), Ctor::Bool(true));
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    #[must_use]
    pub fn fields(&self) -> WitnessFields<'a, T> {
        WitnessFields {
            nodes: self.nodes,
            next: self.pos.checked_sub(1),
            remaining: self.arity(),
        }
    }

    /// Prints this node with the model's variant names.
    ///
    /// # Examples
    ///
    /// ```
    /// # use match_lang::{check, Arm, Model, Pat, Signature};
    /// # struct Opt;
    /// # impl Model for Opt {
    /// #     type Ty = u8;
    /// #     fn signature(&self, ty: &u8) -> Signature<u8> {
    /// #         if *ty == 0 { Signature::Sum { count: 2 } } else { Signature::Bool }
    /// #     }
    /// #     fn fields(&self, _: &u8, v: u32, out: &mut Vec<u8>) { if v == 1 { out.push(1) } }
    /// #     fn variant_name(&self, _: &u8, v: u32) -> Option<&str> {
    /// #         Some(if v == 0 { "None" } else { "Some" })
    /// #     }
    /// # }
    /// let report = check(&Opt, &0, &[Arm::new(Pat::variant(1, [Pat::wild()]))])?;
    /// assert_eq!(report.missing()[0].root().display(&Opt).to_string(), "None");
    /// # Ok::<(), match_lang::Error>(())
    /// ```
    pub fn display<M: Model<Ty = T>>(self, model: &'a M) -> WitnessDisplay<'a, M> {
        WitnessDisplay {
            witness: self,
            model,
        }
    }

    /// Writes the subtree, asking `name` for variant and product names.
    fn write_with<'n>(
        &self,
        f: &mut fmt::Formatter<'_>,
        name: impl Fn(&T, u32) -> Option<&'n str>,
    ) -> fmt::Result {
        enum Step<'a, T> {
            Node(WitnessRef<'a, T>),
            Text(&'static str),
        }
        let mut stack = Vec::new();
        stack.push(Step::Node(*self));
        let mut fields = Vec::new();
        while let Some(step) = stack.pop() {
            let node = match step {
                Step::Text(text) => {
                    f.write_str(text)?;
                    continue;
                }
                Step::Node(node) => node,
            };
            let (open, close) = match node.ctor() {
                Ctor::Wild | Ctor::Other => {
                    f.write_str("_")?;
                    continue;
                }
                Ctor::Bool(b) => {
                    write!(f, "{b}")?;
                    continue;
                }
                Ctor::Int { lo, hi } if lo == hi => {
                    write!(f, "{lo}")?;
                    continue;
                }
                Ctor::Int { lo, hi } => {
                    write!(f, "{lo}..={hi}")?;
                    continue;
                }
                Ctor::Char { lo, hi } if lo == hi => {
                    write!(f, "{lo:?}")?;
                    continue;
                }
                Ctor::Char { lo, hi } => {
                    write!(f, "{lo:?}..={hi:?}")?;
                    continue;
                }
                Ctor::Lit(id) => {
                    write!(f, "lit#{id}")?;
                    continue;
                }
                Ctor::Variant(index) => {
                    match name(node.ty(), index) {
                        Some(n) => f.write_str(n)?,
                        None => write!(f, "#{index}")?,
                    }
                    if node.arity() == 0 {
                        continue;
                    }
                    ("(", ")")
                }
                Ctor::Product => match name(node.ty(), 0) {
                    Some(n) => {
                        f.write_str(n)?;
                        if node.arity() == 0 {
                            continue;
                        }
                        ("(", ")")
                    }
                    None if node.arity() == 1 => ("(", ",)"),
                    None => ("(", ")"),
                },
                Ctor::Slice { .. } => ("[", "]"),
                Ctor::SliceRest { prefix, .. } => {
                    f.write_str("[")?;
                    stack.push(Step::Text("]"));
                    fields.clear();
                    fields.extend(node.fields());
                    let split = (prefix as usize).min(fields.len());
                    let (pre, post) = fields.split_at(split);
                    for (i, field) in post.iter().enumerate().rev() {
                        stack.push(Step::Node(*field));
                        if i > 0 {
                            stack.push(Step::Text(", "));
                        }
                    }
                    stack.push(Step::Text(match (pre.is_empty(), post.is_empty()) {
                        (true, true) => "..",
                        (true, false) => ".., ",
                        (false, true) => ", ..",
                        (false, false) => ", .., ",
                    }));
                    for (i, field) in pre.iter().enumerate().rev() {
                        stack.push(Step::Node(*field));
                        if i > 0 {
                            stack.push(Step::Text(", "));
                        }
                    }
                    continue;
                }
            };
            f.write_str(open)?;
            stack.push(Step::Text(close));
            fields.clear();
            fields.extend(node.fields());
            for (i, field) in fields.iter().enumerate().rev() {
                stack.push(Step::Node(*field));
                if i > 0 {
                    stack.push(Step::Text(", "));
                }
            }
        }
        Ok(())
    }
}

impl<T> fmt::Display for WitnessRef<'_, T> {
    /// Prints the subtree in a neutral syntax: `#i(..)` for variants, tuples
    /// for products, `_` for wildcards and unnamed values.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.write_with(f, |_, _| None)
    }
}

impl<T> fmt::Debug for WitnessRef<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "WitnessRef({self})")
    }
}

/// The fields of a [`WitnessRef`], in order. Returned by
/// [`WitnessRef::fields`].
///
/// # Examples
///
/// ```
/// # use match_lang::{check, Arm, Model, Pat, Signature};
/// # struct Pair;
/// # impl Model for Pair {
/// #     type Ty = bool;
/// #     fn signature(&self, ty: &bool) -> Signature<bool> {
/// #         if *ty { Signature::Product } else { Signature::Bool }
/// #     }
/// #     fn fields(&self, _: &bool, _: u32, out: &mut Vec<bool>) { out.extend([false, false]) }
/// # }
/// let report = check(&Pair, &true, &[])?;
/// assert_eq!(report.missing()[0].fields().len(), 2);
/// # Ok::<(), match_lang::Error>(())
/// ```
pub struct WitnessFields<'a, T> {
    nodes: &'a [WNode<T>],
    /// Where the next field's subtree ends (its root).
    next: Option<usize>,
    remaining: usize,
}

impl<'a, T> Iterator for WitnessFields<'a, T> {
    type Item = WitnessRef<'a, T>;

    fn next(&mut self) -> Option<WitnessRef<'a, T>> {
        if self.remaining == 0 {
            return None;
        }
        let pos = self.next?;
        let node = self.nodes.get(pos)?;
        self.remaining -= 1;
        // The next field's subtree ends just before this one starts.
        self.next = pos.checked_sub(node.size as usize);
        Some(WitnessRef {
            nodes: self.nodes,
            pos,
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }
}

impl<T> ExactSizeIterator for WitnessFields<'_, T> {}

impl<T> fmt::Debug for WitnessFields<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WitnessFields")
            .field("remaining", &self.remaining)
            .finish()
    }
}

/// A witness printed with the model's variant names. Returned by
/// [`Witness::display`] and [`WitnessRef::display`].
///
/// # Examples
///
/// ```
/// # use match_lang::{check, Arm, Model, Pat, Signature};
/// # struct Opt;
/// # impl Model for Opt {
/// #     type Ty = u8;
/// #     fn signature(&self, ty: &u8) -> Signature<u8> {
/// #         if *ty == 0 { Signature::Sum { count: 2 } } else { Signature::Bool }
/// #     }
/// #     fn fields(&self, _: &u8, v: u32, out: &mut Vec<u8>) { if v == 1 { out.push(1) } }
/// #     fn variant_name(&self, _: &u8, v: u32) -> Option<&str> {
/// #         Some(if v == 0 { "None" } else { "Some" })
/// #     }
/// # }
/// let report = check(&Opt, &0, &[Arm::new(Pat::variant(0, []))])?;
/// assert_eq!(format!("{}", report.missing()[0].display(&Opt)), "Some(_)");
/// # Ok::<(), match_lang::Error>(())
/// ```
pub struct WitnessDisplay<'a, M: Model> {
    witness: WitnessRef<'a, M::Ty>,
    model: &'a M,
}

impl<M: Model> fmt::Display for WitnessDisplay<'_, M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.witness
            .write_with(f, |ty, variant| self.model.variant_name(ty, variant))
    }
}

impl<M: Model> fmt::Debug for WitnessDisplay<'_, M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}