nu-protocol 0.112.1

Nushell's internal protocols, including its abstract syntax tree
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
use crate::{SyntaxShape, ast::PathMember};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, fmt::Display};
#[cfg(test)]
use strum_macros::EnumIter;

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Hash, Ord, PartialOrd)]
#[cfg_attr(test, derive(EnumIter))]
pub enum Type {
    /// Top type, supertype of all types
    Any,
    Binary,
    Block,
    Bool,
    CellPath,
    Closure,
    Custom(Box<str>),
    Date,
    Duration,
    Error,
    Filesize,
    Float,
    Int,
    List(Box<Type>),
    #[default]
    Nothing,
    /// Supertype of Int and Float. Equivalent to `oneof<int, float>`
    Number,
    /// Supertype of all types it contains.
    OneOf(Box<[Type]>),
    Range,
    Record(Box<[(String, Type)]>),
    String,
    Glob,
    Table(Box<[(String, Type)]>),
}

fn follow_cell_path_recursive<'a>(
    current: Cow<'a, Type>,
    path_members: &mut dyn Iterator<Item = &'a PathMember>,
) -> Option<Cow<'a, Type>> {
    let Some(first) = path_members.next() else {
        return Some(current);
    };
    match (current.as_ref(), first) {
        (Type::Record(fields), PathMember::String { val, .. }) => {
            let idx = fields.iter().position(|(name, _)| name == val)?;
            let next = match current {
                Cow::Borrowed(Type::Record(f)) => Cow::Borrowed(&f[idx].1),
                Cow::Owned(Type::Record(f)) => Cow::Owned(f[idx].1.to_owned()),
                _ => unreachable!(),
            };
            follow_cell_path_recursive(next, path_members)
        }

        // Table to Record (Int)
        (Type::Table(f), PathMember::Int { .. }) => {
            follow_cell_path_recursive(Cow::Owned(Type::Record(f.clone())), path_members)
        }

        // Table to List (String)
        (Type::Table(fields), PathMember::String { val, .. }) => {
            let (_, sub_type) = fields.iter().find(|(name, _)| name == val)?;
            let list_type = Type::List(Box::new(sub_type.clone()));
            follow_cell_path_recursive(Cow::Owned(list_type), path_members)
        }

        (Type::List(_), PathMember::Int { .. }) => {
            let next = match current {
                Cow::Borrowed(Type::List(i)) => Cow::Borrowed(i.as_ref()),
                Cow::Owned(Type::List(i)) => Cow::Owned(*i),
                _ => unreachable!(),
            };
            follow_cell_path_recursive(next, path_members)
        }

        // List of Records indexed by key names
        (Type::List(_), PathMember::String { .. }) => {
            let next = match current {
                Cow::Borrowed(Type::List(i)) => Cow::Borrowed(i.as_ref()),
                Cow::Owned(Type::List(i)) => Cow::Owned(*i),
                _ => unreachable!(),
            };

            let mut found_int_member = false;
            let mut new_iter = std::iter::once(first).chain(path_members).filter(|pm| {
                let first_int = !found_int_member && matches!(pm, PathMember::Int { .. });
                if first_int {
                    found_int_member = true;
                }
                !first_int
            });
            let inner_ty = follow_cell_path_recursive(next, &mut new_iter);

            // If there's no int path member, need to wrap in a List type
            // e.g. [{foo: bar}].foo -> [bar], list<record<foo: string>> -> list<string>
            if found_int_member {
                inner_ty
            } else {
                inner_ty.map(|inner_ty| Cow::Owned(Type::List(Box::new(inner_ty.into_owned()))))
            }
        }

        _ => None,
    }
}

impl Type {
    pub fn list(inner: Type) -> Self {
        Self::List(Box::new(inner))
    }

    /// Creates a OneOf type from an iterator of types.
    /// Flattens any nested OneOf types and removes duplicates.
    pub fn one_of(types: impl IntoIterator<Item = Type>) -> Self {
        let mut flattened = Vec::new();
        for t in types {
            Self::oneof_add(&mut flattened, t);
        }
        Self::OneOf(flattened.into())
    }

    pub fn record() -> Self {
        Self::Record([].into())
    }

    pub fn table() -> Self {
        Self::Table([].into())
    }

