3 use crate::infer::canonical::Canonical;
4 use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
5 use crate::ty::sty::{ClosureSubsts, GeneratorSubsts};
6 use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};
8 use rustc_hir::def_id::DefId;
9 use rustc_macros::HashStable;
10 use rustc_serialize::{self, Decodable, Decoder, Encodable, Encoder};
11 use rustc_span::{Span, DUMMY_SP};
12 use smallvec::SmallVec;
15 use std::cmp::Ordering;
17 use std::marker::PhantomData;
19 use std::num::NonZeroUsize;
21 /// An entity in the Rust type system, which can be one of
22 /// several kinds (types, lifetimes, and consts).
23 /// To reduce memory usage, a `GenericArg` is a interned pointer,
24 /// with the lowest 2 bits being reserved for a tag to
25 /// indicate the type (`Ty`, `Region`, or `Const`) it points to.
26 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
27 pub struct GenericArg<'tcx> {
29 marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>, &'tcx ty::Const<'tcx>)>,
32 const TAG_MASK: usize = 0b11;
33 const TYPE_TAG: usize = 0b00;
34 const REGION_TAG: usize = 0b01;
35 const CONST_TAG: usize = 0b10;
37 #[derive(Debug, RustcEncodable, RustcDecodable, PartialEq, Eq, PartialOrd, Ord, HashStable)]
38 pub enum GenericArgKind<'tcx> {
39 Lifetime(ty::Region<'tcx>),
41 Const(&'tcx ty::Const<'tcx>),
44 impl<'tcx> GenericArgKind<'tcx> {
45 fn pack(self) -> GenericArg<'tcx> {
46 let (tag, ptr) = match self {
47 GenericArgKind::Lifetime(lt) => {
48 // Ensure we can use the tag bits.
49 assert_eq!(mem::align_of_val(lt) & TAG_MASK, 0);
50 (REGION_TAG, lt as *const _ as usize)
52 GenericArgKind::Type(ty) => {
53 // Ensure we can use the tag bits.
54 assert_eq!(mem::align_of_val(ty) & TAG_MASK, 0);
55 (TYPE_TAG, ty as *const _ as usize)
57 GenericArgKind::Const(ct) => {
58 // Ensure we can use the tag bits.
59 assert_eq!(mem::align_of_val(ct) & TAG_MASK, 0);
60 (CONST_TAG, ct as *const _ as usize)
64 GenericArg { ptr: unsafe { NonZeroUsize::new_unchecked(ptr | tag) }, marker: PhantomData }
68 impl fmt::Debug for GenericArg<'tcx> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 GenericArgKind::Lifetime(lt) => lt.fmt(f),
72 GenericArgKind::Type(ty) => ty.fmt(f),
73 GenericArgKind::Const(ct) => ct.fmt(f),
78 impl<'tcx> Ord for GenericArg<'tcx> {
79 fn cmp(&self, other: &GenericArg<'_>) -> Ordering {
80 self.unpack().cmp(&other.unpack())
84 impl<'tcx> PartialOrd for GenericArg<'tcx> {
85 fn partial_cmp(&self, other: &GenericArg<'_>) -> Option<Ordering> {
86 Some(self.cmp(&other))
90 impl<'tcx> From<ty::Region<'tcx>> for GenericArg<'tcx> {
91 fn from(r: ty::Region<'tcx>) -> GenericArg<'tcx> {
92 GenericArgKind::Lifetime(r).pack()
96 impl<'tcx> From<Ty<'tcx>> for GenericArg<'tcx> {
97 fn from(ty: Ty<'tcx>) -> GenericArg<'tcx> {
98 GenericArgKind::Type(ty).pack()
102 impl<'tcx> From<&'tcx ty::Const<'tcx>> for GenericArg<'tcx> {
103 fn from(c: &'tcx ty::Const<'tcx>) -> GenericArg<'tcx> {
104 GenericArgKind::Const(c).pack()
108 impl<'tcx> GenericArg<'tcx> {
110 pub fn unpack(self) -> GenericArgKind<'tcx> {
111 let ptr = self.ptr.get();
113 match ptr & TAG_MASK {
114 REGION_TAG => GenericArgKind::Lifetime(&*((ptr & !TAG_MASK) as *const _)),
115 TYPE_TAG => GenericArgKind::Type(&*((ptr & !TAG_MASK) as *const _)),
116 CONST_TAG => GenericArgKind::Const(&*((ptr & !TAG_MASK) as *const _)),
117 _ => intrinsics::unreachable(),
122 /// Unpack the `GenericArg` as a type when it is known certainly to be a type.
123 /// This is true in cases where `Substs` is used in places where the kinds are known
124 /// to be limited (e.g. in tuples, where the only parameters are type parameters).
125 pub fn expect_ty(self) -> Ty<'tcx> {
126 match self.unpack() {
127 GenericArgKind::Type(ty) => ty,
128 _ => bug!("expected a type, but found another kind"),
132 /// Unpack the `GenericArg` as a const when it is known certainly to be a const.
133 pub fn expect_const(self) -> &'tcx ty::Const<'tcx> {
134 match self.unpack() {
135 GenericArgKind::Const(c) => c,
136 _ => bug!("expected a const, but found another kind"),
141 impl<'a, 'tcx> Lift<'tcx> for GenericArg<'a> {
142 type Lifted = GenericArg<'tcx>;
144 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
145 match self.unpack() {
146 GenericArgKind::Lifetime(lt) => tcx.lift(<).map(|lt| lt.into()),
147 GenericArgKind::Type(ty) => tcx.lift(&ty).map(|ty| ty.into()),
148 GenericArgKind::Const(ct) => tcx.lift(&ct).map(|ct| ct.into()),
153 impl<'tcx> TypeFoldable<'tcx> for GenericArg<'tcx> {
154 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
155 match self.unpack() {
156 GenericArgKind::Lifetime(lt) => lt.fold_with(folder).into(),
157 GenericArgKind::Type(ty) => ty.fold_with(folder).into(),
158 GenericArgKind::Const(ct) => ct.fold_with(folder).into(),
162 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
163 match self.unpack() {
164 GenericArgKind::Lifetime(lt) => lt.visit_with(visitor),
165 GenericArgKind::Type(ty) => ty.visit_with(visitor),
166 GenericArgKind::Const(ct) => ct.visit_with(visitor),
171 impl<'tcx> Encodable for GenericArg<'tcx> {
172 fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
173 self.unpack().encode(e)
177 impl<'tcx> Decodable for GenericArg<'tcx> {
178 fn decode<D: Decoder>(d: &mut D) -> Result<GenericArg<'tcx>, D::Error> {
179 Ok(GenericArgKind::decode(d)?.pack())
183 /// A substitution mapping generic parameters to new values.
184 pub type InternalSubsts<'tcx> = List<GenericArg<'tcx>>;
186 pub type SubstsRef<'tcx> = &'tcx InternalSubsts<'tcx>;
188 impl<'a, 'tcx> InternalSubsts<'tcx> {
189 /// Interpret these substitutions as the substitutions of a closure type.
190 /// Closure substitutions have a particular structure controlled by the
191 /// compiler that encodes information like the signature and closure kind;
192 /// see `ty::ClosureSubsts` struct for more comments.
193 pub fn as_closure(&'a self) -> ClosureSubsts<'a> {
194 ClosureSubsts { substs: self }
197 /// Interpret these substitutions as the substitutions of a generator type.
198 /// Closure substitutions have a particular structure controlled by the
199 /// compiler that encodes information like the signature and generator kind;
200 /// see `ty::GeneratorSubsts` struct for more comments.
201 pub fn as_generator(&'tcx self) -> GeneratorSubsts<'tcx> {
202 GeneratorSubsts { substs: self }
205 /// Creates a `InternalSubsts` that maps each generic parameter to itself.
206 pub fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx> {
207 Self::for_item(tcx, def_id, |param, _| tcx.mk_param_from_def(param))
210 /// Creates a `InternalSubsts` for generic parameter definitions,
211 /// by calling closures to obtain each kind.
212 /// The closures get to observe the `InternalSubsts` as they're
213 /// being built, which can be used to correctly
214 /// substitute defaults of generic parameters.
215 pub fn for_item<F>(tcx: TyCtxt<'tcx>, def_id: DefId, mut mk_kind: F) -> SubstsRef<'tcx>
217 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
219 let defs = tcx.generics_of(def_id);
220 let count = defs.count();
221 let mut substs = SmallVec::with_capacity(count);
222 Self::fill_item(&mut substs, tcx, defs, &mut mk_kind);
223 tcx.intern_substs(&substs)
226 pub fn extend_to<F>(&self, tcx: TyCtxt<'tcx>, def_id: DefId, mut mk_kind: F) -> SubstsRef<'tcx>
228 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
230 Self::for_item(tcx, def_id, |param, substs| {
231 self.get(param.index as usize).cloned().unwrap_or_else(|| mk_kind(param, substs))
236 substs: &mut SmallVec<[GenericArg<'tcx>; 8]>,
241 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
243 if let Some(def_id) = defs.parent {
244 let parent_defs = tcx.generics_of(def_id);
245 Self::fill_item(substs, tcx, parent_defs, mk_kind);
247 Self::fill_single(substs, defs, mk_kind)
251 substs: &mut SmallVec<[GenericArg<'tcx>; 8]>,
255 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
257 substs.reserve(defs.params.len());
258 for param in &defs.params {
259 let kind = mk_kind(param, substs);
260 assert_eq!(param.index as usize, substs.len());
265 pub fn is_noop(&self) -> bool {
270 pub fn types(&'a self) -> impl DoubleEndedIterator<Item = Ty<'tcx>> + 'a {
272 .filter_map(|k| if let GenericArgKind::Type(ty) = k.unpack() { Some(ty) } else { None })
276 pub fn regions(&'a self) -> impl DoubleEndedIterator<Item = ty::Region<'tcx>> + 'a {
277 self.iter().filter_map(|k| {
278 if let GenericArgKind::Lifetime(lt) = k.unpack() { Some(lt) } else { None }
283 pub fn consts(&'a self) -> impl DoubleEndedIterator<Item = &'tcx ty::Const<'tcx>> + 'a {
284 self.iter().filter_map(|k| {
285 if let GenericArgKind::Const(ct) = k.unpack() { Some(ct) } else { None }
290 pub fn non_erasable_generics(
292 ) -> impl DoubleEndedIterator<Item = GenericArgKind<'tcx>> + 'a {
293 self.iter().filter_map(|k| match k.unpack() {
294 GenericArgKind::Lifetime(_) => None,
295 generic => Some(generic),
300 pub fn type_at(&self, i: usize) -> Ty<'tcx> {
301 if let GenericArgKind::Type(ty) = self[i].unpack() {
304 bug!("expected type for param #{} in {:?}", i, self);
309 pub fn region_at(&self, i: usize) -> ty::Region<'tcx> {
310 if let GenericArgKind::Lifetime(lt) = self[i].unpack() {
313 bug!("expected region for param #{} in {:?}", i, self);
318 pub fn const_at(&self, i: usize) -> &'tcx ty::Const<'tcx> {
319 if let GenericArgKind::Const(ct) = self[i].unpack() {
322 bug!("expected const for param #{} in {:?}", i, self);
327 pub fn type_for_def(&self, def: &ty::GenericParamDef) -> GenericArg<'tcx> {
328 self.type_at(def.index as usize).into()
331 /// Transform from substitutions for a child of `source_ancestor`
332 /// (e.g., a trait or impl) to substitutions for the same child
333 /// in a different item, with `target_substs` as the base for
334 /// the target impl/trait, with the source child-specific
335 /// parameters (e.g., method parameters) on top of that base.
337 /// For example given:
340 /// trait X<S> { fn f<T>(); }
341 /// impl<U> X<U> for U { fn f<V>() {} }
344 /// * If `self` is `[Self, S, T]`: the identity substs of `f` in the trait.
345 /// * If `source_ancestor` is the def_id of the trait.
346 /// * If `target_substs` is `[U]`, the substs for the impl.
347 /// * Then we will return `[U, T]`, the subst for `f` in the impl that
348 /// are needed for it to match the trait.
352 source_ancestor: DefId,
353 target_substs: SubstsRef<'tcx>,
354 ) -> SubstsRef<'tcx> {
355 let defs = tcx.generics_of(source_ancestor);
356 tcx.mk_substs(target_substs.iter().chain(self.iter().skip(defs.params.len())))
359 pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> SubstsRef<'tcx> {
360 tcx.mk_substs(self.iter().take(generics.count()))
364 impl<'tcx> TypeFoldable<'tcx> for SubstsRef<'tcx> {
365 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
366 // This code is hot enough that it's worth specializing for the most
367 // common length lists, to avoid the overhead of `SmallVec` creation.
368 // The match arms are in order of frequency. The 1, 2, and 0 cases are
369 // typically hit in 90--99.99% of cases. When folding doesn't change
370 // the substs, it's faster to reuse the existing substs rather than
371 // calling `intern_substs`.
374 let param0 = self[0].fold_with(folder);
375 if param0 == self[0] { self } else { folder.tcx().intern_substs(&[param0]) }
378 let param0 = self[0].fold_with(folder);
379 let param1 = self[1].fold_with(folder);
380 if param0 == self[0] && param1 == self[1] {
383 folder.tcx().intern_substs(&[param0, param1])
388 let params: SmallVec<[_; 8]> = self.iter().map(|k| k.fold_with(folder)).collect();
389 if params[..] == self[..] { self } else { folder.tcx().intern_substs(¶ms) }
394 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
395 self.iter().any(|t| t.visit_with(visitor))
399 impl<'tcx> rustc_serialize::UseSpecializedDecodable for SubstsRef<'tcx> {}
401 ///////////////////////////////////////////////////////////////////////////
402 // Public trait `Subst`
404 // Just call `foo.subst(tcx, substs)` to perform a substitution across
405 // `foo`. Or use `foo.subst_spanned(tcx, substs, Some(span))` when
406 // there is more information available (for better errors).
408 pub trait Subst<'tcx>: Sized {
409 fn subst(&self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> Self {
410 self.subst_spanned(tcx, substs, None)
416 substs: &[GenericArg<'tcx>],
421 impl<'tcx, T: TypeFoldable<'tcx>> Subst<'tcx> for T {
425 substs: &[GenericArg<'tcx>],
429 SubstFolder { tcx, substs, span, root_ty: None, ty_stack_depth: 0, binders_passed: 0 };
430 (*self).fold_with(&mut folder)
434 ///////////////////////////////////////////////////////////////////////////
435 // The actual substitution engine itself is a type folder.
437 struct SubstFolder<'a, 'tcx> {
439 substs: &'a [GenericArg<'tcx>],
441 /// The location for which the substitution is performed, if available.
444 /// The root type that is being substituted, if available.
445 root_ty: Option<Ty<'tcx>>,
447 /// Depth of type stack
448 ty_stack_depth: usize,
450 /// Number of region binders we have passed through while doing the substitution
454 impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
455 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
459 fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
460 self.binders_passed += 1;
461 let t = t.super_fold_with(self);
462 self.binders_passed -= 1;
466 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
467 // Note: This routine only handles regions that are bound on
468 // type declarations and other outer declarations, not those
469 // bound in *fn types*. Region substitution of the bound
470 // regions that appear in a function signature is done using
471 // the specialized routine `ty::replace_late_regions()`.
473 ty::ReEarlyBound(data) => {
474 let rk = self.substs.get(data.index as usize).map(|k| k.unpack());
476 Some(GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
478 let span = self.span.unwrap_or(DUMMY_SP);
480 "Region parameter out of range \
481 when substituting in region {} (root type={:?}) \
483 data.name, self.root_ty, data.index
485 span_bug!(span, "{}", msg);
493 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
494 if !t.needs_subst() {
498 // track the root type we were asked to substitute
499 let depth = self.ty_stack_depth;
501 self.root_ty = Some(t);
503 self.ty_stack_depth += 1;
505 let t1 = match t.kind {
506 ty::Param(p) => self.ty_for_param(p, t),
507 _ => t.super_fold_with(self),
510 assert_eq!(depth + 1, self.ty_stack_depth);
511 self.ty_stack_depth -= 1;
519 fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
520 if !c.needs_subst() {
524 if let ty::ConstKind::Param(p) = c.val {
525 self.const_for_param(p, c)
527 c.super_fold_with(self)
532 impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
533 fn ty_for_param(&self, p: ty::ParamTy, source_ty: Ty<'tcx>) -> Ty<'tcx> {
534 // Look up the type in the substitutions. It really should be in there.
535 let opt_ty = self.substs.get(p.index as usize).map(|k| k.unpack());
536 let ty = match opt_ty {
537 Some(GenericArgKind::Type(ty)) => ty,
539 let span = self.span.unwrap_or(DUMMY_SP);
542 "expected type for `{:?}` ({:?}/{}) but found {:?} \
543 when substituting (root type={:?}) substs={:?}",
553 let span = self.span.unwrap_or(DUMMY_SP);
556 "type parameter `{:?}` ({:?}/{}) out of range \
557 when substituting (root type={:?}) substs={:?}",
567 self.shift_vars_through_binders(ty)
573 source_ct: &'tcx ty::Const<'tcx>,
574 ) -> &'tcx ty::Const<'tcx> {
575 // Look up the const in the substitutions. It really should be in there.
576 let opt_ct = self.substs.get(p.index as usize).map(|k| k.unpack());
577 let ct = match opt_ct {
578 Some(GenericArgKind::Const(ct)) => ct,
580 let span = self.span.unwrap_or(DUMMY_SP);
583 "expected const for `{:?}` ({:?}/{}) but found {:?} \
584 when substituting substs={:?}",
593 let span = self.span.unwrap_or(DUMMY_SP);
596 "const parameter `{:?}` ({:?}/{}) out of range \
597 when substituting substs={:?}",
606 self.shift_vars_through_binders(ct)
609 /// It is sometimes necessary to adjust the De Bruijn indices during substitution. This occurs
610 /// when we are substituting a type with escaping bound vars into a context where we have
611 /// passed through binders. That's quite a mouthful. Let's see an example:
614 /// type Func<A> = fn(A);
615 /// type MetaFunc = for<'a> fn(Func<&'a i32>)
618 /// The type `MetaFunc`, when fully expanded, will be
620 /// for<'a> fn(fn(&'a i32))
623 /// | | DebruijnIndex of 2
626 /// Here the `'a` lifetime is bound in the outer function, but appears as an argument of the
627 /// inner one. Therefore, that appearance will have a DebruijnIndex of 2, because we must skip
628 /// over the inner binder (remember that we count De Bruijn indices from 1). However, in the
629 /// definition of `MetaFunc`, the binder is not visible, so the type `&'a i32` will have a
630 /// De Bruijn index of 1. It's only during the substitution that we can see we must increase the
631 /// depth by 1 to account for the binder that we passed through.
633 /// As a second example, consider this twist:
636 /// type FuncTuple<A> = (A,fn(A));
637 /// type MetaFuncTuple = for<'a> fn(FuncTuple<&'a i32>)
640 /// Here the final type will be:
642 /// for<'a> fn((&'a i32, fn(&'a i32)))
645 /// DebruijnIndex of 1 |
646 /// DebruijnIndex of 2
648 /// As indicated in the diagram, here the same type `&'a i32` is substituted once, but in the
649 /// first case we do not increase the De Bruijn index and in the second case we do. The reason
650 /// is that only in the second case have we passed through a fn binder.
651 fn shift_vars_through_binders<T: TypeFoldable<'tcx>>(&self, val: T) -> T {
653 "shift_vars(val={:?}, binders_passed={:?}, has_escaping_bound_vars={:?})",
656 val.has_escaping_bound_vars()
659 if self.binders_passed == 0 || !val.has_escaping_bound_vars() {
663 let result = ty::fold::shift_vars(self.tcx(), &val, self.binders_passed);
664 debug!("shift_vars: shifted result = {:?}", result);
669 fn shift_region_through_binders(&self, region: ty::Region<'tcx>) -> ty::Region<'tcx> {
670 if self.binders_passed == 0 || !region.has_escaping_bound_vars() {
673 ty::fold::shift_region(self.tcx, region, self.binders_passed)
677 pub type CanonicalUserSubsts<'tcx> = Canonical<'tcx, UserSubsts<'tcx>>;
679 /// Stores the user-given substs to reach some fully qualified path
680 /// (e.g., `<T>::Item` or `<T as Trait>::Item`).
681 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
682 #[derive(HashStable, TypeFoldable, Lift)]
683 pub struct UserSubsts<'tcx> {
684 /// The substitutions for the item as given by the user.
685 pub substs: SubstsRef<'tcx>,
687 /// The self type, in the case of a `<T>::Item` path (when applied
688 /// to an inherent impl). See `UserSelfTy` below.
689 pub user_self_ty: Option<UserSelfTy<'tcx>>,
692 /// Specifies the user-given self type. In the case of a path that
693 /// refers to a member in an inherent impl, this self type is
694 /// sometimes needed to constrain the type parameters on the impl. For
695 /// example, in this code:
698 /// struct Foo<T> { }
699 /// impl<A> Foo<A> { fn method() { } }
702 /// when you then have a path like `<Foo<&'static u32>>::method`,
703 /// this struct would carry the `DefId` of the impl along with the
704 /// self type `Foo<u32>`. Then we can instantiate the parameters of
705 /// the impl (with the substs from `UserSubsts`) and apply those to
706 /// the self type, giving `Foo<?A>`. Finally, we unify that with
707 /// the self type here, which contains `?A` to be `&'static u32`
708 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
709 #[derive(HashStable, TypeFoldable, Lift)]
710 pub struct UserSelfTy<'tcx> {
711 pub impl_def_id: DefId,
712 pub self_ty: Ty<'tcx>,