1use std::fmt::Debug;
2use std::hash::Hash;
3use std::iter;
4use std::marker::PhantomData;
5
6use rustc_ast_ir::Mutability;
7#[cfg(feature = "nightly")]
8use rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash};
9
10use crate::inherent::*;
11use crate::visit::TypeVisitableExt as _;
12use crate::{self as ty, Interner};
13
14#[derive(#[automatically_derived]
impl<DefId: ::core::clone::Clone> ::core::clone::Clone for
SimplifiedType<DefId> {
#[inline]
fn clone(&self) -> SimplifiedType<DefId> {
match self {
SimplifiedType::Bool => SimplifiedType::Bool,
SimplifiedType::Char => SimplifiedType::Char,
SimplifiedType::Int(__self_0) =>
SimplifiedType::Int(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Uint(__self_0) =>
SimplifiedType::Uint(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Float(__self_0) =>
SimplifiedType::Float(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Adt(__self_0) =>
SimplifiedType::Adt(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Foreign(__self_0) =>
SimplifiedType::Foreign(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Str => SimplifiedType::Str,
SimplifiedType::Array => SimplifiedType::Array,
SimplifiedType::Slice => SimplifiedType::Slice,
SimplifiedType::Ref(__self_0) =>
SimplifiedType::Ref(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Ptr(__self_0) =>
SimplifiedType::Ptr(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Never => SimplifiedType::Never,
SimplifiedType::Tuple(__self_0) =>
SimplifiedType::Tuple(::core::clone::Clone::clone(__self_0)),
SimplifiedType::MarkerTraitObject =>
SimplifiedType::MarkerTraitObject,
SimplifiedType::Trait(__self_0) =>
SimplifiedType::Trait(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Closure(__self_0) =>
SimplifiedType::Closure(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Coroutine(__self_0) =>
SimplifiedType::Coroutine(::core::clone::Clone::clone(__self_0)),
SimplifiedType::CoroutineWitness(__self_0) =>
SimplifiedType::CoroutineWitness(::core::clone::Clone::clone(__self_0)),
SimplifiedType::Function(__self_0) =>
SimplifiedType::Function(::core::clone::Clone::clone(__self_0)),
SimplifiedType::UnsafeBinder => SimplifiedType::UnsafeBinder,
SimplifiedType::Placeholder => SimplifiedType::Placeholder,
SimplifiedType::Error => SimplifiedType::Error,
}
}
}Clone, #[automatically_derived]
impl<DefId: ::core::marker::Copy> ::core::marker::Copy for
SimplifiedType<DefId> {
}Copy, #[automatically_derived]
impl<DefId: ::core::fmt::Debug> ::core::fmt::Debug for SimplifiedType<DefId> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
SimplifiedType::Bool =>
::core::fmt::Formatter::write_str(f, "Bool"),
SimplifiedType::Char =>
::core::fmt::Formatter::write_str(f, "Char"),
SimplifiedType::Int(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Int",
&__self_0),
SimplifiedType::Uint(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Uint",
&__self_0),
SimplifiedType::Float(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Float",
&__self_0),
SimplifiedType::Adt(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Adt",
&__self_0),
SimplifiedType::Foreign(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Foreign", &__self_0),
SimplifiedType::Str =>
::core::fmt::Formatter::write_str(f, "Str"),
SimplifiedType::Array =>
::core::fmt::Formatter::write_str(f, "Array"),
SimplifiedType::Slice =>
::core::fmt::Formatter::write_str(f, "Slice"),
SimplifiedType::Ref(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Ref",
&__self_0),
SimplifiedType::Ptr(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Ptr",
&__self_0),
SimplifiedType::Never =>
::core::fmt::Formatter::write_str(f, "Never"),
SimplifiedType::Tuple(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Tuple",
&__self_0),
SimplifiedType::MarkerTraitObject =>
::core::fmt::Formatter::write_str(f, "MarkerTraitObject"),
SimplifiedType::Trait(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Trait",
&__self_0),
SimplifiedType::Closure(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Closure", &__self_0),
SimplifiedType::Coroutine(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Coroutine", &__self_0),
SimplifiedType::CoroutineWitness(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CoroutineWitness", &__self_0),
SimplifiedType::Function(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Function", &__self_0),
SimplifiedType::UnsafeBinder =>
::core::fmt::Formatter::write_str(f, "UnsafeBinder"),
SimplifiedType::Placeholder =>
::core::fmt::Formatter::write_str(f, "Placeholder"),
SimplifiedType::Error =>
::core::fmt::Formatter::write_str(f, "Error"),
}
}
}Debug, #[automatically_derived]
impl<DefId: ::core::cmp::PartialEq> ::core::cmp::PartialEq for
SimplifiedType<DefId> {
#[inline]
fn eq(&self, other: &SimplifiedType<DefId>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(SimplifiedType::Int(__self_0), SimplifiedType::Int(__arg1_0))
=> __self_0 == __arg1_0,
(SimplifiedType::Uint(__self_0),
SimplifiedType::Uint(__arg1_0)) => __self_0 == __arg1_0,
(SimplifiedType::Float(__self_0),
SimplifiedType::Float(__arg1_0)) => __self_0 == __arg1_0,
(SimplifiedType::Adt(__self_0), SimplifiedType::Adt(__arg1_0))
=> __self_0 == __arg1_0,
(SimplifiedType::Foreign(__self_0),
SimplifiedType::Foreign(__arg1_0)) => __self_0 == __arg1_0,
(SimplifiedType::Ref(__self_0), SimplifiedType::Ref(__arg1_0))
=> __self_0 == __arg1_0,
(SimplifiedType::Ptr(__self_0), SimplifiedType::Ptr(__arg1_0))
=> __self_0 == __arg1_0,
(SimplifiedType::Tuple(__self_0),
SimplifiedType::Tuple(__arg1_0)) => __self_0 == __arg1_0,
(SimplifiedType::Trait(__self_0),
SimplifiedType::Trait(__arg1_0)) => __self_0 == __arg1_0,
(SimplifiedType::Closure(__self_0),
SimplifiedType::Closure(__arg1_0)) => __self_0 == __arg1_0,
(SimplifiedType::Coroutine(__self_0),
SimplifiedType::Coroutine(__arg1_0)) =>
__self_0 == __arg1_0,
(SimplifiedType::CoroutineWitness(__self_0),
SimplifiedType::CoroutineWitness(__arg1_0)) =>
__self_0 == __arg1_0,
(SimplifiedType::Function(__self_0),
SimplifiedType::Function(__arg1_0)) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<DefId: ::core::cmp::Eq> ::core::cmp::Eq for SimplifiedType<DefId> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<ty::IntTy>;
let _: ::core::cmp::AssertParamIsEq<ty::UintTy>;
let _: ::core::cmp::AssertParamIsEq<ty::FloatTy>;
let _: ::core::cmp::AssertParamIsEq<DefId>;
let _: ::core::cmp::AssertParamIsEq<Mutability>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl<DefId: ::core::hash::Hash> ::core::hash::Hash for SimplifiedType<DefId> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
SimplifiedType::Int(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Uint(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Float(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Adt(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Foreign(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Ref(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Ptr(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Tuple(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Trait(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Closure(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Coroutine(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::CoroutineWitness(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
SimplifiedType::Function(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash)]
16#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl<DefId, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for SimplifiedType<DefId> where
DefId: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
SimplifiedType::Bool => { 0usize }
SimplifiedType::Char => { 1usize }
SimplifiedType::Int(ref __binding_0) => { 2usize }
SimplifiedType::Uint(ref __binding_0) => { 3usize }
SimplifiedType::Float(ref __binding_0) => { 4usize }
SimplifiedType::Adt(ref __binding_0) => { 5usize }
SimplifiedType::Foreign(ref __binding_0) => { 6usize }
SimplifiedType::Str => { 7usize }
SimplifiedType::Array => { 8usize }
SimplifiedType::Slice => { 9usize }
SimplifiedType::Ref(ref __binding_0) => { 10usize }
SimplifiedType::Ptr(ref __binding_0) => { 11usize }
SimplifiedType::Never => { 12usize }
SimplifiedType::Tuple(ref __binding_0) => { 13usize }
SimplifiedType::MarkerTraitObject => { 14usize }
SimplifiedType::Trait(ref __binding_0) => { 15usize }
SimplifiedType::Closure(ref __binding_0) => { 16usize }
SimplifiedType::Coroutine(ref __binding_0) => { 17usize }
SimplifiedType::CoroutineWitness(ref __binding_0) => {
18usize
}
SimplifiedType::Function(ref __binding_0) => { 19usize }
SimplifiedType::UnsafeBinder => { 20usize }
SimplifiedType::Placeholder => { 21usize }
SimplifiedType::Error => { 22usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
SimplifiedType::Bool => {}
SimplifiedType::Char => {}
SimplifiedType::Int(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Uint(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Float(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Adt(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Foreign(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Str => {}
SimplifiedType::Array => {}
SimplifiedType::Slice => {}
SimplifiedType::Ref(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Ptr(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Never => {}
SimplifiedType::Tuple(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::MarkerTraitObject => {}
SimplifiedType::Trait(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Closure(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Coroutine(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::CoroutineWitness(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::Function(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
SimplifiedType::UnsafeBinder => {}
SimplifiedType::Placeholder => {}
SimplifiedType::Error => {}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<DefId, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for SimplifiedType<DefId> where
DefId: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { SimplifiedType::Bool }
1usize => { SimplifiedType::Char }
2usize => {
SimplifiedType::Int(::rustc_serialize::Decodable::decode(__decoder))
}
3usize => {
SimplifiedType::Uint(::rustc_serialize::Decodable::decode(__decoder))
}
4usize => {
SimplifiedType::Float(::rustc_serialize::Decodable::decode(__decoder))
}
5usize => {
SimplifiedType::Adt(::rustc_serialize::Decodable::decode(__decoder))
}
6usize => {
SimplifiedType::Foreign(::rustc_serialize::Decodable::decode(__decoder))
}
7usize => { SimplifiedType::Str }
8usize => { SimplifiedType::Array }
9usize => { SimplifiedType::Slice }
10usize => {
SimplifiedType::Ref(::rustc_serialize::Decodable::decode(__decoder))
}
11usize => {
SimplifiedType::Ptr(::rustc_serialize::Decodable::decode(__decoder))
}
12usize => { SimplifiedType::Never }
13usize => {
SimplifiedType::Tuple(::rustc_serialize::Decodable::decode(__decoder))
}
14usize => { SimplifiedType::MarkerTraitObject }
15usize => {
SimplifiedType::Trait(::rustc_serialize::Decodable::decode(__decoder))
}
16usize => {
SimplifiedType::Closure(::rustc_serialize::Decodable::decode(__decoder))
}
17usize => {
SimplifiedType::Coroutine(::rustc_serialize::Decodable::decode(__decoder))
}
18usize => {
SimplifiedType::CoroutineWitness(::rustc_serialize::Decodable::decode(__decoder))
}
19usize => {
SimplifiedType::Function(::rustc_serialize::Decodable::decode(__decoder))
}
20usize => { SimplifiedType::UnsafeBinder }
21usize => { SimplifiedType::Placeholder }
22usize => { SimplifiedType::Error }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `SimplifiedType`, expected 0..23, actual {0}",
n));
}
}
}
}
};Decodable_NoContext, const _: () =
{
impl<DefId> ::rustc_data_structures::stable_hash::StableHash for
SimplifiedType<DefId> where
DefId: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
SimplifiedType::Bool => {}
SimplifiedType::Char => {}
SimplifiedType::Int(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Uint(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Float(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Adt(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Foreign(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Str => {}
SimplifiedType::Array => {}
SimplifiedType::Slice => {}
SimplifiedType::Ref(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Ptr(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Never => {}
SimplifiedType::Tuple(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::MarkerTraitObject => {}
SimplifiedType::Trait(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Closure(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Coroutine(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::CoroutineWitness(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::Function(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
SimplifiedType::UnsafeBinder => {}
SimplifiedType::Placeholder => {}
SimplifiedType::Error => {}
}
}
}
};StableHash))]
17pub enum SimplifiedType<DefId> {
18 Bool,
19 Char,
20 Int(ty::IntTy),
21 Uint(ty::UintTy),
22 Float(ty::FloatTy),
23 Adt(DefId),
24 Foreign(DefId),
25 Str,
26 Array,
27 Slice,
28 Ref(Mutability),
29 Ptr(Mutability),
30 Never,
31 Tuple(usize),
32 MarkerTraitObject,
35 Trait(DefId),
36 Closure(DefId),
37 Coroutine(DefId),
38 CoroutineWitness(DefId),
39 Function(usize),
40 UnsafeBinder,
41 Placeholder,
42 Error,
43}
44
45#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for TreatParams {
#[inline]
fn eq(&self, other: &TreatParams) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for TreatParams {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for TreatParams {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
TreatParams::InstantiateWithInfer => "InstantiateWithInfer",
TreatParams::AsRigid => "AsRigid",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for TreatParams {
#[inline]
fn clone(&self) -> TreatParams { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for TreatParams { }Copy)]
58pub enum TreatParams {
59 InstantiateWithInfer,
62 AsRigid,
71}
72
73pub fn simplify_type<I: Interner>(
95 cx: I,
96 ty: I::Ty,
97 treat_params: TreatParams,
98) -> Option<SimplifiedType<I::DefId>> {
99 match ty.kind() {
100 ty::Bool => Some(SimplifiedType::Bool),
101 ty::Char => Some(SimplifiedType::Char),
102 ty::Int(int_type) => Some(SimplifiedType::Int(int_type)),
103 ty::Uint(uint_type) => Some(SimplifiedType::Uint(uint_type)),
104 ty::Float(float_type) => Some(SimplifiedType::Float(float_type)),
105 ty::Adt(def, _) => Some(SimplifiedType::Adt(def.def_id().into())),
106 ty::Str => Some(SimplifiedType::Str),
107 ty::Array(..) => Some(SimplifiedType::Array),
108 ty::Slice(..) => Some(SimplifiedType::Slice),
109 ty::Pat(ty, ..) => simplify_type(cx, ty, treat_params),
110 ty::RawPtr(_, mutbl) => Some(SimplifiedType::Ptr(mutbl)),
111 ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() {
112 Some(principal_def_id) if !cx.trait_is_auto(principal_def_id) => {
113 Some(SimplifiedType::Trait(principal_def_id.into()))
114 }
115 _ => Some(SimplifiedType::MarkerTraitObject),
116 },
117 ty::Ref(_, _, mutbl) => Some(SimplifiedType::Ref(mutbl)),
118 ty::FnDef(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
119 ty::Closure(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
120 ty::CoroutineClosure(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
121 ty::Coroutine(def_id, _) => Some(SimplifiedType::Coroutine(def_id.into())),
122 ty::CoroutineWitness(def_id, _) => Some(SimplifiedType::CoroutineWitness(def_id.into())),
123 ty::Never => Some(SimplifiedType::Never),
124 ty::Tuple(tys) => Some(SimplifiedType::Tuple(tys.len())),
125 ty::FnPtr(sig_tys, _hdr) => {
126 Some(SimplifiedType::Function(sig_tys.skip_binder().inputs().len()))
127 }
128 ty::UnsafeBinder(_) => Some(SimplifiedType::UnsafeBinder),
129 ty::Placeholder(..) => Some(SimplifiedType::Placeholder),
130 ty::Param(_) => match treat_params {
131 TreatParams::AsRigid => Some(SimplifiedType::Placeholder),
132 TreatParams::InstantiateWithInfer => None,
133 },
134 ty::Alias(..) => match treat_params {
135 TreatParams::AsRigid
138 if !ty.has_non_region_infer() || cx.next_trait_solver_globally() =>
139 {
140 Some(SimplifiedType::Placeholder)
141 }
142 TreatParams::AsRigid | TreatParams::InstantiateWithInfer => None,
143 },
144 ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id.into())),
145 ty::Error(_) => Some(SimplifiedType::Error),
146 ty::Bound(..) | ty::Infer(_) => None,
147 }
148}
149
150impl<DefId> SimplifiedType<DefId> {
151 pub fn def(self) -> Option<DefId> {
152 match self {
153 SimplifiedType::Adt(d)
154 | SimplifiedType::Foreign(d)
155 | SimplifiedType::Trait(d)
156 | SimplifiedType::Closure(d)
157 | SimplifiedType::Coroutine(d)
158 | SimplifiedType::CoroutineWitness(d) => Some(d),
159 _ => None,
160 }
161 }
162}
163
164#[derive(#[automatically_derived]
impl<I: ::core::fmt::Debug + Interner, const INSTANTIATE_LHS_WITH_INFER :
bool, const INSTANTIATE_RHS_WITH_INFER : bool> ::core::fmt::Debug for
DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER, INSTANTIATE_RHS_WITH_INFER>
{
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"DeepRejectCtxt", "_interner", &&self._interner)
}
}Debug, #[automatically_derived]
impl<I: ::core::clone::Clone + Interner, const INSTANTIATE_LHS_WITH_INFER :
bool, const INSTANTIATE_RHS_WITH_INFER : bool> ::core::clone::Clone for
DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER, INSTANTIATE_RHS_WITH_INFER>
{
#[inline]
fn clone(&self)
->
DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER,
INSTANTIATE_RHS_WITH_INFER> {
DeepRejectCtxt {
_interner: ::core::clone::Clone::clone(&self._interner),
}
}
}Clone, #[automatically_derived]
impl<I: ::core::marker::Copy + Interner, const INSTANTIATE_LHS_WITH_INFER :
bool, const INSTANTIATE_RHS_WITH_INFER : bool> ::core::marker::Copy for
DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER, INSTANTIATE_RHS_WITH_INFER>
{
}Copy)]
175pub struct DeepRejectCtxt<
176 I: Interner,
177 const INSTANTIATE_LHS_WITH_INFER: bool,
178 const INSTANTIATE_RHS_WITH_INFER: bool,
179> {
180 _interner: PhantomData<I>,
181}
182
183impl<I: Interner> DeepRejectCtxt<I, false, false> {
184 pub fn relate_rigid_rigid(_interner: I) -> DeepRejectCtxt<I, false, false> {
186 DeepRejectCtxt { _interner: PhantomData }
187 }
188}
189
190impl<I: Interner> DeepRejectCtxt<I, true, true> {
191 pub fn relate_infer_infer(_interner: I) -> DeepRejectCtxt<I, true, true> {
193 DeepRejectCtxt { _interner: PhantomData }
194 }
195}
196
197impl<I: Interner> DeepRejectCtxt<I, false, true> {
198 pub fn relate_rigid_infer(_interner: I) -> DeepRejectCtxt<I, false, true> {
200 DeepRejectCtxt { _interner: PhantomData }
201 }
202}
203
204impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_WITH_INFER: bool>
205 DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER, INSTANTIATE_RHS_WITH_INFER>
206{
207 const STARTING_DEPTH: usize = 8;
210
211 pub fn args_may_unify(
212 self,
213 obligation_args: I::GenericArgs,
214 impl_args: I::GenericArgs,
215 ) -> bool {
216 self.args_may_unify_inner(obligation_args, impl_args, Self::STARTING_DEPTH)
217 }
218
219 pub fn types_may_unify(self, lhs: I::Ty, rhs: I::Ty) -> bool {
220 self.types_may_unify_inner(lhs, rhs, Self::STARTING_DEPTH)
221 }
222
223 pub fn types_may_unify_with_depth(self, lhs: I::Ty, rhs: I::Ty, depth_limit: usize) -> bool {
224 self.types_may_unify_inner(lhs, rhs, depth_limit)
225 }
226
227 fn args_may_unify_inner(
228 self,
229 obligation_args: I::GenericArgs,
230 impl_args: I::GenericArgs,
231 depth: usize,
232 ) -> bool {
233 let may_unify = |(obl, imp): (I::GenericArg, I::GenericArg)| {
237 match (obl.kind(), imp.kind()) {
238 (ty::GenericArgKind::Lifetime(_), ty::GenericArgKind::Lifetime(_)) => true,
240 (ty::GenericArgKind::Type(obl), ty::GenericArgKind::Type(imp)) => {
241 self.types_may_unify_inner(obl, imp, depth)
242 }
243 (ty::GenericArgKind::Const(obl), ty::GenericArgKind::Const(imp)) => {
244 self.consts_may_unify_inner(obl, imp)
245 }
246 _ => {
::core::panicking::panic_fmt(format_args!("kind mismatch: {0:?} {1:?}",
obl, imp));
}panic!("kind mismatch: {obl:?} {imp:?}"),
247 }
248 };
249
250 if let ([obl], [imp]) = (obligation_args.as_slice(), impl_args.as_slice()) {
252 return may_unify((*obl, *imp));
253 }
254
255 iter::zip(obligation_args.iter(), impl_args.iter()).all(may_unify)
256 }
257
258 fn types_may_unify_inner(self, lhs: I::Ty, rhs: I::Ty, depth: usize) -> bool {
259 if lhs == rhs {
260 return true;
261 }
262
263 match rhs.kind() {
264 ty::Param(_) | ty::Alias(ty::IsRigid::Yes, _) => {
267 if INSTANTIATE_RHS_WITH_INFER {
268 return true;
269 }
270 }
271 ty::Error(_) | ty::Alias(ty::IsRigid::No, _) | ty::Bound(..) => return true,
272 ty::Infer(var) => return self.var_and_ty_may_unify(var, lhs),
273
274 ty::Bool
277 | ty::Char
278 | ty::Int(_)
279 | ty::Uint(_)
280 | ty::Float(_)
281 | ty::Adt(..)
282 | ty::Str
283 | ty::Array(..)
284 | ty::Slice(..)
285 | ty::RawPtr(..)
286 | ty::Dynamic(..)
287 | ty::Pat(..)
288 | ty::Ref(..)
289 | ty::Never
290 | ty::Tuple(..)
291 | ty::FnDef(..)
292 | ty::FnPtr(..)
293 | ty::Closure(..)
294 | ty::CoroutineClosure(..)
295 | ty::Coroutine(..)
296 | ty::CoroutineWitness(..)
297 | ty::Foreign(_)
298 | ty::Placeholder(_)
299 | ty::UnsafeBinder(_) => {}
300 };
301
302 let Some(depth) = depth.checked_sub(1) else {
312 return true;
313 };
314
315 match lhs.kind() {
317 ty::Ref(_, lhs_ty, lhs_mutbl) => match rhs.kind() {
318 ty::Ref(_, rhs_ty, rhs_mutbl) => {
319 lhs_mutbl == rhs_mutbl && self.types_may_unify_inner(lhs_ty, rhs_ty, depth)
320 }
321 _ => false,
322 },
323
324 ty::Adt(lhs_def, lhs_args) => match rhs.kind() {
325 ty::Adt(rhs_def, rhs_args) => {
326 lhs_def == rhs_def && self.args_may_unify_inner(lhs_args, rhs_args, depth)
327 }
328 _ => false,
329 },
330
331 ty::Param(lhs) => {
334 INSTANTIATE_LHS_WITH_INFER
335 || match rhs.kind() {
336 ty::Param(rhs) => lhs == rhs,
337 _ => false,
338 }
339 }
340
341 ty::Placeholder(lhs) => {
343 #[allow(non_exhaustive_omitted_patterns)] match rhs.kind() {
ty::Placeholder(rhs) if lhs == rhs => true,
_ => false,
}matches!(rhs.kind(), ty::Placeholder(rhs) if lhs == rhs)
344 }
345
346 ty::Infer(var) => self.var_and_ty_may_unify(var, rhs),
347
348 ty::Alias(ty::IsRigid::Yes, lhs_alias) => {
351 INSTANTIATE_LHS_WITH_INFER
352 || match rhs.kind() {
353 ty::Alias(ty::IsRigid::Yes, rhs_alias) => {
354 lhs_alias.kind == rhs_alias.kind
355 && self.args_may_unify_inner(lhs_alias.args, rhs_alias.args, depth)
356 }
357 _ => false,
358 }
359 }
360 ty::Alias(ty::IsRigid::No, _) => true,
364
365 ty::Int(_)
366 | ty::Uint(_)
367 | ty::Float(_)
368 | ty::Str
369 | ty::Bool
370 | ty::Char
371 | ty::Never
372 | ty::Foreign(_) => lhs == rhs,
373
374 ty::Tuple(lhs) => match rhs.kind() {
375 ty::Tuple(rhs) => {
376 lhs.len() == rhs.len()
377 && iter::zip(lhs.iter(), rhs.iter())
378 .all(|(lhs, rhs)| self.types_may_unify_inner(lhs, rhs, depth))
379 }
380 _ => false,
381 },
382
383 ty::Array(lhs_ty, lhs_len) => match rhs.kind() {
384 ty::Array(rhs_ty, rhs_len) => {
385 self.types_may_unify_inner(lhs_ty, rhs_ty, depth)
386 && self.consts_may_unify_inner(lhs_len, rhs_len)
387 }
388 _ => false,
389 },
390
391 ty::RawPtr(lhs_ty, lhs_mutbl) => match rhs.kind() {
392 ty::RawPtr(rhs_ty, rhs_mutbl) => {
393 lhs_mutbl == rhs_mutbl && self.types_may_unify_inner(lhs_ty, rhs_ty, depth)
394 }
395 _ => false,
396 },
397
398 ty::Slice(lhs_ty) => {
399 #[allow(non_exhaustive_omitted_patterns)] match rhs.kind() {
ty::Slice(rhs_ty) if self.types_may_unify_inner(lhs_ty, rhs_ty, depth) =>
true,
_ => false,
}matches!(rhs.kind(), ty::Slice(rhs_ty) if self.types_may_unify_inner(lhs_ty, rhs_ty, depth))
400 }
401
402 ty::Dynamic(lhs_preds, ..) => {
403 #[allow(non_exhaustive_omitted_patterns)] match rhs.kind() {
ty::Dynamic(rhs_preds, ..) if
lhs_preds.principal_def_id() == rhs_preds.principal_def_id() => true,
_ => false,
}matches!(rhs.kind(), ty::Dynamic(rhs_preds, ..) if
407 lhs_preds.principal_def_id() == rhs_preds.principal_def_id()
408 )
409 }
410
411 ty::FnPtr(lhs_sig_tys, lhs_hdr) => match rhs.kind() {
412 ty::FnPtr(rhs_sig_tys, rhs_hdr) => {
413 let lhs_sig_tys = lhs_sig_tys.skip_binder().inputs_and_output;
414 let rhs_sig_tys = rhs_sig_tys.skip_binder().inputs_and_output;
415
416 lhs_hdr == rhs_hdr
417 && lhs_sig_tys.len() == rhs_sig_tys.len()
418 && iter::zip(lhs_sig_tys.iter(), rhs_sig_tys.iter())
419 .all(|(lhs, rhs)| self.types_may_unify_inner(lhs, rhs, depth))
420 }
421 _ => false,
422 },
423
424 ty::Bound(..) => true,
425
426 ty::FnDef(lhs_def_id, lhs_args) => match rhs.kind() {
427 ty::FnDef(rhs_def_id, rhs_args) => {
428 lhs_def_id == rhs_def_id
429 && self.args_may_unify_inner(
430 lhs_args.no_bound_vars().unwrap(),
431 rhs_args.no_bound_vars().unwrap(),
432 depth,
433 )
434 }
435 _ => false,
436 },
437
438 ty::Closure(lhs_def_id, lhs_args) => match rhs.kind() {
439 ty::Closure(rhs_def_id, rhs_args) => {
440 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
441 }
442 _ => false,
443 },
444
445 ty::CoroutineClosure(lhs_def_id, lhs_args) => match rhs.kind() {
446 ty::CoroutineClosure(rhs_def_id, rhs_args) => {
447 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
448 }
449 _ => false,
450 },
451
452 ty::Coroutine(lhs_def_id, lhs_args) => match rhs.kind() {
453 ty::Coroutine(rhs_def_id, rhs_args) => {
454 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
455 }
456 _ => false,
457 },
458
459 ty::CoroutineWitness(lhs_def_id, lhs_args) => match rhs.kind() {
460 ty::CoroutineWitness(rhs_def_id, rhs_args) => {
461 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
462 }
463 _ => false,
464 },
465
466 ty::Pat(lhs_ty, _) => {
467 #[allow(non_exhaustive_omitted_patterns)] match rhs.kind() {
ty::Pat(rhs_ty, _) if self.types_may_unify_inner(lhs_ty, rhs_ty, depth) =>
true,
_ => false,
}matches!(rhs.kind(), ty::Pat(rhs_ty, _) if self.types_may_unify_inner(lhs_ty, rhs_ty, depth))
469 }
470
471 ty::UnsafeBinder(lhs_ty) => match rhs.kind() {
472 ty::UnsafeBinder(rhs_ty) => {
473 self.types_may_unify(lhs_ty.skip_binder(), rhs_ty.skip_binder())
474 }
475 _ => false,
476 },
477
478 ty::Error(..) => true,
479 }
480 }
481
482 fn consts_may_unify_inner(self, lhs: I::Const, rhs: I::Const) -> bool {
485 match rhs.kind() {
486 ty::ConstKind::Param(_) => {
487 if INSTANTIATE_RHS_WITH_INFER {
488 return true;
489 }
490 }
491
492 ty::ConstKind::Expr(_)
493 | ty::ConstKind::Alias(_, _)
494 | ty::ConstKind::Error(_)
495 | ty::ConstKind::Infer(_)
496 | ty::ConstKind::Bound(..) => {
497 return true;
498 }
499
500 ty::ConstKind::Value(..) | ty::ConstKind::Placeholder(_) => {}
501 };
502
503 match lhs.kind() {
504 ty::ConstKind::Value(lhs_val) => match rhs.kind() {
505 ty::ConstKind::Value(rhs_val) => lhs_val.valtree() == rhs_val.valtree(),
506 _ => false,
507 },
508
509 ty::ConstKind::Param(lhs) => {
510 INSTANTIATE_LHS_WITH_INFER
511 || match rhs.kind() {
512 ty::ConstKind::Param(rhs) => lhs == rhs,
513 _ => false,
514 }
515 }
516
517 ty::ConstKind::Placeholder(lhs) => {
519 #[allow(non_exhaustive_omitted_patterns)] match rhs.kind() {
ty::ConstKind::Placeholder(rhs) if lhs == rhs => true,
_ => false,
}matches!(rhs.kind(), ty::ConstKind::Placeholder(rhs) if lhs == rhs)
520 }
521
522 ty::ConstKind::Expr(_) | ty::ConstKind::Alias(_, _) | ty::ConstKind::Error(_) => true,
525
526 ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) => true,
527 }
528 }
529
530 fn var_and_ty_may_unify(self, var: ty::InferTy, ty: I::Ty) -> bool {
531 if !ty.is_known_rigid() {
532 return true;
533 }
534
535 match var {
536 ty::IntVar(_) => ty.is_integral(),
537 ty::FloatVar(_) => ty.is_floating_point(),
538 _ => true,
539 }
540 }
541}