1#![cfg_attr(bootstrap, feature(never_type))]
11#![cfg_attr(test, feature(test))]
12#![feature(option_into_flat_iter)]
13pub(crate) use rustc_data_structures::fx::{FxIndexMap as Map, FxIndexSet as Set};
16
17pub mod layout;
18mod maybe_transmutable;
19
20#[derive(#[automatically_derived]
impl ::core::marker::Copy for Assume { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for Assume { }
#[automatically_derived]
impl ::core::clone::Clone for Assume {
#[inline]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Assume {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f, "Assume",
"alignment", &self.alignment, "lifetimes", &self.lifetimes,
"safety", &self.safety, "validity", &&self.validity)
}
}Debug, #[automatically_derived]
impl ::core::default::Default for Assume {
#[inline]
fn default() -> Self {
Self {
alignment: ::core::default::Default::default(),
lifetimes: ::core::default::Default::default(),
safety: ::core::default::Default::default(),
validity: ::core::default::Default::default(),
}
}
}Default)]
25pub struct Assume {
26 pub alignment: bool,
27 pub lifetimes: bool,
28 pub safety: bool,
29 pub validity: bool,
30}
31
32#[derive(#[automatically_derived]
impl<R: ::core::fmt::Debug, T: ::core::fmt::Debug> ::core::fmt::Debug for
Answer<R, T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Yes => ::core::fmt::Formatter::write_str(f, "Yes"),
Self::No(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "No",
&__self_0),
Self::If(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "If",
&__self_0),
}
}
}Debug, #[automatically_derived]
impl<R: ::core::hash::Hash, T: ::core::hash::Hash> ::core::hash::Hash for
Answer<R, T> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state);
match self {
Self::No(__self_0) => ::core::hash::Hash::hash(__self_0, state),
Self::If(__self_0) => ::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash, #[automatically_derived]
impl<R: ::core::cmp::Eq, T: ::core::cmp::Eq> ::core::cmp::Eq for Answer<R, T>
{
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Reason<T>>;
let _: ::core::cmp::AssertParamIsEq<Condition<R, T>>;
}
}Eq, #[automatically_derived]
impl<R: ::core::cmp::PartialEq, T: ::core::cmp::PartialEq>
::core::marker::StructuralPartialEq for Answer<R, T> {
}
#[automatically_derived]
impl<R: ::core::cmp::PartialEq, T: ::core::cmp::PartialEq>
::core::cmp::PartialEq for Answer<R, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::No(__self_0), Self::No(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::If(__self_0), Self::If(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<R: ::core::clone::Clone, T: ::core::clone::Clone> ::core::clone::Clone
for Answer<R, T> {
#[inline]
fn clone(&self) -> Self {
match self {
Self::Yes => Self::Yes,
Self::No(__self_0) =>
Self::No(::core::clone::Clone::clone(__self_0)),
Self::If(__self_0) =>
Self::If(::core::clone::Clone::clone(__self_0)),
}
}
}Clone)]
34pub enum Answer<R, T> {
35 Yes,
37 No(Reason<T>),
39 If(Condition<R, T>),
41}
42
43#[derive(#[automatically_derived]
impl<R: ::core::fmt::Debug, T: ::core::fmt::Debug> ::core::fmt::Debug for
Condition<R, T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Transmutable { src: __self_0, dst: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Transmutable", "src", __self_0, "dst", &__self_1),
Self::Outlives { long: __self_0, short: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Outlives", "long", __self_0, "short", &__self_1),
Self::Immutable { ty: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Immutable", "ty", &__self_0),
Self::IfAll(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "IfAll",
&__self_0),
Self::IfAny(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "IfAny",
&__self_0),
}
}
}Debug, #[automatically_derived]
impl<R: ::core::hash::Hash, T: ::core::hash::Hash> ::core::hash::Hash for
Condition<R, T> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state);
match self {
Self::Transmutable { src: __self_0, dst: __self_1 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Self::Outlives { long: __self_0, short: __self_1 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Self::Immutable { ty: __self_0 } =>
::core::hash::Hash::hash(__self_0, state),
Self::IfAll(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Self::IfAny(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
}
}
}Hash, #[automatically_derived]
impl<R: ::core::cmp::Eq, T: ::core::cmp::Eq> ::core::cmp::Eq for
Condition<R, T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<T>;
let _: ::core::cmp::AssertParamIsEq<R>;
let _: ::core::cmp::AssertParamIsEq<Vec<Condition<R, T>>>;
let _: ::core::cmp::AssertParamIsEq<Vec<Condition<R, T>>>;
}
}Eq, #[automatically_derived]
impl<R: ::core::cmp::PartialEq, T: ::core::cmp::PartialEq>
::core::marker::StructuralPartialEq for Condition<R, T> {
}
#[automatically_derived]
impl<R: ::core::cmp::PartialEq, T: ::core::cmp::PartialEq>
::core::cmp::PartialEq for Condition<R, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::Transmutable { src: __self_0, dst: __self_1 },
Self::Transmutable { src: __arg1_0, dst: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(Self::Outlives { long: __self_0, short: __self_1 },
Self::Outlives { long: __arg1_0, short: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(Self::Immutable { ty: __self_0 }, Self::Immutable {
ty: __arg1_0 }) => __self_0 == __arg1_0,
(Self::IfAll(__self_0), Self::IfAll(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::IfAny(__self_0), Self::IfAny(__arg1_0)) =>
__self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]
impl<R: ::core::clone::Clone, T: ::core::clone::Clone> ::core::clone::Clone
for Condition<R, T> {
#[inline]
fn clone(&self) -> Self {
match self {
Self::Transmutable { src: __self_0, dst: __self_1 } =>
Self::Transmutable {
src: ::core::clone::Clone::clone(__self_0),
dst: ::core::clone::Clone::clone(__self_1),
},
Self::Outlives { long: __self_0, short: __self_1 } =>
Self::Outlives {
long: ::core::clone::Clone::clone(__self_0),
short: ::core::clone::Clone::clone(__self_1),
},
Self::Immutable { ty: __self_0 } =>
Self::Immutable { ty: ::core::clone::Clone::clone(__self_0) },
Self::IfAll(__self_0) =>
Self::IfAll(::core::clone::Clone::clone(__self_0)),
Self::IfAny(__self_0) =>
Self::IfAny(::core::clone::Clone::clone(__self_0)),
}
}
}Clone)]
45pub enum Condition<R, T> {
46 Transmutable { src: T, dst: T },
48
49 Outlives { long: R, short: R },
51
52 Immutable { ty: T },
54
55 IfAll(Vec<Condition<R, T>>),
57
58 IfAny(Vec<Condition<R, T>>),
60}
61
62#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for Reason<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::SrcIsNotYetSupported =>
::core::fmt::Formatter::write_str(f, "SrcIsNotYetSupported"),
Self::DstIsNotYetSupported =>
::core::fmt::Formatter::write_str(f, "DstIsNotYetSupported"),
Self::DstIsBitIncompatible =>
::core::fmt::Formatter::write_str(f, "DstIsBitIncompatible"),
Self::DstUninhabited =>
::core::fmt::Formatter::write_str(f, "DstUninhabited"),
Self::DstMayHaveSafetyInvariants =>
::core::fmt::Formatter::write_str(f,
"DstMayHaveSafetyInvariants"),
Self::DstIsTooBig =>
::core::fmt::Formatter::write_str(f, "DstIsTooBig"),
Self::DstRefIsTooBig {
src: __self_0,
src_size: __self_1,
dst: __self_2,
dst_size: __self_3 } =>
::core::fmt::Formatter::debug_struct_field4_finish(f,
"DstRefIsTooBig", "src", __self_0, "src_size", __self_1,
"dst", __self_2, "dst_size", &__self_3),
Self::DstHasStricterAlignment {
src_min_align: __self_0, dst_min_align: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"DstHasStricterAlignment", "src_min_align", __self_0,
"dst_min_align", &__self_1),
Self::DstIsMoreUnique =>
::core::fmt::Formatter::write_str(f, "DstIsMoreUnique"),
Self::TypeError =>
::core::fmt::Formatter::write_str(f, "TypeError"),
Self::SrcLayoutUnknown =>
::core::fmt::Formatter::write_str(f, "SrcLayoutUnknown"),
Self::DstLayoutUnknown =>
::core::fmt::Formatter::write_str(f, "DstLayoutUnknown"),
Self::SrcSizeOverflow =>
::core::fmt::Formatter::write_str(f, "SrcSizeOverflow"),
Self::DstSizeOverflow =>
::core::fmt::Formatter::write_str(f, "DstSizeOverflow"),
}
}
}Debug, #[automatically_derived]
impl<T: ::core::hash::Hash> ::core::hash::Hash for Reason<T> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state);
match self {
Self::DstRefIsTooBig {
src: __self_0,
src_size: __self_1,
dst: __self_2,
dst_size: __self_3 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state);
::core::hash::Hash::hash(__self_2, state);
::core::hash::Hash::hash(__self_3, state)
}
Self::DstHasStricterAlignment {
src_min_align: __self_0, dst_min_align: __self_1 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
_ => {}
}
}
}Hash, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for Reason<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<T>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::marker::StructuralPartialEq for
Reason<T> {
}
#[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for Reason<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::DstRefIsTooBig {
src: __self_0,
src_size: __self_1,
dst: __self_2,
dst_size: __self_3 }, Self::DstRefIsTooBig {
src: __arg1_0,
src_size: __arg1_1,
dst: __arg1_2,
dst_size: __arg1_3 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1 &&
__self_2 == __arg1_2 && __self_3 == __arg1_3,
(Self::DstHasStricterAlignment {
src_min_align: __self_0, dst_min_align: __self_1 },
Self::DstHasStricterAlignment {
src_min_align: __arg1_0, dst_min_align: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<T: ::core::cmp::PartialOrd> ::core::cmp::PartialOrd for Reason<T> {
#[inline]
fn partial_cmp(&self, other: &Self)
-> ::core::option::Option<::core::cmp::Ordering> {
match (self, other) {
(Self::DstRefIsTooBig {
src: __self_0,
src_size: __self_1,
dst: __self_2,
dst_size: __self_3 }, Self::DstRefIsTooBig {
src: __arg1_0,
src_size: __arg1_1,
dst: __arg1_2,
dst_size: __arg1_3 }) =>
match ::core::cmp::PartialOrd::partial_cmp(__self_0, __arg1_0)
{
::core::option::Option::Some(::core::cmp::Ordering::Equal)
=>
match ::core::cmp::PartialOrd::partial_cmp(__self_1,
__arg1_1) {
::core::option::Option::Some(::core::cmp::Ordering::Equal)
=>
match ::core::cmp::PartialOrd::partial_cmp(__self_2,
__arg1_2) {
::core::option::Option::Some(::core::cmp::Ordering::Equal)
=> ::core::cmp::PartialOrd::partial_cmp(__self_3, __arg1_3),
cmp => cmp,
},
cmp => cmp,
},
cmp => cmp,
},
(Self::DstHasStricterAlignment {
src_min_align: __self_0, dst_min_align: __self_1 },
Self::DstHasStricterAlignment {
src_min_align: __arg1_0, dst_min_align: __arg1_1 }) =>
match ::core::cmp::PartialOrd::partial_cmp(__self_0, __arg1_0)
{
::core::option::Option::Some(::core::cmp::Ordering::Equal)
=> ::core::cmp::PartialOrd::partial_cmp(__self_1, __arg1_1),
cmp => cmp,
},
_ =>
::core::cmp::PartialOrd::partial_cmp(&::core::intrinsics::discriminant_value(self),
&::core::intrinsics::discriminant_value(other)),
}
}
}PartialOrd, #[automatically_derived]
impl<T: ::core::cmp::Ord> ::core::cmp::Ord for Reason<T> {
#[inline]
fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
match (self, other) {
(Self::DstRefIsTooBig {
src: __self_0,
src_size: __self_1,
dst: __self_2,
dst_size: __self_3 }, Self::DstRefIsTooBig {
src: __arg1_0,
src_size: __arg1_1,
dst: __arg1_2,
dst_size: __arg1_3 }) =>
match ::core::cmp::Ord::cmp(__self_0, __arg1_0) {
::core::cmp::Ordering::Equal =>
match ::core::cmp::Ord::cmp(__self_1, __arg1_1) {
::core::cmp::Ordering::Equal =>
match ::core::cmp::Ord::cmp(__self_2, __arg1_2) {
::core::cmp::Ordering::Equal =>
::core::cmp::Ord::cmp(__self_3, __arg1_3),
cmp => cmp,
},
cmp => cmp,
},
cmp => cmp,
},
(Self::DstHasStricterAlignment {
src_min_align: __self_0, dst_min_align: __self_1 },
Self::DstHasStricterAlignment {
src_min_align: __arg1_0, dst_min_align: __arg1_1 }) =>
match ::core::cmp::Ord::cmp(__self_0, __arg1_0) {
::core::cmp::Ordering::Equal =>
::core::cmp::Ord::cmp(__self_1, __arg1_1),
cmp => cmp,
},
_ =>
::core::cmp::Ord::cmp(&::core::intrinsics::discriminant_value(self),
&::core::intrinsics::discriminant_value(other)),
}
}
}Ord, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for Reason<T> {
#[inline]
fn clone(&self) -> Self {
match self {
Self::SrcIsNotYetSupported => Self::SrcIsNotYetSupported,
Self::DstIsNotYetSupported => Self::DstIsNotYetSupported,
Self::DstIsBitIncompatible => Self::DstIsBitIncompatible,
Self::DstUninhabited => Self::DstUninhabited,
Self::DstMayHaveSafetyInvariants =>
Self::DstMayHaveSafetyInvariants,
Self::DstIsTooBig => Self::DstIsTooBig,
Self::DstRefIsTooBig {
src: __self_0,
src_size: __self_1,
dst: __self_2,
dst_size: __self_3 } =>
Self::DstRefIsTooBig {
src: ::core::clone::Clone::clone(__self_0),
src_size: ::core::clone::Clone::clone(__self_1),
dst: ::core::clone::Clone::clone(__self_2),
dst_size: ::core::clone::Clone::clone(__self_3),
},
Self::DstHasStricterAlignment {
src_min_align: __self_0, dst_min_align: __self_1 } =>
Self::DstHasStricterAlignment {
src_min_align: ::core::clone::Clone::clone(__self_0),
dst_min_align: ::core::clone::Clone::clone(__self_1),
},
Self::DstIsMoreUnique => Self::DstIsMoreUnique,
Self::TypeError => Self::TypeError,
Self::SrcLayoutUnknown => Self::SrcLayoutUnknown,
Self::DstLayoutUnknown => Self::DstLayoutUnknown,
Self::SrcSizeOverflow => Self::SrcSizeOverflow,
Self::DstSizeOverflow => Self::DstSizeOverflow,
}
}
}Clone)]
64pub enum Reason<T> {
65 SrcIsNotYetSupported,
67 DstIsNotYetSupported,
69 DstIsBitIncompatible,
71 DstUninhabited,
73 DstMayHaveSafetyInvariants,
75 DstIsTooBig,
77 DstRefIsTooBig {
79 src: T,
81 src_size: usize,
83 dst: T,
85 dst_size: usize,
87 },
88 DstHasStricterAlignment { src_min_align: usize, dst_min_align: usize },
90 DstIsMoreUnique,
92 TypeError,
94 SrcLayoutUnknown,
96 DstLayoutUnknown,
98 SrcSizeOverflow,
100 DstSizeOverflow,
102}
103
104#[cfg(feature = "rustc")]
105mod rustc {
106 use rustc_attr_ir::lang_items::LangItem;
107 use rustc_middle::ty::consts::ConstExt;
108 use rustc_middle::ty::{Const, Region, Ty, TyCtxt};
109
110 use super::*;
111
112 pub struct TransmuteTypeEnv<'tcx> {
113 tcx: TyCtxt<'tcx>,
114 }
115
116 impl<'tcx> TransmuteTypeEnv<'tcx> {
117 pub fn new(tcx: TyCtxt<'tcx>) -> Self {
118 Self { tcx }
119 }
120
121 pub fn is_transmutable(
122 &mut self,
123 src: Ty<'tcx>,
124 dst: Ty<'tcx>,
125 assume: crate::Assume,
126 ) -> crate::Answer<Region<'tcx>, Ty<'tcx>> {
127 crate::maybe_transmutable::MaybeTransmutableQuery::new(src, dst, assume, self.tcx)
128 .answer()
129 }
130 }
131
132 impl Assume {
133 pub fn from_const<'tcx>(tcx: TyCtxt<'tcx>, ct: Const<'tcx>) -> Option<Self> {
135 use rustc_middle::ty::ScalarInt;
136 use rustc_span::sym;
137
138 let cv = ct.try_to_value()?;
139 let adt_def = cv.ty.ty_adt_def()?;
140
141 if !tcx.is_lang_item(adt_def.did(), LangItem::TransmuteOpts) {
142 tcx.dcx().delayed_bug(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("The given `const` was not marked with the `{0}` lang item.",
LangItem::TransmuteOpts.name()))
})format!(
143 "The given `const` was not marked with the `{}` lang item.",
144 LangItem::TransmuteOpts.name()
145 ));
146 return Some(Self {
147 alignment: true,
148 lifetimes: true,
149 safety: true,
150 validity: true,
151 });
152 }
153
154 let variant = adt_def.non_enum_variant();
155 let fields = cv.to_branch();
156
157 let get_field = |name| {
158 let (field_idx, _) = variant
159 .fields
160 .iter()
161 .enumerate()
162 .find(|(_, field_def)| name == field_def.name)
163 .unwrap_or_else(|| {
::core::panicking::panic_fmt(format_args!("There were no fields named `{0}`.",
name));
}panic!("There were no fields named `{name}`."));
164 fields[field_idx].try_to_leaf().map(|leaf| leaf == ScalarInt::TRUE)
165 };
166
167 Some(Self {
168 alignment: get_field(sym::alignment)?,
169 lifetimes: get_field(sym::lifetimes)?,
170 safety: get_field(sym::safety)?,
171 validity: get_field(sym::validity)?,
172 })
173 }
174 }
175}
176
177#[cfg(feature = "rustc")]
178pub use rustc::*;