Skip to main content

hir_ty/next_solver/
abi.rs

1//! ABI-related things in the next-trait-solver.
2use rustc_abi::ExternAbi;
3use rustc_ast_ir::visit::VisitorResult;
4use rustc_type_ir::{
5    FallibleTypeFolder, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor, error::TypeError,
6    relate::Relate,
7};
8
9use super::interner::DbInterner;
10
11#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
12pub enum Safety {
13    Unsafe,
14    Safe,
15}
16
17impl<'db> Relate<DbInterner<'db>> for Safety {
18    fn relate<R: rustc_type_ir::relate::TypeRelation<DbInterner<'db>>>(
19        _relation: &mut R,
20        a: Self,
21        b: Self,
22    ) -> rustc_type_ir::relate::RelateResult<DbInterner<'db>, Self> {
23        if a != b {
24            Err(TypeError::SafetyMismatch(rustc_type_ir::error::ExpectedFound::new(a, b)))
25        } else {
26            Ok(a)
27        }
28    }
29}
30
31impl<'db> rustc_type_ir::inherent::Safety<DbInterner<'db>> for Safety {
32    fn safe() -> Self {
33        Self::Safe
34    }
35
36    fn is_safe(self) -> bool {
37        matches!(self, Safety::Safe)
38    }
39
40    fn prefix_str(self) -> &'static str {
41        match self {
42            Self::Unsafe => "unsafe ",
43            Self::Safe => "",
44        }
45    }
46
47    fn unsafe_mode() -> Self {
48        Safety::Unsafe
49    }
50}
51
52impl<'db> TypeVisitable<DbInterner<'db>> for ExternAbi {
53    fn visit_with<V: TypeVisitor<DbInterner<'db>>>(&self, _visitor: &mut V) -> V::Result {
54        V::Result::output()
55    }
56}
57
58impl<'db> TypeFoldable<DbInterner<'db>> for ExternAbi {
59    fn try_fold_with<F: FallibleTypeFolder<DbInterner<'db>>>(
60        self,
61        _folder: &mut F,
62    ) -> Result<Self, F::Error> {
63        Ok(self)
64    }
65
66    fn fold_with<F: TypeFolder<DbInterner<'db>>>(self, _folder: &mut F) -> Self {
67        self
68    }
69}
70
71impl<'db> Relate<DbInterner<'db>> for ExternAbi {
72    fn relate<R: rustc_type_ir::relate::TypeRelation<DbInterner<'db>>>(
73        _relation: &mut R,
74        a: Self,
75        b: Self,
76    ) -> rustc_type_ir::relate::RelateResult<DbInterner<'db>, Self> {
77        if a == b {
78            Ok(a)
79        } else {
80            Err(TypeError::AbiMismatch(rustc_type_ir::error::ExpectedFound::new(a, b)))
81        }
82    }
83}