mir-types 0.61.0

Type system primitives for the mir PHP static analyzer
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
use std::fmt;

use crate::atomic::Atomic;
use crate::union::Type;

/// Write `items` separated by `sep` straight into the formatter, avoiding the
/// intermediate `Vec<String>` + `join` allocations.
fn write_joined<T: fmt::Display>(
    f: &mut fmt::Formatter<'_>,
    items: impl IntoIterator<Item = T>,
    sep: &str,
) -> fmt::Result {
    for (i, item) in items.into_iter().enumerate() {
        if i > 0 {
            f.write_str(sep)?;
        }
        write!(f, "{item}")?;
    }
    Ok(())
}

/// True when `t` is precisely `mixed` and nothing else — deliberately
/// stricter than [`Type::is_mixed`], which also treats an unconstrained
/// template parameter as mixed. A template placeholder is a meaningful part
/// of a generic signature (e.g. `array<TKey, TValue>`) and must still be
/// printed, whereas a literal, unconstrained `mixed` is a default that adds
/// no information and can be collapsed away.
fn is_exactly_mixed(t: &Type) -> bool {
    matches!(t.types.as_slice(), [Atomic::TMixed])
}

/// Write a comma-separated callable/closure parameter type list, printing
/// `mixed` for untyped params.
fn write_param_types(f: &mut fmt::Formatter<'_>, params: &[crate::atomic::FnParam]) -> fmt::Result {
    for (i, p) in params.iter().enumerate() {
        if i > 0 {
            f.write_str(", ")?;
        }
        match &p.ty {
            Some(ty) => write!(f, "{ty}")?,
            None => f.write_str("mixed")?,
        }
    }
    Ok(())
}

/// PHP's `iterable` is defined as exactly `array|Traversable`, so a union
/// containing a matching `TArray{key, value}` + `Traversable` pair prints no
/// more information as those two members than it would as `iterable`. Returns
/// the pair's indices (lower, higher) and the rendered replacement text.
///
/// The `Traversable` member only counts as a match when it's unparameterized
/// (the bare-`iterable` decomposition) or when its type params are exactly
/// `[key, value]` (the `iterable<K, V>` decomposition) — a bare `Traversable`
/// paired with a *more specific* array (e.g. `array<int, string>|Traversable`)
/// must NOT collapse, since the bare `Traversable` side carries no such
/// key/value guarantee and collapsing would overclaim precision.
fn iterable_span(types: &[Atomic]) -> Option<(usize, usize, String)> {
    for (ai, a) in types.iter().enumerate() {
        let Atomic::TArray { key, value } = a else {
            continue;
        };
        for (ti, t) in types.iter().enumerate() {
            if ti == ai {
                continue;
            }
            let Atomic::TNamedObject { fqcn, type_params } = t else {
                continue;
            };
            if fqcn.as_ref() != "Traversable" {
                continue;
            }
            let is_default_key_value =
                is_exactly_mixed(value) && (is_exactly_mixed(key) || key.is_array_key());
            let matches = if type_params.is_empty() {
                // A bare (unparameterized) Traversable makes no key/value
                // guarantee, so it only pairs safely with the fully generic
                // `array` — pairing it with a more specific array would
                // claim the Traversable side shares that specificity too.
                is_default_key_value
            } else {
                type_params.len() == 2
                    && &type_params[0] == key.as_ref()
                    && &type_params[1] == value.as_ref()
            };
            if !matches {
                continue;
            }
            let rendered = if is_default_key_value {
                "iterable".to_string()
            } else {
                format!("iterable<{key}, {value}>")
            };
            return Some((ai.min(ti), ai.max(ti), rendered));
        }
    }
    None
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.types.is_empty() {
            return write!(f, "never");
        }
        if let Some((lo, hi, rendered)) = iterable_span(&self.types) {
            let mut first = true;
            for (i, t) in self.types.iter().enumerate() {
                if i == hi {
                    continue;
                }
                if !first {
                    f.write_str("|")?;
                }
                first = false;
                if i == lo {
                    f.write_str(&rendered)?;
                } else {
                    write!(f, "{t}")?;
                }
            }
            return Ok(());
        }
        write_joined(f, self.types.iter(), "|")
    }
}

