Skip to main content

mir_types/
union.rs

1use rustc_hash::FxHashMap;
2use serde::{Deserialize, Serialize};
3use smallvec::SmallVec;
4use std::sync::{Arc, OnceLock};
5
6use crate::atomic::Atomic;
7use crate::symbol::Name;
8
9/// Returns a cached empty `Arc<[Type]>` for `type_params` / `parts` fields.
10/// Re-uses a single Arc allocation so all empty parameter lists share one
11/// control block instead of allocating one per TNamedObject construction.
12pub fn empty_type_params() -> Arc<[Type]> {
13    static EMPTY: OnceLock<Arc<[Type]>> = OnceLock::new();
14    EMPTY.get_or_init(|| Arc::from([] as [Type; 0])).clone()
15}
16
17/// Convert a `Vec<Type>` to `Arc<[Type]>`, using the cached empty Arc when
18/// the vec is empty to avoid an allocation for the common no-generic case.
19pub fn vec_to_type_params(v: Vec<Type>) -> Arc<[Type]> {
20    if v.is_empty() {
21        empty_type_params()
22    } else {
23        Arc::from(v)
24    }
25}
26
27// Most unions contain 1-2 atomics (e.g. `string|null`), so we inline two.
28pub type AtomicVec = SmallVec<[Atomic; 2]>;
29
30/// Result of classifying a type for `clone` validity (see [`Type::clone_validity`]).
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum CloneValidity {
33    /// Every member is (or may be) an object — cloning is fine.
34    Cloneable,
35    /// Every member is definitely a non-object — cloning is an error.
36    Invalid,
37    /// Some members are non-objects, some are objects — cloning may be an error.
38    PossiblyInvalid,
39    /// Empty/unknown type — no diagnostic.
40    Unknown,
41}
42
43// ---------------------------------------------------------------------------
44// Type — the primary type carrier
45// ---------------------------------------------------------------------------
46
47#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
48pub struct Type {
49    pub types: AtomicVec,
50    /// The variable holding this type may not be initialized at this point.
51    pub possibly_undefined: bool,
52    /// This type originated from a docblock annotation rather than inference.
53    pub from_docblock: bool,
54}
55
56impl Type {
57    // --- Constructors -------------------------------------------------------
58
59    pub fn empty() -> Self {
60        Self {
61            types: SmallVec::new(),
62            possibly_undefined: false,
63            from_docblock: false,
64        }
65    }
66
67    pub fn single(atomic: Atomic) -> Self {
68        let mut types = SmallVec::new();
69        types.push(atomic);
70        Self {
71            types,
72            possibly_undefined: false,
73            from_docblock: false,
74        }
75    }
76
77    pub fn mixed() -> Self {
78        Self::single(Atomic::TMixed)
79    }
80
81    pub fn void() -> Self {
82        Self::single(Atomic::TVoid)
83    }
84
85    pub fn never() -> Self {
86        Self::single(Atomic::TNever)
87    }
88
89    pub fn null() -> Self {
90        Self::single(Atomic::TNull)
91    }
92
93    pub fn bool() -> Self {
94        Self::single(Atomic::TBool)
95    }
96
97    pub fn int() -> Self {
98        Self::single(Atomic::TInt)
99    }
100
101    pub fn float() -> Self {
102        Self::single(Atomic::TFloat)
103    }
104
105    pub fn string() -> Self {
106        Self::single(Atomic::TString)
107    }
108
109    /// `T|null`
110    pub fn nullable(atomic: Atomic) -> Self {
111        // `mixed|null` = `mixed` — null is already included in mixed.
112        if matches!(atomic, Atomic::TMixed) {
113            return Self::mixed();
114        }
115        let mut types = SmallVec::new();
116        types.push(atomic);
117        types.push(Atomic::TNull);
118        Self {
119            types,
120            possibly_undefined: false,
121            from_docblock: false,
122        }
123    }
124
125    /// Build a union from multiple atomics, de-duplicating on the fly.
126    pub fn from_vec(atomics: Vec<Atomic>) -> Self {
127        let mut u = Self::empty();
128        for a in atomics {
129            u.add_type(a);
130        }
131        u
132    }
133
134    // --- Introspection -------------------------------------------------------
135
136    pub fn is_empty(&self) -> bool {
137        self.types.is_empty()
138    }
139
140    pub fn is_single(&self) -> bool {
141        self.types.len() == 1
142    }
143
144    pub fn is_nullable(&self) -> bool {
145        self.types.iter().any(|t| matches!(t, Atomic::TNull))
146    }
147
148    pub fn is_mixed(&self) -> bool {
149        self.types.iter().any(|t| match t {
150            Atomic::TMixed => true,
151            Atomic::TTemplateParam { as_type, .. } => as_type.is_mixed(),
152            _ => false,
153        })
154    }
155
156    pub fn is_never(&self) -> bool {
157        self.types.iter().all(|t| matches!(t, Atomic::TNever)) && !self.types.is_empty()
158    }
159
160    /// Classify this type for `clone` validity. Recurses into template-param
161    /// bounds (like [`Type::is_mixed`]). Callers handle `mixed` separately.
162    pub fn clone_validity(&self) -> CloneValidity {
163        if self.types.is_empty() {
164            return CloneValidity::Unknown;
165        }
166        let mut has_non_object = false;
167        let mut has_other = false; // object or ambiguous (callable, mixed, conditional, …)
168        for t in &self.types {
169            match t {
170                Atomic::TTemplateParam { as_type, .. } => match as_type.clone_validity() {
171                    CloneValidity::Invalid => has_non_object = true,
172                    CloneValidity::PossiblyInvalid => {
173                        has_non_object = true;
174                        has_other = true;
175                    }
176                    CloneValidity::Cloneable | CloneValidity::Unknown => has_other = true,
177                },
178                other if other.is_definitely_non_object() => has_non_object = true,
179                _ => has_other = true,
180            }
181        }
182        match (has_non_object, has_other) {
183            (true, false) => CloneValidity::Invalid,
184            (true, true) => CloneValidity::PossiblyInvalid,
185            _ => CloneValidity::Cloneable,
186        }
187    }
188
189    pub fn is_void(&self) -> bool {
190        self.is_single() && matches!(self.types[0], Atomic::TVoid)
191    }
192
193    pub fn can_be_falsy(&self) -> bool {
194        self.types.iter().any(|t| t.can_be_falsy())
195    }
196
197    pub fn can_be_truthy(&self) -> bool {
198        self.types.iter().any(|t| t.can_be_truthy())
199    }
200
201    pub fn contains<F: Fn(&Atomic) -> bool>(&self, f: F) -> bool {
202        self.types.iter().any(f)
203    }
204
205    pub fn has_named_object(&self, fqcn: &str) -> bool {
206        self.types.iter().any(|t| match t {
207            Atomic::TNamedObject { fqcn: f, .. } => f.as_ref() == fqcn,
208            _ => false,
209        })
210    }
211
212    // --- Mutation ------------------------------------------------------------
213
214    /// Add an atomic to this union, skipping duplicates.
215    /// Subsumption rules: anything ⊆ TMixed; TLiteralInt ⊆ TInt; etc.
216    pub fn add_type(&mut self, atomic: Atomic) {
217        // If we already have TMixed, nothing to add.
218        if self.types.iter().any(|t| matches!(t, Atomic::TMixed)) {
219            return;
220        }
221
222        // Adding TMixed subsumes everything.
223        if matches!(atomic, Atomic::TMixed) {
224            self.types.clear();
225            self.types.push(Atomic::TMixed);
226            return;
227        }
228
229        // Simplify trivial conditional types: (X is ? T : T) → T
230        // Recursively simplify branches first so nested trivial conditionals collapse.
231        let atomic = if let Atomic::TConditional {
232            param_name: _,
233            subject: _,
234            if_true,
235            if_false,
236        } = &atomic
237        {
238            let mut simplified_true = Type::empty();
239            for t in &if_true.types {
240                simplified_true.add_type(t.clone());
241            }
242            let mut simplified_false = Type::empty();
243            for t in &if_false.types {
244                simplified_false.add_type(t.clone());
245            }
246            if simplified_true == simplified_false {
247                for t in simplified_true.types {
248                    self.add_type(t);
249                }
250                return;
251            }
252            atomic
253        } else {
254            atomic
255        };
256
257        // Avoid exact duplicates.
258        if self.types.contains(&atomic) {
259            return;
260        }
261
262        // TLiteralInt(n) is subsumed by TInt.
263        if let Atomic::TLiteralInt(_) = &atomic {
264            if self.types.iter().any(|t| matches!(t, Atomic::TInt)) {
265                return;
266            }
267        }
268        // TLiteralString(s) is subsumed by TString.
269        if let Atomic::TLiteralString(_) = &atomic {
270            if self.types.iter().any(|t| matches!(t, Atomic::TString)) {
271                return;
272            }
273        }
274        // TTrue / TFalse are subsumed by TBool.
275        if matches!(atomic, Atomic::TTrue | Atomic::TFalse)
276            && self.types.iter().any(|t| matches!(t, Atomic::TBool))
277        {
278            return;
279        }
280        // Adding TInt widens away all TLiteralInt variants.
281        if matches!(atomic, Atomic::TInt) {
282            self.types.retain(|t| !matches!(t, Atomic::TLiteralInt(_)));
283        }
284        // Adding TString widens away all TLiteralString variants.
285        if matches!(atomic, Atomic::TString) {
286            self.types
287                .retain(|t| !matches!(t, Atomic::TLiteralString(_)));
288        }
289        // Adding TBool widens away TTrue/TFalse.
290        if matches!(atomic, Atomic::TBool) {
291            self.types
292                .retain(|t| !matches!(t, Atomic::TTrue | Atomic::TFalse));
293        }
294
295        // TNever is the bottom type: T | never = T.
296        if matches!(atomic, Atomic::TNever) {
297            if !self.types.is_empty() {
298                return;
299            }
300        } else {
301            self.types.retain(|t| !matches!(t, Atomic::TNever));
302        }
303
304        // Empty keyed array (array{}) is a subtype of any generic array or list.
305        // Remove array{} if we already have a generic array<K,V> or list<V>.
306        if let Atomic::TKeyedArray { properties, .. } = &atomic {
307            if properties.is_empty() {
308                for existing in &self.types {
309                    match existing {
310                        Atomic::TArray { .. }
311                        | Atomic::TNonEmptyArray { .. }
312                        | Atomic::TList { .. }
313                        | Atomic::TNonEmptyList { .. } => {
314                            return; // Don't add empty array, it's subsumed
315                        }
316                        _ => {}
317                    }
318                }
319            }
320        }
321
322        // When adding a generic array or list, remove any empty keyed arrays since they're subtypes.
323        let is_generic_array_or_list = matches!(
324            &atomic,
325            Atomic::TArray { .. }
326                | Atomic::TNonEmptyArray { .. }
327                | Atomic::TList { .. }
328                | Atomic::TNonEmptyList { .. }
329        );
330        if is_generic_array_or_list {
331            self.types.retain(|t| {
332                if let Atomic::TKeyedArray { properties, .. } = t {
333                    !properties.is_empty()
334                } else {
335                    true
336                }
337            });
338        }
339
340        self.types.push(atomic);
341    }
342
343    // --- Narrowing -----------------------------------------------------------
344
345    /// Remove `null` from the union (e.g. after a null check).
346    pub fn remove_null(&self) -> Type {
347        self.filter(|t| !matches!(t, Atomic::TNull))
348    }
349
350    /// Remove `false` from the union.
351    /// `TFalse` is dropped; `TBool` becomes `TTrue` since `bool - false = true`.
352    pub fn remove_false(&self) -> Type {
353        let mut result = self.filter(|t| !matches!(t, Atomic::TFalse | Atomic::TBool));
354        if self.types.iter().any(|t| matches!(t, Atomic::TBool)) {
355            result.add_type(Atomic::TTrue);
356        }
357        result
358    }
359
360    /// Remove both `null` and `false` from the union (core type without nullable/falsy variants).
361    pub fn core_type(&self) -> Type {
362        self.remove_null().remove_false()
363    }
364
365    /// Keep only truthy atomics (e.g. after `if ($x)`).
366    pub fn narrow_to_truthy(&self) -> Type {
367        if self.is_mixed() {
368            return Type::mixed();
369        }
370        let mut result = Type::empty();
371        result.from_docblock = self.from_docblock;
372        for t in &self.types {
373            match t {
374                // Always-falsy — exclude entirely.
375                Atomic::TLiteralInt(0)
376                | Atomic::TLiteralFloat(0, 0)
377                | Atomic::TNull
378                | Atomic::TFalse => {}
379                Atomic::TLiteralString(s) if s.as_ref() == "" || s.as_ref() == "0" => {}
380                // bool contains both true (truthy) and false (falsy); truthy branch is true.
381                Atomic::TBool => result.add_type(Atomic::TTrue),
382                // array/list: empty ↔ falsy; truthy branch is non-empty-array/list.
383                Atomic::TArray { key, value } => result.add_type(Atomic::TNonEmptyArray {
384                    key: key.clone(),
385                    value: value.clone(),
386                }),
387                Atomic::TList { value } => result.add_type(Atomic::TNonEmptyList {
388                    value: value.clone(),
389                }),
390                // string: only "" and "0" are falsy; truthy branch is non-empty-string.
391                // non-empty-string still includes "0" (which is falsy) but that is the
392                // standard approximation used by Psalm and other analyzers.
393                Atomic::TString => result.add_type(Atomic::TNonEmptyString),
394                // numeric-string: "0" is the only falsy value; non-zero numerics are truthy.
395                // No named "non-zero numeric-string" type exists; keep as-is conservatively.
396                // int<0, max> only has 0 as its falsy value; truthy branch is int<1, max>.
397                // (int<0, 0> is handled by the can_be_truthy() false guard below.)
398                Atomic::TNonNegativeInt => result.add_type(Atomic::TPositiveInt),
399                Atomic::TIntRange { min: Some(0), max } if max.is_none_or(|m| m >= 1) => {
400                    let atom = if max.is_none() {
401                        Atomic::TPositiveInt
402                    } else {
403                        Atomic::TIntRange {
404                            min: Some(1),
405                            max: *max,
406                        }
407                    };
408                    result.add_type(atom);
409                }
410                // int<min, 0>: 0 is the only falsy value; truthy branch excludes it → int<min, -1>.
411                Atomic::TIntRange { min, max: Some(0) } => {
412                    let atom = match min {
413                        None => Atomic::TNegativeInt,
414                        Some(n) if *n <= -1 => Atomic::TIntRange {
415                            min: *min,
416                            max: Some(-1),
417                        },
418                        _ => continue, // min >= 0 with max == 0 → range is {0} — can_be_truthy() handles this
419                    };
420                    result.add_type(atom);
421                }
422                // Anything else that can never be truthy — drop.
423                t if !t.can_be_truthy() => {}
424                _ => result.add_type(t.clone()),
425            }
426        }
427        result
428    }
429
430    /// Keep only falsy atomics (e.g. after `if (!$x)`).
431    pub fn narrow_to_falsy(&self) -> Type {
432        if self.is_mixed() {
433            return Type::from_vec(vec![
434                Atomic::TNull,
435                Atomic::TFalse,
436                Atomic::TLiteralInt(0),
437                Atomic::TLiteralString("".into()),
438            ]);
439        }
440        let mut result = Type::empty();
441        result.from_docblock = self.from_docblock;
442        for t in &self.types {
443            match t {
444                // bool: only false is falsy; falsy branch is false.
445                Atomic::TBool => result.add_type(Atomic::TFalse),
446                // int: only 0 is falsy.
447                Atomic::TInt => result.add_type(Atomic::TLiteralInt(0)),
448                // float: only 0.0 is falsy.
449                Atomic::TFloat => result.add_type(Atomic::TLiteralFloat(0, 0)),
450                // string: only "" and "0" are falsy.
451                Atomic::TString => {
452                    result.add_type(Atomic::TLiteralString("".into()));
453                    result.add_type(Atomic::TLiteralString("0".into()));
454                }
455                // numeric-string: only "0" is a falsy numeric string.
456                Atomic::TNumericString => result.add_type(Atomic::TLiteralString("0".into())),
457                // non-negative-int: only 0 is falsy.
458                Atomic::TNonNegativeInt => result.add_type(Atomic::TLiteralInt(0)),
459                // int<0, hi>: only 0 is falsy.
460                Atomic::TIntRange {
461                    min: Some(0),
462                    max: Some(_) | None,
463                } => result.add_type(Atomic::TLiteralInt(0)),
464                // int<min, 0>: only 0 is falsy.
465                Atomic::TIntRange { max: Some(0), .. } => result.add_type(Atomic::TLiteralInt(0)),
466                t if !t.can_be_falsy() => {} // always truthy — exclude
467                _ => result.add_type(t.clone()),
468            }
469        }
470        result
471    }
472
473    /// Narrow this type as if `$x instanceof ClassName` is true.
474    ///
475    /// The instanceof check guarantees the value IS an instance of `class`, so we
476    /// replace any object / mixed constituents with the specific named object.  Scalar
477    /// constituents are dropped (they can never satisfy instanceof).
478    pub fn narrow_instanceof(&self, class: &str) -> Type {
479        let narrowed_ty = Atomic::TNamedObject {
480            fqcn: class.into(),
481            type_params: empty_type_params(),
482        };
483        // If any constituent is an object-like type, the result is the specific class.
484        let has_object = self.types.iter().any(|t| {
485            matches!(
486                t,
487                Atomic::TObject | Atomic::TNamedObject { .. } | Atomic::TMixed | Atomic::TNull // null fails instanceof, but mixed/object may include null
488            )
489        });
490        if has_object || self.is_empty() {
491            Type::single(narrowed_ty)
492        } else {
493            // Pure scalars — instanceof is always false here, but return the class
494            // defensively so callers don't see an empty union.
495            Type::single(narrowed_ty)
496        }
497    }
498
499    /// Narrow as if `is_string($x)` is true.
500    pub fn narrow_to_string(&self) -> Type {
501        self.filter(|t| t.is_string() || matches!(t, Atomic::TMixed | Atomic::TScalar))
502    }
503
504    /// Narrow as if `is_int($x)` is true.
505    pub fn narrow_to_int(&self) -> Type {
506        self.filter(|t| {
507            t.is_int() || matches!(t, Atomic::TMixed | Atomic::TScalar | Atomic::TNumeric)
508        })
509    }
510
511    /// Narrow as if `is_float($x)` is true.
512    pub fn narrow_to_float(&self) -> Type {
513        self.filter(|t| {
514            matches!(
515                t,
516                Atomic::TFloat
517                    | Atomic::TLiteralFloat(..)
518                    | Atomic::TMixed
519                    | Atomic::TScalar
520                    | Atomic::TNumeric
521            )
522        })
523    }
524
525    /// Narrow as if `is_bool($x)` is true.
526    pub fn narrow_to_bool(&self) -> Type {
527        self.filter(|t| {
528            matches!(
529                t,
530                Atomic::TBool | Atomic::TTrue | Atomic::TFalse | Atomic::TMixed | Atomic::TScalar
531            )
532        })
533    }
534
535    /// Narrow as if `is_null($x)` is true.
536    pub fn narrow_to_null(&self) -> Type {
537        self.filter(|t| matches!(t, Atomic::TNull | Atomic::TMixed))
538    }
539
540    /// Narrow as if `is_array($x)` is true.
541    pub fn narrow_to_array(&self) -> Type {
542        self.filter(|t| t.is_array() || matches!(t, Atomic::TMixed))
543    }
544
545    /// Narrow array/list types to their non-empty variants (for `count() > 0` etc.).
546    pub fn narrow_to_non_empty_collection(&self) -> Type {
547        let mut out = Type::empty();
548        out.from_docblock = self.from_docblock;
549        for t in &self.types {
550            match t {
551                Atomic::TArray { key, value } => out.add_type(Atomic::TNonEmptyArray {
552                    key: key.clone(),
553                    value: value.clone(),
554                }),
555                Atomic::TList { value } => out.add_type(Atomic::TNonEmptyList {
556                    value: value.clone(),
557                }),
558                _ => out.add_type(t.clone()),
559            }
560        }
561        out
562    }
563
564    /// Narrow as if `array_is_list($x)` is true.
565    /// Lists have sequential integer keys starting from 0, so:
566    /// - `list<T>` / `non-empty-list<T>` are kept unchanged.
567    /// - `array<int, T>` is narrowed to `list<T>` (could be sequential).
568    /// - `non-empty-array<int, T>` is narrowed to `non-empty-list<T>`.
569    /// - `mixed` becomes `list<mixed>` (array_is_list implies array).
570    /// - All other types (string-keyed arrays, non-arrays) are dropped.
571    pub fn narrow_to_list(&self) -> Type {
572        let mut out = Type::empty();
573        out.from_docblock = self.from_docblock;
574        for t in &self.types {
575            match t {
576                Atomic::TList { .. } | Atomic::TNonEmptyList { .. } => out.add_type(t.clone()),
577                Atomic::TArray { key, value } if matches!(key.types.as_slice(), [Atomic::TInt]) => {
578                    out.add_type(Atomic::TList {
579                        value: value.clone(),
580                    });
581                }
582                Atomic::TNonEmptyArray { key, value }
583                    if matches!(key.types.as_slice(), [Atomic::TInt]) =>
584                {
585                    out.add_type(Atomic::TNonEmptyList {
586                        value: value.clone(),
587                    });
588                }
589                Atomic::TMixed => out.add_type(Atomic::TList {
590                    value: Box::new(Type::mixed()),
591                }),
592                _ => {}
593            }
594        }
595        if out.is_empty() {
596            self.filter(|t| matches!(t, Atomic::TList { .. } | Atomic::TNonEmptyList { .. }))
597        } else {
598            out
599        }
600    }
601
602    /// Narrow as if `is_object($x)` is true. A `mixed` becomes a concrete bare
603    /// `object` (rather than staying `mixed`) so downstream object-only
604    /// operations — `clone`, `instanceof`, method calls — see an object type
605    /// instead of reporting `Mixed*`.
606    pub fn narrow_to_object(&self) -> Type {
607        let mut out = Type::empty();
608        for t in &self.types {
609            if matches!(t, Atomic::TMixed) {
610                out.add_type(Atomic::TObject);
611            } else if t.is_object() {
612                out.add_type(t.clone());
613            }
614        }
615        if out.types.is_empty() {
616            self.filter(|t| t.is_object())
617        } else {
618            out
619        }
620    }
621
622    /// Narrow as if `is_callable($x)` is true.
623    pub fn narrow_to_callable(&self) -> Type {
624        self.filter(|t| t.is_callable() || matches!(t, Atomic::TMixed))
625    }
626
627    /// Narrow as if `is_scalar($x)` is true (int | string | float | bool).
628    pub fn narrow_to_scalar(&self) -> Type {
629        self.filter(|t| {
630            t.is_string()
631                || t.is_int()
632                || matches!(
633                    t,
634                    Atomic::TFloat
635                        | Atomic::TLiteralFloat(..)
636                        | Atomic::TBool
637                        | Atomic::TTrue
638                        | Atomic::TFalse
639                        | Atomic::TScalar
640                        | Atomic::TNumeric
641                        | Atomic::TNumericString
642                        | Atomic::TMixed
643                )
644        })
645    }
646
647    /// Narrow as if `is_iterable($x)` is true (array | Traversable).
648    /// For simplicity, this narrows to arrays or objects (can't easily verify interfaces).
649    pub fn narrow_to_iterable(&self) -> Type {
650        self.filter(|t| t.is_array() || t.is_object() || matches!(t, Atomic::TMixed))
651    }
652
653    /// Narrow as if `is_countable($x)` is true (array | Countable).
654    /// For simplicity, this narrows to arrays or objects (can't easily verify Countable interface).
655    pub fn narrow_to_countable(&self) -> Type {
656        self.filter(|t| t.is_array() || t.is_object() || matches!(t, Atomic::TMixed))
657    }
658
659    /// Narrow as if `is_resource($x)` is true.
660    /// Note: No TResource atomic type exists in the type system; this is a no-op.
661    /// Resources are declining in modern PHP and not actively tracked.
662    pub fn narrow_to_resource(&self) -> Type {
663        // No resource type in the system; just return mixed (allows any type)
664        self.filter(|t| matches!(t, Atomic::TMixed))
665    }
666
667    // --- Merge (branch join) ------------------------------------------------
668
669    /// Merge two unions at a branch join point (e.g. after if/else).
670    /// The result is the union of all types in both.
671    pub fn merge(a: &Type, b: &Type) -> Type {
672        // Fast path: b is empty — nothing to add.
673        if b.types.is_empty() {
674            let mut result = a.clone();
675            result.possibly_undefined = a.possibly_undefined || b.possibly_undefined;
676            return result;
677        }
678        // Fast path: a is empty — clone b.
679        if a.types.is_empty() {
680            let mut result = b.clone();
681            result.possibly_undefined = a.possibly_undefined || b.possibly_undefined;
682            return result;
683        }
684        // Fast path: a is already mixed — b cannot widen it further.
685        if a.types.len() == 1 && matches!(a.types[0], Atomic::TMixed) {
686            let mut result = a.clone();
687            result.possibly_undefined = a.possibly_undefined || b.possibly_undefined;
688            return result;
689        }
690        // Fast path: b contains mixed — result collapses to mixed.
691        if b.types.iter().any(|t| matches!(t, Atomic::TMixed)) {
692            return Type {
693                types: smallvec::smallvec![Atomic::TMixed],
694                possibly_undefined: a.possibly_undefined || b.possibly_undefined,
695                from_docblock: a.from_docblock || b.from_docblock,
696            };
697        }
698        let mut result = a.clone();
699        result.merge_with(b);
700        result
701    }
702
703    /// Merge `other` into `self` in-place (avoids cloning `self`).
704    pub fn merge_with(&mut self, other: &Type) {
705        if self.types.iter().any(|t| matches!(t, Atomic::TMixed)) {
706            self.possibly_undefined |= other.possibly_undefined;
707            return;
708        }
709        if other.types.iter().any(|t| matches!(t, Atomic::TMixed)) {
710            self.types.clear();
711            self.types.push(Atomic::TMixed);
712            self.possibly_undefined |= other.possibly_undefined;
713            return;
714        }
715        for atomic in &other.types {
716            self.add_type(atomic.clone());
717        }
718        self.possibly_undefined |= other.possibly_undefined;
719    }
720
721    /// Intersect with another union: keep only types present in `other`, widening
722    /// where `self` contains `mixed` (which is compatible with everything).
723    /// Used for match-arm subject narrowing.
724    pub fn intersect_with(&self, other: &Type) -> Type {
725        if self.is_mixed() {
726            return other.clone();
727        }
728        if other.is_mixed() {
729            return self.clone();
730        }
731        // Keep atomics from self that are also in other (by equality or subtype)
732        let mut result = Type::empty();
733        for a in &self.types {
734            for b in &other.types {
735                if a == b || atomic_subtype(a, b) || atomic_subtype(b, a) {
736                    result.add_type(a.clone());
737                    break;
738                }
739            }
740        }
741        if result.is_empty() {
742            Type::never()
743        } else {
744            result
745        }
746    }
747
748    // --- Template substitution ----------------------------------------------
749
750    /// Replace template param references with their resolved types.
751    pub fn substitute_templates(&self, bindings: &FxHashMap<Name, Type>) -> Type {
752        if bindings.is_empty() {
753            return self.clone();
754        }
755        let mut result = Type::empty();
756        result.possibly_undefined = self.possibly_undefined;
757        result.from_docblock = self.from_docblock;
758        for atomic in &self.types {
759            match atomic {
760                Atomic::TTemplateParam { name, .. } => {
761                    if let Some(resolved) = bindings.get(name) {
762                        for t in &resolved.types {
763                            result.add_type(t.clone());
764                        }
765                    } else {
766                        result.add_type(atomic.clone());
767                    }
768                }
769                Atomic::TArray { key, value } => {
770                    result.add_type(Atomic::TArray {
771                        key: Box::new(key.substitute_templates(bindings)),
772                        value: Box::new(value.substitute_templates(bindings)),
773                    });
774                }
775                Atomic::TList { value } => {
776                    result.add_type(Atomic::TList {
777                        value: Box::new(value.substitute_templates(bindings)),
778                    });
779                }
780                Atomic::TNonEmptyArray { key, value } => {
781                    result.add_type(Atomic::TNonEmptyArray {
782                        key: Box::new(key.substitute_templates(bindings)),
783                        value: Box::new(value.substitute_templates(bindings)),
784                    });
785                }
786                Atomic::TNonEmptyList { value } => {
787                    result.add_type(Atomic::TNonEmptyList {
788                        value: Box::new(value.substitute_templates(bindings)),
789                    });
790                }
791                Atomic::TKeyedArray {
792                    properties,
793                    is_open,
794                    is_list,
795                } => {
796                    use crate::atomic::KeyedProperty;
797                    let new_props = properties
798                        .iter()
799                        .map(|(k, prop)| {
800                            (
801                                k.clone(),
802                                KeyedProperty {
803                                    ty: prop.ty.substitute_templates(bindings),
804                                    optional: prop.optional,
805                                },
806                            )
807                        })
808                        .collect();
809                    result.add_type(Atomic::TKeyedArray {
810                        properties: new_props,
811                        is_open: *is_open,
812                        is_list: *is_list,
813                    });
814                }
815                Atomic::TCallable {
816                    params,
817                    return_type,
818                } => {
819                    result.add_type(Atomic::TCallable {
820                        params: params.as_ref().map(|ps| {
821                            ps.iter()
822                                .map(|p| substitute_in_fn_param(p, bindings))
823                                .collect()
824                        }),
825                        return_type: return_type
826                            .as_ref()
827                            .map(|r| Box::new(r.substitute_templates(bindings))),
828                    });
829                }
830                Atomic::TClosure {
831                    params,
832                    return_type,
833                    this_type,
834                } => {
835                    result.add_type(Atomic::TClosure {
836                        params: params
837                            .iter()
838                            .map(|p| substitute_in_fn_param(p, bindings))
839                            .collect(),
840                        return_type: Box::new(return_type.substitute_templates(bindings)),
841                        this_type: this_type
842                            .as_ref()
843                            .map(|t| Box::new(t.substitute_templates(bindings))),
844                    });
845                }
846                Atomic::TConditional {
847                    param_name,
848                    subject,
849                    if_true,
850                    if_false,
851                } => {
852                    let new_subject = subject.substitute_templates(bindings);
853                    let new_if_true = if_true.substitute_templates(bindings);
854                    let new_if_false = if_false.substitute_templates(bindings);
855
856                    // If param_name names a template that is bound in this substitution,
857                    // resolve the conditional immediately using the same predicate logic as
858                    // `resolve_conditional_returns` for the $param form.
859                    let resolved = if let Some(name) = param_name {
860                        if let Some(bound) = bindings.get(name) {
861                            if new_subject.types.len() == 1 {
862                                resolve_conditional_branch(
863                                    &new_subject.types[0],
864                                    bound,
865                                    &new_if_true,
866                                    &new_if_false,
867                                )
868                            } else {
869                                None
870                            }
871                        } else {
872                            None
873                        }
874                    } else {
875                        None
876                    };
877
878                    if let Some(branch) = resolved {
879                        for t in branch.types {
880                            result.add_type(t);
881                        }
882                    } else {
883                        result.add_type(Atomic::TConditional {
884                            param_name: *param_name,
885                            subject: Box::new(new_subject),
886                            if_true: Box::new(new_if_true),
887                            if_false: Box::new(new_if_false),
888                        });
889                    }
890                }
891                Atomic::TIntersection { parts } => {
892                    result.add_type(Atomic::TIntersection {
893                        parts: vec_to_type_params(
894                            parts
895                                .iter()
896                                .map(|p| p.substitute_templates(bindings))
897                                .collect(),
898                        ),
899                    });
900                }
901                Atomic::TNamedObject { fqcn, type_params } => {
902                    // TODO: the docblock parser emits TNamedObject { fqcn: "T" } for bare @return T
903                    // annotations instead of TTemplateParam, because it lacks template context at
904                    // parse time. This block works around that by treating bare unqualified names
905                    // as template param references when they appear in the binding map. Proper fix:
906                    // make the docblock parser template-aware so it emits TTemplateParam directly.
907                    // See issue #26 for context.
908                    if type_params.is_empty() && !fqcn.contains('\\') {
909                        if let Some(resolved) = bindings.get(fqcn) {
910                            for t in &resolved.types {
911                                result.add_type(t.clone());
912                            }
913                            continue;
914                        }
915                    }
916                    let new_params: Vec<Type> = type_params
917                        .iter()
918                        .map(|p| p.substitute_templates(bindings))
919                        .collect();
920                    result.add_type(Atomic::TNamedObject {
921                        fqcn: *fqcn,
922                        type_params: vec_to_type_params(new_params),
923                    });
924                }
925                // class-string<T> → substitute T from bindings
926                Atomic::TClassString(Some(param_name)) => {
927                    if let Some(resolved) = bindings.get(param_name) {
928                        for r_atomic in &resolved.types {
929                            let cls_name = if let Atomic::TNamedObject { fqcn, .. } = r_atomic {
930                                Some(*fqcn)
931                            } else {
932                                None
933                            };
934                            result.add_type(Atomic::TClassString(cls_name));
935                        }
936                    } else {
937                        result.add_type(atomic.clone());
938                    }
939                }
940                _ => {
941                    result.add_type(atomic.clone());
942                }
943            }
944        }
945        result
946    }
947
948    /// Resolves `TConditional` atoms whose discriminator is known at the call site.
949    ///
950    /// `lookup(param_name)` returns the call-site argument type for the named parameter,
951    /// or `None` if the argument is not available. Handles `is null`, `is string`, and
952    /// `is array` conditions; other condition types pass through unchanged.
953    pub fn resolve_conditional_returns<F>(self, lookup: F) -> Type
954    where
955        F: Fn(&str) -> Option<Type>,
956    {
957        self.resolve_conditional_inner(&lookup)
958    }
959
960    fn resolve_conditional_inner<F>(self, lookup: &F) -> Type
961    where
962        F: Fn(&str) -> Option<Type>,
963    {
964        let mut result = Type::empty();
965        for atomic in self.types {
966            match atomic {
967                Atomic::TConditional {
968                    ref param_name,
969                    ref subject,
970                    ref if_true,
971                    ref if_false,
972                } => {
973                    let resolved = if subject.types.len() == 1 {
974                        if let Some(name) = param_name {
975                            if let Some(arg_ty) = lookup(name.as_ref()) {
976                                resolve_conditional_branch(
977                                    &subject.types[0],
978                                    &arg_ty,
979                                    if_true,
980                                    if_false,
981                                )
982                            } else {
983                                None
984                            }
985                        } else {
986                            None
987                        }
988                    } else {
989                        None
990                    };
991
992                    if let Some(branch) = resolved {
993                        // Recursively resolve nested conditionals in the selected branch.
994                        for t in branch.resolve_conditional_inner(lookup).types {
995                            result.add_type(t);
996                        }
997                    } else {
998                        // Cannot resolve at this call site: widen to the union of both branches.
999                        // Recursively resolve nested conditionals in each branch.
1000                        for t in if_true.clone().resolve_conditional_inner(lookup).types {
1001                            result.add_type(t);
1002                        }
1003                        for t in if_false.clone().resolve_conditional_inner(lookup).types {
1004                            result.add_type(t);
1005                        }
1006                    }
1007                }
1008                other => result.add_type(other),
1009            }
1010        }
1011        result
1012    }
1013
1014    // --- Subtype check -------------------------------------------------------
1015
1016    /// Returns true if every atomic in `self` is a subtype of some atomic in `other`,
1017    /// using **only structural rules** — no `extends` / `implements` walk.
1018    ///
1019    /// Two distinct user-defined classes are never related here, even when one
1020    /// extends the other. Within `mir-analyzer`, when a `db` is in scope,
1021    /// prefer `crate::subtype::is_subtype(db, sub, sup)` which layers
1022    /// inheritance resolution on top of this check.
1023    pub fn is_subtype_structural(&self, other: &Type) -> bool {
1024        if other.is_mixed() {
1025            return true;
1026        }
1027        if self.is_never() {
1028            return true; // never <: everything
1029        }
1030        self.types
1031            .iter()
1032            .all(|a| other.types.iter().any(|b| atomic_subtype(a, b)))
1033    }
1034
1035    // --- Utilities ----------------------------------------------------------
1036
1037    fn filter<F: Fn(&Atomic) -> bool>(&self, f: F) -> Type {
1038        let mut result = Type::empty();
1039        result.possibly_undefined = self.possibly_undefined;
1040        result.from_docblock = self.from_docblock;
1041        for atomic in &self.types {
1042            if f(atomic) {
1043                result.types.push(atomic.clone());
1044            }
1045        }
1046        result
1047    }
1048
1049    /// Mark this union as possibly-undefined and return it.
1050    pub fn possibly_undefined(mut self) -> Self {
1051        self.possibly_undefined = true;
1052        self
1053    }
1054
1055    /// Mark this union as coming from a docblock annotation.
1056    pub fn from_docblock(mut self) -> Self {
1057        self.from_docblock = true;
1058        self
1059    }
1060}
1061
1062// ---------------------------------------------------------------------------
1063// Conditional return resolution helpers
1064// ---------------------------------------------------------------------------
1065
1066fn is_string_atomic(a: &Atomic) -> bool {
1067    matches!(
1068        a,
1069        Atomic::TString
1070            | Atomic::TNonEmptyString
1071            | Atomic::TLiteralString(_)
1072            | Atomic::TNumericString
1073            | Atomic::TClassString(_)
1074            | Atomic::TCallableString
1075    )
1076}
1077
1078fn is_array_atomic(a: &Atomic) -> bool {
1079    matches!(
1080        a,
1081        Atomic::TArray { .. }
1082            | Atomic::TNonEmptyArray { .. }
1083            | Atomic::TKeyedArray { .. }
1084            | Atomic::TList { .. }
1085            | Atomic::TNonEmptyList { .. }
1086    )
1087}
1088
1089fn is_list_atomic(a: &Atomic) -> bool {
1090    match a {
1091        Atomic::TList { .. } | Atomic::TNonEmptyList { .. } => true,
1092        Atomic::TKeyedArray { is_list, .. } => *is_list,
1093        _ => false,
1094    }
1095}
1096
1097/// Resolve one branch of a conditional return type given the subject discriminant
1098/// atomic and the actual argument type at the call site.
1099///
1100/// Returns `Some(branch)` when the branch can be determined statically, or `None`
1101/// to signal that the caller should widen to the union of both branches.
1102fn resolve_conditional_branch(
1103    subject: &Atomic,
1104    arg_ty: &Type,
1105    if_true: &Type,
1106    if_false: &Type,
1107) -> Option<Type> {
1108    let predicate: fn(&Atomic) -> bool = match subject {
1109        Atomic::TNull => |a| matches!(a, Atomic::TNull),
1110        Atomic::TTrue => |a| matches!(a, Atomic::TTrue),
1111        Atomic::TFalse => |a| matches!(a, Atomic::TFalse),
1112        Atomic::TString => is_string_atomic,
1113        Atomic::TList { .. } => is_list_atomic,
1114        Atomic::TArray { .. } => is_array_atomic,
1115        _ => return None,
1116    };
1117
1118    if arg_ty.types.is_empty() {
1119        return None;
1120    }
1121    let all_match = arg_ty.types.iter().all(&predicate);
1122    let none_match = !arg_ty.types.iter().any(predicate);
1123    if all_match {
1124        Some(if_true.clone())
1125    } else if none_match {
1126        Some(if_false.clone())
1127    } else {
1128        None
1129    }
1130}
1131
1132// ---------------------------------------------------------------------------
1133// Template substitution helpers
1134// ---------------------------------------------------------------------------
1135
1136fn substitute_in_fn_param(
1137    p: &crate::atomic::FnParam,
1138    bindings: &FxHashMap<Name, Type>,
1139) -> crate::atomic::FnParam {
1140    crate::atomic::FnParam {
1141        name: p.name,
1142        ty: p.ty.as_ref().map(|t| {
1143            let u = t.to_union();
1144            let substituted = u.substitute_templates(bindings);
1145            crate::compact::SimpleType::from_union(substituted)
1146        }),
1147        default: p.default.as_ref().map(|d| {
1148            let u = d.to_union();
1149            let substituted = u.substitute_templates(bindings);
1150            crate::compact::SimpleType::from_union(substituted)
1151        }),
1152        is_variadic: p.is_variadic,
1153        is_byref: p.is_byref,
1154        is_optional: p.is_optional,
1155    }
1156}
1157
1158// ---------------------------------------------------------------------------
1159// Atomic subtype (no codebase — structural check only)
1160// ---------------------------------------------------------------------------
1161
1162fn atomic_subtype(sub: &Atomic, sup: &Atomic) -> bool {
1163    if sub == sup {
1164        return true;
1165    }
1166    match (sub, sup) {
1167        // Bottom type
1168        (Atomic::TNever, _) => true,
1169        // Top types — anything goes in both directions for mixed
1170        (_, Atomic::TMixed) => true,
1171        (Atomic::TMixed, _) => true,
1172        // Template param in supertype position: any value satisfies an unconstrained
1173        // template (as_type = mixed), or a constrained one if it satisfies the bound.
1174        // This handles union bounds like `T of string|list<I>|array<K, V>` where
1175        // I/K/V are free template params — any type satisfies them structurally.
1176        (_, Atomic::TTemplateParam { as_type, .. }) => {
1177            as_type.is_mixed() || as_type.types.iter().any(|b| atomic_subtype(sub, b))
1178        }
1179
1180        // Scalars
1181        (Atomic::TLiteralInt(_), Atomic::TInt) => true,
1182        (Atomic::TLiteralInt(_), Atomic::TNumeric) => true,
1183        (Atomic::TLiteralInt(_), Atomic::TScalar) => true,
1184        (Atomic::TLiteralInt(n), Atomic::TPositiveInt) => *n > 0,
1185        (Atomic::TLiteralInt(n), Atomic::TNonNegativeInt) => *n >= 0,
1186        (Atomic::TLiteralInt(n), Atomic::TNegativeInt) => *n < 0,
1187        (Atomic::TPositiveInt, Atomic::TInt) => true,
1188        (Atomic::TPositiveInt, Atomic::TNonNegativeInt) => true,
1189        (Atomic::TPositiveInt, Atomic::TNumeric) => true,
1190        (Atomic::TPositiveInt, Atomic::TScalar) => true,
1191        (Atomic::TNegativeInt, Atomic::TInt) => true,
1192        (Atomic::TNegativeInt, Atomic::TNumeric) => true,
1193        (Atomic::TNegativeInt, Atomic::TScalar) => true,
1194        (Atomic::TNonNegativeInt, Atomic::TInt) => true,
1195        (Atomic::TNonNegativeInt, Atomic::TNumeric) => true,
1196        (Atomic::TNonNegativeInt, Atomic::TScalar) => true,
1197        (Atomic::TIntRange { .. }, Atomic::TInt) => true,
1198        (Atomic::TIntRange { .. }, Atomic::TNumeric) => true,
1199        (Atomic::TIntRange { .. }, Atomic::TScalar) => true,
1200        // positive-int is int<1, ∞>: subtype of int<sup_min, ∞> when sup_min <= 1
1201        (Atomic::TPositiveInt, Atomic::TIntRange { min, max }) => {
1202            max.is_none() && min.is_none_or(|m| m <= 1)
1203        }
1204        // negative-int is int<-∞, -1>: subtype of int<-∞, sup_max> when sup_max >= -1
1205        (Atomic::TNegativeInt, Atomic::TIntRange { min, max }) => {
1206            min.is_none() && max.is_none_or(|m| m >= -1)
1207        }
1208        // non-negative-int is int<0, ∞>: subtype of int<sup_min, ∞> when sup_min <= 0
1209        (Atomic::TNonNegativeInt, Atomic::TIntRange { min, max }) => {
1210            max.is_none() && min.is_none_or(|m| m <= 0)
1211        }
1212        // A bounded int range is a subtype of a named int subtype when every value fits
1213        (Atomic::TIntRange { min: sub_min, .. }, Atomic::TPositiveInt) => {
1214            sub_min.is_some_and(|lo| lo >= 1)
1215        }
1216        (Atomic::TIntRange { min: sub_min, .. }, Atomic::TNonNegativeInt) => {
1217            sub_min.is_some_and(|lo| lo >= 0)
1218        }
1219        (Atomic::TIntRange { max: sub_max, .. }, Atomic::TNegativeInt) => {
1220            sub_max.is_some_and(|hi| hi <= -1)
1221        }
1222        // int<sub_min, sub_max> <: int<sup_min, sup_max> when ranges nest
1223        (
1224            Atomic::TIntRange {
1225                min: sub_min,
1226                max: sub_max,
1227            },
1228            Atomic::TIntRange {
1229                min: sup_min,
1230                max: sup_max,
1231            },
1232        ) => {
1233            let lower_ok = match (sub_min, sup_min) {
1234                (_, None) => true,
1235                (None, Some(_)) => false,
1236                (Some(sl), Some(su)) => sl >= su,
1237            };
1238            let upper_ok = match (sub_max, sup_max) {
1239                (None, None) | (Some(_), None) => true,
1240                (None, Some(_)) => false,
1241                (Some(sl), Some(su)) => sl <= su,
1242            };
1243            lower_ok && upper_ok
1244        }
1245
1246        (Atomic::TLiteralFloat(..), Atomic::TFloat) => true,
1247        (Atomic::TLiteralFloat(..), Atomic::TNumeric) => true,
1248        (Atomic::TLiteralFloat(..), Atomic::TScalar) => true,
1249
1250        (Atomic::TLiteralString(s), Atomic::TString) => {
1251            let _ = s;
1252            true
1253        }
1254        (Atomic::TLiteralString(s), Atomic::TCallableString) => {
1255            let _ = s;
1256            true
1257        }
1258        (Atomic::TLiteralString(s), Atomic::TNonEmptyString) => !s.is_empty(),
1259        (Atomic::TLiteralString(s), Atomic::TNumericString) => s.parse::<f64>().is_ok(),
1260        // A literal string is type-compatible with class-string; validate_class_string_argument
1261        // separately checks whether the string names a real class (UndefinedClass).
1262        (Atomic::TLiteralString(_), Atomic::TClassString(_)) => true,
1263        (Atomic::TLiteralString(_), Atomic::TScalar) => true,
1264        (Atomic::TNonEmptyString, Atomic::TString) => true,
1265        (Atomic::TCallableString, Atomic::TString) => true,
1266        // numeric-string is always non-empty (e.g. "42", "-1", "0.5") — "" is not numeric.
1267        (Atomic::TNumericString, Atomic::TNonEmptyString) => true,
1268        (Atomic::TNumericString, Atomic::TString) => true,
1269        (Atomic::TClassString(_), Atomic::TString) => true,
1270        (Atomic::TInterfaceString, Atomic::TString) => true,
1271        (Atomic::TEnumString, Atomic::TString) => true,
1272        (Atomic::TTraitString, Atomic::TString) => true,
1273
1274        (Atomic::TTrue, Atomic::TBool) => true,
1275        (Atomic::TFalse, Atomic::TBool) => true,
1276
1277        (Atomic::TInt, Atomic::TNumeric) => true,
1278        (Atomic::TFloat, Atomic::TNumeric) => true,
1279        (Atomic::TNumericString, Atomic::TNumeric) => true,
1280
1281        (Atomic::TInt, Atomic::TScalar) => true,
1282        (Atomic::TFloat, Atomic::TScalar) => true,
1283        (Atomic::TString, Atomic::TScalar) => true,
1284        (Atomic::TBool, Atomic::TScalar) => true,
1285        (Atomic::TNumeric, Atomic::TScalar) => true,
1286        (Atomic::TTrue, Atomic::TScalar) => true,
1287        (Atomic::TFalse, Atomic::TScalar) => true,
1288
1289        // Object hierarchy (structural, no codebase)
1290        (Atomic::TNamedObject { .. }, Atomic::TObject) => true,
1291        (Atomic::TStaticObject { .. }, Atomic::TObject) => true,
1292        (Atomic::TSelf { .. }, Atomic::TObject) => true,
1293        // self(X) and static(X) satisfy TNamedObject(X) with same FQCN
1294        (Atomic::TSelf { fqcn: a }, Atomic::TNamedObject { fqcn: b, .. }) => a == b,
1295        (Atomic::TStaticObject { fqcn: a }, Atomic::TNamedObject { fqcn: b, .. }) => a == b,
1296        // TNamedObject(X) satisfies self(X) / static(X) with same FQCN
1297        (Atomic::TNamedObject { fqcn: a, .. }, Atomic::TSelf { fqcn: b }) => a == b,
1298        (Atomic::TNamedObject { fqcn: a, .. }, Atomic::TStaticObject { fqcn: b }) => a == b,
1299        // Bare generic property accepts parameterized value: Box accepts Box<string>.
1300        // The reverse is NOT true — bare Box value does not satisfy Box<string> property
1301        // (invariant check). Only sup being bare (empty type_params) is the wildcard.
1302        (
1303            Atomic::TNamedObject {
1304                fqcn: sub_fqcn,
1305                type_params: sub_params,
1306            },
1307            Atomic::TNamedObject {
1308                fqcn: sup_fqcn,
1309                type_params: sup_params,
1310            },
1311        ) => {
1312            sub_fqcn == sup_fqcn
1313                && (sup_params.is_empty() || type_params_compatible(sub_params, sup_params))
1314        }
1315
1316        // Literal int widens to float in PHP
1317        (Atomic::TLiteralInt(_), Atomic::TFloat) => true,
1318        (Atomic::TPositiveInt, Atomic::TFloat) => true,
1319        (Atomic::TNegativeInt, Atomic::TFloat) => true,
1320        (Atomic::TNonNegativeInt, Atomic::TFloat) => true,
1321        (Atomic::TInt, Atomic::TFloat) => true,
1322
1323        // Literal int satisfies an int range only when the value is within bounds
1324        (Atomic::TLiteralInt(n), Atomic::TIntRange { min, max }) => {
1325            min.is_none_or(|lo| *n >= lo) && max.is_none_or(|hi| *n <= hi)
1326        }
1327
1328        // PHP callables: string and array are valid callable values
1329        (Atomic::TString, Atomic::TCallable { .. }) => true,
1330        (Atomic::TNonEmptyString, Atomic::TCallable { .. }) => true,
1331        (Atomic::TLiteralString(_), Atomic::TCallable { .. }) => true,
1332        (Atomic::TArray { .. }, Atomic::TCallable { .. }) => true,
1333        (Atomic::TNonEmptyArray { .. }, Atomic::TCallable { .. }) => true,
1334        (Atomic::TKeyedArray { .. }, Atomic::TCallable { .. }) => true,
1335
1336        // Closure <: callable, typed Closure <: Closure
1337        (Atomic::TClosure { .. }, Atomic::TCallable { .. }) => true,
1338        // callable <: Closure: callable is wider but not flagged at default error level
1339        (Atomic::TCallable { .. }, Atomic::TClosure { .. }) => true,
1340        // Any TClosure satisfies another TClosure (structural compatibility simplified)
1341        (Atomic::TClosure { .. }, Atomic::TClosure { .. }) => true,
1342        // callable <: callable (trivial)
1343        (Atomic::TCallable { .. }, Atomic::TCallable { .. }) => true,
1344        // TClosure satisfies `Closure` named object or `object`
1345        (Atomic::TClosure { .. }, Atomic::TNamedObject { fqcn, .. }) => {
1346            fqcn.as_ref().eq_ignore_ascii_case("closure")
1347        }
1348        (Atomic::TClosure { .. }, Atomic::TObject) => true,
1349        // bare `Closure` (named object without signature) satisfies any typed Closure(): T
1350        (Atomic::TNamedObject { fqcn, .. }, Atomic::TClosure { .. }) => {
1351            fqcn.as_ref().eq_ignore_ascii_case("closure")
1352        }
1353        // `Closure` named-object satisfies `callable`
1354        (Atomic::TNamedObject { fqcn, .. }, Atomic::TCallable { .. }) => {
1355            fqcn.as_ref().eq_ignore_ascii_case("closure")
1356        }
1357
1358        // List <: array  (list key is always int; int must satisfy the array's key type)
1359        (Atomic::TList { value }, Atomic::TArray { key, value: av }) => {
1360            Type::single(Atomic::TInt).is_subtype_structural(key) && value.is_subtype_structural(av)
1361        }
1362        (Atomic::TNonEmptyList { value }, Atomic::TArray { key, value: av }) => {
1363            Type::single(Atomic::TInt).is_subtype_structural(key) && value.is_subtype_structural(av)
1364        }
1365        (Atomic::TNonEmptyList { value }, Atomic::TNonEmptyArray { key, value: av }) => {
1366            Type::single(Atomic::TInt).is_subtype_structural(key) && value.is_subtype_structural(av)
1367        }
1368        (Atomic::TNonEmptyList { value }, Atomic::TList { value: lv }) => {
1369            value.is_subtype_structural(lv)
1370        }
1371        // array<int, X> is accepted where list<X> or non-empty-list<X> expected
1372        (Atomic::TArray { key, value: av }, Atomic::TList { value: lv }) => {
1373            matches!(key.types.as_slice(), [Atomic::TInt | Atomic::TMixed])
1374                && av.is_subtype_structural(lv)
1375        }
1376        (Atomic::TArray { key, value: av }, Atomic::TNonEmptyList { value: lv }) => {
1377            matches!(key.types.as_slice(), [Atomic::TInt | Atomic::TMixed])
1378                && av.is_subtype_structural(lv)
1379        }
1380        (Atomic::TNonEmptyArray { key, value: av }, Atomic::TList { value: lv }) => {
1381            matches!(key.types.as_slice(), [Atomic::TInt | Atomic::TMixed])
1382                && av.is_subtype_structural(lv)
1383        }
1384        (Atomic::TNonEmptyArray { key, value: av }, Atomic::TNonEmptyList { value: lv }) => {
1385            matches!(key.types.as_slice(), [Atomic::TInt | Atomic::TMixed])
1386                && av.is_subtype_structural(lv)
1387        }
1388        // TList <: TList value covariance
1389        (Atomic::TList { value: v1 }, Atomic::TList { value: v2 }) => v1.is_subtype_structural(v2),
1390        (Atomic::TNonEmptyArray { key: k1, value: v1 }, Atomic::TArray { key: k2, value: v2 }) => {
1391            k1.is_subtype_structural(k2) && v1.is_subtype_structural(v2)
1392        }
1393
1394        // array<A, B> <: array<C, D>  iff  A <: C && B <: D
1395        (Atomic::TArray { key: k1, value: v1 }, Atomic::TArray { key: k2, value: v2 }) => {
1396            k1.is_subtype_structural(k2) && v1.is_subtype_structural(v2)
1397        }
1398
1399        // A keyed/shape array is a subtype of array<K, V> / non-empty-array<K, V>
1400        // when all property KEYS are subtypes of K. Value compatibility is checked
1401        // structurally only for scalar types; named-object values are deferred to
1402        // class-hierarchy checks in return_arrays_compatible (mir-analyzer).
1403        // Open shapes (is_open=true) may have extra unknown keys: keep permissive.
1404        (
1405            Atomic::TKeyedArray {
1406                properties,
1407                is_open,
1408                ..
1409            },
1410            Atomic::TArray { key, value },
1411        ) => {
1412            if *is_open {
1413                return true;
1414            }
1415            properties.iter().all(|(prop_key, prop)| {
1416                let key_atomic = match prop_key {
1417                    crate::atomic::ArrayKey::String(s) => Atomic::TLiteralString(s.clone()),
1418                    crate::atomic::ArrayKey::Int(n) => Atomic::TLiteralInt(*n),
1419                };
1420                if !Type::single(key_atomic).is_subtype_structural(key) {
1421                    return false; // key mismatch — definitively incompatible
1422                }
1423                // Named-object values require class-hierarchy checks not available here.
1424                let has_named_obj = prop.ty.types.iter().any(|a| {
1425                    matches!(
1426                        a,
1427                        Atomic::TNamedObject { .. }
1428                            | Atomic::TSelf { .. }
1429                            | Atomic::TStaticObject { .. }
1430                            | Atomic::TClosure { .. }
1431                            | Atomic::TTemplateParam { .. }
1432                    )
1433                });
1434                has_named_obj || prop.ty.is_subtype_structural(value)
1435            })
1436        }
1437        (
1438            Atomic::TKeyedArray {
1439                properties,
1440                is_open,
1441                ..
1442            },
1443            Atomic::TNonEmptyArray { key, value },
1444        ) => {
1445            if *is_open {
1446                return !properties.is_empty();
1447            }
1448            properties.iter().any(|(_, p)| !p.optional)
1449                && properties.iter().all(|(prop_key, prop)| {
1450                    let key_atomic = match prop_key {
1451                        crate::atomic::ArrayKey::String(s) => Atomic::TLiteralString(s.clone()),
1452                        crate::atomic::ArrayKey::Int(n) => Atomic::TLiteralInt(*n),
1453                    };
1454                    if !Type::single(key_atomic).is_subtype_structural(key) {
1455                        return false;
1456                    }
1457                    let has_named_obj = prop.ty.types.iter().any(|a| {
1458                        matches!(
1459                            a,
1460                            Atomic::TNamedObject { .. }
1461                                | Atomic::TSelf { .. }
1462                                | Atomic::TStaticObject { .. }
1463                                | Atomic::TClosure { .. }
1464                                | Atomic::TTemplateParam { .. }
1465                        )
1466                    });
1467                    has_named_obj || prop.ty.is_subtype_structural(value)
1468                })
1469        }
1470
1471        // A list-shaped keyed array (is_list=true, all int keys) is a subtype of list<X>.
1472        (
1473            Atomic::TKeyedArray {
1474                properties,
1475                is_list,
1476                ..
1477            },
1478            Atomic::TList { value: lv },
1479        ) => *is_list && properties.values().all(|p| p.ty.is_subtype_structural(lv)),
1480        (
1481            Atomic::TKeyedArray {
1482                properties,
1483                is_list,
1484                ..
1485            },
1486            Atomic::TNonEmptyList { value: lv },
1487        ) => {
1488            *is_list
1489                && !properties.is_empty()
1490                && properties.values().all(|p| p.ty.is_subtype_structural(lv))
1491        }
1492
1493        _ => false,
1494    }
1495}
1496
1497/// Whether each generic type-argument in `sub` is compatible with the
1498/// corresponding argument in `sup`. Arguments are invariant (require structural
1499/// equality) with one exception: an empty array literal (`array{}`) is accepted
1500/// against any array/list argument, so `new Box([])` — inferred as
1501/// `Box<array{}>` — satisfies a declared `Box<list<T>>` for any `T`.
1502fn type_params_compatible(sub: &[Type], sup: &[Type]) -> bool {
1503    if sub.len() != sup.len() {
1504        return false;
1505    }
1506    sub.iter()
1507        .zip(sup.iter())
1508        .all(|(a, b)| a == b || (is_empty_array_literal(a) && is_array_like(b)))
1509}
1510
1511/// True for a non-empty union whose atoms are all empty keyed arrays (`array{}`),
1512/// i.e. the type of an empty array literal `[]`.
1513fn is_empty_array_literal(t: &Type) -> bool {
1514    !t.types.is_empty()
1515        && t.types.iter().all(
1516            |atom| matches!(atom, Atomic::TKeyedArray { properties, .. } if properties.is_empty()),
1517        )
1518}
1519
1520/// True for a non-empty union whose atoms are all array/list types.
1521fn is_array_like(t: &Type) -> bool {
1522    !t.types.is_empty() && t.types.iter().all(|atom| atom.is_array())
1523}
1524
1525// ---------------------------------------------------------------------------
1526// Tests
1527// ---------------------------------------------------------------------------
1528
1529#[cfg(test)]
1530mod tests {
1531    use std::sync::Arc;
1532
1533    use super::*;
1534
1535    #[test]
1536    fn single_is_single() {
1537        let u = Type::single(Atomic::TString);
1538        assert!(u.is_single());
1539        assert!(!u.is_nullable());
1540    }
1541
1542    #[test]
1543    fn nullable_has_null() {
1544        let u = Type::nullable(Atomic::TString);
1545        assert!(u.is_nullable());
1546        assert_eq!(u.types.len(), 2);
1547    }
1548
1549    #[test]
1550    fn add_type_deduplicates() {
1551        let mut u = Type::single(Atomic::TString);
1552        u.add_type(Atomic::TString);
1553        assert_eq!(u.types.len(), 1);
1554    }
1555
1556    #[test]
1557    fn add_type_literal_subsumed_by_base() {
1558        let mut u = Type::single(Atomic::TInt);
1559        u.add_type(Atomic::TLiteralInt(42));
1560        assert_eq!(u.types.len(), 1);
1561        assert!(matches!(u.types[0], Atomic::TInt));
1562    }
1563
1564    #[test]
1565    fn add_type_base_widens_literals() {
1566        let mut u = Type::single(Atomic::TLiteralInt(1));
1567        u.add_type(Atomic::TLiteralInt(2));
1568        u.add_type(Atomic::TInt);
1569        assert_eq!(u.types.len(), 1);
1570        assert!(matches!(u.types[0], Atomic::TInt));
1571    }
1572
1573    #[test]
1574    fn mixed_subsumes_everything() {
1575        let mut u = Type::single(Atomic::TString);
1576        u.add_type(Atomic::TMixed);
1577        assert_eq!(u.types.len(), 1);
1578        assert!(u.is_mixed());
1579    }
1580
1581    #[test]
1582    fn remove_null() {
1583        let u = Type::nullable(Atomic::TString);
1584        let narrowed = u.remove_null();
1585        assert!(!narrowed.is_nullable());
1586        assert_eq!(narrowed.types.len(), 1);
1587    }
1588
1589    #[test]
1590    fn narrow_to_truthy_removes_null_false() {
1591        let mut u = Type::empty();
1592        u.add_type(Atomic::TString);
1593        u.add_type(Atomic::TNull);
1594        u.add_type(Atomic::TFalse);
1595        let truthy = u.narrow_to_truthy();
1596        assert!(!truthy.is_nullable());
1597        assert!(!truthy.contains(|t| matches!(t, Atomic::TFalse)));
1598    }
1599
1600    #[test]
1601    fn merge_combines_types() {
1602        let a = Type::single(Atomic::TString);
1603        let b = Type::single(Atomic::TInt);
1604        let merged = Type::merge(&a, &b);
1605        assert_eq!(merged.types.len(), 2);
1606    }
1607
1608    #[test]
1609    fn subtype_literal_int_under_int() {
1610        let sub = Type::single(Atomic::TLiteralInt(5));
1611        let sup = Type::single(Atomic::TInt);
1612        assert!(sub.is_subtype_structural(&sup));
1613    }
1614
1615    #[test]
1616    fn subtype_never_is_bottom() {
1617        let never = Type::never();
1618        let string = Type::single(Atomic::TString);
1619        assert!(never.is_subtype_structural(&string));
1620    }
1621
1622    #[test]
1623    fn subtype_everything_under_mixed() {
1624        let string = Type::single(Atomic::TString);
1625        let mixed = Type::mixed();
1626        assert!(string.is_subtype_structural(&mixed));
1627    }
1628
1629    #[test]
1630    fn template_substitution() {
1631        let mut bindings = FxHashMap::default();
1632        bindings.insert(Name::new("T"), Type::single(Atomic::TString));
1633
1634        let tmpl = Type::single(Atomic::TTemplateParam {
1635            name: Name::new("T"),
1636            as_type: Box::new(Type::mixed()),
1637            defining_entity: Name::new("MyClass"),
1638        });
1639
1640        let resolved = tmpl.substitute_templates(&bindings);
1641        assert_eq!(resolved.types.len(), 1);
1642        assert!(matches!(resolved.types[0], Atomic::TString));
1643    }
1644
1645    #[test]
1646    fn intersection_is_object() {
1647        let parts = vec![
1648            Type::single(Atomic::TNamedObject {
1649                fqcn: Name::new("Iterator"),
1650                type_params: empty_type_params(),
1651            }),
1652            Type::single(Atomic::TNamedObject {
1653                fqcn: Name::new("Countable"),
1654                type_params: empty_type_params(),
1655            }),
1656        ];
1657        let atomic = Atomic::TIntersection {
1658            parts: vec_to_type_params(parts),
1659        };
1660        assert!(atomic.is_object());
1661        assert!(!atomic.can_be_falsy());
1662        assert!(atomic.can_be_truthy());
1663    }
1664
1665    #[test]
1666    fn intersection_display_two_parts() {
1667        let parts = vec![
1668            Type::single(Atomic::TNamedObject {
1669                fqcn: Name::new("Iterator"),
1670                type_params: empty_type_params(),
1671            }),
1672            Type::single(Atomic::TNamedObject {
1673                fqcn: Name::new("Countable"),
1674                type_params: empty_type_params(),
1675            }),
1676        ];
1677        let u = Type::single(Atomic::TIntersection {
1678            parts: vec_to_type_params(parts),
1679        });
1680        assert_eq!(format!("{u}"), "Iterator&Countable");
1681    }
1682
1683    #[test]
1684    fn intersection_display_three_parts() {
1685        let parts = vec![
1686            Type::single(Atomic::TNamedObject {
1687                fqcn: Name::new("A"),
1688                type_params: empty_type_params(),
1689            }),
1690            Type::single(Atomic::TNamedObject {
1691                fqcn: Name::new("B"),
1692                type_params: empty_type_params(),
1693            }),
1694            Type::single(Atomic::TNamedObject {
1695                fqcn: Name::new("C"),
1696                type_params: empty_type_params(),
1697            }),
1698        ];
1699        let u = Type::single(Atomic::TIntersection {
1700            parts: vec_to_type_params(parts),
1701        });
1702        assert_eq!(format!("{u}"), "A&B&C");
1703    }
1704
1705    #[test]
1706    fn intersection_in_nullable_union_display() {
1707        let intersection = Atomic::TIntersection {
1708            parts: vec_to_type_params(vec![
1709                Type::single(Atomic::TNamedObject {
1710                    fqcn: Name::new("Iterator"),
1711                    type_params: empty_type_params(),
1712                }),
1713                Type::single(Atomic::TNamedObject {
1714                    fqcn: Name::new("Countable"),
1715                    type_params: empty_type_params(),
1716                }),
1717            ]),
1718        };
1719        let mut u = Type::single(intersection);
1720        u.add_type(Atomic::TNull);
1721        assert!(u.is_nullable());
1722        assert!(u.contains(|t| matches!(t, Atomic::TIntersection { .. })));
1723    }
1724
1725    // --- substitute_templates coverage for previously-missing arms ----------
1726
1727    fn t_param(name: &str) -> Type {
1728        Type::single(Atomic::TTemplateParam {
1729            name: Name::new(name),
1730            as_type: Box::new(Type::mixed()),
1731            defining_entity: Name::new("Fn"),
1732        })
1733    }
1734
1735    fn bindings_t_string() -> FxHashMap<Name, Type> {
1736        let mut b = FxHashMap::default();
1737        b.insert(Name::new("T"), Type::single(Atomic::TString));
1738        b
1739    }
1740
1741    #[test]
1742    fn substitute_non_empty_array_key_and_value() {
1743        let ty = Type::single(Atomic::TNonEmptyArray {
1744            key: Box::new(t_param("T")),
1745            value: Box::new(t_param("T")),
1746        });
1747        let result = ty.substitute_templates(&bindings_t_string());
1748        assert_eq!(result.types.len(), 1);
1749        let Atomic::TNonEmptyArray { key, value } = &result.types[0] else {
1750            panic!("expected TNonEmptyArray");
1751        };
1752        assert!(matches!(key.types[0], Atomic::TString));
1753        assert!(matches!(value.types[0], Atomic::TString));
1754    }
1755
1756    #[test]
1757    fn substitute_non_empty_list_value() {
1758        let ty = Type::single(Atomic::TNonEmptyList {
1759            value: Box::new(t_param("T")),
1760        });
1761        let result = ty.substitute_templates(&bindings_t_string());
1762        let Atomic::TNonEmptyList { value } = &result.types[0] else {
1763            panic!("expected TNonEmptyList");
1764        };
1765        assert!(matches!(value.types[0], Atomic::TString));
1766    }
1767
1768    #[test]
1769    fn substitute_keyed_array_property_types() {
1770        use crate::atomic::{ArrayKey, KeyedProperty};
1771        use indexmap::IndexMap;
1772        let mut props = IndexMap::new();
1773        props.insert(
1774            ArrayKey::String(Arc::from("name")),
1775            KeyedProperty {
1776                ty: t_param("T"),
1777                optional: false,
1778            },
1779        );
1780        props.insert(
1781            ArrayKey::String(Arc::from("tag")),
1782            KeyedProperty {
1783                ty: t_param("T"),
1784                optional: true,
1785            },
1786        );
1787        let ty = Type::single(Atomic::TKeyedArray {
1788            properties: props,
1789            is_open: true,
1790            is_list: false,
1791        });
1792        let result = ty.substitute_templates(&bindings_t_string());
1793        let Atomic::TKeyedArray {
1794            properties,
1795            is_open,
1796            is_list,
1797        } = &result.types[0]
1798        else {
1799            panic!("expected TKeyedArray");
1800        };
1801        assert!(is_open);
1802        assert!(!is_list);
1803        assert!(matches!(
1804            properties[&ArrayKey::String(Arc::from("name"))].ty.types[0],
1805            Atomic::TString
1806        ));
1807        assert!(properties[&ArrayKey::String(Arc::from("tag"))].optional);
1808        assert!(matches!(
1809            properties[&ArrayKey::String(Arc::from("tag"))].ty.types[0],
1810            Atomic::TString
1811        ));
1812    }
1813
1814    #[test]
1815    fn substitute_callable_params_and_return() {
1816        use crate::atomic::FnParam;
1817        let ty = Type::single(Atomic::TCallable {
1818            params: Some(vec![FnParam {
1819                name: Name::new("x"),
1820                ty: Some(crate::compact::SimpleType::from_union(t_param("T"))),
1821                default: None,
1822                is_variadic: false,
1823                is_byref: false,
1824                is_optional: false,
1825            }]),
1826            return_type: Some(Box::new(t_param("T"))),
1827        });
1828        let result = ty.substitute_templates(&bindings_t_string());
1829        let Atomic::TCallable {
1830            params,
1831            return_type,
1832        } = &result.types[0]
1833        else {
1834            panic!("expected TCallable");
1835        };
1836        let param_ty = params.as_ref().unwrap()[0].ty.as_ref().unwrap();
1837        let param_union = param_ty.to_union();
1838        assert!(matches!(param_union.types[0], Atomic::TString));
1839        let ret = return_type.as_ref().unwrap();
1840        assert!(matches!(ret.types[0], Atomic::TString));
1841    }
1842
1843    #[test]
1844    fn substitute_callable_bare_no_panic() {
1845        // callable with no params/return — must not panic and must pass through unchanged
1846        let ty = Type::single(Atomic::TCallable {
1847            params: None,
1848            return_type: None,
1849        });
1850        let result = ty.substitute_templates(&bindings_t_string());
1851        assert!(matches!(
1852            result.types[0],
1853            Atomic::TCallable {
1854                params: None,
1855                return_type: None
1856            }
1857        ));
1858    }
1859
1860    #[test]
1861    fn substitute_closure_params_return_and_this() {
1862        use crate::atomic::FnParam;
1863        let ty = Type::single(Atomic::TClosure {
1864            params: vec![FnParam {
1865                name: Name::new("a"),
1866                ty: Some(crate::compact::SimpleType::from_union(t_param("T"))),
1867                default: Some(crate::compact::SimpleType::from_union(t_param("T"))),
1868                is_variadic: true,
1869                is_byref: true,
1870                is_optional: true,
1871            }],
1872            return_type: Box::new(t_param("T")),
1873            this_type: Some(Box::new(t_param("T"))),
1874        });
1875        let result = ty.substitute_templates(&bindings_t_string());
1876        let Atomic::TClosure {
1877            params,
1878            return_type,
1879            this_type,
1880        } = &result.types[0]
1881        else {
1882            panic!("expected TClosure");
1883        };
1884        let p = &params[0];
1885        let ty_union = p.ty.as_ref().unwrap().to_union();
1886        let default_union = p.default.as_ref().unwrap().to_union();
1887        assert!(matches!(ty_union.types[0], Atomic::TString));
1888        assert!(matches!(default_union.types[0], Atomic::TString));
1889        // flags preserved
1890        assert!(p.is_variadic);
1891        assert!(p.is_byref);
1892        assert!(p.is_optional);
1893        assert!(matches!(return_type.types[0], Atomic::TString));
1894        assert!(matches!(
1895            this_type.as_ref().unwrap().types[0],
1896            Atomic::TString
1897        ));
1898    }
1899
1900    #[test]
1901    fn substitute_conditional_all_branches() {
1902        let ty = Type::single(Atomic::TConditional {
1903            param_name: None,
1904            subject: Box::new(t_param("T")),
1905            if_true: Box::new(t_param("T")),
1906            if_false: Box::new(Type::single(Atomic::TInt)),
1907        });
1908        let result = ty.substitute_templates(&bindings_t_string());
1909        let Atomic::TConditional {
1910            param_name: _,
1911            subject,
1912            if_true,
1913            if_false,
1914        } = &result.types[0]
1915        else {
1916            panic!("expected TConditional");
1917        };
1918        assert!(matches!(subject.types[0], Atomic::TString));
1919        assert!(matches!(if_true.types[0], Atomic::TString));
1920        assert!(matches!(if_false.types[0], Atomic::TInt));
1921    }
1922
1923    #[test]
1924    fn resolve_conditional_is_null_non_null_arg() {
1925        let ty = Type::single(Atomic::TConditional {
1926            param_name: Some(Name::new("x")),
1927            subject: Box::new(Type::single(Atomic::TNull)),
1928            if_true: Box::new(Type::single(Atomic::TInt)),
1929            if_false: Box::new(Type::single(Atomic::TString)),
1930        });
1931        let result = ty.resolve_conditional_returns(|name| {
1932            if name == "x" {
1933                Some(Type::single(Atomic::TString)) // definitely not null
1934            } else {
1935                None
1936            }
1937        });
1938        assert!(result.types.len() == 1);
1939        assert!(matches!(result.types[0], Atomic::TString));
1940    }
1941
1942    #[test]
1943    fn resolve_conditional_is_null_null_arg() {
1944        let ty = Type::single(Atomic::TConditional {
1945            param_name: Some(Name::new("x")),
1946            subject: Box::new(Type::single(Atomic::TNull)),
1947            if_true: Box::new(Type::single(Atomic::TInt)),
1948            if_false: Box::new(Type::single(Atomic::TString)),
1949        });
1950        let result = ty.resolve_conditional_returns(|name| {
1951            if name == "x" {
1952                Some(Type::single(Atomic::TNull)) // definitely null
1953            } else {
1954                None
1955            }
1956        });
1957        assert!(result.types.len() == 1);
1958        assert!(matches!(result.types[0], Atomic::TInt));
1959    }
1960
1961    #[test]
1962    fn resolve_conditional_is_null_nullable_arg_widens_to_branch_union() {
1963        let mut nullable_str = Type::single(Atomic::TString);
1964        nullable_str.add_type(Atomic::TNull);
1965        let ty = Type::single(Atomic::TConditional {
1966            param_name: Some(Name::new("x")),
1967            subject: Box::new(Type::single(Atomic::TNull)),
1968            if_true: Box::new(Type::single(Atomic::TInt)),
1969            if_false: Box::new(Type::single(Atomic::TString)),
1970        });
1971        let result = ty.resolve_conditional_returns(|name| {
1972            if name == "x" {
1973                Some(nullable_str.clone())
1974            } else {
1975                None
1976            }
1977        });
1978        // uncertain discriminator → widen to if_true | if_false
1979        assert_eq!(result.types.len(), 2);
1980        assert!(result.types.iter().any(|t| matches!(t, Atomic::TInt)));
1981        assert!(result.types.iter().any(|t| matches!(t, Atomic::TString)));
1982    }
1983
1984    #[test]
1985    fn resolve_conditional_nested_widens_inner_branch() {
1986        // ($x is null ? int : ($x is string ? string : float))
1987        // When $x is unknown, should widen to int|string|float (no TConditional remaining).
1988        let inner = Type::single(Atomic::TConditional {
1989            param_name: Some(Name::new("x")),
1990            subject: Box::new(Type::single(Atomic::TString)),
1991            if_true: Box::new(Type::single(Atomic::TString)),
1992            if_false: Box::new(Type::single(Atomic::TFloat)),
1993        });
1994        let ty = Type::single(Atomic::TConditional {
1995            param_name: Some(Name::new("x")),
1996            subject: Box::new(Type::single(Atomic::TNull)),
1997            if_true: Box::new(Type::single(Atomic::TInt)),
1998            if_false: Box::new(inner),
1999        });
2000        // unknown arg → widen both outer branches, inner conditional must also be widened
2001        let result = ty.resolve_conditional_returns(|_| None);
2002        assert!(
2003            result
2004                .types
2005                .iter()
2006                .all(|t| !matches!(t, Atomic::TConditional { .. })),
2007            "no TConditional should survive: {:?}",
2008            result.types
2009        );
2010        assert!(result.types.iter().any(|t| matches!(t, Atomic::TInt)));
2011        assert!(result.types.iter().any(|t| matches!(t, Atomic::TString)));
2012        assert!(result.types.iter().any(|t| matches!(t, Atomic::TFloat)));
2013    }
2014
2015    #[test]
2016    fn resolve_conditional_nested_resolves_inner_branch() {
2017        // ($x is null ? int : ($x is string ? string : float))
2018        // When $x is definitely not null but unknown string-or-not → resolves outer to inner,
2019        // then inner must also be resolved.
2020        let inner = Type::single(Atomic::TConditional {
2021            param_name: Some(Name::new("x")),
2022            subject: Box::new(Type::single(Atomic::TString)),
2023            if_true: Box::new(Type::single(Atomic::TString)),
2024            if_false: Box::new(Type::single(Atomic::TFloat)),
2025        });
2026        let ty = Type::single(Atomic::TConditional {
2027            param_name: Some(Name::new("x")),
2028            subject: Box::new(Type::single(Atomic::TNull)),
2029            if_true: Box::new(Type::single(Atomic::TInt)),
2030            if_false: Box::new(inner),
2031        });
2032        // $x = string → outer: not null → if_false (inner); inner: is string → if_true = string
2033        let result = ty.resolve_conditional_returns(|name| {
2034            if name == "x" {
2035                Some(Type::single(Atomic::TString))
2036            } else {
2037                None
2038            }
2039        });
2040        assert!(
2041            result
2042                .types
2043                .iter()
2044                .all(|t| !matches!(t, Atomic::TConditional { .. })),
2045            "no TConditional should survive: {:?}",
2046            result.types
2047        );
2048        assert_eq!(result.types.len(), 1);
2049        assert!(matches!(result.types[0], Atomic::TString));
2050    }
2051
2052    #[test]
2053    fn substitute_intersection_parts() {
2054        let ty = Type::single(Atomic::TIntersection {
2055            parts: vec_to_type_params(vec![
2056                Type::single(Atomic::TNamedObject {
2057                    fqcn: Name::new("Countable"),
2058                    type_params: empty_type_params(),
2059                }),
2060                t_param("T"),
2061            ]),
2062        });
2063        let result = ty.substitute_templates(&bindings_t_string());
2064        let Atomic::TIntersection { parts } = &result.types[0] else {
2065            panic!("expected TIntersection");
2066        };
2067        assert_eq!(parts.len(), 2);
2068        assert!(matches!(parts[0].types[0], Atomic::TNamedObject { .. }));
2069        assert!(matches!(parts[1].types[0], Atomic::TString));
2070    }
2071
2072    #[test]
2073    fn substitute_no_template_params_identity() {
2074        let ty = Type::single(Atomic::TInt);
2075        let result = ty.substitute_templates(&bindings_t_string());
2076        assert!(matches!(result.types[0], Atomic::TInt));
2077    }
2078}