    pub fn custom(name: impl Into<Box<str>>) -> Self {
        Self::Custom(name.into())
    }

    /// Determine of the [`Type`] is a [subtype](https://en.wikipedia.org/wiki/Subtyping) of `other`.
    ///
    /// This should only be used at parse-time.
    /// If you have a concrete [`Value`](crate::Value) or [`PipelineData`](crate::PipelineData),
    /// you should use their respective `is_subtype_of` methods instead.
    pub fn is_subtype_of(&self, other: &Type) -> bool {
        // Structural subtyping
        let is_subtype_collection = |this: &[(String, Type)], that: &[(String, Type)]| {
            if this.is_empty() || that.is_empty() {
                true
            } else if this.len() < that.len() {
                false
            } else {
                that.iter().all(|(col_y, ty_y)| {
                    if let Some((_, ty_x)) = this.iter().find(|(col_x, _)| col_x == col_y) {
                        ty_x.is_subtype_of(ty_y)
                    } else {
                        false
                    }
                })
            }
        };

        match (self, other) {
            (t, u) if t == u => true,
            (_, Type::Any) => true,
            // We want `get`/`select`/etc to accept string and int values, so it's convenient to
            // use them with variables, without having to explicitly convert them into cell-paths
            (Type::String | Type::Int, Type::CellPath) => true,
            (Type::OneOf(oneof), Type::CellPath) => {
                oneof.iter().all(|t| t.is_subtype_of(&Type::CellPath))
            }
            (Type::Float | Type::Int, Type::Number) => true,
            (Type::Glob, Type::String) | (Type::String, Type::Glob) => true,
            (Type::List(t), Type::List(u)) if t.is_subtype_of(u) => true, // List is covariant
            (Type::Record(this), Type::Record(that)) | (Type::Table(this), Type::Table(that)) => {
                is_subtype_collection(this, that)
            }
            (Type::Table(_), Type::List(that)) if matches!(**that, Type::Any) => true,
            (Type::Table(this), Type::List(that)) => {
                matches!(that.as_ref(), Type::Record(that) if is_subtype_collection(this, that))
            }
            (Type::List(this), Type::Table(that)) => {
                matches!(this.as_ref(), Type::Record(this) if is_subtype_collection(this, that))
            }
            (Type::OneOf(this), that @ Type::OneOf(_)) => {
                this.iter().all(|t| t.is_subtype_of(that))
            }
            (this, Type::OneOf(that)) => that.iter().any(|t| this.is_subtype_of(t)),
            _ => false,
        }
    }

    /// Returns supertype of arguments without creating a `oneof`, or falling back to `any` (unless one or both of the arguments are `any`)
    fn flat_widen(lhs: Type, rhs: Type) -> Result<Type, (Type, Type)> {
        // Fast-paths that don't require cloning.
        if lhs == rhs {
            return Ok(lhs);
        }

        // Any value yields the top type.
        if matches!(lhs, Type::Any) || matches!(rhs, Type::Any) {
            return Ok(Type::Any);
        }

        // primitive number hierarchy is extremely common; handle it before any more expensive logic (including subtype checks) to keep
        // `type_widen_simple` fast.
        if matches!(lhs, Type::Int | Type::Float | Type::Number)
            && matches!(rhs, Type::Int | Type::Float | Type::Number)
        {
            return Ok(Type::Number);
        }

        // disjoint glob/string pair. We don't want to consume lhs/rhs here because subsequent code still needs them.
        if (matches!(lhs, Type::Glob) && matches!(rhs, Type::String))
            || (matches!(lhs, Type::String) && matches!(rhs, Type::Glob))
        {
            return Err((lhs, rhs));
        }

        // structural collections; clones are unavoidable because we need owned data for the result, but we only clone
        // the inner vectors, not the entire `Type` twice.
        match (&lhs, &rhs) {
            (Type::Record(this), Type::Record(that)) => {
                let widened = Self::widen_collection(this.clone(), that.clone());
                return Ok(Type::Record(widened));
            }
            (Type::Table(this), Type::Table(that)) => {
                let widened = Self::widen_collection(this.clone(), that.clone());
                return Ok(Type::Table(widened));
            }

            (Type::List(_list_item), Type::Table(_table))
            | (Type::Table(_table), Type::List(_list_item)) => {
                // `lhs` and `rhs` are still owned, so we can match on the original values once again to avoid needless cloning.
                let item = match (lhs, rhs) {
                    (Type::List(list_item), Type::Table(table)) => match *list_item {
                        Type::Record(record) => Type::Record(Self::widen_collection(record, table)),
                        list_item => Type::one_of([list_item, Type::Record(table)]),
                    },
                    (Type::Table(table), Type::List(list_item)) => match *list_item {
                        Type::Record(record) => Type::Record(Self::widen_collection(record, table)),
                        list_item => Type::one_of([list_item, Type::Record(table)]),
                    },
                    _ => unreachable!(),
                };
                return Ok(Type::List(Box::new(item)));
            }

            (Type::List(lhs), Type::List(rhs)) => {
                // We have to take ownership of the inner types, so clone here.
                let lhs_inner = lhs.clone();
                let rhs_inner = rhs.clone();
                return Ok(Type::list(lhs_inner.widen(*rhs_inner)));
            }

            _ => {}
        }

        // If one type is already a subtype of the other, we can skip all of the heavier logic below.
        if lhs.is_subtype_of(&rhs) {
            return Ok(rhs);
        }
        if rhs.is_subtype_of(&lhs) {
            return Ok(lhs);
        }

        // Fallback - the two types are unrelated. Move them out so that callers don't have to clone again.
        Err((lhs, rhs))
    }

