]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/util.rs
Rollup merge of #68288 - RalfJung:fmt, r=oli-obk
[rust.git] / src / librustc / ty / util.rs
1 //! Miscellaneous type-system utilities that are too small to deserve their own modules.
2
3 use crate::hir::map::DefPathData;
4 use crate::ich::NodeIdHashingMode;
5 use crate::mir::interpret::{sign_extend, truncate};
6 use crate::ty::layout::{Integer, IntegerExt, Size};
7 use crate::ty::query::TyCtxtAt;
8 use crate::ty::subst::{GenericArgKind, InternalSubsts, Subst, SubstsRef};
9 use crate::ty::TyKind::*;
10 use crate::ty::{self, DefIdTree, GenericParamDefKind, Ty, TyCtxt, TypeFoldable};
11 use crate::util::common::ErrorReported;
12 use rustc_apfloat::Float as _;
13 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
14 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
15 use rustc_hir as hir;
16 use rustc_hir::def::DefKind;
17 use rustc_hir::def_id::DefId;
18 use rustc_macros::HashStable;
19 use rustc_span::Span;
20 use std::{cmp, fmt};
21 use syntax::ast;
22 use syntax::attr::{self, SignedInt, UnsignedInt};
23
24 #[derive(Copy, Clone, Debug)]
25 pub struct Discr<'tcx> {
26     /// Bit representation of the discriminant (e.g., `-128i8` is `0xFF_u128`).
27     pub val: u128,
28     pub ty: Ty<'tcx>,
29 }
30
31 impl<'tcx> fmt::Display for Discr<'tcx> {
32     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
33         match self.ty.kind {
34             ty::Int(ity) => {
35                 let size = ty::tls::with(|tcx| Integer::from_attr(&tcx, SignedInt(ity)).size());
36                 let x = self.val;
37                 // sign extend the raw representation to be an i128
38                 let x = sign_extend(x, size) as i128;
39                 write!(fmt, "{}", x)
40             }
41             _ => write!(fmt, "{}", self.val),
42         }
43     }
44 }
45
46 fn signed_min(size: Size) -> i128 {
47     sign_extend(1_u128 << (size.bits() - 1), size) as i128
48 }
49
50 fn signed_max(size: Size) -> i128 {
51     i128::max_value() >> (128 - size.bits())
52 }
53
54 fn unsigned_max(size: Size) -> u128 {
55     u128::max_value() >> (128 - size.bits())
56 }
57
58 fn int_size_and_signed<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (Size, bool) {
59     let (int, signed) = match ty.kind {
60         Int(ity) => (Integer::from_attr(&tcx, SignedInt(ity)), true),
61         Uint(uty) => (Integer::from_attr(&tcx, UnsignedInt(uty)), false),
62         _ => bug!("non integer discriminant"),
63     };
64     (int.size(), signed)
65 }
66
67 impl<'tcx> Discr<'tcx> {
68     /// Adds `1` to the value and wraps around if the maximum for the type is reached.
69     pub fn wrap_incr(self, tcx: TyCtxt<'tcx>) -> Self {
70         self.checked_add(tcx, 1).0
71     }
72     pub fn checked_add(self, tcx: TyCtxt<'tcx>, n: u128) -> (Self, bool) {
73         let (size, signed) = int_size_and_signed(tcx, self.ty);
74         let (val, oflo) = if signed {
75             let min = signed_min(size);
76             let max = signed_max(size);
77             let val = sign_extend(self.val, size) as i128;
78             assert!(n < (i128::max_value() as u128));
79             let n = n as i128;
80             let oflo = val > max - n;
81             let val = if oflo { min + (n - (max - val) - 1) } else { val + n };
82             // zero the upper bits
83             let val = val as u128;
84             let val = truncate(val, size);
85             (val, oflo)
86         } else {
87             let max = unsigned_max(size);
88             let val = self.val;
89             let oflo = val > max - n;
90             let val = if oflo { n - (max - val) - 1 } else { val + n };
91             (val, oflo)
92         };
93         (Self { val, ty: self.ty }, oflo)
94     }
95 }
96
97 pub trait IntTypeExt {
98     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
99     fn disr_incr<'tcx>(&self, tcx: TyCtxt<'tcx>, val: Option<Discr<'tcx>>) -> Option<Discr<'tcx>>;
100     fn initial_discriminant<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Discr<'tcx>;
101 }
102
103 impl IntTypeExt for attr::IntType {
104     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
105         match *self {
106             SignedInt(ast::IntTy::I8) => tcx.types.i8,
107             SignedInt(ast::IntTy::I16) => tcx.types.i16,
108             SignedInt(ast::IntTy::I32) => tcx.types.i32,
109             SignedInt(ast::IntTy::I64) => tcx.types.i64,
110             SignedInt(ast::IntTy::I128) => tcx.types.i128,
111             SignedInt(ast::IntTy::Isize) => tcx.types.isize,
112             UnsignedInt(ast::UintTy::U8) => tcx.types.u8,
113             UnsignedInt(ast::UintTy::U16) => tcx.types.u16,
114             UnsignedInt(ast::UintTy::U32) => tcx.types.u32,
115             UnsignedInt(ast::UintTy::U64) => tcx.types.u64,
116             UnsignedInt(ast::UintTy::U128) => tcx.types.u128,
117             UnsignedInt(ast::UintTy::Usize) => tcx.types.usize,
118         }
119     }
120
121     fn initial_discriminant<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Discr<'tcx> {
122         Discr { val: 0, ty: self.to_ty(tcx) }
123     }
124
125     fn disr_incr<'tcx>(&self, tcx: TyCtxt<'tcx>, val: Option<Discr<'tcx>>) -> Option<Discr<'tcx>> {
126         if let Some(val) = val {
127             assert_eq!(self.to_ty(tcx), val.ty);
128             let (new, oflo) = val.checked_add(tcx, 1);
129             if oflo { None } else { Some(new) }
130         } else {
131             Some(self.initial_discriminant(tcx))
132         }
133     }
134 }
135
136 /// Describes whether a type is representable. For types that are not
137 /// representable, 'SelfRecursive' and 'ContainsRecursive' are used to
138 /// distinguish between types that are recursive with themselves and types that
139 /// contain a different recursive type. These cases can therefore be treated
140 /// differently when reporting errors.
141 ///
142 /// The ordering of the cases is significant. They are sorted so that cmp::max
143 /// will keep the "more erroneous" of two values.
144 #[derive(Clone, PartialOrd, Ord, Eq, PartialEq, Debug)]
145 pub enum Representability {
146     Representable,
147     ContainsRecursive,
148     SelfRecursive(Vec<Span>),
149 }
150
151 impl<'tcx> TyCtxt<'tcx> {
152     /// Creates a hash of the type `Ty` which will be the same no matter what crate
153     /// context it's calculated within. This is used by the `type_id` intrinsic.
154     pub fn type_id_hash(self, ty: Ty<'tcx>) -> u64 {
155         let mut hasher = StableHasher::new();
156         let mut hcx = self.create_stable_hashing_context();
157
158         // We want the type_id be independent of the types free regions, so we
159         // erase them. The erase_regions() call will also anonymize bound
160         // regions, which is desirable too.
161         let ty = self.erase_regions(&ty);
162
163         hcx.while_hashing_spans(false, |hcx| {
164             hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
165                 ty.hash_stable(hcx, &mut hasher);
166             });
167         });
168         hasher.finish()
169     }
170 }
171
172 impl<'tcx> TyCtxt<'tcx> {
173     pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
174         if let ty::Adt(def, substs) = ty.kind {
175             for field in def.all_fields() {
176                 let field_ty = field.ty(self, substs);
177                 if let Error = field_ty.kind {
178                     return true;
179                 }
180             }
181         }
182         false
183     }
184
185     /// Attempts to returns the deeply last field of nested structures, but
186     /// does not apply any normalization in its search. Returns the same type
187     /// if input `ty` is not a structure at all.
188     pub fn struct_tail_without_normalization(self, ty: Ty<'tcx>) -> Ty<'tcx> {
189         let tcx = self;
190         tcx.struct_tail_with_normalize(ty, |ty| ty)
191     }
192
193     /// Returns the deeply last field of nested structures, or the same type if
194     /// not a structure at all. Corresponds to the only possible unsized field,
195     /// and its type can be used to determine unsizing strategy.
196     ///
197     /// Should only be called if `ty` has no inference variables and does not
198     /// need its lifetimes preserved (e.g. as part of codegen); otherwise
199     /// normalization attempt may cause compiler bugs.
200     pub fn struct_tail_erasing_lifetimes(
201         self,
202         ty: Ty<'tcx>,
203         param_env: ty::ParamEnv<'tcx>,
204     ) -> Ty<'tcx> {
205         let tcx = self;
206         tcx.struct_tail_with_normalize(ty, |ty| tcx.normalize_erasing_regions(param_env, ty))
207     }
208
209     /// Returns the deeply last field of nested structures, or the same type if
210     /// not a structure at all. Corresponds to the only possible unsized field,
211     /// and its type can be used to determine unsizing strategy.
212     ///
213     /// This is parameterized over the normalization strategy (i.e. how to
214     /// handle `<T as Trait>::Assoc` and `impl Trait`); pass the identity
215     /// function to indicate no normalization should take place.
216     ///
217     /// See also `struct_tail_erasing_lifetimes`, which is suitable for use
218     /// during codegen.
219     pub fn struct_tail_with_normalize(
220         self,
221         mut ty: Ty<'tcx>,
222         normalize: impl Fn(Ty<'tcx>) -> Ty<'tcx>,
223     ) -> Ty<'tcx> {
224         loop {
225             match ty.kind {
226                 ty::Adt(def, substs) => {
227                     if !def.is_struct() {
228                         break;
229                     }
230                     match def.non_enum_variant().fields.last() {
231                         Some(f) => ty = f.ty(self, substs),
232                         None => break,
233                     }
234                 }
235
236                 ty::Tuple(tys) => {
237                     if let Some((&last_ty, _)) = tys.split_last() {
238                         ty = last_ty.expect_ty();
239                     } else {
240                         break;
241                     }
242                 }
243
244                 ty::Projection(_) | ty::Opaque(..) => {
245                     let normalized = normalize(ty);
246                     if ty == normalized {
247                         return ty;
248                     } else {
249                         ty = normalized;
250                     }
251                 }
252
253                 _ => {
254                     break;
255                 }
256             }
257         }
258         ty
259     }
260
261     /// Same as applying `struct_tail` on `source` and `target`, but only
262     /// keeps going as long as the two types are instances of the same
263     /// structure definitions.
264     /// For `(Foo<Foo<T>>, Foo<dyn Trait>)`, the result will be `(Foo<T>, Trait)`,
265     /// whereas struct_tail produces `T`, and `Trait`, respectively.
266     ///
267     /// Should only be called if the types have no inference variables and do
268     /// not need their lifetimes preserved (e.g., as part of codegen); otherwise,
269     /// normalization attempt may cause compiler bugs.
270     pub fn struct_lockstep_tails_erasing_lifetimes(
271         self,
272         source: Ty<'tcx>,
273         target: Ty<'tcx>,
274         param_env: ty::ParamEnv<'tcx>,
275     ) -> (Ty<'tcx>, Ty<'tcx>) {
276         let tcx = self;
277         tcx.struct_lockstep_tails_with_normalize(source, target, |ty| {
278             tcx.normalize_erasing_regions(param_env, ty)
279         })
280     }
281
282     /// Same as applying `struct_tail` on `source` and `target`, but only
283     /// keeps going as long as the two types are instances of the same
284     /// structure definitions.
285     /// For `(Foo<Foo<T>>, Foo<dyn Trait>)`, the result will be `(Foo<T>, Trait)`,
286     /// whereas struct_tail produces `T`, and `Trait`, respectively.
287     ///
288     /// See also `struct_lockstep_tails_erasing_lifetimes`, which is suitable for use
289     /// during codegen.
290     pub fn struct_lockstep_tails_with_normalize(
291         self,
292         source: Ty<'tcx>,
293         target: Ty<'tcx>,
294         normalize: impl Fn(Ty<'tcx>) -> Ty<'tcx>,
295     ) -> (Ty<'tcx>, Ty<'tcx>) {
296         let (mut a, mut b) = (source, target);
297         loop {
298             match (&a.kind, &b.kind) {
299                 (&Adt(a_def, a_substs), &Adt(b_def, b_substs))
300                     if a_def == b_def && a_def.is_struct() =>
301                 {
302                     if let Some(f) = a_def.non_enum_variant().fields.last() {
303                         a = f.ty(self, a_substs);
304                         b = f.ty(self, b_substs);
305                     } else {
306                         break;
307                     }
308                 }
309                 (&Tuple(a_tys), &Tuple(b_tys)) if a_tys.len() == b_tys.len() => {
310                     if let Some(a_last) = a_tys.last() {
311                         a = a_last.expect_ty();
312                         b = b_tys.last().unwrap().expect_ty();
313                     } else {
314                         break;
315                     }
316                 }
317                 (ty::Projection(_), _)
318                 | (ty::Opaque(..), _)
319                 | (_, ty::Projection(_))
320                 | (_, ty::Opaque(..)) => {
321                     // If either side is a projection, attempt to
322                     // progress via normalization. (Should be safe to
323                     // apply to both sides as normalization is
324                     // idempotent.)
325                     let a_norm = normalize(a);
326                     let b_norm = normalize(b);
327                     if a == a_norm && b == b_norm {
328                         break;
329                     } else {
330                         a = a_norm;
331                         b = b_norm;
332                     }
333                 }
334
335                 _ => break,
336             }
337         }
338         (a, b)
339     }
340
341     /// Calculate the destructor of a given type.
342     pub fn calculate_dtor(
343         self,
344         adt_did: DefId,
345         validate: &mut dyn FnMut(Self, DefId) -> Result<(), ErrorReported>,
346     ) -> Option<ty::Destructor> {
347         let drop_trait = if let Some(def_id) = self.lang_items().drop_trait() {
348             def_id
349         } else {
350             return None;
351         };
352
353         self.ensure().coherent_trait(drop_trait);
354
355         let mut dtor_did = None;
356         let ty = self.type_of(adt_did);
357         self.for_each_relevant_impl(drop_trait, ty, |impl_did| {
358             if let Some(item) = self.associated_items(impl_did).next() {
359                 if validate(self, impl_did).is_ok() {
360                     dtor_did = Some(item.def_id);
361                 }
362             }
363         });
364
365         Some(ty::Destructor { did: dtor_did? })
366     }
367
368     /// Returns the set of types that are required to be alive in
369     /// order to run the destructor of `def` (see RFCs 769 and
370     /// 1238).
371     ///
372     /// Note that this returns only the constraints for the
373     /// destructor of `def` itself. For the destructors of the
374     /// contents, you need `adt_dtorck_constraint`.
375     pub fn destructor_constraints(self, def: &'tcx ty::AdtDef) -> Vec<ty::subst::GenericArg<'tcx>> {
376         let dtor = match def.destructor(self) {
377             None => {
378                 debug!("destructor_constraints({:?}) - no dtor", def.did);
379                 return vec![];
380             }
381             Some(dtor) => dtor.did,
382         };
383
384         let impl_def_id = self.associated_item(dtor).container.id();
385         let impl_generics = self.generics_of(impl_def_id);
386
387         // We have a destructor - all the parameters that are not
388         // pure_wrt_drop (i.e, don't have a #[may_dangle] attribute)
389         // must be live.
390
391         // We need to return the list of parameters from the ADTs
392         // generics/substs that correspond to impure parameters on the
393         // impl's generics. This is a bit ugly, but conceptually simple:
394         //
395         // Suppose our ADT looks like the following
396         //
397         //     struct S<X, Y, Z>(X, Y, Z);
398         //
399         // and the impl is
400         //
401         //     impl<#[may_dangle] P0, P1, P2> Drop for S<P1, P2, P0>
402         //
403         // We want to return the parameters (X, Y). For that, we match
404         // up the item-substs <X, Y, Z> with the substs on the impl ADT,
405         // <P1, P2, P0>, and then look up which of the impl substs refer to
406         // parameters marked as pure.
407
408         let impl_substs = match self.type_of(impl_def_id).kind {
409             ty::Adt(def_, substs) if def_ == def => substs,
410             _ => bug!(),
411         };
412
413         let item_substs = match self.type_of(def.did).kind {
414             ty::Adt(def_, substs) if def_ == def => substs,
415             _ => bug!(),
416         };
417
418         let result = item_substs
419             .iter()
420             .zip(impl_substs.iter())
421             .filter(|&(_, &k)| {
422                 match k.unpack() {
423                     GenericArgKind::Lifetime(&ty::RegionKind::ReEarlyBound(ref ebr)) => {
424                         !impl_generics.region_param(ebr, self).pure_wrt_drop
425                     }
426                     GenericArgKind::Type(&ty::TyS { kind: ty::Param(ref pt), .. }) => {
427                         !impl_generics.type_param(pt, self).pure_wrt_drop
428                     }
429                     GenericArgKind::Const(&ty::Const {
430                         val: ty::ConstKind::Param(ref pc), ..
431                     }) => !impl_generics.const_param(pc, self).pure_wrt_drop,
432                     GenericArgKind::Lifetime(_)
433                     | GenericArgKind::Type(_)
434                     | GenericArgKind::Const(_) => {
435                         // Not a type, const or region param: this should be reported
436                         // as an error.
437                         false
438                     }
439                 }
440             })
441             .map(|(&item_param, _)| item_param)
442             .collect();
443         debug!("destructor_constraint({:?}) = {:?}", def.did, result);
444         result
445     }
446
447     /// Returns `true` if `def_id` refers to a closure (e.g., `|x| x * 2`). Note
448     /// that closures have a `DefId`, but the closure *expression* also
449     /// has a `HirId` that is located within the context where the
450     /// closure appears (and, sadly, a corresponding `NodeId`, since
451     /// those are not yet phased out). The parent of the closure's
452     /// `DefId` will also be the context where it appears.
453     pub fn is_closure(self, def_id: DefId) -> bool {
454         self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr
455     }
456
457     /// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
458     pub fn is_trait(self, def_id: DefId) -> bool {
459         self.def_kind(def_id) == Some(DefKind::Trait)
460     }
461
462     /// Returns `true` if `def_id` refers to a trait alias (i.e., `trait Foo = ...;`),
463     /// and `false` otherwise.
464     pub fn is_trait_alias(self, def_id: DefId) -> bool {
465         self.def_kind(def_id) == Some(DefKind::TraitAlias)
466     }
467
468     /// Returns `true` if this `DefId` refers to the implicit constructor for
469     /// a tuple struct like `struct Foo(u32)`, and `false` otherwise.
470     pub fn is_constructor(self, def_id: DefId) -> bool {
471         self.def_key(def_id).disambiguated_data.data == DefPathData::Ctor
472     }
473
474     /// Given the def-ID of a fn or closure, returns the def-ID of
475     /// the innermost fn item that the closure is contained within.
476     /// This is a significant `DefId` because, when we do
477     /// type-checking, we type-check this fn item and all of its
478     /// (transitive) closures together. Therefore, when we fetch the
479     /// `typeck_tables_of` the closure, for example, we really wind up
480     /// fetching the `typeck_tables_of` the enclosing fn item.
481     pub fn closure_base_def_id(self, def_id: DefId) -> DefId {
482         let mut def_id = def_id;
483         while self.is_closure(def_id) {
484             def_id = self.parent(def_id).unwrap_or_else(|| {
485                 bug!("closure {:?} has no parent", def_id);
486             });
487         }
488         def_id
489     }
490
491     /// Given the `DefId` and substs a closure, creates the type of
492     /// `self` argument that the closure expects. For example, for a
493     /// `Fn` closure, this would return a reference type `&T` where
494     /// `T = closure_ty`.
495     ///
496     /// Returns `None` if this closure's kind has not yet been inferred.
497     /// This should only be possible during type checking.
498     ///
499     /// Note that the return value is a late-bound region and hence
500     /// wrapped in a binder.
501     pub fn closure_env_ty(
502         self,
503         closure_def_id: DefId,
504         closure_substs: SubstsRef<'tcx>,
505     ) -> Option<ty::Binder<Ty<'tcx>>> {
506         let closure_ty = self.mk_closure(closure_def_id, closure_substs);
507         let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
508         let closure_kind_ty = closure_substs.as_closure().kind_ty(closure_def_id, self);
509         let closure_kind = closure_kind_ty.to_opt_closure_kind()?;
510         let env_ty = match closure_kind {
511             ty::ClosureKind::Fn => self.mk_imm_ref(self.mk_region(env_region), closure_ty),
512             ty::ClosureKind::FnMut => self.mk_mut_ref(self.mk_region(env_region), closure_ty),
513             ty::ClosureKind::FnOnce => closure_ty,
514         };
515         Some(ty::Binder::bind(env_ty))
516     }
517
518     /// Given the `DefId` of some item that has no type or const parameters, make
519     /// a suitable "empty substs" for it.
520     pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> SubstsRef<'tcx> {
521         InternalSubsts::for_item(self, item_def_id, |param, _| match param.kind {
522             GenericParamDefKind::Lifetime => self.lifetimes.re_erased.into(),
523             GenericParamDefKind::Type { .. } => {
524                 bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
525             }
526             GenericParamDefKind::Const { .. } => {
527                 bug!("empty_substs_for_def_id: {:?} has const parameters", item_def_id)
528             }
529         })
530     }
531
532     /// Returns `true` if the node pointed to by `def_id` is a `static` item.
533     pub fn is_static(&self, def_id: DefId) -> bool {
534         self.static_mutability(def_id).is_some()
535     }
536
537     /// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
538     pub fn is_mutable_static(&self, def_id: DefId) -> bool {
539         self.static_mutability(def_id) == Some(hir::Mutability::Mut)
540     }
541
542     /// Get the type of the pointer to the static that we use in MIR.
543     pub fn static_ptr_ty(&self, def_id: DefId) -> Ty<'tcx> {
544         // Make sure that any constants in the static's type are evaluated.
545         let static_ty = self.normalize_erasing_regions(ty::ParamEnv::empty(), self.type_of(def_id));
546
547         if self.is_mutable_static(def_id) {
548             self.mk_mut_ptr(static_ty)
549         } else {
550             self.mk_imm_ref(self.lifetimes.re_erased, static_ty)
551         }
552     }
553
554     /// Expands the given impl trait type, stopping if the type is recursive.
555     pub fn try_expand_impl_trait_type(
556         self,
557         def_id: DefId,
558         substs: SubstsRef<'tcx>,
559     ) -> Result<Ty<'tcx>, Ty<'tcx>> {
560         use crate::ty::fold::TypeFolder;
561
562         struct OpaqueTypeExpander<'tcx> {
563             // Contains the DefIds of the opaque types that are currently being
564             // expanded. When we expand an opaque type we insert the DefId of
565             // that type, and when we finish expanding that type we remove the
566             // its DefId.
567             seen_opaque_tys: FxHashSet<DefId>,
568             // Cache of all expansions we've seen so far. This is a critical
569             // optimization for some large types produced by async fn trees.
570             expanded_cache: FxHashMap<(DefId, SubstsRef<'tcx>), Ty<'tcx>>,
571             primary_def_id: DefId,
572             found_recursion: bool,
573             tcx: TyCtxt<'tcx>,
574         }
575
576         impl<'tcx> OpaqueTypeExpander<'tcx> {
577             fn expand_opaque_ty(
578                 &mut self,
579                 def_id: DefId,
580                 substs: SubstsRef<'tcx>,
581             ) -> Option<Ty<'tcx>> {
582                 if self.found_recursion {
583                     return None;
584                 }
585                 let substs = substs.fold_with(self);
586                 if self.seen_opaque_tys.insert(def_id) {
587                     let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) {
588                         Some(expanded_ty) => expanded_ty,
589                         None => {
590                             let generic_ty = self.tcx.type_of(def_id);
591                             let concrete_ty = generic_ty.subst(self.tcx, substs);
592                             let expanded_ty = self.fold_ty(concrete_ty);
593                             self.expanded_cache.insert((def_id, substs), expanded_ty);
594                             expanded_ty
595                         }
596                     };
597                     self.seen_opaque_tys.remove(&def_id);
598                     Some(expanded_ty)
599                 } else {
600                     // If another opaque type that we contain is recursive, then it
601                     // will report the error, so we don't have to.
602                     self.found_recursion = def_id == self.primary_def_id;
603                     None
604                 }
605             }
606         }
607
608         impl<'tcx> TypeFolder<'tcx> for OpaqueTypeExpander<'tcx> {
609             fn tcx(&self) -> TyCtxt<'tcx> {
610                 self.tcx
611             }
612
613             fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
614                 if let ty::Opaque(def_id, substs) = t.kind {
615                     self.expand_opaque_ty(def_id, substs).unwrap_or(t)
616                 } else if t.has_projections() {
617                     t.super_fold_with(self)
618                 } else {
619                     t
620                 }
621             }
622         }
623
624         let mut visitor = OpaqueTypeExpander {
625             seen_opaque_tys: FxHashSet::default(),
626             expanded_cache: FxHashMap::default(),
627             primary_def_id: def_id,
628             found_recursion: false,
629             tcx: self,
630         };
631         let expanded_type = visitor.expand_opaque_ty(def_id, substs).unwrap();
632         if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
633     }
634 }
635
636 impl<'tcx> ty::TyS<'tcx> {
637     /// Returns the maximum value for the given numeric type (including `char`s)
638     /// or returns `None` if the type is not numeric.
639     pub fn numeric_max_val(&'tcx self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ty::Const<'tcx>> {
640         let val = match self.kind {
641             ty::Int(_) | ty::Uint(_) => {
642                 let (size, signed) = int_size_and_signed(tcx, self);
643                 let val = if signed { signed_max(size) as u128 } else { unsigned_max(size) };
644                 Some(val)
645             }
646             ty::Char => Some(std::char::MAX as u128),
647             ty::Float(fty) => Some(match fty {
648                 ast::FloatTy::F32 => ::rustc_apfloat::ieee::Single::INFINITY.to_bits(),
649                 ast::FloatTy::F64 => ::rustc_apfloat::ieee::Double::INFINITY.to_bits(),
650             }),
651             _ => None,
652         };
653         val.map(|v| ty::Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
654     }
655
656     /// Returns the minimum value for the given numeric type (including `char`s)
657     /// or returns `None` if the type is not numeric.
658     pub fn numeric_min_val(&'tcx self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ty::Const<'tcx>> {
659         let val = match self.kind {
660             ty::Int(_) | ty::Uint(_) => {
661                 let (size, signed) = int_size_and_signed(tcx, self);
662                 let val = if signed { truncate(signed_min(size) as u128, size) } else { 0 };
663                 Some(val)
664             }
665             ty::Char => Some(0),
666             ty::Float(fty) => Some(match fty {
667                 ast::FloatTy::F32 => (-::rustc_apfloat::ieee::Single::INFINITY).to_bits(),
668                 ast::FloatTy::F64 => (-::rustc_apfloat::ieee::Double::INFINITY).to_bits(),
669             }),
670             _ => None,
671         };
672         val.map(|v| ty::Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
673     }
674
675     /// Checks whether values of this type `T` are *moved* or *copied*
676     /// when referenced -- this amounts to a check for whether `T:
677     /// Copy`, but note that we **don't** consider lifetimes when
678     /// doing this check. This means that we may generate MIR which
679     /// does copies even when the type actually doesn't satisfy the
680     /// full requirements for the `Copy` trait (cc #29149) -- this
681     /// winds up being reported as an error during NLL borrow check.
682     pub fn is_copy_modulo_regions(
683         &'tcx self,
684         tcx: TyCtxt<'tcx>,
685         param_env: ty::ParamEnv<'tcx>,
686         span: Span,
687     ) -> bool {
688         tcx.at(span).is_copy_raw(param_env.and(self))
689     }
690
691     /// Checks whether values of this type `T` have a size known at
692     /// compile time (i.e., whether `T: Sized`). Lifetimes are ignored
693     /// for the purposes of this check, so it can be an
694     /// over-approximation in generic contexts, where one can have
695     /// strange rules like `<T as Foo<'static>>::Bar: Sized` that
696     /// actually carry lifetime requirements.
697     pub fn is_sized(&'tcx self, tcx_at: TyCtxtAt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
698         tcx_at.is_sized_raw(param_env.and(self))
699     }
700
701     /// Checks whether values of this type `T` implement the `Freeze`
702     /// trait -- frozen types are those that do not contain a
703     /// `UnsafeCell` anywhere. This is a language concept used to
704     /// distinguish "true immutability", which is relevant to
705     /// optimization as well as the rules around static values. Note
706     /// that the `Freeze` trait is not exposed to end users and is
707     /// effectively an implementation detail.
708     pub fn is_freeze(
709         &'tcx self,
710         tcx: TyCtxt<'tcx>,
711         param_env: ty::ParamEnv<'tcx>,
712         span: Span,
713     ) -> bool {
714         tcx.at(span).is_freeze_raw(param_env.and(self))
715     }
716
717     /// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely
718     /// non-copy and *might* have a destructor attached; if it returns
719     /// `false`, then `ty` definitely has no destructor (i.e., no drop glue).
720     ///
721     /// (Note that this implies that if `ty` has a destructor attached,
722     /// then `needs_drop` will definitely return `true` for `ty`.)
723     ///
724     /// Note that this method is used to check eligible types in unions.
725     #[inline]
726     pub fn needs_drop(&'tcx self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
727         tcx.needs_drop_raw(param_env.and(self)).0
728     }
729
730     pub fn same_type(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
731         match (&a.kind, &b.kind) {
732             (&Adt(did_a, substs_a), &Adt(did_b, substs_b)) => {
733                 if did_a != did_b {
734                     return false;
735                 }
736
737                 substs_a.types().zip(substs_b.types()).all(|(a, b)| Self::same_type(a, b))
738             }
739             _ => a == b,
740         }
741     }
742
743     /// Check whether a type is representable. This means it cannot contain unboxed
744     /// structural recursion. This check is needed for structs and enums.
745     pub fn is_representable(&'tcx self, tcx: TyCtxt<'tcx>, sp: Span) -> Representability {
746         // Iterate until something non-representable is found
747         fn fold_repr<It: Iterator<Item = Representability>>(iter: It) -> Representability {
748             iter.fold(Representability::Representable, |r1, r2| match (r1, r2) {
749                 (Representability::SelfRecursive(v1), Representability::SelfRecursive(v2)) => {
750                     Representability::SelfRecursive(v1.into_iter().chain(v2).collect())
751                 }
752                 (r1, r2) => cmp::max(r1, r2),
753             })
754         }
755
756         fn are_inner_types_recursive<'tcx>(
757             tcx: TyCtxt<'tcx>,
758             sp: Span,
759             seen: &mut Vec<Ty<'tcx>>,
760             representable_cache: &mut FxHashMap<Ty<'tcx>, Representability>,
761             ty: Ty<'tcx>,
762         ) -> Representability {
763             match ty.kind {
764                 Tuple(..) => {
765                     // Find non representable
766                     fold_repr(ty.tuple_fields().map(|ty| {
767                         is_type_structurally_recursive(tcx, sp, seen, representable_cache, ty)
768                     }))
769                 }
770                 // Fixed-length vectors.
771                 // FIXME(#11924) Behavior undecided for zero-length vectors.
772                 Array(ty, _) => {
773                     is_type_structurally_recursive(tcx, sp, seen, representable_cache, ty)
774                 }
775                 Adt(def, substs) => {
776                     // Find non representable fields with their spans
777                     fold_repr(def.all_fields().map(|field| {
778                         let ty = field.ty(tcx, substs);
779                         let span = tcx.hir().span_if_local(field.did).unwrap_or(sp);
780                         match is_type_structurally_recursive(
781                             tcx,
782                             span,
783                             seen,
784                             representable_cache,
785                             ty,
786                         ) {
787                             Representability::SelfRecursive(_) => {
788                                 Representability::SelfRecursive(vec![span])
789                             }
790                             x => x,
791                         }
792                     }))
793                 }
794                 Closure(..) => {
795                     // this check is run on type definitions, so we don't expect
796                     // to see closure types
797                     bug!("requires check invoked on inapplicable type: {:?}", ty)
798                 }
799                 _ => Representability::Representable,
800             }
801         }
802
803         fn same_struct_or_enum<'tcx>(ty: Ty<'tcx>, def: &'tcx ty::AdtDef) -> bool {
804             match ty.kind {
805                 Adt(ty_def, _) => ty_def == def,
806                 _ => false,
807             }
808         }
809
810         // Does the type `ty` directly (without indirection through a pointer)
811         // contain any types on stack `seen`?
812         fn is_type_structurally_recursive<'tcx>(
813             tcx: TyCtxt<'tcx>,
814             sp: Span,
815             seen: &mut Vec<Ty<'tcx>>,
816             representable_cache: &mut FxHashMap<Ty<'tcx>, Representability>,
817             ty: Ty<'tcx>,
818         ) -> Representability {
819             debug!("is_type_structurally_recursive: {:?} {:?}", ty, sp);
820             if let Some(representability) = representable_cache.get(ty) {
821                 debug!(
822                     "is_type_structurally_recursive: {:?} {:?} - (cached) {:?}",
823                     ty, sp, representability
824                 );
825                 return representability.clone();
826             }
827
828             let representability =
829                 is_type_structurally_recursive_inner(tcx, sp, seen, representable_cache, ty);
830
831             representable_cache.insert(ty, representability.clone());
832             representability
833         }
834
835         fn is_type_structurally_recursive_inner<'tcx>(
836             tcx: TyCtxt<'tcx>,
837             sp: Span,
838             seen: &mut Vec<Ty<'tcx>>,
839             representable_cache: &mut FxHashMap<Ty<'tcx>, Representability>,
840             ty: Ty<'tcx>,
841         ) -> Representability {
842             match ty.kind {
843                 Adt(def, _) => {
844                     {
845                         // Iterate through stack of previously seen types.
846                         let mut iter = seen.iter();
847
848                         // The first item in `seen` is the type we are actually curious about.
849                         // We want to return SelfRecursive if this type contains itself.
850                         // It is important that we DON'T take generic parameters into account
851                         // for this check, so that Bar<T> in this example counts as SelfRecursive:
852                         //
853                         // struct Foo;
854                         // struct Bar<T> { x: Bar<Foo> }
855
856                         if let Some(&seen_type) = iter.next() {
857                             if same_struct_or_enum(seen_type, def) {
858                                 debug!("SelfRecursive: {:?} contains {:?}", seen_type, ty);
859                                 return Representability::SelfRecursive(vec![sp]);
860                             }
861                         }
862
863                         // We also need to know whether the first item contains other types
864                         // that are structurally recursive. If we don't catch this case, we
865                         // will recurse infinitely for some inputs.
866                         //
867                         // It is important that we DO take generic parameters into account
868                         // here, so that code like this is considered SelfRecursive, not
869                         // ContainsRecursive:
870                         //
871                         // struct Foo { Option<Option<Foo>> }
872
873                         for &seen_type in iter {
874                             if ty::TyS::same_type(ty, seen_type) {
875                                 debug!("ContainsRecursive: {:?} contains {:?}", seen_type, ty);
876                                 return Representability::ContainsRecursive;
877                             }
878                         }
879                     }
880
881                     // For structs and enums, track all previously seen types by pushing them
882                     // onto the 'seen' stack.
883                     seen.push(ty);
884                     let out = are_inner_types_recursive(tcx, sp, seen, representable_cache, ty);
885                     seen.pop();
886                     out
887                 }
888                 _ => {
889                     // No need to push in other cases.
890                     are_inner_types_recursive(tcx, sp, seen, representable_cache, ty)
891                 }
892             }
893         }
894
895         debug!("is_type_representable: {:?}", self);
896
897         // To avoid a stack overflow when checking an enum variant or struct that
898         // contains a different, structurally recursive type, maintain a stack
899         // of seen types and check recursion for each of them (issues #3008, #3779).
900         let mut seen: Vec<Ty<'_>> = Vec::new();
901         let mut representable_cache = FxHashMap::default();
902         let r = is_type_structurally_recursive(tcx, sp, &mut seen, &mut representable_cache, self);
903         debug!("is_type_representable: {:?} is {:?}", self, r);
904         r
905     }
906
907     /// Peel off all reference types in this type until there are none left.
908     ///
909     /// This method is idempotent, i.e. `ty.peel_refs().peel_refs() == ty.peel_refs()`.
910     ///
911     /// # Examples
912     ///
913     /// - `u8` -> `u8`
914     /// - `&'a mut u8` -> `u8`
915     /// - `&'a &'b u8` -> `u8`
916     /// - `&'a *const &'b u8 -> *const &'b u8`
917     pub fn peel_refs(&'tcx self) -> Ty<'tcx> {
918         let mut ty = self;
919         while let Ref(_, inner_ty, _) = ty.kind {
920             ty = inner_ty;
921         }
922         ty
923     }
924 }
925
926 #[derive(Clone, HashStable)]
927 pub struct NeedsDrop(pub bool);
928
929 pub enum ExplicitSelf<'tcx> {
930     ByValue,
931     ByReference(ty::Region<'tcx>, hir::Mutability),
932     ByRawPointer(hir::Mutability),
933     ByBox,
934     Other,
935 }
936
937 impl<'tcx> ExplicitSelf<'tcx> {
938     /// Categorizes an explicit self declaration like `self: SomeType`
939     /// into either `self`, `&self`, `&mut self`, `Box<self>`, or
940     /// `Other`.
941     /// This is mainly used to require the arbitrary_self_types feature
942     /// in the case of `Other`, to improve error messages in the common cases,
943     /// and to make `Other` non-object-safe.
944     ///
945     /// Examples:
946     ///
947     /// ```
948     /// impl<'a> Foo for &'a T {
949     ///     // Legal declarations:
950     ///     fn method1(self: &&'a T); // ExplicitSelf::ByReference
951     ///     fn method2(self: &'a T); // ExplicitSelf::ByValue
952     ///     fn method3(self: Box<&'a T>); // ExplicitSelf::ByBox
953     ///     fn method4(self: Rc<&'a T>); // ExplicitSelf::Other
954     ///
955     ///     // Invalid cases will be caught by `check_method_receiver`:
956     ///     fn method_err1(self: &'a mut T); // ExplicitSelf::Other
957     ///     fn method_err2(self: &'static T) // ExplicitSelf::ByValue
958     ///     fn method_err3(self: &&T) // ExplicitSelf::ByReference
959     /// }
960     /// ```
961     ///
962     pub fn determine<P>(self_arg_ty: Ty<'tcx>, is_self_ty: P) -> ExplicitSelf<'tcx>
963     where
964         P: Fn(Ty<'tcx>) -> bool,
965     {
966         use self::ExplicitSelf::*;
967
968         match self_arg_ty.kind {
969             _ if is_self_ty(self_arg_ty) => ByValue,
970             ty::Ref(region, ty, mutbl) if is_self_ty(ty) => ByReference(region, mutbl),
971             ty::RawPtr(ty::TypeAndMut { ty, mutbl }) if is_self_ty(ty) => ByRawPointer(mutbl),
972             ty::Adt(def, _) if def.is_box() && is_self_ty(self_arg_ty.boxed_ty()) => ByBox,
973             _ => Other,
974         }
975     }
976 }