]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/util.rs
Rollup merge of #106798 - scottmcm:signum-via-cmp, r=Mark-Simulacrum
[rust.git] / compiler / rustc_middle / src / ty / util.rs
1 //! Miscellaneous type-system utilities that are too small to deserve their own modules.
2
3 use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
4 use crate::mir;
5 use crate::ty::layout::IntegerExt;
6 use crate::ty::{
7     self, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
8     TypeVisitable,
9 };
10 use crate::ty::{GenericArgKind, SubstsRef};
11 use rustc_apfloat::Float as _;
12 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
13 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
14 use rustc_errors::ErrorGuaranteed;
15 use rustc_hir as hir;
16 use rustc_hir::def::{CtorOf, DefKind, Res};
17 use rustc_hir::def_id::DefId;
18 use rustc_index::bit_set::GrowableBitSet;
19 use rustc_index::vec::{Idx, IndexVec};
20 use rustc_macros::HashStable;
21 use rustc_span::{sym, DUMMY_SP};
22 use rustc_target::abi::{Integer, IntegerType, Size, TargetDataLayout};
23 use rustc_target::spec::abi::Abi;
24 use smallvec::SmallVec;
25 use std::{fmt, iter};
26
27 #[derive(Copy, Clone, Debug)]
28 pub struct Discr<'tcx> {
29     /// Bit representation of the discriminant (e.g., `-128i8` is `0xFF_u128`).
30     pub val: u128,
31     pub ty: Ty<'tcx>,
32 }
33
34 /// Used as an input to [`TyCtxt::uses_unique_generic_params`].
35 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
36 pub enum IgnoreRegions {
37     Yes,
38     No,
39 }
40
41 #[derive(Copy, Clone, Debug)]
42 pub enum NotUniqueParam<'tcx> {
43     DuplicateParam(ty::GenericArg<'tcx>),
44     NotParam(ty::GenericArg<'tcx>),
45 }
46
47 impl<'tcx> fmt::Display for Discr<'tcx> {
48     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
49         match *self.ty.kind() {
50             ty::Int(ity) => {
51                 let size = ty::tls::with(|tcx| Integer::from_int_ty(&tcx, ity).size());
52                 let x = self.val;
53                 // sign extend the raw representation to be an i128
54                 let x = size.sign_extend(x) as i128;
55                 write!(fmt, "{}", x)
56             }
57             _ => write!(fmt, "{}", self.val),
58         }
59     }
60 }
61
62 fn int_size_and_signed<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (Size, bool) {
63     let (int, signed) = match *ty.kind() {
64         ty::Int(ity) => (Integer::from_int_ty(&tcx, ity), true),
65         ty::Uint(uty) => (Integer::from_uint_ty(&tcx, uty), false),
66         _ => bug!("non integer discriminant"),
67     };
68     (int.size(), signed)
69 }
70
71 impl<'tcx> Discr<'tcx> {
72     /// Adds `1` to the value and wraps around if the maximum for the type is reached.
73     pub fn wrap_incr(self, tcx: TyCtxt<'tcx>) -> Self {
74         self.checked_add(tcx, 1).0
75     }
76     pub fn checked_add(self, tcx: TyCtxt<'tcx>, n: u128) -> (Self, bool) {
77         let (size, signed) = int_size_and_signed(tcx, self.ty);
78         let (val, oflo) = if signed {
79             let min = size.signed_int_min();
80             let max = size.signed_int_max();
81             let val = size.sign_extend(self.val) as i128;
82             assert!(n < (i128::MAX as u128));
83             let n = n as i128;
84             let oflo = val > max - n;
85             let val = if oflo { min + (n - (max - val) - 1) } else { val + n };
86             // zero the upper bits
87             let val = val as u128;
88             let val = size.truncate(val);
89             (val, oflo)
90         } else {
91             let max = size.unsigned_int_max();
92             let val = self.val;
93             let oflo = val > max - n;
94             let val = if oflo { n - (max - val) - 1 } else { val + n };
95             (val, oflo)
96         };
97         (Self { val, ty: self.ty }, oflo)
98     }
99 }
100
101 pub trait IntTypeExt {
102     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
103     fn disr_incr<'tcx>(&self, tcx: TyCtxt<'tcx>, val: Option<Discr<'tcx>>) -> Option<Discr<'tcx>>;
104     fn initial_discriminant<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Discr<'tcx>;
105 }
106
107 impl IntTypeExt for IntegerType {
108     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
109         match self {
110             IntegerType::Pointer(true) => tcx.types.isize,
111             IntegerType::Pointer(false) => tcx.types.usize,
112             IntegerType::Fixed(i, s) => i.to_ty(tcx, *s),
113         }
114     }
115
116     fn initial_discriminant<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Discr<'tcx> {
117         Discr { val: 0, ty: self.to_ty(tcx) }
118     }
119
120     fn disr_incr<'tcx>(&self, tcx: TyCtxt<'tcx>, val: Option<Discr<'tcx>>) -> Option<Discr<'tcx>> {
121         if let Some(val) = val {
122             assert_eq!(self.to_ty(tcx), val.ty);
123             let (new, oflo) = val.checked_add(tcx, 1);
124             if oflo { None } else { Some(new) }
125         } else {
126             Some(self.initial_discriminant(tcx))
127         }
128     }
129 }
130
131 impl<'tcx> TyCtxt<'tcx> {
132     /// Creates a hash of the type `Ty` which will be the same no matter what crate
133     /// context it's calculated within. This is used by the `type_id` intrinsic.
134     pub fn type_id_hash(self, ty: Ty<'tcx>) -> u64 {
135         // We want the type_id be independent of the types free regions, so we
136         // erase them. The erase_regions() call will also anonymize bound
137         // regions, which is desirable too.
138         let ty = self.erase_regions(ty);
139
140         self.with_stable_hashing_context(|mut hcx| {
141             let mut hasher = StableHasher::new();
142             hcx.while_hashing_spans(false, |hcx| ty.hash_stable(hcx, &mut hasher));
143             hasher.finish()
144         })
145     }
146
147     pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
148         match res {
149             Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {
150                 Some(self.parent(self.parent(def_id)))
151             }
152             Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
153                 Some(self.parent(def_id))
154             }
155             // Other `DefKind`s don't have generics and would ICE when calling
156             // `generics_of`.
157             Res::Def(
158                 DefKind::Struct
159                 | DefKind::Union
160                 | DefKind::Enum
161                 | DefKind::Trait
162                 | DefKind::OpaqueTy
163                 | DefKind::TyAlias
164                 | DefKind::ForeignTy
165                 | DefKind::TraitAlias
166                 | DefKind::AssocTy
167                 | DefKind::Fn
168                 | DefKind::AssocFn
169                 | DefKind::AssocConst
170                 | DefKind::Impl,
171                 def_id,
172             ) => Some(def_id),
173             Res::Err => None,
174             _ => None,
175         }
176     }
177
178     pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
179         if let ty::Adt(def, substs) = *ty.kind() {
180             for field in def.all_fields() {
181                 let field_ty = field.ty(self, substs);
182                 if let ty::Error(_) = field_ty.kind() {
183                     return true;
184                 }
185             }
186         }
187         false
188     }
189
190     /// Attempts to returns the deeply last field of nested structures, but
191     /// does not apply any normalization in its search. Returns the same type
192     /// if input `ty` is not a structure at all.
193     pub fn struct_tail_without_normalization(self, ty: Ty<'tcx>) -> Ty<'tcx> {
194         let tcx = self;
195         tcx.struct_tail_with_normalize(ty, |ty| ty, || {})
196     }
197
198     /// Returns the deeply last field of nested structures, or the same type if
199     /// not a structure at all. Corresponds to the only possible unsized field,
200     /// and its type can be used to determine unsizing strategy.
201     ///
202     /// Should only be called if `ty` has no inference variables and does not
203     /// need its lifetimes preserved (e.g. as part of codegen); otherwise
204     /// normalization attempt may cause compiler bugs.
205     pub fn struct_tail_erasing_lifetimes(
206         self,
207         ty: Ty<'tcx>,
208         param_env: ty::ParamEnv<'tcx>,
209     ) -> Ty<'tcx> {
210         let tcx = self;
211         tcx.struct_tail_with_normalize(ty, |ty| tcx.normalize_erasing_regions(param_env, ty), || {})
212     }
213
214     /// Returns the deeply last field of nested structures, or the same type if
215     /// not a structure at all. Corresponds to the only possible unsized field,
216     /// and its type can be used to determine unsizing strategy.
217     ///
218     /// This is parameterized over the normalization strategy (i.e. how to
219     /// handle `<T as Trait>::Assoc` and `impl Trait`); pass the identity
220     /// function to indicate no normalization should take place.
221     ///
222     /// See also `struct_tail_erasing_lifetimes`, which is suitable for use
223     /// during codegen.
224     pub fn struct_tail_with_normalize(
225         self,
226         mut ty: Ty<'tcx>,
227         mut normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
228         // This is currently used to allow us to walk a ValTree
229         // in lockstep with the type in order to get the ValTree branch that
230         // corresponds to an unsized field.
231         mut f: impl FnMut() -> (),
232     ) -> Ty<'tcx> {
233         let recursion_limit = self.recursion_limit();
234         for iteration in 0.. {
235             if !recursion_limit.value_within_limit(iteration) {
236                 return self.ty_error_with_message(
237                     DUMMY_SP,
238                     &format!("reached the recursion limit finding the struct tail for {}", ty),
239                 );
240             }
241             match *ty.kind() {
242                 ty::Adt(def, substs) => {
243                     if !def.is_struct() {
244                         break;
245                     }
246                     match def.non_enum_variant().fields.last() {
247                         Some(field) => {
248                             f();
249                             ty = field.ty(self, substs);
250                         }
251                         None => break,
252                     }
253                 }
254
255                 ty::Tuple(tys) if let Some((&last_ty, _)) = tys.split_last() => {
256                     f();
257                     ty = last_ty;
258                 }
259
260                 ty::Tuple(_) => break,
261
262                 ty::Alias(..) => {
263                     let normalized = normalize(ty);
264                     if ty == normalized {
265                         return ty;
266                     } else {
267                         ty = normalized;
268                     }
269                 }
270
271                 _ => {
272                     break;
273                 }
274             }
275         }
276         ty
277     }
278
279     /// Same as applying `struct_tail` on `source` and `target`, but only
280     /// keeps going as long as the two types are instances of the same
281     /// structure definitions.
282     /// For `(Foo<Foo<T>>, Foo<dyn Trait>)`, the result will be `(Foo<T>, Trait)`,
283     /// whereas struct_tail produces `T`, and `Trait`, respectively.
284     ///
285     /// Should only be called if the types have no inference variables and do
286     /// not need their lifetimes preserved (e.g., as part of codegen); otherwise,
287     /// normalization attempt may cause compiler bugs.
288     pub fn struct_lockstep_tails_erasing_lifetimes(
289         self,
290         source: Ty<'tcx>,
291         target: Ty<'tcx>,
292         param_env: ty::ParamEnv<'tcx>,
293     ) -> (Ty<'tcx>, Ty<'tcx>) {
294         let tcx = self;
295         tcx.struct_lockstep_tails_with_normalize(source, target, |ty| {
296             tcx.normalize_erasing_regions(param_env, ty)
297         })
298     }
299
300     /// Same as applying `struct_tail` on `source` and `target`, but only
301     /// keeps going as long as the two types are instances of the same
302     /// structure definitions.
303     /// For `(Foo<Foo<T>>, Foo<dyn Trait>)`, the result will be `(Foo<T>, Trait)`,
304     /// whereas struct_tail produces `T`, and `Trait`, respectively.
305     ///
306     /// See also `struct_lockstep_tails_erasing_lifetimes`, which is suitable for use
307     /// during codegen.
308     pub fn struct_lockstep_tails_with_normalize(
309         self,
310         source: Ty<'tcx>,
311         target: Ty<'tcx>,
312         normalize: impl Fn(Ty<'tcx>) -> Ty<'tcx>,
313     ) -> (Ty<'tcx>, Ty<'tcx>) {
314         let (mut a, mut b) = (source, target);
315         loop {
316             match (&a.kind(), &b.kind()) {
317                 (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs))
318                     if a_def == b_def && a_def.is_struct() =>
319                 {
320                     if let Some(f) = a_def.non_enum_variant().fields.last() {
321                         a = f.ty(self, a_substs);
322                         b = f.ty(self, b_substs);
323                     } else {
324                         break;
325                     }
326                 }
327                 (&ty::Tuple(a_tys), &ty::Tuple(b_tys)) if a_tys.len() == b_tys.len() => {
328                     if let Some(&a_last) = a_tys.last() {
329                         a = a_last;
330                         b = *b_tys.last().unwrap();
331                     } else {
332                         break;
333                     }
334                 }
335                 (ty::Alias(..), _) | (_, ty::Alias(..)) => {
336                     // If either side is a projection, attempt to
337                     // progress via normalization. (Should be safe to
338                     // apply to both sides as normalization is
339                     // idempotent.)
340                     let a_norm = normalize(a);
341                     let b_norm = normalize(b);
342                     if a == a_norm && b == b_norm {
343                         break;
344                     } else {
345                         a = a_norm;
346                         b = b_norm;
347                     }
348                 }
349
350                 _ => break,
351             }
352         }
353         (a, b)
354     }
355
356     /// Calculate the destructor of a given type.
357     pub fn calculate_dtor(
358         self,
359         adt_did: DefId,
360         validate: impl Fn(Self, DefId) -> Result<(), ErrorGuaranteed>,
361     ) -> Option<ty::Destructor> {
362         let drop_trait = self.lang_items().drop_trait()?;
363         self.ensure().coherent_trait(drop_trait);
364
365         let ty = self.type_of(adt_did);
366         let (did, constness) = self.find_map_relevant_impl(drop_trait, ty, |impl_did| {
367             if let Some(item_id) = self.associated_item_def_ids(impl_did).first() {
368                 if validate(self, impl_did).is_ok() {
369                     return Some((*item_id, self.constness(impl_did)));
370                 }
371             }
372             None
373         })?;
374
375         Some(ty::Destructor { did, constness })
376     }
377
378     /// Returns the set of types that are required to be alive in
379     /// order to run the destructor of `def` (see RFCs 769 and
380     /// 1238).
381     ///
382     /// Note that this returns only the constraints for the
383     /// destructor of `def` itself. For the destructors of the
384     /// contents, you need `adt_dtorck_constraint`.
385     pub fn destructor_constraints(self, def: ty::AdtDef<'tcx>) -> Vec<ty::subst::GenericArg<'tcx>> {
386         let dtor = match def.destructor(self) {
387             None => {
388                 debug!("destructor_constraints({:?}) - no dtor", def.did());
389                 return vec![];
390             }
391             Some(dtor) => dtor.did,
392         };
393
394         let impl_def_id = self.parent(dtor);
395         let impl_generics = self.generics_of(impl_def_id);
396
397         // We have a destructor - all the parameters that are not
398         // pure_wrt_drop (i.e, don't have a #[may_dangle] attribute)
399         // must be live.
400
401         // We need to return the list of parameters from the ADTs
402         // generics/substs that correspond to impure parameters on the
403         // impl's generics. This is a bit ugly, but conceptually simple:
404         //
405         // Suppose our ADT looks like the following
406         //
407         //     struct S<X, Y, Z>(X, Y, Z);
408         //
409         // and the impl is
410         //
411         //     impl<#[may_dangle] P0, P1, P2> Drop for S<P1, P2, P0>
412         //
413         // We want to return the parameters (X, Y). For that, we match
414         // up the item-substs <X, Y, Z> with the substs on the impl ADT,
415         // <P1, P2, P0>, and then look up which of the impl substs refer to
416         // parameters marked as pure.
417
418         let impl_substs = match *self.type_of(impl_def_id).kind() {
419             ty::Adt(def_, substs) if def_ == def => substs,
420             _ => bug!(),
421         };
422
423         let item_substs = match *self.type_of(def.did()).kind() {
424             ty::Adt(def_, substs) if def_ == def => substs,
425             _ => bug!(),
426         };
427
428         let result = iter::zip(item_substs, impl_substs)
429             .filter(|&(_, k)| {
430                 match k.unpack() {
431                     GenericArgKind::Lifetime(region) => match region.kind() {
432                         ty::ReEarlyBound(ref ebr) => {
433                             !impl_generics.region_param(ebr, self).pure_wrt_drop
434                         }
435                         // Error: not a region param
436                         _ => false,
437                     },
438                     GenericArgKind::Type(ty) => match ty.kind() {
439                         ty::Param(ref pt) => !impl_generics.type_param(pt, self).pure_wrt_drop,
440                         // Error: not a type param
441                         _ => false,
442                     },
443                     GenericArgKind::Const(ct) => match ct.kind() {
444                         ty::ConstKind::Param(ref pc) => {
445                             !impl_generics.const_param(pc, self).pure_wrt_drop
446                         }
447                         // Error: not a const param
448                         _ => false,
449                     },
450                 }
451             })
452             .map(|(item_param, _)| item_param)
453             .collect();
454         debug!("destructor_constraint({:?}) = {:?}", def.did(), result);
455         result
456     }
457
458     /// Checks whether each generic argument is simply a unique generic parameter.
459     pub fn uses_unique_generic_params(
460         self,
461         substs: SubstsRef<'tcx>,
462         ignore_regions: IgnoreRegions,
463     ) -> Result<(), NotUniqueParam<'tcx>> {
464         let mut seen = GrowableBitSet::default();
465         for arg in substs {
466             match arg.unpack() {
467                 GenericArgKind::Lifetime(lt) => {
468                     if ignore_regions == IgnoreRegions::No {
469                         let ty::ReEarlyBound(p) = lt.kind() else {
470                             return Err(NotUniqueParam::NotParam(lt.into()))
471                         };
472                         if !seen.insert(p.index) {
473                             return Err(NotUniqueParam::DuplicateParam(lt.into()));
474                         }
475                     }
476                 }
477                 GenericArgKind::Type(t) => match t.kind() {
478                     ty::Param(p) => {
479                         if !seen.insert(p.index) {
480                             return Err(NotUniqueParam::DuplicateParam(t.into()));
481                         }
482                     }
483                     _ => return Err(NotUniqueParam::NotParam(t.into())),
484                 },
485                 GenericArgKind::Const(c) => match c.kind() {
486                     ty::ConstKind::Param(p) => {
487                         if !seen.insert(p.index) {
488                             return Err(NotUniqueParam::DuplicateParam(c.into()));
489                         }
490                     }
491                     _ => return Err(NotUniqueParam::NotParam(c.into())),
492                 },
493             }
494         }
495
496         Ok(())
497     }
498
499     /// Returns `true` if `def_id` refers to a closure (e.g., `|x| x * 2`). Note
500     /// that closures have a `DefId`, but the closure *expression* also
501     /// has a `HirId` that is located within the context where the
502     /// closure appears (and, sadly, a corresponding `NodeId`, since
503     /// those are not yet phased out). The parent of the closure's
504     /// `DefId` will also be the context where it appears.
505     pub fn is_closure(self, def_id: DefId) -> bool {
506         matches!(self.def_kind(def_id), DefKind::Closure | DefKind::Generator)
507     }
508
509     /// Returns `true` if `def_id` refers to a definition that does not have its own
510     /// type-checking context, i.e. closure, generator or inline const.
511     pub fn is_typeck_child(self, def_id: DefId) -> bool {
512         matches!(
513             self.def_kind(def_id),
514             DefKind::Closure | DefKind::Generator | DefKind::InlineConst
515         )
516     }
517
518     /// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
519     pub fn is_trait(self, def_id: DefId) -> bool {
520         self.def_kind(def_id) == DefKind::Trait
521     }
522
523     /// Returns `true` if `def_id` refers to a trait alias (i.e., `trait Foo = ...;`),
524     /// and `false` otherwise.
525     pub fn is_trait_alias(self, def_id: DefId) -> bool {
526         self.def_kind(def_id) == DefKind::TraitAlias
527     }
528
529     /// Returns `true` if this `DefId` refers to the implicit constructor for
530     /// a tuple struct like `struct Foo(u32)`, and `false` otherwise.
531     pub fn is_constructor(self, def_id: DefId) -> bool {
532         matches!(self.def_kind(def_id), DefKind::Ctor(..))
533     }
534
535     /// Given the `DefId`, returns the `DefId` of the innermost item that
536     /// has its own type-checking context or "inference environment".
537     ///
538     /// For example, a closure has its own `DefId`, but it is type-checked
539     /// with the containing item. Similarly, an inline const block has its
540     /// own `DefId` but it is type-checked together with the containing item.
541     ///
542     /// Therefore, when we fetch the
543     /// `typeck` the closure, for example, we really wind up
544     /// fetching the `typeck` the enclosing fn item.
545     pub fn typeck_root_def_id(self, def_id: DefId) -> DefId {
546         let mut def_id = def_id;
547         while self.is_typeck_child(def_id) {
548             def_id = self.parent(def_id);
549         }
550         def_id
551     }
552
553     /// Given the `DefId` and substs a closure, creates the type of
554     /// `self` argument that the closure expects. For example, for a
555     /// `Fn` closure, this would return a reference type `&T` where
556     /// `T = closure_ty`.
557     ///
558     /// Returns `None` if this closure's kind has not yet been inferred.
559     /// This should only be possible during type checking.
560     ///
561     /// Note that the return value is a late-bound region and hence
562     /// wrapped in a binder.
563     pub fn closure_env_ty(
564         self,
565         closure_def_id: DefId,
566         closure_substs: SubstsRef<'tcx>,
567         env_region: ty::RegionKind<'tcx>,
568     ) -> Option<Ty<'tcx>> {
569         let closure_ty = self.mk_closure(closure_def_id, closure_substs);
570         let closure_kind_ty = closure_substs.as_closure().kind_ty();
571         let closure_kind = closure_kind_ty.to_opt_closure_kind()?;
572         let env_ty = match closure_kind {
573             ty::ClosureKind::Fn => self.mk_imm_ref(self.mk_region(env_region), closure_ty),
574             ty::ClosureKind::FnMut => self.mk_mut_ref(self.mk_region(env_region), closure_ty),
575             ty::ClosureKind::FnOnce => closure_ty,
576         };
577         Some(env_ty)
578     }
579
580     /// Returns `true` if the node pointed to by `def_id` is a `static` item.
581     #[inline]
582     pub fn is_static(self, def_id: DefId) -> bool {
583         matches!(self.def_kind(def_id), DefKind::Static(_))
584     }
585
586     #[inline]
587     pub fn static_mutability(self, def_id: DefId) -> Option<hir::Mutability> {
588         if let DefKind::Static(mt) = self.def_kind(def_id) { Some(mt) } else { None }
589     }
590
591     /// Returns `true` if this is a `static` item with the `#[thread_local]` attribute.
592     pub fn is_thread_local_static(self, def_id: DefId) -> bool {
593         self.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
594     }
595
596     /// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
597     #[inline]
598     pub fn is_mutable_static(self, def_id: DefId) -> bool {
599         self.static_mutability(def_id) == Some(hir::Mutability::Mut)
600     }
601
602     /// Get the type of the pointer to the static that we use in MIR.
603     pub fn static_ptr_ty(self, def_id: DefId) -> Ty<'tcx> {
604         // Make sure that any constants in the static's type are evaluated.
605         let static_ty = self.normalize_erasing_regions(ty::ParamEnv::empty(), self.type_of(def_id));
606
607         // Make sure that accesses to unsafe statics end up using raw pointers.
608         // For thread-locals, this needs to be kept in sync with `Rvalue::ty`.
609         if self.is_mutable_static(def_id) {
610             self.mk_mut_ptr(static_ty)
611         } else if self.is_foreign_item(def_id) {
612             self.mk_imm_ptr(static_ty)
613         } else {
614             self.mk_imm_ref(self.lifetimes.re_erased, static_ty)
615         }
616     }
617
618     /// Return the set of types that should be taken into accound when checking
619     /// trait bounds on a generator's internal state.
620     pub fn generator_hidden_types(
621         self,
622         def_id: DefId,
623     ) -> impl Iterator<Item = ty::EarlyBinder<Ty<'tcx>>> {
624         let generator_layout = &self.mir_generator_witnesses(def_id);
625         generator_layout
626             .field_tys
627             .iter()
628             .filter(|decl| !decl.ignore_for_traits)
629             .map(|decl| ty::EarlyBinder(decl.ty))
630     }
631
632     /// Normalizes all opaque types in the given value, replacing them
633     /// with their underlying types.
634     pub fn expand_opaque_types(self, val: Ty<'tcx>) -> Ty<'tcx> {
635         let mut visitor = OpaqueTypeExpander {
636             seen_opaque_tys: FxHashSet::default(),
637             expanded_cache: FxHashMap::default(),
638             primary_def_id: None,
639             found_recursion: false,
640             found_any_recursion: false,
641             check_recursion: false,
642             expand_generators: false,
643             tcx: self,
644         };
645         val.fold_with(&mut visitor)
646     }
647
648     /// Expands the given impl trait type, stopping if the type is recursive.
649     #[instrument(skip(self), level = "debug", ret)]
650     pub fn try_expand_impl_trait_type(
651         self,
652         def_id: DefId,
653         substs: SubstsRef<'tcx>,
654     ) -> Result<Ty<'tcx>, Ty<'tcx>> {
655         let mut visitor = OpaqueTypeExpander {
656             seen_opaque_tys: FxHashSet::default(),
657             expanded_cache: FxHashMap::default(),
658             primary_def_id: Some(def_id),
659             found_recursion: false,
660             found_any_recursion: false,
661             check_recursion: true,
662             expand_generators: true,
663             tcx: self,
664         };
665
666         let expanded_type = visitor.expand_opaque_ty(def_id, substs).unwrap();
667         if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
668     }
669
670     pub fn bound_return_position_impl_trait_in_trait_tys(
671         self,
672         def_id: DefId,
673     ) -> ty::EarlyBinder<Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed>> {
674         ty::EarlyBinder(self.collect_return_position_impl_trait_in_trait_tys(def_id))
675     }
676
677     pub fn bound_explicit_item_bounds(
678         self,
679         def_id: DefId,
680     ) -> ty::EarlyBinder<&'tcx [(ty::Predicate<'tcx>, rustc_span::Span)]> {
681         ty::EarlyBinder(self.explicit_item_bounds(def_id))
682     }
683
684     pub fn bound_impl_subject(self, def_id: DefId) -> ty::EarlyBinder<ty::ImplSubject<'tcx>> {
685         ty::EarlyBinder(self.impl_subject(def_id))
686     }
687
688     /// Returns names of captured upvars for closures and generators.
689     ///
690     /// Here are some examples:
691     ///  - `name__field1__field2` when the upvar is captured by value.
692     ///  - `_ref__name__field` when the upvar is captured by reference.
693     ///
694     /// For generators this only contains upvars that are shared by all states.
695     pub fn closure_saved_names_of_captured_variables(
696         self,
697         def_id: DefId,
698     ) -> SmallVec<[String; 16]> {
699         let body = self.optimized_mir(def_id);
700
701         body.var_debug_info
702             .iter()
703             .filter_map(|var| {
704                 let is_ref = match var.value {
705                     mir::VarDebugInfoContents::Place(place)
706                         if place.local == mir::Local::new(1) =>
707                     {
708                         // The projection is either `[.., Field, Deref]` or `[.., Field]`. It
709                         // implies whether the variable is captured by value or by reference.
710                         matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref)
711                     }
712                     _ => return None,
713                 };
714                 let prefix = if is_ref { "_ref__" } else { "" };
715                 Some(prefix.to_owned() + var.name.as_str())
716             })
717             .collect()
718     }
719
720     // FIXME(eddyb) maybe precompute this? Right now it's computed once
721     // per generator monomorphization, but it doesn't depend on substs.
722     pub fn generator_layout_and_saved_local_names(
723         self,
724         def_id: DefId,
725     ) -> (
726         &'tcx ty::GeneratorLayout<'tcx>,
727         IndexVec<mir::GeneratorSavedLocal, Option<rustc_span::Symbol>>,
728     ) {
729         let tcx = self;
730         let body = tcx.optimized_mir(def_id);
731         let generator_layout = body.generator_layout().unwrap();
732         let mut generator_saved_local_names =
733             IndexVec::from_elem(None, &generator_layout.field_tys);
734
735         let state_arg = mir::Local::new(1);
736         for var in &body.var_debug_info {
737             let mir::VarDebugInfoContents::Place(place) = &var.value else { continue };
738             if place.local != state_arg {
739                 continue;
740             }
741             match place.projection[..] {
742                 [
743                     // Deref of the `Pin<&mut Self>` state argument.
744                     mir::ProjectionElem::Field(..),
745                     mir::ProjectionElem::Deref,
746                     // Field of a variant of the state.
747                     mir::ProjectionElem::Downcast(_, variant),
748                     mir::ProjectionElem::Field(field, _),
749                 ] => {
750                     let name = &mut generator_saved_local_names
751                         [generator_layout.variant_fields[variant][field]];
752                     if name.is_none() {
753                         name.replace(var.name);
754                     }
755                 }
756                 _ => {}
757             }
758         }
759         (generator_layout, generator_saved_local_names)
760     }
761 }
762
763 struct OpaqueTypeExpander<'tcx> {
764     // Contains the DefIds of the opaque types that are currently being
765     // expanded. When we expand an opaque type we insert the DefId of
766     // that type, and when we finish expanding that type we remove the
767     // its DefId.
768     seen_opaque_tys: FxHashSet<DefId>,
769     // Cache of all expansions we've seen so far. This is a critical
770     // optimization for some large types produced by async fn trees.
771     expanded_cache: FxHashMap<(DefId, SubstsRef<'tcx>), Ty<'tcx>>,
772     primary_def_id: Option<DefId>,
773     found_recursion: bool,
774     found_any_recursion: bool,
775     expand_generators: bool,
776     /// Whether or not to check for recursive opaque types.
777     /// This is `true` when we're explicitly checking for opaque type
778     /// recursion, and 'false' otherwise to avoid unnecessary work.
779     check_recursion: bool,
780     tcx: TyCtxt<'tcx>,
781 }
782
783 impl<'tcx> OpaqueTypeExpander<'tcx> {
784     fn expand_opaque_ty(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> Option<Ty<'tcx>> {
785         if self.found_any_recursion {
786             return None;
787         }
788         let substs = substs.fold_with(self);
789         if !self.check_recursion || self.seen_opaque_tys.insert(def_id) {
790             let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) {
791                 Some(expanded_ty) => *expanded_ty,
792                 None => {
793                     let generic_ty = self.tcx.bound_type_of(def_id);
794                     let concrete_ty = generic_ty.subst(self.tcx, substs);
795                     let expanded_ty = self.fold_ty(concrete_ty);
796                     self.expanded_cache.insert((def_id, substs), expanded_ty);
797                     expanded_ty
798                 }
799             };
800             if self.check_recursion {
801                 self.seen_opaque_tys.remove(&def_id);
802             }
803             Some(expanded_ty)
804         } else {
805             // If another opaque type that we contain is recursive, then it
806             // will report the error, so we don't have to.
807             self.found_any_recursion = true;
808             self.found_recursion = def_id == *self.primary_def_id.as_ref().unwrap();
809             None
810         }
811     }
812
813     fn expand_generator(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> Option<Ty<'tcx>> {
814         if self.found_any_recursion {
815             return None;
816         }
817         let substs = substs.fold_with(self);
818         if !self.check_recursion || self.seen_opaque_tys.insert(def_id) {
819             let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) {
820                 Some(expanded_ty) => *expanded_ty,
821                 None => {
822                     for bty in self.tcx.generator_hidden_types(def_id) {
823                         let hidden_ty = bty.subst(self.tcx, substs);
824                         self.fold_ty(hidden_ty);
825                     }
826                     let expanded_ty = self.tcx.mk_generator_witness_mir(def_id, substs);
827                     self.expanded_cache.insert((def_id, substs), expanded_ty);
828                     expanded_ty
829                 }
830             };
831             if self.check_recursion {
832                 self.seen_opaque_tys.remove(&def_id);
833             }
834             Some(expanded_ty)
835         } else {
836             // If another opaque type that we contain is recursive, then it
837             // will report the error, so we don't have to.
838             self.found_any_recursion = true;
839             self.found_recursion = def_id == *self.primary_def_id.as_ref().unwrap();
840             None
841         }
842     }
843 }
844
845 impl<'tcx> TypeFolder<'tcx> for OpaqueTypeExpander<'tcx> {
846     fn tcx(&self) -> TyCtxt<'tcx> {
847         self.tcx
848     }
849
850     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
851         let mut t = if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) = *t.kind() {
852             self.expand_opaque_ty(def_id, substs).unwrap_or(t)
853         } else if t.has_opaque_types() || t.has_generators() {
854             t.super_fold_with(self)
855         } else {
856             t
857         };
858         if self.expand_generators {
859             if let ty::GeneratorWitnessMIR(def_id, substs) = *t.kind() {
860                 t = self.expand_generator(def_id, substs).unwrap_or(t);
861             }
862         }
863         t
864     }
865 }
866
867 impl<'tcx> Ty<'tcx> {
868     /// Returns the maximum value for the given numeric type (including `char`s)
869     /// or returns `None` if the type is not numeric.
870     pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<ty::Const<'tcx>> {
871         let val = match self.kind() {
872             ty::Int(_) | ty::Uint(_) => {
873                 let (size, signed) = int_size_and_signed(tcx, self);
874                 let val =
875                     if signed { size.signed_int_max() as u128 } else { size.unsigned_int_max() };
876                 Some(val)
877             }
878             ty::Char => Some(std::char::MAX as u128),
879             ty::Float(fty) => Some(match fty {
880                 ty::FloatTy::F32 => rustc_apfloat::ieee::Single::INFINITY.to_bits(),
881                 ty::FloatTy::F64 => rustc_apfloat::ieee::Double::INFINITY.to_bits(),
882             }),
883             _ => None,
884         };
885
886         val.map(|v| ty::Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
887     }
888
889     /// Returns the minimum value for the given numeric type (including `char`s)
890     /// or returns `None` if the type is not numeric.
891     pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<ty::Const<'tcx>> {
892         let val = match self.kind() {
893             ty::Int(_) | ty::Uint(_) => {
894                 let (size, signed) = int_size_and_signed(tcx, self);
895                 let val = if signed { size.truncate(size.signed_int_min() as u128) } else { 0 };
896                 Some(val)
897             }
898             ty::Char => Some(0),
899             ty::Float(fty) => Some(match fty {
900                 ty::FloatTy::F32 => (-::rustc_apfloat::ieee::Single::INFINITY).to_bits(),
901                 ty::FloatTy::F64 => (-::rustc_apfloat::ieee::Double::INFINITY).to_bits(),
902             }),
903             _ => None,
904         };
905
906         val.map(|v| ty::Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
907     }
908
909     /// Checks whether values of this type `T` are *moved* or *copied*
910     /// when referenced -- this amounts to a check for whether `T:
911     /// Copy`, but note that we **don't** consider lifetimes when
912     /// doing this check. This means that we may generate MIR which
913     /// does copies even when the type actually doesn't satisfy the
914     /// full requirements for the `Copy` trait (cc #29149) -- this
915     /// winds up being reported as an error during NLL borrow check.
916     pub fn is_copy_modulo_regions(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
917         self.is_trivially_pure_clone_copy() || tcx.is_copy_raw(param_env.and(self))
918     }
919
920     /// Checks whether values of this type `T` have a size known at
921     /// compile time (i.e., whether `T: Sized`). Lifetimes are ignored
922     /// for the purposes of this check, so it can be an
923     /// over-approximation in generic contexts, where one can have
924     /// strange rules like `<T as Foo<'static>>::Bar: Sized` that
925     /// actually carry lifetime requirements.
926     pub fn is_sized(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
927         self.is_trivially_sized(tcx) || tcx.is_sized_raw(param_env.and(self))
928     }
929
930     /// Checks whether values of this type `T` implement the `Freeze`
931     /// trait -- frozen types are those that do not contain an
932     /// `UnsafeCell` anywhere. This is a language concept used to
933     /// distinguish "true immutability", which is relevant to
934     /// optimization as well as the rules around static values. Note
935     /// that the `Freeze` trait is not exposed to end users and is
936     /// effectively an implementation detail.
937     pub fn is_freeze(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
938         self.is_trivially_freeze() || tcx.is_freeze_raw(param_env.and(self))
939     }
940
941     /// Fast path helper for testing if a type is `Freeze`.
942     ///
943     /// Returning true means the type is known to be `Freeze`. Returning
944     /// `false` means nothing -- could be `Freeze`, might not be.
945     fn is_trivially_freeze(self) -> bool {
946         match self.kind() {
947             ty::Int(_)
948             | ty::Uint(_)
949             | ty::Float(_)
950             | ty::Bool
951             | ty::Char
952             | ty::Str
953             | ty::Never
954             | ty::Ref(..)
955             | ty::RawPtr(_)
956             | ty::FnDef(..)
957             | ty::Error(_)
958             | ty::FnPtr(_) => true,
959             ty::Tuple(fields) => fields.iter().all(Self::is_trivially_freeze),
960             ty::Slice(elem_ty) | ty::Array(elem_ty, _) => elem_ty.is_trivially_freeze(),
961             ty::Adt(..)
962             | ty::Bound(..)
963             | ty::Closure(..)
964             | ty::Dynamic(..)
965             | ty::Foreign(_)
966             | ty::Generator(..)
967             | ty::GeneratorWitness(_)
968             | ty::GeneratorWitnessMIR(..)
969             | ty::Infer(_)
970             | ty::Alias(..)
971             | ty::Param(_)
972             | ty::Placeholder(_) => false,
973         }
974     }
975
976     /// Checks whether values of this type `T` implement the `Unpin` trait.
977     pub fn is_unpin(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
978         self.is_trivially_unpin() || tcx.is_unpin_raw(param_env.and(self))
979     }
980
981     /// Fast path helper for testing if a type is `Unpin`.
982     ///
983     /// Returning true means the type is known to be `Unpin`. Returning
984     /// `false` means nothing -- could be `Unpin`, might not be.
985     fn is_trivially_unpin(self) -> bool {
986         match self.kind() {
987             ty::Int(_)
988             | ty::Uint(_)
989             | ty::Float(_)
990             | ty::Bool
991             | ty::Char
992             | ty::Str
993             | ty::Never
994             | ty::Ref(..)
995             | ty::RawPtr(_)
996             | ty::FnDef(..)
997             | ty::Error(_)
998             | ty::FnPtr(_) => true,
999             ty::Tuple(fields) => fields.iter().all(Self::is_trivially_unpin),
1000             ty::Slice(elem_ty) | ty::Array(elem_ty, _) => elem_ty.is_trivially_unpin(),
1001             ty::Adt(..)
1002             | ty::Bound(..)
1003             | ty::Closure(..)
1004             | ty::Dynamic(..)
1005             | ty::Foreign(_)
1006             | ty::Generator(..)
1007             | ty::GeneratorWitness(_)
1008             | ty::GeneratorWitnessMIR(..)
1009             | ty::Infer(_)
1010             | ty::Alias(..)
1011             | ty::Param(_)
1012             | ty::Placeholder(_) => false,
1013         }
1014     }
1015
1016     /// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely
1017     /// non-copy and *might* have a destructor attached; if it returns
1018     /// `false`, then `ty` definitely has no destructor (i.e., no drop glue).
1019     ///
1020     /// (Note that this implies that if `ty` has a destructor attached,
1021     /// then `needs_drop` will definitely return `true` for `ty`.)
1022     ///
1023     /// Note that this method is used to check eligible types in unions.
1024     #[inline]
1025     pub fn needs_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
1026         // Avoid querying in simple cases.
1027         match needs_drop_components(self, &tcx.data_layout) {
1028             Err(AlwaysRequiresDrop) => true,
1029             Ok(components) => {
1030                 let query_ty = match *components {
1031                     [] => return false,
1032                     // If we've got a single component, call the query with that
1033                     // to increase the chance that we hit the query cache.
1034                     [component_ty] => component_ty,
1035                     _ => self,
1036                 };
1037
1038                 // This doesn't depend on regions, so try to minimize distinct
1039                 // query keys used.
1040                 // If normalization fails, we just use `query_ty`.
1041                 let query_ty =
1042                     tcx.try_normalize_erasing_regions(param_env, query_ty).unwrap_or(query_ty);
1043
1044                 tcx.needs_drop_raw(param_env.and(query_ty))
1045             }
1046         }
1047     }
1048
1049     /// Checks if `ty` has a significant drop.
1050     ///
1051     /// Note that this method can return false even if `ty` has a destructor
1052     /// attached; even if that is the case then the adt has been marked with
1053     /// the attribute `rustc_insignificant_dtor`.
1054     ///
1055     /// Note that this method is used to check for change in drop order for
1056     /// 2229 drop reorder migration analysis.
1057     #[inline]
1058     pub fn has_significant_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
1059         // Avoid querying in simple cases.
1060         match needs_drop_components(self, &tcx.data_layout) {
1061             Err(AlwaysRequiresDrop) => true,
1062             Ok(components) => {
1063                 let query_ty = match *components {
1064                     [] => return false,
1065                     // If we've got a single component, call the query with that
1066                     // to increase the chance that we hit the query cache.
1067                     [component_ty] => component_ty,
1068                     _ => self,
1069                 };
1070
1071                 // FIXME(#86868): We should be canonicalizing, or else moving this to a method of inference
1072                 // context, or *something* like that, but for now just avoid passing inference
1073                 // variables to queries that can't cope with them. Instead, conservatively
1074                 // return "true" (may change drop order).
1075                 if query_ty.needs_infer() {
1076                     return true;
1077                 }
1078
1079                 // This doesn't depend on regions, so try to minimize distinct
1080                 // query keys used.
1081                 let erased = tcx.normalize_erasing_regions(param_env, query_ty);
1082                 tcx.has_significant_drop_raw(param_env.and(erased))
1083             }
1084         }
1085     }
1086
1087     /// Returns `true` if equality for this type is both reflexive and structural.
1088     ///
1089     /// Reflexive equality for a type is indicated by an `Eq` impl for that type.
1090     ///
1091     /// Primitive types (`u32`, `str`) have structural equality by definition. For composite data
1092     /// types, equality for the type as a whole is structural when it is the same as equality
1093     /// between all components (fields, array elements, etc.) of that type. For ADTs, structural
1094     /// equality is indicated by an implementation of `PartialStructuralEq` and `StructuralEq` for
1095     /// that type.
1096     ///
1097     /// This function is "shallow" because it may return `true` for a composite type whose fields
1098     /// are not `StructuralEq`. For example, `[T; 4]` has structural equality regardless of `T`
1099     /// because equality for arrays is determined by the equality of each array element. If you
1100     /// want to know whether a given call to `PartialEq::eq` will proceed structurally all the way
1101     /// down, you will need to use a type visitor.
1102     #[inline]
1103     pub fn is_structural_eq_shallow(self, tcx: TyCtxt<'tcx>) -> bool {
1104         match self.kind() {
1105             // Look for an impl of both `PartialStructuralEq` and `StructuralEq`.
1106             ty::Adt(..) => tcx.has_structural_eq_impls(self),
1107
1108             // Primitive types that satisfy `Eq`.
1109             ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str | ty::Never => true,
1110
1111             // Composite types that satisfy `Eq` when all of their fields do.
1112             //
1113             // Because this function is "shallow", we return `true` for these composites regardless
1114             // of the type(s) contained within.
1115             ty::Ref(..) | ty::Array(..) | ty::Slice(_) | ty::Tuple(..) => true,
1116
1117             // Raw pointers use bitwise comparison.
1118             ty::RawPtr(_) | ty::FnPtr(_) => true,
1119
1120             // Floating point numbers are not `Eq`.
1121             ty::Float(_) => false,
1122
1123             // Conservatively return `false` for all others...
1124
1125             // Anonymous function types
1126             ty::FnDef(..) | ty::Closure(..) | ty::Dynamic(..) | ty::Generator(..) => false,
1127
1128             // Generic or inferred types
1129             //
1130             // FIXME(ecstaticmorse): Maybe we should `bug` here? This should probably only be
1131             // called for known, fully-monomorphized types.
1132             ty::Alias(..) | ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) => {
1133                 false
1134             }
1135
1136             ty::Foreign(_)
1137             | ty::GeneratorWitness(..)
1138             | ty::GeneratorWitnessMIR(..)
1139             | ty::Error(_) => false,
1140         }
1141     }
1142
1143     /// Peel off all reference types in this type until there are none left.
1144     ///
1145     /// This method is idempotent, i.e. `ty.peel_refs().peel_refs() == ty.peel_refs()`.
1146     ///
1147     /// # Examples
1148     ///
1149     /// - `u8` -> `u8`
1150     /// - `&'a mut u8` -> `u8`
1151     /// - `&'a &'b u8` -> `u8`
1152     /// - `&'a *const &'b u8 -> *const &'b u8`
1153     pub fn peel_refs(self) -> Ty<'tcx> {
1154         let mut ty = self;
1155         while let ty::Ref(_, inner_ty, _) = ty.kind() {
1156             ty = *inner_ty;
1157         }
1158         ty
1159     }
1160
1161     #[inline]
1162     pub fn outer_exclusive_binder(self) -> ty::DebruijnIndex {
1163         self.0.outer_exclusive_binder
1164     }
1165 }
1166
1167 pub enum ExplicitSelf<'tcx> {
1168     ByValue,
1169     ByReference(ty::Region<'tcx>, hir::Mutability),
1170     ByRawPointer(hir::Mutability),
1171     ByBox,
1172     Other,
1173 }
1174
1175 impl<'tcx> ExplicitSelf<'tcx> {
1176     /// Categorizes an explicit self declaration like `self: SomeType`
1177     /// into either `self`, `&self`, `&mut self`, `Box<self>`, or
1178     /// `Other`.
1179     /// This is mainly used to require the arbitrary_self_types feature
1180     /// in the case of `Other`, to improve error messages in the common cases,
1181     /// and to make `Other` non-object-safe.
1182     ///
1183     /// Examples:
1184     ///
1185     /// ```ignore (illustrative)
1186     /// impl<'a> Foo for &'a T {
1187     ///     // Legal declarations:
1188     ///     fn method1(self: &&'a T); // ExplicitSelf::ByReference
1189     ///     fn method2(self: &'a T); // ExplicitSelf::ByValue
1190     ///     fn method3(self: Box<&'a T>); // ExplicitSelf::ByBox
1191     ///     fn method4(self: Rc<&'a T>); // ExplicitSelf::Other
1192     ///
1193     ///     // Invalid cases will be caught by `check_method_receiver`:
1194     ///     fn method_err1(self: &'a mut T); // ExplicitSelf::Other
1195     ///     fn method_err2(self: &'static T) // ExplicitSelf::ByValue
1196     ///     fn method_err3(self: &&T) // ExplicitSelf::ByReference
1197     /// }
1198     /// ```
1199     ///
1200     pub fn determine<P>(self_arg_ty: Ty<'tcx>, is_self_ty: P) -> ExplicitSelf<'tcx>
1201     where
1202         P: Fn(Ty<'tcx>) -> bool,
1203     {
1204         use self::ExplicitSelf::*;
1205
1206         match *self_arg_ty.kind() {
1207             _ if is_self_ty(self_arg_ty) => ByValue,
1208             ty::Ref(region, ty, mutbl) if is_self_ty(ty) => ByReference(region, mutbl),
1209             ty::RawPtr(ty::TypeAndMut { ty, mutbl }) if is_self_ty(ty) => ByRawPointer(mutbl),
1210             ty::Adt(def, _) if def.is_box() && is_self_ty(self_arg_ty.boxed_ty()) => ByBox,
1211             _ => Other,
1212         }
1213     }
1214 }
1215
1216 /// Returns a list of types such that the given type needs drop if and only if
1217 /// *any* of the returned types need drop. Returns `Err(AlwaysRequiresDrop)` if
1218 /// this type always needs drop.
1219 pub fn needs_drop_components<'tcx>(
1220     ty: Ty<'tcx>,
1221     target_layout: &TargetDataLayout,
1222 ) -> Result<SmallVec<[Ty<'tcx>; 2]>, AlwaysRequiresDrop> {
1223     match ty.kind() {
1224         ty::Infer(ty::FreshIntTy(_))
1225         | ty::Infer(ty::FreshFloatTy(_))
1226         | ty::Bool
1227         | ty::Int(_)
1228         | ty::Uint(_)
1229         | ty::Float(_)
1230         | ty::Never
1231         | ty::FnDef(..)
1232         | ty::FnPtr(_)
1233         | ty::Char
1234         | ty::GeneratorWitness(..)
1235         | ty::GeneratorWitnessMIR(..)
1236         | ty::RawPtr(_)
1237         | ty::Ref(..)
1238         | ty::Str => Ok(SmallVec::new()),
1239
1240         // Foreign types can never have destructors.
1241         ty::Foreign(..) => Ok(SmallVec::new()),
1242
1243         ty::Dynamic(..) | ty::Error(_) => Err(AlwaysRequiresDrop),
1244
1245         ty::Slice(ty) => needs_drop_components(*ty, target_layout),
1246         ty::Array(elem_ty, size) => {
1247             match needs_drop_components(*elem_ty, target_layout) {
1248                 Ok(v) if v.is_empty() => Ok(v),
1249                 res => match size.kind().try_to_bits(target_layout.pointer_size) {
1250                     // Arrays of size zero don't need drop, even if their element
1251                     // type does.
1252                     Some(0) => Ok(SmallVec::new()),
1253                     Some(_) => res,
1254                     // We don't know which of the cases above we are in, so
1255                     // return the whole type and let the caller decide what to
1256                     // do.
1257                     None => Ok(smallvec![ty]),
1258                 },
1259             }
1260         }
1261         // If any field needs drop, then the whole tuple does.
1262         ty::Tuple(fields) => fields.iter().try_fold(SmallVec::new(), move |mut acc, elem| {
1263             acc.extend(needs_drop_components(elem, target_layout)?);
1264             Ok(acc)
1265         }),
1266
1267         // These require checking for `Copy` bounds or `Adt` destructors.
1268         ty::Adt(..)
1269         | ty::Alias(..)
1270         | ty::Param(_)
1271         | ty::Bound(..)
1272         | ty::Placeholder(..)
1273         | ty::Infer(_)
1274         | ty::Closure(..)
1275         | ty::Generator(..) => Ok(smallvec![ty]),
1276     }
1277 }
1278
1279 pub fn is_trivially_const_drop(ty: Ty<'_>) -> bool {
1280     match *ty.kind() {
1281         ty::Bool
1282         | ty::Char
1283         | ty::Int(_)
1284         | ty::Uint(_)
1285         | ty::Float(_)
1286         | ty::Infer(ty::IntVar(_))
1287         | ty::Infer(ty::FloatVar(_))
1288         | ty::Str
1289         | ty::RawPtr(_)
1290         | ty::Ref(..)
1291         | ty::FnDef(..)
1292         | ty::FnPtr(_)
1293         | ty::Never
1294         | ty::Foreign(_) => true,
1295
1296         ty::Alias(..)
1297         | ty::Dynamic(..)
1298         | ty::Error(_)
1299         | ty::Bound(..)
1300         | ty::Param(_)
1301         | ty::Placeholder(_)
1302         | ty::Infer(_) => false,
1303
1304         // Not trivial because they have components, and instead of looking inside,
1305         // we'll just perform trait selection.
1306         ty::Closure(..)
1307         | ty::Generator(..)
1308         | ty::GeneratorWitness(_)
1309         | ty::GeneratorWitnessMIR(..)
1310         | ty::Adt(..) => false,
1311
1312         ty::Array(ty, _) | ty::Slice(ty) => is_trivially_const_drop(ty),
1313
1314         ty::Tuple(tys) => tys.iter().all(|ty| is_trivially_const_drop(ty)),
1315     }
1316 }
1317
1318 /// Does the equivalent of
1319 /// ```ignore (ilustrative)
1320 /// let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1321 /// folder.tcx().intern_*(&v)
1322 /// ```
1323 pub fn fold_list<'tcx, F, T>(
1324     list: &'tcx ty::List<T>,
1325     folder: &mut F,
1326     intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
1327 ) -> Result<&'tcx ty::List<T>, F::Error>
1328 where
1329     F: FallibleTypeFolder<'tcx>,
1330     T: TypeFoldable<'tcx> + PartialEq + Copy,
1331 {
1332     let mut iter = list.iter();
1333     // Look for the first element that changed
1334     match iter.by_ref().enumerate().find_map(|(i, t)| match t.try_fold_with(folder) {
1335         Ok(new_t) if new_t == t => None,
1336         new_t => Some((i, new_t)),
1337     }) {
1338         Some((i, Ok(new_t))) => {
1339             // An element changed, prepare to intern the resulting list
1340             let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
1341             new_list.extend_from_slice(&list[..i]);
1342             new_list.push(new_t);
1343             for t in iter {
1344                 new_list.push(t.try_fold_with(folder)?)
1345             }
1346             Ok(intern(folder.tcx(), &new_list))
1347         }
1348         Some((_, Err(err))) => {
1349             return Err(err);
1350         }
1351         None => Ok(list),
1352     }
1353 }
1354
1355 #[derive(Copy, Clone, Debug, HashStable, TyEncodable, TyDecodable)]
1356 pub struct AlwaysRequiresDrop;
1357
1358 /// Reveals all opaque types in the given value, replacing them
1359 /// with their underlying types.
1360 pub fn reveal_opaque_types_in_bounds<'tcx>(
1361     tcx: TyCtxt<'tcx>,
1362     val: &'tcx ty::List<ty::Predicate<'tcx>>,
1363 ) -> &'tcx ty::List<ty::Predicate<'tcx>> {
1364     let mut visitor = OpaqueTypeExpander {
1365         seen_opaque_tys: FxHashSet::default(),
1366         expanded_cache: FxHashMap::default(),
1367         primary_def_id: None,
1368         found_recursion: false,
1369         found_any_recursion: false,
1370         check_recursion: false,
1371         expand_generators: false,
1372         tcx,
1373     };
1374     val.fold_with(&mut visitor)
1375 }
1376
1377 /// Determines whether an item is annotated with `doc(hidden)`.
1378 fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1379     assert!(def_id.is_local());
1380     tcx.get_attrs(def_id, sym::doc)
1381         .filter_map(|attr| attr.meta_item_list())
1382         .any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
1383 }
1384
1385 /// Determines whether an item is annotated with `doc(notable_trait)`.
1386 pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1387     tcx.get_attrs(def_id, sym::doc)
1388         .filter_map(|attr| attr.meta_item_list())
1389         .any(|items| items.iter().any(|item| item.has_name(sym::notable_trait)))
1390 }
1391
1392 /// Determines whether an item is an intrinsic by Abi.
1393 pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1394     matches!(tcx.fn_sig(def_id).skip_binder().abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic)
1395 }
1396
1397 pub fn provide(providers: &mut ty::query::Providers) {
1398     *providers = ty::query::Providers {
1399         reveal_opaque_types_in_bounds,
1400         is_doc_hidden,
1401         is_doc_notable_trait,
1402         is_intrinsic,
1403         ..*providers
1404     }
1405 }