rustc_type_ir/
error.rs

1use derive_where::derive_where;
2use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
3
4use crate::solve::NoSolution;
5use crate::{self as ty, Interner};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8#[derive(TypeFoldable_Generic, TypeVisitable_Generic)]
9pub struct ExpectedFound<T> {
10    pub expected: T,
11    pub found: T,
12}
13
14impl<T> ExpectedFound<T> {
15    pub fn new(expected: T, found: T) -> Self {
16        ExpectedFound { expected, found }
17    }
18}
19
20// Data structures used in type unification
21#[derive_where(Clone, Copy, PartialEq, Debug; I: Interner)]
22#[derive(TypeVisitable_Generic)]
23#[cfg_attr(feature = "nightly", rustc_pass_by_value)]
24pub enum TypeError<I: Interner> {
25    Mismatch,
26    PolarityMismatch(#[type_visitable(ignore)] ExpectedFound<ty::PredicatePolarity>),
27    SafetyMismatch(#[type_visitable(ignore)] ExpectedFound<I::Safety>),
28    AbiMismatch(#[type_visitable(ignore)] ExpectedFound<I::Abi>),
29    Mutability,
30    ArgumentMutability(usize),
31    TupleSize(ExpectedFound<usize>),
32    ArraySize(ExpectedFound<I::Const>),
33    ArgCount,
34
35    RegionsDoesNotOutlive(I::Region, I::Region),
36    RegionsInsufficientlyPolymorphic(I::BoundRegion, I::Region),
37    RegionsPlaceholderMismatch,
38
39    Sorts(ExpectedFound<I::Ty>),
40    ArgumentSorts(ExpectedFound<I::Ty>, usize),
41    Traits(ExpectedFound<I::TraitId>),
42    VariadicMismatch(ExpectedFound<bool>),
43
44    /// Instantiating a type variable with the given type would have
45    /// created a cycle (because it appears somewhere within that
46    /// type).
47    CyclicTy(I::Ty),
48    CyclicConst(I::Const),
49    ProjectionMismatched(ExpectedFound<I::DefId>),
50    ExistentialMismatch(ExpectedFound<I::BoundExistentialPredicates>),
51    ConstMismatch(ExpectedFound<I::Const>),
52
53    IntrinsicCast,
54    /// `#[rustc_force_inline]` functions must be inlined and must not be codegened independently,
55    /// so casting to a function pointer must be prohibited.
56    ForceInlineCast,
57    /// Safe `#[target_feature]` functions are not assignable to safe function pointers.
58    TargetFeatureCast(I::DefId),
59}
60
61impl<I: Interner> Eq for TypeError<I> {}
62
63impl<I: Interner> TypeError<I> {
64    pub fn involves_regions(self) -> bool {
65        match self {
66            TypeError::RegionsDoesNotOutlive(_, _)
67            | TypeError::RegionsInsufficientlyPolymorphic(_, _)
68            | TypeError::RegionsPlaceholderMismatch => true,
69            _ => false,
70        }
71    }
72
73    pub fn must_include_note(self) -> bool {
74        use self::TypeError::*;
75        match self {
76            CyclicTy(_) | CyclicConst(_) | SafetyMismatch(_) | PolarityMismatch(_) | Mismatch
77            | AbiMismatch(_) | ArraySize(_) | ArgumentSorts(..) | Sorts(_)
78            | VariadicMismatch(_) | TargetFeatureCast(_) => false,
79
80            Mutability
81            | ArgumentMutability(_)
82            | TupleSize(_)
83            | ArgCount
84            | RegionsDoesNotOutlive(..)
85            | RegionsInsufficientlyPolymorphic(..)
86            | RegionsPlaceholderMismatch
87            | Traits(_)
88            | ProjectionMismatched(_)
89            | ExistentialMismatch(_)
90            | ConstMismatch(_)
91            | ForceInlineCast
92            | IntrinsicCast => true,
93        }
94    }
95}
96
97impl<I: Interner> From<TypeError<I>> for NoSolution {
98    fn from(_: TypeError<I>) -> NoSolution {
99        NoSolution
100    }
101}