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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#![feature(
alloc_layout_extra,
control_flow_enum,
decl_macro,
iterator_try_reduce,
never_type,
result_into_ok_or_err
)]
#![allow(dead_code, unused_variables)]
#[macro_use]
extern crate tracing;
#[cfg(feature = "rustc")]
pub(crate) use rustc_data_structures::fx::{FxHashMap as Map, FxHashSet as Set};
#[cfg(not(feature = "rustc"))]
pub(crate) use std::collections::{HashMap as Map, HashSet as Set};
pub(crate) mod layout;
pub(crate) mod maybe_transmutable;
#[derive(Default)]
pub struct Assume {
pub alignment: bool,
pub lifetimes: bool,
pub validity: bool,
pub visibility: bool,
}
#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Clone)]
pub enum Answer<R>
where
R: layout::Ref,
{
Yes,
No(Reason),
IfTransmutable { src: R, dst: R },
IfAll(Vec<Answer<R>>),
IfAny(Vec<Answer<R>>),
}
#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Clone)]
pub enum Reason {
SrcIsUnspecified,
DstIsUnspecified,
DstIsBitIncompatible,
DstIsPrivate,
DstIsTooBig,
}
#[cfg(feature = "rustc")]
mod rustc {
use rustc_infer::infer::InferCtxt;
use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::Binder;
use rustc_middle::ty::Ty;
#[derive(TypeFoldable, TypeVisitable, Debug, Clone, Copy)]
pub struct Types<'tcx> {
pub src: Ty<'tcx>,
pub dst: Ty<'tcx>,
}
pub struct TransmuteTypeEnv<'cx, 'tcx> {
infcx: &'cx InferCtxt<'cx, 'tcx>,
}
impl<'cx, 'tcx> TransmuteTypeEnv<'cx, 'tcx> {
pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>) -> Self {
Self { infcx }
}
#[allow(unused)]
pub fn is_transmutable(
&mut self,
cause: ObligationCause<'tcx>,
src_and_dst: Binder<'tcx, Types<'tcx>>,
scope: Ty<'tcx>,
assume: crate::Assume,
) -> crate::Answer<crate::layout::rustc::Ref<'tcx>> {
let src = src_and_dst.map_bound(|types| types.src).skip_binder();
let dst = src_and_dst.map_bound(|types| types.dst).skip_binder();
crate::maybe_transmutable::MaybeTransmutableQuery::new(
src,
dst,
scope,
assume,
self.infcx.tcx,
)
.answer()
}
}
}
#[cfg(feature = "rustc")]
pub use rustc::*;