rustc_type_ir/
fast_reject.rs

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/// See `simplify_type`.
19#[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    /// A trait object, all of whose components are markers
40    /// (e.g., `dyn Send + Sync`).
41    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/// Generic parameters are pretty much just bound variables, e.g.
66/// the type of `fn foo<'a, T>(x: &'a T) -> u32 { ... }` can be thought of as
67/// `for<'a, T> fn(&'a T) -> u32`.
68///
69/// Typecheck of `foo` has to succeed for all possible generic arguments, so
70/// during typeck, we have to treat its generic parameters as if they
71/// were placeholders.
72///
73/// But when calling `foo` we only have to provide a specific generic argument.
74/// In that case the generic parameters are instantiated with inference variables.
75/// As we use `simplify_type` before that instantiation happens, we just treat
76/// generic parameters as if they were inference variables in that case.
77#[derive(PartialEq, Eq, Debug, Clone, Copy)]
78pub enum TreatParams {
79    /// Treat parameters as infer vars. This is the correct mode for caching
80    /// an impl's type for lookup.
81    InstantiateWithInfer,
82    /// Treat parameters as placeholders in the given environment. This is the
83    /// correct mode for *lookup*, as during candidate selection.
84    ///
85    /// This also treats projections with inference variables as infer vars
86    /// since they could be further normalized.
87    AsRigid,
88}
89
90/// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
91///
92/// **This function should only be used if you need to store or retrieve the type from some
93/// hashmap. If you want to quickly decide whether two types may unify, use the [DeepRejectCtxt]
94/// instead.**
95///
96/// The idea is to get something simple that we can use to quickly decide if two types could unify,
97/// for example during method lookup. If this function returns `Some(x)` it can only unify with
98/// types for which this method returns either `Some(x)` as well or `None`.
99///
100/// A special case here are parameters and projections, which are only injective
101/// if they are treated as placeholders.
102///
103/// For example when storing impls based on their simplified self type, we treat
104/// generic parameters as if they were inference variables. We must not simplify them here,
105/// as they can unify with any other type.
106///
107/// With projections we have to be even more careful, as treating them as placeholders
108/// is only correct if they are fully normalized.
109///
110/// ¹ meaning that if the outermost layers are different, then the whole types are also different.
111pub 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            // When treating `ty::Param` as a placeholder, projections also
153            // don't unify with anything else as long as they are fully normalized.
154            // FIXME(-Znext-solver): Can remove this `if` and always simplify to `Placeholder`
155            // when the new solver is enabled by default.
156            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/// Given generic arguments, could they be unified after
180/// replacing parameters with inference variables or placeholders.
181/// This behavior is toggled using the const generics.
182///
183/// We use this to quickly reject impl/wc candidates without needing
184/// to instantiate generic arguments/having to enter a probe.
185///
186/// We also use this function during coherence. For coherence the
187/// impls only have to overlap for some value, so we treat parameters
188/// on both sides like inference variables.
189#[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    /// Treat parameters in both the lhs and the rhs as rigid.
200    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    /// Treat parameters in both the lhs and the rhs as infer vars.
207    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    /// Treat parameters in the lhs as rigid, and in rhs as infer vars.
214    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    // Quite arbitrary. Large enough to only affect a very tiny amount of impls/crates
223    // and small enough to prevent hangs.
224    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        // No need to decrement the depth here as this function is only
245        // recursively reachable via `types_may_unify_inner` which already
246        // increments the depth for us.
247        iter::zip(obligation_args.iter(), impl_args.iter()).all(|(obl, imp)| {
248            match (obl.kind(), imp.kind()) {
249                // We don't fast reject based on regions.
250                (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            // Start by checking whether the `rhs` type may unify with
265            // pretty much everything. Just return `true` in that case.
266            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            // These types only unify with inference variables or their own
275            // variant.
276            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        // The type system needs to support exponentially large types
303        // as long as they are self-similar. While most other folders
304        // use caching to handle them, this folder exists purely as a
305        // perf optimization and is incredibly hot. In pretty much all
306        // uses checking the cache is slower than simply recursing, so
307        // we instead just add an arbitrary depth cutoff.
308        //
309        // We only decrement the depth here as the match on `rhs`
310        // does not recurse.
311        let Some(depth) = depth.checked_sub(1) else {
312            return true;
313        };
314
315        // For purely rigid types, use structural equivalence.
316        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            // Depending on the value of const generics, we either treat generic parameters
332            // like placeholders or like inference variables.
333            ty::Param(lhs) => {
334                INSTANTIATE_LHS_WITH_INFER
335                    || match rhs.kind() {
336                        ty::Param(rhs) => lhs == rhs,
337                        _ => false,
338                    }
339            }
340
341            // Placeholder types don't unify with anything on their own.
342            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            // As we're walking the whole type, it may encounter projections
349            // inside of binders and what not, so we're just going to assume that
350            // projections can unify with other stuff.
351            //
352            // Looking forward to lazy normalization this is the safer strategy anyways.
353            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                // Ideally we would walk the existential predicates here or at least
394                // compare their length. But considering that the relevant `Relate` impl
395                // actually sorts and deduplicates these, that doesn't work.
396                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                // FIXME(pattern_types): take pattern into account
453                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    // Unlike `types_may_unify_inner`, this does not take a depth as
468    // we never recurse from this function.
469    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            // Placeholder consts don't unify with anything on their own
503            ty::ConstKind::Placeholder(lhs) => {
504                matches!(rhs.kind(), ty::ConstKind::Placeholder(rhs) if lhs == rhs)
505            }
506
507            // As we don't necessarily eagerly evaluate constants,
508            // they might unify with any value.
509            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}