    fn widen_collection(
        lhs: Box<[(String, Type)]>,
        rhs: Box<[(String, Type)]>,
    ) -> Box<[(String, Type)]> {
        if lhs.is_empty() || rhs.is_empty() {
            return [].into();
        }

        // iterate the shorter list to reduce quadratic behaviour
        let (small, big) = if lhs.len() <= rhs.len() {
            (lhs, rhs)
        } else {
            (rhs, lhs)
        };

        const MAP_THRESH: usize = 16;
        if big.len() > MAP_THRESH {
            use std::collections::HashMap;
            let mut big_map: HashMap<String, Type> = big.into_iter().collect();
            small
                .into_iter()
                .filter_map(|(col, typ)| big_map.remove(&col).map(|b_typ| (col, typ.widen(b_typ))))
                .collect()
        } else {
            small
                .into_iter()
                .filter_map(|(col, typ)| {
                    big.iter()
                        .find_map(|(b_col, b_typ)| (&col == b_col).then(|| b_typ.clone()))
                        .map(|b_typ| (col, typ.widen(b_typ)))
                })
                .collect()
        }
    }

    /// Returns the supertype between `self` and `other`, or `Type::Any` if they're unrelated
    pub fn widen(self, other: Type) -> Type {
        // defensive fast-path: if one value is already a subtype of the other, return the supertype immediately.
        //
        // A subtle exception: a list-of-records is considered a subtype of a table with matching columns.
        fn shortcut_allowed(lhs: &Type, rhs: &Type) -> bool {
            !matches!(
                (lhs, rhs),
                (Type::List(_), Type::Table(_)) | (Type::Table(_), Type::List(_))
            )
        }

        // only shortcut when the relationship is one-way; for pairs like glob/string `is_subtype_of` returns true both ways,
        // and we must not collapse them to a single type.
        if self.is_subtype_of(&other)
            && !other.is_subtype_of(&self)
            && shortcut_allowed(&self, &other)
        {
            return other;
        }

        let tu = match Self::flat_widen(self, other) {
            Ok(t) => return t,
            Err(tu) => tu,
        };

        match tu {
            (Type::OneOf(ts), Type::OneOf(us)) => {
                let (big, small) = match ts.len() >= us.len() {
                    true => (ts, us),
                    false => (us, ts),
                };
                let mut out = big.into_vec();
                for t in small.into_iter() {
                    Self::oneof_add_widen(&mut out, t);
                }
                Type::one_of(out)
            }
            (Type::OneOf(oneof), t) | (t, Type::OneOf(oneof)) => {
                let mut out = oneof.into_vec();
                Self::oneof_add_widen(&mut out, t);
                Type::one_of(out)
            }
            (this, other) => Type::one_of([this, other]),
        }
    }