impl fmt::Display for Atomic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Atomic::TString => write!(f, "string"),
            Atomic::TLiteralString(s) => write!(f, "\"{s}\""),
            Atomic::TCallableString => write!(f, "callable-string"),
            Atomic::TClassString(None) => write!(f, "class-string"),
            Atomic::TClassString(Some(cls)) => write!(f, "class-string<{cls}>"),
            Atomic::TNonEmptyString => write!(f, "non-empty-string"),
            Atomic::TNumericString => write!(f, "numeric-string"),

            Atomic::TInt => write!(f, "int"),
            Atomic::TLiteralInt(n) => write!(f, "{n}"),
            Atomic::TIntRange { min, max } => match (min, max) {
                (None, None) => write!(f, "int"),
                (lo, hi) => {
                    let lo = lo.map_or_else(|| "min".to_string(), |n| n.to_string());
                    let hi = hi.map_or_else(|| "max".to_string(), |n| n.to_string());
                    write!(f, "int<{lo}, {hi}>")
                }
            },
            Atomic::TPositiveInt => write!(f, "positive-int"),
            Atomic::TNegativeInt => write!(f, "negative-int"),
            Atomic::TNonNegativeInt => write!(f, "non-negative-int"),

            Atomic::TFloat | Atomic::TIntegralFloat => write!(f, "float"),
            Atomic::TLiteralFloat(high, low) => {
                let bits = ((*high as u64) << 32) | (*low as u32 as u64);
                let value = f64::from_bits(bits);
                write!(f, "{value}")
            }

            Atomic::TBool => write!(f, "bool"),
            Atomic::TTrue => write!(f, "true"),
            Atomic::TFalse => write!(f, "false"),

            Atomic::TNull => write!(f, "null"),
            Atomic::TVoid => write!(f, "void"),
            Atomic::TNever => write!(f, "never"),
            Atomic::TMixed => write!(f, "mixed"),
            Atomic::TScalar => write!(f, "scalar"),
            Atomic::TNumeric => write!(f, "numeric"),

            Atomic::TObject => write!(f, "object"),
            Atomic::TNamedObject { fqcn, type_params } => {
                // `Traversable<mixed, mixed>`/`Generator<mixed, mixed, mixed, mixed>`
                // carry no more information than the bare class name — every
                // param resolving to the unconstrained default is exactly the
                // case an omitted type-param list would represent.
                if type_params.is_empty() || type_params.iter().all(is_exactly_mixed) {
                    write!(f, "{fqcn}")
                } else {
                    write!(f, "{fqcn}<")?;
                    write_joined(f, type_params.iter(), ", ")?;
                    f.write_str(">")
                }
            }
            Atomic::TStaticObject { fqcn } => write!(f, "static({fqcn})"),
            Atomic::TSelf { fqcn } => write!(f, "self({fqcn})"),
            Atomic::TParent { fqcn } => write!(f, "parent({fqcn})"),

            Atomic::TCallable {
                params: None,
                return_type: None,
            } => write!(f, "callable"),
            Atomic::TCallable {
                params: Some(params),
                return_type,
            } => {
                f.write_str("callable(")?;
                write_param_types(f, params)?;
                match return_type {
                    Some(r) => write!(f, "): {r}"),
                    None => f.write_str("): mixed"),
                }
            }
            Atomic::TCallable {
                params: None,
                return_type: Some(ret),
            } => {
                write!(f, "callable(): {ret}")
            }
            Atomic::TClosure { data } => {
                f.write_str("Closure(")?;
                write_param_types(f, &data.params)?;
                write!(f, "): {}", data.return_type)
            }

            Atomic::TArray { key, value } => {
                // `array<mixed, mixed>` and `array<array-key, mixed>` are both
                // just `array` — `array-key` (int|string) is already the
                // maximal legal key domain, so it's as much a "default" key
                // as `mixed` is.
                if is_exactly_mixed(value) && (is_exactly_mixed(key) || key.is_array_key()) {
                    write!(f, "array")
                } else {
                    write!(f, "array<{key}, {value}>")
                }
            }
            Atomic::TList { value } => {
                if is_exactly_mixed(value) {
                    write!(f, "list")
                } else {
                    write!(f, "list<{value}>")
                }
            }
            Atomic::TNonEmptyArray { key, value } => {
                if is_exactly_mixed(value) && (is_exactly_mixed(key) || key.is_array_key()) {
                    write!(f, "non-empty-array")
                } else {
                    write!(f, "non-empty-array<{key}, {value}>")
                }
            }
            Atomic::TNonEmptyList { value } => {
                if is_exactly_mixed(value) {
                    write!(f, "non-empty-list")
                } else {
                    write!(f, "non-empty-list<{value}>")
                }
            }
            Atomic::TKeyedArray { properties, .. } => {
                f.write_str("array{")?;
                for (i, (k, v)) in properties.iter().enumerate() {
                    if i > 0 {
                        f.write_str(", ")?;
                    }
                    match k {
                        crate::atomic::ArrayKey::String(s) => write!(f, "'{s}'")?,
                        crate::atomic::ArrayKey::Int(n) => write!(f, "{n}")?,
                    }
                    if v.optional {
                        f.write_str("?")?;
                    }
                    write!(f, ": {}", v.ty)?;
                }
                f.write_str("}")
            }

            Atomic::TTemplateParam { name, .. } => write!(f, "{name}"),
            Atomic::TConditional { data } => {
                let (subject, if_true, if_false) = (&data.subject, &data.if_true, &data.if_false);
                match &data.param_name {
                    Some(name) => write!(f, "(${name} is {subject} ? {if_true} : {if_false})"),
                    None => write!(f, "({subject} is ? {if_true} : {if_false})"),
                }
            }

            Atomic::TInterfaceString(None) => write!(f, "interface-string"),
            Atomic::TInterfaceString(Some(iface)) => write!(f, "interface-string<{iface}>"),
            Atomic::TEnumString => write!(f, "enum-string"),
            Atomic::TTraitString => write!(f, "trait-string"),
            Atomic::TLiteralEnumCase {
                enum_fqcn,
                case_name,
            } => {
                write!(f, "{enum_fqcn}::{case_name}")
            }

            Atomic::TIntersection { parts } => {
                // Skip exact-duplicate parts (e.g. a redundant `Foo&Foo`) —
                // hierarchy-aware redundancy (like `Iterator&Traversable`)
                // needs class info this crate doesn't have, so only drop
                // parts that are structurally identical to an earlier one.
                let mut printed: Vec<&Type> = Vec::new();
                let mut first = true;
                for part in parts.iter() {
                    if printed.contains(&part) {
                        continue;
                    }
                    printed.push(part);
                    if !first {
                        f.write_str("&")?;
                    }
                    first = false;
                    write!(f, "{part}")?;
                }
                Ok(())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn int_range_unbounded_displays_as_int() {
        assert_eq!(
            format!(
                "{}",
                Atomic::TIntRange {
                    min: None,
                    max: None
                }
            ),
            "int"
        );
    }

    #[test]
    fn int_range_bounded_min_displays_range() {
        assert_eq!(
            format!(
                "{}",
                Atomic::TIntRange {
                    min: Some(0),
                    max: None
                }
            ),
            "int<0, max>"
        );
    }

    #[test]
    fn int_range_bounded_max_displays_range() {
        assert_eq!(
            format!(
                "{}",
                Atomic::TIntRange {
                    min: None,
                    max: Some(100)
                }
            ),
            "int<min, 100>"
        );
    }

    #[test]
    fn int_range_fully_bounded_displays_range() {
        assert_eq!(
            format!(
                "{}",
                Atomic::TIntRange {
                    min: Some(1),
                    max: Some(10)
                }
            ),
            "int<1, 10>"
        );
    }

    #[test]
    fn unbounded_int_range_in_union_displays_as_int() {
        let mut u = Type::empty();
        u.add_type(Atomic::TIntRange {
            min: None,
            max: None,
        });
        u.add_type(Atomic::TFalse);
        assert_eq!(format!("{u}"), "int|false");
    }

    #[test]
    fn array_of_mixed_mixed_collapses_to_array() {
        let atomic = Atomic::TArray {
            key: Box::new(Type::mixed()),
            value: Box::new(Type::mixed()),
        };
        assert_eq!(format!("{atomic}"), "array");
    }

    #[test]
    fn array_of_array_key_mixed_collapses_to_array() {
        let atomic = Atomic::TArray {
            key: Box::new(Type::array_key()),
            value: Box::new(Type::mixed()),
        };
        assert_eq!(format!("{atomic}"), "array");
    }

    #[test]
    fn array_of_int_mixed_does_not_collapse() {
        let atomic = Atomic::TArray {
            key: Box::new(Type::int()),
            value: Box::new(Type::mixed()),
        };
        assert_eq!(format!("{atomic}"), "array<int, mixed>");
    }

    #[test]
    fn array_of_mixed_string_does_not_collapse() {
        let atomic = Atomic::TArray {
            key: Box::new(Type::mixed()),
            value: Box::new(Type::string()),
        };
        assert_eq!(format!("{atomic}"), "array<mixed, string>");
    }

    #[test]
    fn non_empty_array_of_mixed_mixed_collapses() {
        let atomic = Atomic::TNonEmptyArray {
            key: Box::new(Type::mixed()),
            value: Box::new(Type::mixed()),
        };
        assert_eq!(format!("{atomic}"), "non-empty-array");
    }

    #[test]
    fn list_of_mixed_collapses_to_list() {
        let atomic = Atomic::TList {
            value: Box::new(Type::mixed()),
        };
        assert_eq!(format!("{atomic}"), "list");
    }

    #[test]
    fn list_of_string_does_not_collapse() {
        let atomic = Atomic::TList {
            value: Box::new(Type::string()),
        };
        assert_eq!(format!("{atomic}"), "list<string>");
    }

    #[test]
    fn non_empty_list_of_mixed_collapses() {
        let atomic = Atomic::TNonEmptyList {
            value: Box::new(Type::mixed()),
        };
        assert_eq!(format!("{atomic}"), "non-empty-list");
    }

    #[test]
    fn named_object_all_mixed_params_collapses_to_bare_name() {
        let atomic = Atomic::TNamedObject {
            fqcn: "Traversable".into(),
            type_params: crate::union::vec_to_type_params(vec![Type::mixed(), Type::mixed()]),
        };
        assert_eq!(format!("{atomic}"), "Traversable");
    }

    #[test]
    fn named_object_with_one_concrete_param_does_not_collapse() {
        let atomic = Atomic::TNamedObject {
            fqcn: "Traversable".into(),
            type_params: crate::union::vec_to_type_params(vec![Type::int(), Type::mixed()]),
        };
        assert_eq!(format!("{atomic}"), "Traversable<int, mixed>");
    }

    #[test]
    fn named_object_with_mixed_bounded_template_param_does_not_collapse() {
        // An unresolved `T` template parameter (even one bounded by `mixed`)
        // is a meaningful part of a generic signature and must never be
        // confused with a literal, information-free `mixed` default.
        let template_param = Atomic::TTemplateParam {
            name: "T".into(),
            as_type: Box::new(Type::mixed()),
            defining_entity: "MyClass".into(),
        };
        let atomic = Atomic::TNamedObject {
            fqcn: "MyClass".into(),
            type_params: crate::union::vec_to_type_params(vec![Type::single(template_param)]),
        };
        assert_eq!(format!("{atomic}"), "MyClass<T>");
    }

    fn bare_traversable() -> Atomic {
        Atomic::TNamedObject {
            fqcn: "Traversable".into(),
            type_params: crate::union::empty_type_params(),
        }
    }

    #[test]
    fn array_mixed_mixed_or_traversable_collapses_to_iterable() {
        let mut u = Type::single(Atomic::TArray {
            key: Box::new(Type::array_key()),
            value: Box::new(Type::mixed()),
        });
        u.add_type(bare_traversable());
        assert_eq!(format!("{u}"), "iterable");
    }

    #[test]
    fn parameterized_array_or_matching_traversable_collapses_to_iterable() {
        let key = Type::string();
        let value = Type::int();
        let mut u = Type::single(Atomic::TArray {
            key: Box::new(key.clone()),
            value: Box::new(value.clone()),
        });
        u.add_type(Atomic::TNamedObject {
            fqcn: "Traversable".into(),
            type_params: crate::union::vec_to_type_params(vec![key, value]),
        });
        assert_eq!(format!("{u}"), "iterable<string, int>");
    }

    #[test]
    fn iterable_collapse_preserves_other_union_members() {
        let mut u = Type::single(Atomic::TArray {
            key: Box::new(Type::array_key()),
            value: Box::new(Type::mixed()),
        });
        u.add_type(bare_traversable());
        u.add_type(Atomic::TNull);
        assert_eq!(format!("{u}"), "iterable|null");
    }

    #[test]
    fn array_with_mismatched_bare_traversable_does_not_collapse() {
        // A bare (unparameterized) `Traversable` carries no key/value
        // guarantee, so pairing it with a *more specific* array must not be
        // rendered as `iterable<int, string>` — that would overclaim that the
        // `Traversable` side is bound to the same key/value types too.
        let mut u = Type::single(Atomic::TArray {
            key: Box::new(Type::int()),
            value: Box::new(Type::string()),
        });
        u.add_type(bare_traversable());
        assert_eq!(format!("{u}"), "array<int, string>|Traversable");
    }

    #[test]
    fn array_with_non_matching_parameterized_traversable_does_not_collapse() {
        let mut u = Type::single(Atomic::TArray {
            key: Box::new(Type::int()),
            value: Box::new(Type::string()),
        });
        u.add_type(Atomic::TNamedObject {
            fqcn: "Traversable".into(),
            type_params: crate::union::vec_to_type_params(vec![Type::string(), Type::int()]),
        });
        assert_eq!(
            format!("{u}"),
            "array<int, string>|Traversable<string, int>"
        );
    }

    fn named_object(fqcn: &str) -> Atomic {
        Atomic::TNamedObject {
            fqcn: fqcn.into(),
            type_params: crate::union::empty_type_params(),
        }
    }

    #[test]
    fn intersection_drops_exact_duplicate_part() {
        let atomic = Atomic::TIntersection {
            parts: crate::union::vec_to_type_params(vec![
                Type::single(named_object("Foo")),
                Type::single(named_object("Foo")),
            ]),
        };
        assert_eq!(format!("{atomic}"), "Foo");
    }

    #[test]
    fn intersection_keeps_distinct_parts() {
        let atomic = Atomic::TIntersection {
            parts: crate::union::vec_to_type_params(vec![
                Type::single(named_object("Countable")),
                Type::single(named_object("Traversable")),
            ]),
        };
        assert_eq!(format!("{atomic}"), "Countable&Traversable");
    }

    #[test]
    fn intersection_drops_duplicate_among_three_parts() {
        let atomic = Atomic::TIntersection {
            parts: crate::union::vec_to_type_params(vec![
                Type::single(named_object("Foo")),
                Type::single(named_object("Bar")),
                Type::single(named_object("Foo")),
            ]),
        };
        assert_eq!(format!("{atomic}"), "Foo&Bar");
    }
}