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_data_structures::fingerprint::Fingerprint;
9#[cfg(feature = "nightly")]
10use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
11#[cfg(feature = "nightly")]
12use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
13
14use crate::inherent::*;
15use crate::visit::TypeVisitableExt as _;
16use crate::{self as ty, Interner};
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
20#[cfg_attr(
21 feature = "nightly",
22 derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
23)]
24pub enum SimplifiedType<DefId> {
25 Bool,
26 Char,
27 Int(ty::IntTy),
28 Uint(ty::UintTy),
29 Float(ty::FloatTy),
30 Adt(DefId),
31 Foreign(DefId),
32 Str,
33 Array,
34 Slice,
35 Ref(Mutability),
36 Ptr(Mutability),
37 Never,
38 Tuple(usize),
39 MarkerTraitObject,
42 Trait(DefId),
43 Closure(DefId),
44 Coroutine(DefId),
45 CoroutineWitness(DefId),
46 Function(usize),
47 UnsafeBinder,
48 Placeholder,
49 Error,
50}
51
52#[cfg(feature = "nightly")]
53impl<HCX: Clone, DefId: HashStable<HCX>> ToStableHashKey<HCX> for SimplifiedType<DefId> {
54 type KeyType = Fingerprint;
55
56 #[inline]
57 fn to_stable_hash_key(&self, hcx: &HCX) -> Fingerprint {
58 let mut hasher = StableHasher::new();
59 let mut hcx: HCX = hcx.clone();
60 self.hash_stable(&mut hcx, &mut hasher);
61 hasher.finish()
62 }
63}
64
65#[derive(PartialEq, Eq, Debug, Clone, Copy)]
78pub enum TreatParams {
79 InstantiateWithInfer,
82 AsRigid,
88}
89
90pub fn simplify_type<I: Interner>(
112 cx: I,
113 ty: I::Ty,
114 treat_params: TreatParams,
115) -> Option<SimplifiedType<I::DefId>> {
116 match ty.kind() {
117 ty::Bool => Some(SimplifiedType::Bool),
118 ty::Char => Some(SimplifiedType::Char),
119 ty::Int(int_type) => Some(SimplifiedType::Int(int_type)),
120 ty::Uint(uint_type) => Some(SimplifiedType::Uint(uint_type)),
121 ty::Float(float_type) => Some(SimplifiedType::Float(float_type)),
122 ty::Adt(def, _) => Some(SimplifiedType::Adt(def.def_id())),
123 ty::Str => Some(SimplifiedType::Str),
124 ty::Array(..) => Some(SimplifiedType::Array),
125 ty::Slice(..) => Some(SimplifiedType::Slice),
126 ty::Pat(ty, ..) => simplify_type(cx, ty, treat_params),
127 ty::RawPtr(_, mutbl) => Some(SimplifiedType::Ptr(mutbl)),
128 ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() {
129 Some(principal_def_id) if !cx.trait_is_auto(principal_def_id) => {
130 Some(SimplifiedType::Trait(principal_def_id))
131 }
132 _ => Some(SimplifiedType::MarkerTraitObject),
133 },
134 ty::Ref(_, _, mutbl) => Some(SimplifiedType::Ref(mutbl)),
135 ty::FnDef(def_id, _) | ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
136 Some(SimplifiedType::Closure(def_id))
137 }
138 ty::Coroutine(def_id, _) => Some(SimplifiedType::Coroutine(def_id)),
139 ty::CoroutineWitness(def_id, _) => Some(SimplifiedType::CoroutineWitness(def_id)),
140 ty::Never => Some(SimplifiedType::Never),
141 ty::Tuple(tys) => Some(SimplifiedType::Tuple(tys.len())),
142 ty::FnPtr(sig_tys, _hdr) => {
143 Some(SimplifiedType::Function(sig_tys.skip_binder().inputs().len()))
144 }
145 ty::UnsafeBinder(_) => Some(SimplifiedType::UnsafeBinder),
146 ty::Placeholder(..) => Some(SimplifiedType::Placeholder),
147 ty::Param(_) => match treat_params {
148 TreatParams::AsRigid => Some(SimplifiedType::Placeholder),
149 TreatParams::InstantiateWithInfer => None,
150 },
151 ty::Alias(..) => match treat_params {
152 TreatParams::AsRigid if !ty.has_non_region_infer() => Some(SimplifiedType::Placeholder),
157 TreatParams::AsRigid | TreatParams::InstantiateWithInfer => None,
158 },
159 ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id)),
160 ty::Error(_) => Some(SimplifiedType::Error),
161 ty::Bound(..) | ty::Infer(_) => None,
162 }
163}
164
165impl<DefId> SimplifiedType<DefId> {
166 pub fn def(self) -> Option<DefId> {
167 match self {
168 SimplifiedType::Adt(d)
169 | SimplifiedType::Foreign(d)
170 | SimplifiedType::Trait(d)
171 | SimplifiedType::Closure(d)
172 | SimplifiedType::Coroutine(d)
173 | SimplifiedType::CoroutineWitness(d) => Some(d),
174 _ => None,
175 }
176 }
177}
178
179#[derive(Debug, Clone, Copy)]
190pub struct DeepRejectCtxt<
191 I: Interner,
192 const INSTANTIATE_LHS_WITH_INFER: bool,
193 const INSTANTIATE_RHS_WITH_INFER: bool,
194> {
195 _interner: PhantomData<I>,
196}
197
198impl<I: Interner> DeepRejectCtxt<I, false, false> {
199 pub fn relate_rigid_rigid(_interner: I) -> DeepRejectCtxt<I, false, false> {
201 DeepRejectCtxt { _interner: PhantomData }
202 }
203}
204
205impl<I: Interner> DeepRejectCtxt<I, true, true> {
206 pub fn relate_infer_infer(_interner: I) -> DeepRejectCtxt<I, true, true> {
208 DeepRejectCtxt { _interner: PhantomData }
209 }
210}
211
212impl<I: Interner> DeepRejectCtxt<I, false, true> {
213 pub fn relate_rigid_infer(_interner: I) -> DeepRejectCtxt<I, false, true> {
215 DeepRejectCtxt { _interner: PhantomData }
216 }
217}
218
219impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_WITH_INFER: bool>
220 DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER, INSTANTIATE_RHS_WITH_INFER>
221{
222 const STARTING_DEPTH: usize = 8;
225
226 pub fn args_may_unify(
227 self,
228 obligation_args: I::GenericArgs,
229 impl_args: I::GenericArgs,
230 ) -> bool {
231 self.args_may_unify_inner(obligation_args, impl_args, Self::STARTING_DEPTH)
232 }
233
234 pub fn types_may_unify(self, lhs: I::Ty, rhs: I::Ty) -> bool {
235 self.types_may_unify_inner(lhs, rhs, Self::STARTING_DEPTH)
236 }
237
238 fn args_may_unify_inner(
239 self,
240 obligation_args: I::GenericArgs,
241 impl_args: I::GenericArgs,
242 depth: usize,
243 ) -> bool {
244 iter::zip(obligation_args.iter(), impl_args.iter()).all(|(obl, imp)| {
248 match (obl.kind(), imp.kind()) {
249 (ty::GenericArgKind::Lifetime(_), ty::GenericArgKind::Lifetime(_)) => true,
251 (ty::GenericArgKind::Type(obl), ty::GenericArgKind::Type(imp)) => {
252 self.types_may_unify_inner(obl, imp, depth)
253 }
254 (ty::GenericArgKind::Const(obl), ty::GenericArgKind::Const(imp)) => {
255 self.consts_may_unify_inner(obl, imp)
256 }
257 _ => panic!("kind mismatch: {obl:?} {imp:?}"),
258 }
259 })
260 }
261
262 fn types_may_unify_inner(self, lhs: I::Ty, rhs: I::Ty, depth: usize) -> bool {
263 match rhs.kind() {
264 ty::Param(_) => {
267 if INSTANTIATE_RHS_WITH_INFER {
268 return true;
269 }
270 }
271 ty::Error(_) | ty::Alias(..) | 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 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(..) => true,
354
355 ty::Int(_)
356 | ty::Uint(_)
357 | ty::Float(_)
358 | ty::Str
359 | ty::Bool
360 | ty::Char
361 | ty::Never
362 | ty::Foreign(_) => lhs == rhs,
363
364 ty::Tuple(lhs) => match rhs.kind() {
365 ty::Tuple(rhs) => {
366 lhs.len() == rhs.len()
367 && iter::zip(lhs.iter(), rhs.iter())
368 .all(|(lhs, rhs)| self.types_may_unify_inner(lhs, rhs, depth))
369 }
370 _ => false,
371 },
372
373 ty::Array(lhs_ty, lhs_len) => match rhs.kind() {
374 ty::Array(rhs_ty, rhs_len) => {
375 self.types_may_unify_inner(lhs_ty, rhs_ty, depth)
376 && self.consts_may_unify_inner(lhs_len, rhs_len)
377 }
378 _ => false,
379 },
380
381 ty::RawPtr(lhs_ty, lhs_mutbl) => match rhs.kind() {
382 ty::RawPtr(rhs_ty, rhs_mutbl) => {
383 lhs_mutbl == rhs_mutbl && self.types_may_unify_inner(lhs_ty, rhs_ty, depth)
384 }
385 _ => false,
386 },
387
388 ty::Slice(lhs_ty) => {
389 matches!(rhs.kind(), ty::Slice(rhs_ty) if self.types_may_unify_inner(lhs_ty, rhs_ty, depth))
390 }
391
392 ty::Dynamic(lhs_preds, ..) => {
393 matches!(rhs.kind(), ty::Dynamic(rhs_preds, ..) if
397 lhs_preds.principal_def_id() == rhs_preds.principal_def_id()
398 )
399 }
400
401 ty::FnPtr(lhs_sig_tys, lhs_hdr) => match rhs.kind() {
402 ty::FnPtr(rhs_sig_tys, rhs_hdr) => {
403 let lhs_sig_tys = lhs_sig_tys.skip_binder().inputs_and_output;
404 let rhs_sig_tys = rhs_sig_tys.skip_binder().inputs_and_output;
405
406 lhs_hdr == rhs_hdr
407 && lhs_sig_tys.len() == rhs_sig_tys.len()
408 && iter::zip(lhs_sig_tys.iter(), rhs_sig_tys.iter())
409 .all(|(lhs, rhs)| self.types_may_unify_inner(lhs, rhs, depth))
410 }
411 _ => false,
412 },
413
414 ty::Bound(..) => true,
415
416 ty::FnDef(lhs_def_id, lhs_args) => match rhs.kind() {
417 ty::FnDef(rhs_def_id, rhs_args) => {
418 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
419 }
420 _ => false,
421 },
422
423 ty::Closure(lhs_def_id, lhs_args) => match rhs.kind() {
424 ty::Closure(rhs_def_id, rhs_args) => {
425 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
426 }
427 _ => false,
428 },
429
430 ty::CoroutineClosure(lhs_def_id, lhs_args) => match rhs.kind() {
431 ty::CoroutineClosure(rhs_def_id, rhs_args) => {
432 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
433 }
434 _ => false,
435 },
436
437 ty::Coroutine(lhs_def_id, lhs_args) => match rhs.kind() {
438 ty::Coroutine(rhs_def_id, rhs_args) => {
439 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
440 }
441 _ => false,
442 },
443
444 ty::CoroutineWitness(lhs_def_id, lhs_args) => match rhs.kind() {
445 ty::CoroutineWitness(rhs_def_id, rhs_args) => {
446 lhs_def_id == rhs_def_id && self.args_may_unify_inner(lhs_args, rhs_args, depth)
447 }
448 _ => false,
449 },
450
451 ty::Pat(lhs_ty, _) => {
452 matches!(rhs.kind(), ty::Pat(rhs_ty, _) if self.types_may_unify_inner(lhs_ty, rhs_ty, depth))
454 }
455
456 ty::UnsafeBinder(lhs_ty) => match rhs.kind() {
457 ty::UnsafeBinder(rhs_ty) => {
458 self.types_may_unify(lhs_ty.skip_binder(), rhs_ty.skip_binder())
459 }
460 _ => false,
461 },
462
463 ty::Error(..) => true,
464 }
465 }
466
467 fn consts_may_unify_inner(self, lhs: I::Const, rhs: I::Const) -> bool {
470 match rhs.kind() {
471 ty::ConstKind::Param(_) => {
472 if INSTANTIATE_RHS_WITH_INFER {
473 return true;
474 }
475 }
476
477 ty::ConstKind::Expr(_)
478 | ty::ConstKind::Unevaluated(_)
479 | ty::ConstKind::Error(_)
480 | ty::ConstKind::Infer(_)
481 | ty::ConstKind::Bound(..) => {
482 return true;
483 }
484
485 ty::ConstKind::Value(..) | ty::ConstKind::Placeholder(_) => {}
486 };
487
488 match lhs.kind() {
489 ty::ConstKind::Value(lhs_val) => match rhs.kind() {
490 ty::ConstKind::Value(rhs_val) => lhs_val.valtree() == rhs_val.valtree(),
491 _ => false,
492 },
493
494 ty::ConstKind::Param(lhs) => {
495 INSTANTIATE_LHS_WITH_INFER
496 || match rhs.kind() {
497 ty::ConstKind::Param(rhs) => lhs == rhs,
498 _ => false,
499 }
500 }
501
502 ty::ConstKind::Placeholder(lhs) => {
504 matches!(rhs.kind(), ty::ConstKind::Placeholder(rhs) if lhs == rhs)
505 }
506
507 ty::ConstKind::Expr(_) | ty::ConstKind::Unevaluated(_) | ty::ConstKind::Error(_) => {
510 true
511 }
512
513 ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) => true,
514 }
515 }
516
517 fn var_and_ty_may_unify(self, var: ty::InferTy, ty: I::Ty) -> bool {
518 if !ty.is_known_rigid() {
519 return true;
520 }
521
522 match var {
523 ty::IntVar(_) => ty.is_integral(),
524 ty::FloatVar(_) => ty.is_floating_point(),
525 _ => true,
526 }
527 }
528}