    /// Adds a type to a OneOf union, flattening nested OneOfs, deduplicating, and attempting to widen existing types.
    fn oneof_add_widen(oneof: &mut Vec<Type>, mut t: Type) {
        // handle nested unions first
        if let Type::OneOf(inner) = t {
            for sub_t in inner.into_vec() {
                Self::oneof_add_widen(oneof, sub_t);
            }
            return;
        }

        let mut i = 0;
        while i < oneof.len() {
            let one = std::mem::replace(&mut oneof[i], Type::Any);
            match Self::flat_widen(one, t) {
                Ok(one_t) => {
                    oneof[i] = one_t;
                    return;
                }
                Err((one_old, t_old)) => {
                    oneof[i] = one_old;
                    t = t_old; // `t` is mutable here
                    i += 1;
                }
            }
        }

        oneof.push(t);
    }

    /// Adds a type to a OneOf union, flattening nested OneOfs and deduplicating.
    fn oneof_add(oneof: &mut Vec<Type>, t: Type) {
        match t {
            Type::OneOf(inner) => {
                for sub_t in inner.into_vec() {
                    Self::oneof_add(oneof, sub_t);
                }
            }
            t => {
                if !oneof.contains(&t) {
                    oneof.push(t);
                }
            }
        }
    }

    /// Returns the supertype of all types within `it`. Short-circuits on, and falls back to, `Type::Any`.
    pub fn supertype_of(it: impl IntoIterator<Item = Type>) -> Option<Self> {
        let mut it = it.into_iter();
        it.next().and_then(|head| {
            it.try_fold(head, |acc, e| match acc.widen(e) {
                Type::Any => None,
                r => Some(r),
            })
        })
    }

    pub fn is_numeric(&self) -> bool {
        matches!(self, Type::Int | Type::Float | Type::Number)
    }

    pub fn is_list(&self) -> bool {
        matches!(self, Type::List(_))
    }

    /// Does this type represent a data structure containing values that can be addressed using 'cell paths'?
    pub fn accepts_cell_paths(&self) -> bool {
        matches!(self, Type::List(_) | Type::Record(_) | Type::Table(_))
    }

    pub fn to_shape(&self) -> SyntaxShape {
        let mk_shape = |tys: &[(String, Type)]| {
            tys.iter()
                .map(|(key, val)| (key.clone(), val.to_shape()))
                .collect()
        };

        match self {
            Type::Int => SyntaxShape::Int,
            Type::Float => SyntaxShape::Float,
            Type::Range => SyntaxShape::Range,
            Type::Bool => SyntaxShape::Boolean,
            Type::String => SyntaxShape::String,
            Type::Block => SyntaxShape::Block, // FIXME needs more accuracy
            Type::Closure => SyntaxShape::Closure(None), // FIXME needs more accuracy
            Type::CellPath => SyntaxShape::CellPath,
            Type::Duration => SyntaxShape::Duration,
            Type::Date => SyntaxShape::DateTime,
            Type::Filesize => SyntaxShape::Filesize,
            Type::List(x) => SyntaxShape::List(Box::new(x.to_shape())),
            Type::Number => SyntaxShape::Number,
            Type::OneOf(types) => SyntaxShape::OneOf(types.iter().map(Type::to_shape).collect()),
            Type::Nothing => SyntaxShape::Nothing,
            Type::Record(entries) => SyntaxShape::Record(mk_shape(entries)),
            Type::Table(columns) => SyntaxShape::Table(mk_shape(columns)),
            Type::Any => SyntaxShape::Any,
            Type::Error => SyntaxShape::Any,
            Type::Binary => SyntaxShape::Binary,
            Type::Custom(_) => SyntaxShape::Any,
            Type::Glob => SyntaxShape::GlobPattern,
        }
    }

