about summary refs log tree commit diff
path: root/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs
blob: 6be0cb1190436a5a156aeb9264e30de55300d17e (plain)
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
use crate::layout;

/// Context necessary to answer the question "Are these types transmutable?".
pub(crate) trait QueryContext {
    type Def: layout::Def;
    type Region: layout::Region;
    type Type: layout::Type;
}

#[cfg(test)]
pub(crate) mod test {
    use std::marker::PhantomData;

    use super::QueryContext;

    pub(crate) struct UltraMinimal<R = !, T = !>(PhantomData<(R, T)>);

    impl<R, T> Default for UltraMinimal<R, T> {
        fn default() -> Self {
            Self(PhantomData)
        }
    }

    #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
    pub(crate) enum Def {
        HasSafetyInvariants,
        NoSafetyInvariants,
    }

    impl crate::layout::Def for Def {
        fn has_safety_invariants(&self) -> bool {
            self == &Self::HasSafetyInvariants
        }
    }

    impl<R, T> QueryContext for UltraMinimal<R, T>
    where
        R: crate::layout::Region,
        T: crate::layout::Type,
    {
        type Def = Def;
        type Region = R;
        type Type = T;
    }
}

#[cfg(feature = "rustc")]
mod rustc {
    use rustc_middle::ty::{Region, Ty, TyCtxt};

    use super::*;

    impl<'tcx> super::QueryContext for TyCtxt<'tcx> {
        type Def = layout::rustc::Def<'tcx>;
        type Region = Region<'tcx>;
        type Type = Ty<'tcx>;
    }
}