Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! ABI-related things in the next-trait-solver.
use rustc_type_ir::{error::TypeError, relate::Relate};

use crate::FnAbi;

use super::interner::DbInterner;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Safety {
    Unsafe,
    Safe,
}

impl<'db> Relate<DbInterner<'db>> for Safety {
    fn relate<R: rustc_type_ir::relate::TypeRelation<DbInterner<'db>>>(
        _relation: &mut R,
        a: Self,
        b: Self,
    ) -> rustc_type_ir::relate::RelateResult<DbInterner<'db>, Self> {
        if a != b {
            Err(TypeError::SafetyMismatch(rustc_type_ir::error::ExpectedFound::new(a, b)))
        } else {
            Ok(a)
        }
    }
}

impl<'db> rustc_type_ir::inherent::Safety<DbInterner<'db>> for Safety {
    fn safe() -> Self {
        Self::Safe
    }

    fn is_safe(self) -> bool {
        matches!(self, Safety::Safe)
    }

    fn prefix_str(self) -> &'static str {
        match self {
            Self::Unsafe => "unsafe ",
            Self::Safe => "",
        }
    }
}

impl<'db> Relate<DbInterner<'db>> for FnAbi {
    fn relate<R: rustc_type_ir::relate::TypeRelation<DbInterner<'db>>>(
        _relation: &mut R,
        a: Self,
        b: Self,
    ) -> rustc_type_ir::relate::RelateResult<DbInterner<'db>, Self> {
        if a == b {
            Ok(a)
        } else {
            Err(TypeError::AbiMismatch(rustc_type_ir::error::ExpectedFound::new(a, b)))
        }
    }
}

impl<'db> rustc_type_ir::inherent::Abi<DbInterner<'db>> for FnAbi {
    fn rust() -> Self {
        FnAbi::Rust
    }

    fn is_rust(self) -> bool {
        // TODO: rustc does not consider `RustCall` to be true here, but Chalk does
        matches!(self, FnAbi::Rust | FnAbi::RustCall)
    }
}