    /// Get a string representation, without inner type specification of lists,
    /// tables and records (get `list` instead of `list<any>`
    pub fn get_non_specified_string(&self) -> String {
        match self {
            Type::Closure => String::from("closure"),
            Type::Bool => String::from("bool"),
            Type::Block => String::from("block"),
            Type::CellPath => String::from("cell-path"),
            Type::Date => String::from("datetime"),
            Type::Duration => String::from("duration"),
            Type::Filesize => String::from("filesize"),
            Type::Float => String::from("float"),
            Type::Int => String::from("int"),
            Type::Range => String::from("range"),
            Type::Record(_) => String::from("record"),
            Type::Table(_) => String::from("table"),
            Type::List(_) => String::from("list"),
            Type::Nothing => String::from("nothing"),
            Type::Number => String::from("number"),
            Type::OneOf(_) => String::from("oneof"),
            Type::String => String::from("string"),
            Type::Any => String::from("any"),
            Type::Error => String::from("error"),
            Type::Binary => String::from("binary"),
            Type::Custom(_) => String::from("custom"),
            Type::Glob => String::from("glob"),
        }
    }

    pub fn follow_cell_path<'a>(&'a self, path_members: &'a [PathMember]) -> Option<Cow<'a, Self>> {
        follow_cell_path_recursive(Cow::Borrowed(self), &mut path_members.iter())
    }
}

impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Type::Block => write!(f, "block"),
            Type::Closure => write!(f, "closure"),
            Type::Bool => write!(f, "bool"),
            Type::CellPath => write!(f, "cell-path"),
            Type::Date => write!(f, "datetime"),
            Type::Duration => write!(f, "duration"),
            Type::Filesize => write!(f, "filesize"),
            Type::Float => write!(f, "float"),
            Type::Int => write!(f, "int"),
            Type::Range => write!(f, "range"),
            Type::Record(fields) => {
                if fields.is_empty() {
                    write!(f, "record")
                } else {
                    write!(
                        f,
                        "record<{}>",
                        fields
                            .iter()
                            .map(|(x, y)| format!("{x}: {y}"))
                            .collect::<Vec<String>>()
                            .join(", "),
                    )
                }
            }
            Type::Table(columns) => {
                if columns.is_empty() {
                    write!(f, "table")
                } else {
                    write!(
                        f,
                        "table<{}>",
                        columns
                            .iter()
                            .map(|(x, y)| format!("{x}: {y}"))
                            .collect::<Vec<String>>()
                            .join(", ")
                    )
                }
            }
            Type::List(l) => write!(f, "list<{l}>"),
            Type::Nothing => write!(f, "nothing"),
            Type::Number => write!(f, "number"),
            Type::OneOf(types) => {
                write!(f, "oneof")?;
                let [first, rest @ ..] = &**types else {
                    return Ok(());
                };
                write!(f, "<{first}")?;
                for t in rest {
                    write!(f, ", {t}")?;
                }
                f.write_str(">")
            }
            Type::String => write!(f, "string"),
            Type::Any => write!(f, "any"),
            Type::Error => write!(f, "error"),
            Type::Binary => write!(f, "binary"),
            Type::Custom(custom) => write!(f, "{custom}"),
            Type::Glob => write!(f, "glob"),
        }
    }
}

