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, Eq, 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::DefId>),
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> TypeError<I> {
62    pub fn involves_regions(self) -> bool {
63        match self {
64            TypeError::RegionsDoesNotOutlive(_, _)
65            | TypeError::RegionsInsufficientlyPolymorphic(_, _)
66            | TypeError::RegionsPlaceholderMismatch => true,
67            _ => false,
68        }
69    }
70
71    pub fn must_include_note(self) -> bool {
72        use self::TypeError::*;
73        match self {
74            CyclicTy(_) | CyclicConst(_) | SafetyMismatch(_) | PolarityMismatch(_) | Mismatch
75            | AbiMismatch(_) | ArraySize(_) | ArgumentSorts(..) | Sorts(_)
76            | VariadicMismatch(_) | TargetFeatureCast(_) => false,
77
78            Mutability
79            | ArgumentMutability(_)
80            | TupleSize(_)
81            | ArgCount
82            | RegionsDoesNotOutlive(..)
83            | RegionsInsufficientlyPolymorphic(..)
84            | RegionsPlaceholderMismatch
85            | Traits(_)
86            | ProjectionMismatched(_)
87            | ExistentialMismatch(_)
88            | ConstMismatch(_)
89            | ForceInlineCast
90            | IntrinsicCast => true,
91        }
92    }
93}
94
95impl<I: Interner> From<TypeError<I>> for NoSolution {
96    fn from(_: TypeError<I>) -> NoSolution {
97        NoSolution
98    }
99}