/// Get a string nicely combining multiple types
///
/// Helpful for listing types in errors
pub fn combined_type_string(types: &[Type], join_word: &str) -> Option<String> {
    use std::fmt::Write as _;

    // Deduplicate types to avoid confusing repeated entries like
    // "binary, binary, binary, or binary" in error messages.
    let mut seen = Vec::new();
    for t in types {
        if !seen.contains(t) {
            seen.push(t.clone());
        }
    }

    match seen.as_slice() {
        [] => None,
        [one] => Some(one.to_string()),
        [one, two] => Some(format!("{one} {join_word} {two}")),
        [initial @ .., last] => {
            let mut out = String::new();
            for ele in initial {
                let _ = write!(out, "{ele}, ");
            }
            let _ = write!(out, "{join_word} {last}");
            Some(out)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Type;
    use strum::IntoEnumIterator;

    mod subtype_relation {
        use super::*;

        #[test]
        fn test_reflexivity() {
            for ty in Type::iter() {
                assert!(ty.is_subtype_of(&ty));
            }
        }

        #[test]
        fn test_any_is_top_type() {
            for ty in Type::iter() {
                assert!(ty.is_subtype_of(&Type::Any));
            }
        }

        #[test]
        fn test_number_supertype() {
            assert!(Type::Int.is_subtype_of(&Type::Number));
            assert!(Type::Float.is_subtype_of(&Type::Number));
        }

        #[test]
        fn test_list_covariance() {
            for ty1 in Type::iter() {
                for ty2 in Type::iter() {
                    let list_ty1 = Type::List(Box::new(ty1.clone()));
                    let list_ty2 = Type::List(Box::new(ty2.clone()));
                    assert_eq!(list_ty1.is_subtype_of(&list_ty2), ty1.is_subtype_of(&ty2));
                }
            }
        }
    }

    mod oneof_flattening {
        use super::*;

        #[test]
        fn test_oneof_creation_flattens() {
            let nested = Type::one_of([
                Type::String,
                Type::one_of([Type::Int, Type::Float]),
                Type::Bool,
            ]);
            if let Type::OneOf(types) = nested {
                let types_vec = types.to_vec();
                assert_eq!(types_vec.len(), 4);
                assert!(types_vec.contains(&Type::String));
                assert!(types_vec.contains(&Type::Int));
                assert!(types_vec.contains(&Type::Float));
                assert!(types_vec.contains(&Type::Bool));
            } else {
                panic!("Expected OneOf");
            }
        }

        #[test]
        fn test_widen_flattens_oneof() {
            let a = Type::one_of([Type::String, Type::Int]);
            let b = Type::one_of([Type::Float, Type::Bool]);
            let widened = a.widen(b);
            if let Type::OneOf(types) = widened {
                let types_vec = types.to_vec();
                assert_eq!(types_vec.len(), 3);
                assert!(types_vec.contains(&Type::String));
                assert!(types_vec.contains(&Type::Number)); // Int + Float -> Number
                assert!(types_vec.contains(&Type::Bool));
            } else {
                panic!("Expected OneOf");
            }
        }

        #[test]
        fn test_oneof_deduplicates() {
            let record_type =
                Type::Record(vec![("content".to_string(), Type::list(Type::String))].into());
            let oneof = Type::one_of([Type::String, record_type.clone(), record_type.clone()]);
            if let Type::OneOf(types) = oneof {
                let types_vec = types.to_vec();
                assert_eq!(types_vec.len(), 2);
                assert!(types_vec.contains(&Type::String));
                assert!(types_vec.contains(&record_type));
            } else {
                panic!("Expected OneOf");
            }
        }
    }

    // regressions and performance tests for the subtype shortcut added above
    mod widen_shortcuts {
        use super::*;

        #[test]
        fn test_widen_subtype_shortcut() {
            // widening a union that already covers the new type should return the original union unchanged.
            let union = Type::one_of([Type::String, Type::Number]);
            let result = union.clone().widen(Type::Int);
            assert_eq!(result, union);

            // symmetric case where the left side is the subtype
            let union2 = Type::one_of([Type::Int, Type::String]);
            let result2 = Type::Int.widen(union2.clone());
            assert_eq!(result2, union2);
        }

        #[test]
        fn test_chain_shortcut() {
            // repeatedly widen the same type pair
            let mut t = Type::String;
            for _ in 0..100 {
                t = t.widen(Type::Int);
            }
            let expected = Type::one_of([Type::String, Type::Int]);
            assert_eq!(t, expected);
        }

        #[test]
        fn test_list_table_widen_preserves_list() {
            // verify that list<record> widened with table does not drop the list wrapper.
            let list_record = Type::List(Box::new(Type::Record(
                vec![("a".to_string(), Type::Int)].into(),
            )));
            let table = Type::Table(vec![("a".to_string(), Type::Int)].into());

            let widened = list_record.clone().widen(table.clone());
            let expected = Type::List(Box::new(Type::Record(
                vec![("a".to_string(), Type::Int)].into(),
            )));
            assert_eq!(widened, expected);

            // and the other way around
            let widened2 = table.widen(list_record.clone());
            assert_eq!(widened2, expected);
        }

        #[test]
        fn test_glob_string_union() {
            let g = Type::Glob;
            let s = Type::String;
            let w1 = g.clone().widen(s.clone());
            let w2 = s.clone().widen(g.clone());
            let expected1 = Type::one_of([Type::Glob, Type::String]);
            let expected2 = Type::one_of([Type::String, Type::Glob]);
            assert_eq!(w1, expected1);
            assert_eq!(w2, expected2);
        }
    }
}