1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module contains TypeVariants and its major components
14 use middle::def_id::DefId;
16 use middle::subst::{self, Substs};
18 use middle::ty::{self, AdtDef, ToPredicate, TypeFlags, Ty, TyCtxt, TyS, TypeFoldable};
19 use util::common::ErrorReported;
21 use collections::enum_set::{self, EnumSet, CLike};
26 use syntax::ast::{self, Name};
27 use syntax::parse::token::special_idents;
29 use serialize::{Decodable, Decoder};
33 use self::FnOutput::*;
35 use self::TypeVariants::*;
37 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
38 pub struct TypeAndMut<'tcx> {
40 pub mutbl: hir::Mutability,
43 #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash,
44 RustcEncodable, RustcDecodable, Copy)]
45 /// A "free" region `fr` can be interpreted as "some region
46 /// at least as big as the scope `fr.scope`".
47 pub struct FreeRegion {
48 pub scope: region::CodeExtent,
49 pub bound_region: BoundRegion
52 #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash,
53 RustcEncodable, RustcDecodable, Copy)]
54 pub enum BoundRegion {
55 /// An anonymous region parameter for a given fn (&T)
58 /// Named region parameters for functions (a in &'a T)
60 /// The def-id is needed to distinguish free regions in
61 /// the event of shadowing.
64 /// Fresh bound identifiers created during GLB computations.
67 // Anonymous region for the implicit env pointer parameter
72 // NB: If you change this, you'll probably want to change the corresponding
73 // AST structure in libsyntax/ast.rs as well.
74 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
75 pub enum TypeVariants<'tcx> {
76 /// The primitive boolean type. Written as `bool`.
79 /// The primitive character type; holds a Unicode scalar value
80 /// (a non-surrogate code point). Written as `char`.
83 /// A primitive signed integer type. For example, `i32`.
86 /// A primitive unsigned integer type. For example, `u32`.
89 /// A primitive floating-point type. For example, `f64`.
90 TyFloat(ast::FloatTy),
92 /// An enumerated type, defined with `enum`.
94 /// Substs here, possibly against intuition, *may* contain `TyParam`s.
95 /// That is, even after substitution it is possible that there are type
96 /// variables. This happens when the `TyEnum` corresponds to an enum
97 /// definition and not a concrete use of it. To get the correct `TyEnum`
98 /// from the tcx, use the `NodeId` from the `ast::Ty` and look it up in
99 /// the `ast_ty_to_ty_cache`. This is probably true for `TyStruct` as
101 TyEnum(AdtDef<'tcx>, &'tcx Substs<'tcx>),
103 /// A structure type, defined with `struct`.
105 /// See warning about substitutions for enumerated types.
106 TyStruct(AdtDef<'tcx>, &'tcx Substs<'tcx>),
108 /// `Box<T>`; this is nominally a struct in the documentation, but is
109 /// special-cased internally. For example, it is possible to implicitly
110 /// move the contents of a box out of that box, and methods of any type
111 /// can have type `Box<Self>`.
114 /// The pointee of a string slice. Written as `str`.
117 /// An array with the given length. Written as `[T; n]`.
118 TyArray(Ty<'tcx>, usize),
120 /// The pointee of an array slice. Written as `[T]`.
123 /// A raw pointer. Written as `*mut T` or `*const T`
124 TyRawPtr(TypeAndMut<'tcx>),
126 /// A reference; a pointer with an associated lifetime. Written as
127 /// `&a mut T` or `&'a T`.
128 TyRef(&'tcx Region, TypeAndMut<'tcx>),
130 /// The anonymous type of a function declaration/definition. Each
131 /// function has a unique type.
132 TyFnDef(DefId, &'tcx Substs<'tcx>, &'tcx BareFnTy<'tcx>),
134 /// A pointer to a function. Written as `fn() -> i32`.
135 /// FIXME: This is currently also used to represent the callee of a method;
136 /// see ty::MethodCallee etc.
137 TyFnPtr(&'tcx BareFnTy<'tcx>),
139 /// A trait, defined with `trait`.
140 TyTrait(Box<TraitTy<'tcx>>),
142 /// The anonymous type of a closure. Used to represent the type of
144 TyClosure(DefId, Box<ClosureSubsts<'tcx>>),
146 /// A tuple type. For example, `(i32, bool)`.
147 TyTuple(Vec<Ty<'tcx>>),
149 /// The projection of an associated type. For example,
150 /// `<T as Trait<..>>::N`.
151 TyProjection(ProjectionTy<'tcx>),
153 /// A type parameter; for example, `T` in `fn f<T>(x: T) {}
156 /// A type variable used during type-checking.
159 /// A placeholder for a type which could not be computed; this is
160 /// propagated to avoid useless error messages.
164 /// A closure can be modeled as a struct that looks like:
166 /// struct Closure<'l0...'li, T0...Tj, U0...Uk> {
172 /// where 'l0...'li and T0...Tj are the lifetime and type parameters
173 /// in scope on the function that defined the closure, and U0...Uk are
174 /// type parameters representing the types of its upvars (borrowed, if
177 /// So, for example, given this function:
179 /// fn foo<'a, T>(data: &'a mut T) {
180 /// do(|| data.count += 1)
183 /// the type of the closure would be something like:
185 /// struct Closure<'a, T, U0> {
189 /// Note that the type of the upvar is not specified in the struct.
190 /// You may wonder how the impl would then be able to use the upvar,
191 /// if it doesn't know it's type? The answer is that the impl is
192 /// (conceptually) not fully generic over Closure but rather tied to
193 /// instances with the expected upvar types:
195 /// impl<'b, 'a, T> FnMut() for Closure<'a, T, &'b mut &'a mut T> {
199 /// You can see that the *impl* fully specified the type of the upvar
200 /// and thus knows full well that `data` has type `&'b mut &'a mut T`.
201 /// (Here, I am assuming that `data` is mut-borrowed.)
203 /// Now, the last question you may ask is: Why include the upvar types
204 /// as extra type parameters? The reason for this design is that the
205 /// upvar types can reference lifetimes that are internal to the
206 /// creating function. In my example above, for example, the lifetime
207 /// `'b` represents the extent of the closure itself; this is some
208 /// subset of `foo`, probably just the extent of the call to the to
209 /// `do()`. If we just had the lifetime/type parameters from the
210 /// enclosing function, we couldn't name this lifetime `'b`. Note that
211 /// there can also be lifetimes in the types of the upvars themselves,
212 /// if one of them happens to be a reference to something that the
213 /// creating fn owns.
215 /// OK, you say, so why not create a more minimal set of parameters
216 /// that just includes the extra lifetime parameters? The answer is
217 /// primarily that it would be hard --- we don't know at the time when
218 /// we create the closure type what the full types of the upvars are,
219 /// nor do we know which are borrowed and which are not. In this
220 /// design, we can just supply a fresh type parameter and figure that
223 /// All right, you say, but why include the type parameters from the
224 /// original function then? The answer is that trans may need them
225 /// when monomorphizing, and they may not appear in the upvars. A
226 /// closure could capture no variables but still make use of some
227 /// in-scope type parameter with a bound (e.g., if our example above
228 /// had an extra `U: Default`, and the closure called `U::default()`).
230 /// There is another reason. This design (implicitly) prohibits
231 /// closures from capturing themselves (except via a trait
232 /// object). This simplifies closure inference considerably, since it
233 /// means that when we infer the kind of a closure or its upvars, we
234 /// don't have to handle cycles where the decisions we make for
235 /// closure C wind up influencing the decisions we ought to make for
236 /// closure C (which would then require fixed point iteration to
237 /// handle). Plus it fixes an ICE. :P
238 #[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
239 pub struct ClosureSubsts<'tcx> {
240 /// Lifetime and type parameters from the enclosing function.
241 /// These are separated out because trans wants to pass them around
242 /// when monomorphizing.
243 pub func_substs: &'tcx Substs<'tcx>,
245 /// The types of the upvars. The list parallels the freevars and
246 /// `upvar_borrows` lists. These are kept distinct so that we can
247 /// easily index into them.
248 pub upvar_tys: Vec<Ty<'tcx>>
251 impl<'tcx> Decodable for &'tcx ClosureSubsts<'tcx> {
252 fn decode<S: Decoder>(s: &mut S) -> Result<&'tcx ClosureSubsts<'tcx>, S::Error> {
253 let closure_substs = Decodable::decode(s)?;
254 let dummy_def_id: DefId = unsafe { mem::zeroed() };
256 cstore::tls::with_decoding_context(s, |dcx, _| {
258 let ty = dcx.tcx().mk_closure_from_closure_substs(dummy_def_id,
259 Box::new(closure_substs));
261 TyClosure(_, ref closure_substs) => Ok(&**closure_substs),
268 #[derive(Clone, PartialEq, Eq, Hash)]
269 pub struct TraitTy<'tcx> {
270 pub principal: ty::PolyTraitRef<'tcx>,
271 pub bounds: ExistentialBounds<'tcx>,
274 impl<'tcx> TraitTy<'tcx> {
275 pub fn principal_def_id(&self) -> DefId {
276 self.principal.0.def_id
279 /// Object types don't have a self-type specified. Therefore, when
280 /// we convert the principal trait-ref into a normal trait-ref,
281 /// you must give *some* self-type. A common choice is `mk_err()`
282 /// or some skolemized type.
283 pub fn principal_trait_ref_with_self_ty(&self,
286 -> ty::PolyTraitRef<'tcx>
288 // otherwise the escaping regions would be captured by the binder
289 assert!(!self_ty.has_escaping_regions());
291 ty::Binder(TraitRef {
292 def_id: self.principal.0.def_id,
293 substs: tcx.mk_substs(self.principal.0.substs.with_self_ty(self_ty)),
297 pub fn projection_bounds_with_self_ty(&self,
300 -> Vec<ty::PolyProjectionPredicate<'tcx>>
302 // otherwise the escaping regions would be captured by the binders
303 assert!(!self_ty.has_escaping_regions());
305 self.bounds.projection_bounds.iter()
306 .map(|in_poly_projection_predicate| {
307 let in_projection_ty = &in_poly_projection_predicate.0.projection_ty;
308 let substs = tcx.mk_substs(in_projection_ty.trait_ref.substs.with_self_ty(self_ty));
309 let trait_ref = ty::TraitRef::new(in_projection_ty.trait_ref.def_id,
311 let projection_ty = ty::ProjectionTy {
312 trait_ref: trait_ref,
313 item_name: in_projection_ty.item_name
315 ty::Binder(ty::ProjectionPredicate {
316 projection_ty: projection_ty,
317 ty: in_poly_projection_predicate.0.ty
324 /// A complete reference to a trait. These take numerous guises in syntax,
325 /// but perhaps the most recognizable form is in a where clause:
329 /// This would be represented by a trait-reference where the def-id is the
330 /// def-id for the trait `Foo` and the substs defines `T` as parameter 0 in the
331 /// `SelfSpace` and `U` as parameter 0 in the `TypeSpace`.
333 /// Trait references also appear in object types like `Foo<U>`, but in
334 /// that case the `Self` parameter is absent from the substitutions.
336 /// Note that a `TraitRef` introduces a level of region binding, to
337 /// account for higher-ranked trait bounds like `T : for<'a> Foo<&'a
338 /// U>` or higher-ranked object types.
339 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
340 pub struct TraitRef<'tcx> {
342 pub substs: &'tcx Substs<'tcx>,
345 pub type PolyTraitRef<'tcx> = Binder<TraitRef<'tcx>>;
347 impl<'tcx> PolyTraitRef<'tcx> {
348 pub fn self_ty(&self) -> Ty<'tcx> {
352 pub fn def_id(&self) -> DefId {
356 pub fn substs(&self) -> &'tcx Substs<'tcx> {
357 // FIXME(#20664) every use of this fn is probably a bug, it should yield Binder<>
361 pub fn input_types(&self) -> &[Ty<'tcx>] {
362 // FIXME(#20664) every use of this fn is probably a bug, it should yield Binder<>
366 pub fn to_poly_trait_predicate(&self) -> ty::PolyTraitPredicate<'tcx> {
367 // Note that we preserve binding levels
368 Binder(ty::TraitPredicate { trait_ref: self.0.clone() })
372 /// Binder is a binder for higher-ranked lifetimes. It is part of the
373 /// compiler's representation for things like `for<'a> Fn(&'a isize)`
374 /// (which would be represented by the type `PolyTraitRef ==
375 /// Binder<TraitRef>`). Note that when we skolemize, instantiate,
376 /// erase, or otherwise "discharge" these bound regions, we change the
377 /// type from `Binder<T>` to just `T` (see
378 /// e.g. `liberate_late_bound_regions`).
379 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
380 pub struct Binder<T>(pub T);
383 /// Skips the binder and returns the "bound" value. This is a
384 /// risky thing to do because it's easy to get confused about
385 /// debruijn indices and the like. It is usually better to
386 /// discharge the binder using `no_late_bound_regions` or
387 /// `replace_late_bound_regions` or something like
388 /// that. `skip_binder` is only valid when you are either
389 /// extracting data that has nothing to do with bound regions, you
390 /// are doing some sort of test that does not involve bound
391 /// regions, or you are being very careful about your depth
394 /// Some examples where `skip_binder` is reasonable:
395 /// - extracting the def-id from a PolyTraitRef;
396 /// - comparing the self type of a PolyTraitRef to see if it is equal to
397 /// a type parameter `X`, since the type `X` does not reference any regions
398 pub fn skip_binder(&self) -> &T {
402 pub fn as_ref(&self) -> Binder<&T> {
406 pub fn map_bound_ref<F,U>(&self, f: F) -> Binder<U>
407 where F: FnOnce(&T) -> U
409 self.as_ref().map_bound(f)
412 pub fn map_bound<F,U>(self, f: F) -> Binder<U>
413 where F: FnOnce(T) -> U
415 ty::Binder(f(self.0))
419 impl fmt::Debug for TypeFlags {
420 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
421 write!(f, "{}", self.bits)
425 /// Represents the projection of an associated type. In explicit UFCS
426 /// form this would be written `<T as Trait<..>>::N`.
427 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
428 pub struct ProjectionTy<'tcx> {
429 /// The trait reference `T as Trait<..>`.
430 pub trait_ref: ty::TraitRef<'tcx>,
432 /// The name `N` of the associated type.
436 impl<'tcx> ProjectionTy<'tcx> {
437 pub fn sort_key(&self) -> (DefId, Name) {
438 (self.trait_ref.def_id, self.item_name)
442 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
443 pub struct BareFnTy<'tcx> {
444 pub unsafety: hir::Unsafety,
446 pub sig: PolyFnSig<'tcx>,
449 #[derive(Clone, PartialEq, Eq, Hash)]
450 pub struct ClosureTy<'tcx> {
451 pub unsafety: hir::Unsafety,
453 pub sig: PolyFnSig<'tcx>,
456 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
457 pub enum FnOutput<'tcx> {
458 FnConverging(Ty<'tcx>),
462 impl<'tcx> FnOutput<'tcx> {
463 pub fn diverges(&self) -> bool {
467 pub fn unwrap(self) -> Ty<'tcx> {
469 ty::FnConverging(t) => t,
470 ty::FnDiverging => unreachable!()
474 pub fn unwrap_or(self, def: Ty<'tcx>) -> Ty<'tcx> {
476 ty::FnConverging(t) => t,
477 ty::FnDiverging => def
482 pub type PolyFnOutput<'tcx> = Binder<FnOutput<'tcx>>;
484 impl<'tcx> PolyFnOutput<'tcx> {
485 pub fn diverges(&self) -> bool {
490 /// Signature of a function type, which I have arbitrarily
491 /// decided to use to refer to the input/output types.
493 /// - `inputs` is the list of arguments and their modes.
494 /// - `output` is the return type.
495 /// - `variadic` indicates whether this is a variadic function. (only true for foreign fns)
496 #[derive(Clone, PartialEq, Eq, Hash)]
497 pub struct FnSig<'tcx> {
498 pub inputs: Vec<Ty<'tcx>>,
499 pub output: FnOutput<'tcx>,
503 pub type PolyFnSig<'tcx> = Binder<FnSig<'tcx>>;
505 impl<'tcx> PolyFnSig<'tcx> {
506 pub fn inputs(&self) -> ty::Binder<Vec<Ty<'tcx>>> {
507 self.map_bound_ref(|fn_sig| fn_sig.inputs.clone())
509 pub fn input(&self, index: usize) -> ty::Binder<Ty<'tcx>> {
510 self.map_bound_ref(|fn_sig| fn_sig.inputs[index])
512 pub fn output(&self) -> ty::Binder<FnOutput<'tcx>> {
513 self.map_bound_ref(|fn_sig| fn_sig.output.clone())
515 pub fn variadic(&self) -> bool {
516 self.skip_binder().variadic
520 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
522 pub space: subst::ParamSpace,
528 pub fn new(space: subst::ParamSpace,
532 ParamTy { space: space, idx: index, name: name }
535 pub fn for_self() -> ParamTy {
536 ParamTy::new(subst::SelfSpace, 0, special_idents::type_self.name)
539 pub fn for_def(def: &ty::TypeParameterDef) -> ParamTy {
540 ParamTy::new(def.space, def.index, def.name)
543 pub fn to_ty<'tcx>(self, tcx: &TyCtxt<'tcx>) -> Ty<'tcx> {
544 tcx.mk_param(self.space, self.idx, self.name)
547 pub fn is_self(&self) -> bool {
548 self.space == subst::SelfSpace && self.idx == 0
552 /// A [De Bruijn index][dbi] is a standard means of representing
553 /// regions (and perhaps later types) in a higher-ranked setting. In
554 /// particular, imagine a type like this:
556 /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
559 /// | +------------+ 1 | |
561 /// +--------------------------------+ 2 |
563 /// +------------------------------------------+ 1
565 /// In this type, there are two binders (the outer fn and the inner
566 /// fn). We need to be able to determine, for any given region, which
567 /// fn type it is bound by, the inner or the outer one. There are
568 /// various ways you can do this, but a De Bruijn index is one of the
569 /// more convenient and has some nice properties. The basic idea is to
570 /// count the number of binders, inside out. Some examples should help
571 /// clarify what I mean.
573 /// Let's start with the reference type `&'b isize` that is the first
574 /// argument to the inner function. This region `'b` is assigned a De
575 /// Bruijn index of 1, meaning "the innermost binder" (in this case, a
576 /// fn). The region `'a` that appears in the second argument type (`&'a
577 /// isize`) would then be assigned a De Bruijn index of 2, meaning "the
578 /// second-innermost binder". (These indices are written on the arrays
581 /// What is interesting is that De Bruijn index attached to a particular
582 /// variable will vary depending on where it appears. For example,
583 /// the final type `&'a char` also refers to the region `'a` declared on
584 /// the outermost fn. But this time, this reference is not nested within
585 /// any other binders (i.e., it is not an argument to the inner fn, but
586 /// rather the outer one). Therefore, in this case, it is assigned a
587 /// De Bruijn index of 1, because the innermost binder in that location
590 /// [dbi]: http://en.wikipedia.org/wiki/De_Bruijn_index
591 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug, Copy)]
592 pub struct DebruijnIndex {
593 // We maintain the invariant that this is never 0. So 1 indicates
594 // the innermost binder. To ensure this, create with `DebruijnIndex::new`.
598 /// Representation of regions.
600 /// Unlike types, most region variants are "fictitious", not concrete,
601 /// regions. Among these, `ReStatic`, `ReEmpty` and `ReScope` are the only
602 /// ones representing concrete regions.
606 /// These are regions that are stored behind a binder and must be substituted
607 /// with some concrete region before being used. There are 2 kind of
608 /// bound regions: early-bound, which are bound in a TypeScheme/TraitDef,
609 /// and are substituted by a Substs, and late-bound, which are part of
610 /// higher-ranked types (e.g. `for<'a> fn(&'a ())`) and are substituted by
611 /// the likes of `liberate_late_bound_regions`. The distinction exists
612 /// because higher-ranked lifetimes aren't supported in all places. See [1][2].
614 /// Unlike TyParam-s, bound regions are not supposed to exist "in the wild"
615 /// outside their binder, e.g. in types passed to type inference, and
616 /// should first be substituted (by skolemized regions, free regions,
617 /// or region variables).
619 /// ## Skolemized and Free Regions
621 /// One often wants to work with bound regions without knowing their precise
622 /// identity. For example, when checking a function, the lifetime of a borrow
623 /// can end up being assigned to some region parameter. In these cases,
624 /// it must be ensured that bounds on the region can't be accidentally
625 /// assumed without being checked.
627 /// The process of doing that is called "skolemization". The bound regions
628 /// are replaced by skolemized markers, which don't satisfy any relation
629 /// not explicity provided.
631 /// There are 2 kinds of skolemized regions in rustc: `ReFree` and
632 /// `ReSkolemized`. When checking an item's body, `ReFree` is supposed
633 /// to be used. These also support explicit bounds: both the internally-stored
634 /// *scope*, which the region is assumed to outlive, as well as other
635 /// relations stored in the `FreeRegionMap`. Note that these relations
636 /// aren't checked when you `make_subregion` (or `mk_eqty`), only by
637 /// `resolve_regions_and_report_errors`.
639 /// When working with higher-ranked types, some region relations aren't
640 /// yet known, so you can't just call `resolve_regions_and_report_errors`.
641 /// `ReSkolemized` is designed for this purpose. In these contexts,
642 /// there's also the risk that some inference variable laying around will
643 /// get unified with your skolemized region: if you want to check whether
644 /// `for<'a> Foo<'_>: 'a`, and you substitute your bound region `'a`
645 /// with a skolemized region `'%a`, the variable `'_` would just be
646 /// instantiated to the skolemized region `'%a`, which is wrong because
647 /// the inference variable is supposed to satisfy the relation
648 /// *for every value of the skolemized region*. To ensure that doesn't
649 /// happen, you can use `leak_check`. This is more clearly explained
650 /// by infer/higher_ranked/README.md.
652 /// [1] http://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
653 /// [2] http://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
654 #[derive(Clone, PartialEq, Eq, Hash, Copy, RustcEncodable, RustcDecodable)]
656 // Region bound in a type or fn declaration which will be
657 // substituted 'early' -- that is, at the same time when type
658 // parameters are substituted.
659 ReEarlyBound(EarlyBoundRegion),
661 // Region bound in a function scope, which will be substituted when the
662 // function is called.
663 ReLateBound(DebruijnIndex, BoundRegion),
665 /// When checking a function body, the types of all arguments and so forth
666 /// that refer to bound region parameters are modified to refer to free
667 /// region parameters.
670 /// A concrete region naming some statically determined extent
671 /// (e.g. an expression or sequence of statements) within the
672 /// current function.
673 ReScope(region::CodeExtent),
675 /// Static data that has an "infinite" lifetime. Top in the region lattice.
678 /// A region variable. Should not exist after typeck.
681 /// A skolemized region - basically the higher-ranked version of ReFree.
682 /// Should not exist after typeck.
683 ReSkolemized(SkolemizedRegionVid, BoundRegion),
685 /// Empty lifetime is for data that is never accessed.
686 /// Bottom in the region lattice. We treat ReEmpty somewhat
687 /// specially; at least right now, we do not generate instances of
688 /// it during the GLB computations, but rather
689 /// generate an error instead. This is to improve error messages.
690 /// The only way to get an instance of ReEmpty is to have a region
691 /// variable with no constraints.
695 #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
696 pub struct EarlyBoundRegion {
697 pub space: subst::ParamSpace,
702 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
707 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
712 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
713 pub struct FloatVid {
717 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
718 pub struct RegionVid {
722 #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
723 pub struct SkolemizedRegionVid {
727 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
733 /// A `FreshTy` is one that is generated as a replacement for an
734 /// unbound type variable. This is convenient for caching etc. See
735 /// `middle::infer::freshen` for more details.
741 /// Bounds suitable for an existentially quantified type parameter
742 /// such as those that appear in object types or closure types.
743 #[derive(PartialEq, Eq, Hash, Clone)]
744 pub struct ExistentialBounds<'tcx> {
745 pub region_bound: ty::Region,
746 pub builtin_bounds: BuiltinBounds,
747 pub projection_bounds: Vec<ty::PolyProjectionPredicate<'tcx>>,
750 impl<'tcx> ExistentialBounds<'tcx> {
751 pub fn new(region_bound: ty::Region,
752 builtin_bounds: BuiltinBounds,
753 projection_bounds: Vec<ty::PolyProjectionPredicate<'tcx>>)
755 let mut projection_bounds = projection_bounds;
756 projection_bounds.sort_by(|a, b| a.sort_key().cmp(&b.sort_key()));
758 region_bound: region_bound,
759 builtin_bounds: builtin_bounds,
760 projection_bounds: projection_bounds
765 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
766 pub struct BuiltinBounds(EnumSet<BuiltinBound>);
769 pub fn empty() -> BuiltinBounds {
770 BuiltinBounds(EnumSet::new())
773 pub fn iter(&self) -> enum_set::Iter<BuiltinBound> {
777 pub fn to_predicates<'tcx>(&self,
779 self_ty: Ty<'tcx>) -> Vec<ty::Predicate<'tcx>> {
780 self.iter().filter_map(|builtin_bound|
781 match traits::trait_ref_for_builtin_bound(tcx, builtin_bound, self_ty) {
782 Ok(trait_ref) => Some(trait_ref.to_predicate()),
783 Err(ErrorReported) => { None }
789 impl ops::Deref for BuiltinBounds {
790 type Target = EnumSet<BuiltinBound>;
791 fn deref(&self) -> &Self::Target { &self.0 }
794 impl ops::DerefMut for BuiltinBounds {
795 fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
798 impl<'a> IntoIterator for &'a BuiltinBounds {
799 type Item = BuiltinBound;
800 type IntoIter = enum_set::Iter<BuiltinBound>;
801 fn into_iter(self) -> Self::IntoIter {
806 #[derive(Clone, RustcEncodable, PartialEq, Eq, RustcDecodable, Hash,
809 pub enum BuiltinBound {
816 impl CLike for BuiltinBound {
817 fn to_usize(&self) -> usize {
820 fn from_usize(v: usize) -> BuiltinBound {
821 unsafe { mem::transmute(v) }
825 impl<'tcx> TyCtxt<'tcx> {
826 pub fn try_add_builtin_trait(&self,
828 builtin_bounds: &mut EnumSet<BuiltinBound>)
831 //! Checks whether `trait_ref` refers to one of the builtin
832 //! traits, like `Send`, and adds the corresponding
833 //! bound to the set `builtin_bounds` if so. Returns true if `trait_ref`
834 //! is a builtin trait.
836 match self.lang_items.to_builtin_kind(trait_def_id) {
837 Some(bound) => { builtin_bounds.insert(bound); true }
844 pub fn new(depth: u32) -> DebruijnIndex {
846 DebruijnIndex { depth: depth }
849 pub fn shifted(&self, amount: u32) -> DebruijnIndex {
850 DebruijnIndex { depth: self.depth + amount }
856 pub fn is_bound(&self) -> bool {
858 ty::ReEarlyBound(..) => true,
859 ty::ReLateBound(..) => true,
864 pub fn needs_infer(&self) -> bool {
866 ty::ReVar(..) | ty::ReSkolemized(..) => true,
871 pub fn escapes_depth(&self, depth: u32) -> bool {
873 ty::ReLateBound(debruijn, _) => debruijn.depth > depth,
878 /// Returns the depth of `self` from the (1-based) binding level `depth`
879 pub fn from_depth(&self, depth: u32) -> Region {
881 ty::ReLateBound(debruijn, r) => ty::ReLateBound(DebruijnIndex {
882 depth: debruijn.depth - (depth - 1)
890 impl<'tcx> TyS<'tcx> {
891 pub fn as_opt_param_ty(&self) -> Option<ty::ParamTy> {
893 ty::TyParam(ref d) => Some(d.clone()),
898 pub fn is_nil(&self) -> bool {
900 TyTuple(ref tys) => tys.is_empty(),
905 pub fn is_empty(&self, _cx: &TyCtxt) -> bool {
906 // FIXME(#24885): be smarter here
908 TyEnum(def, _) | TyStruct(def, _) => def.is_empty(),
913 pub fn is_primitive(&self) -> bool {
915 TyBool | TyChar | TyInt(_) | TyUint(_) | TyFloat(_) => true,
920 pub fn is_ty_var(&self) -> bool {
922 TyInfer(TyVar(_)) => true,
927 pub fn is_phantom_data(&self) -> bool {
928 if let TyStruct(def, _) = self.sty {
929 def.is_phantom_data()
935 pub fn is_bool(&self) -> bool { self.sty == TyBool }
937 pub fn is_param(&self, space: subst::ParamSpace, index: u32) -> bool {
939 ty::TyParam(ref data) => data.space == space && data.idx == index,
944 pub fn is_self(&self) -> bool {
946 TyParam(ref p) => p.space == subst::SelfSpace,
951 pub fn is_slice(&self) -> bool {
953 TyRawPtr(mt) | TyRef(_, mt) => match mt.ty.sty {
954 TySlice(_) | TyStr => true,
961 pub fn is_structural(&self) -> bool {
963 TyStruct(..) | TyTuple(_) | TyEnum(..) |
964 TyArray(..) | TyClosure(..) => true,
965 _ => self.is_slice() | self.is_trait()
970 pub fn is_simd(&self) -> bool {
972 TyStruct(def, _) => def.is_simd(),
977 pub fn sequence_element_type(&self, cx: &TyCtxt<'tcx>) -> Ty<'tcx> {
979 TyArray(ty, _) | TySlice(ty) => ty,
980 TyStr => cx.mk_mach_uint(ast::UintTy::U8),
981 _ => cx.sess.bug(&format!("sequence_element_type called on non-sequence value: {}",
986 pub fn simd_type(&self, cx: &TyCtxt<'tcx>) -> Ty<'tcx> {
988 TyStruct(def, substs) => {
989 def.struct_variant().fields[0].ty(cx, substs)
991 _ => panic!("simd_type called on invalid type")
995 pub fn simd_size(&self, _cx: &TyCtxt) -> usize {
997 TyStruct(def, _) => def.struct_variant().fields.len(),
998 _ => panic!("simd_size called on invalid type")
1002 pub fn is_region_ptr(&self) -> bool {
1009 pub fn is_unsafe_ptr(&self) -> bool {
1011 TyRawPtr(_) => return true,
1016 pub fn is_unique(&self) -> bool {
1024 A scalar type is one that denotes an atomic datum, with no sub-components.
1025 (A TyRawPtr is scalar because it represents a non-managed pointer, so its
1026 contents are abstract to rustc.)
1028 pub fn is_scalar(&self) -> bool {
1030 TyBool | TyChar | TyInt(_) | TyFloat(_) | TyUint(_) |
1031 TyInfer(IntVar(_)) | TyInfer(FloatVar(_)) |
1032 TyFnDef(..) | TyFnPtr(_) | TyRawPtr(_) => true,
1037 /// Returns true if this type is a floating point type and false otherwise.
1038 pub fn is_floating_point(&self) -> bool {
1041 TyInfer(FloatVar(_)) => true,
1046 pub fn is_trait(&self) -> bool {
1048 TyTrait(..) => true,
1053 pub fn is_integral(&self) -> bool {
1055 TyInfer(IntVar(_)) | TyInt(_) | TyUint(_) => true,
1060 pub fn is_fresh(&self) -> bool {
1062 TyInfer(FreshTy(_)) => true,
1063 TyInfer(FreshIntTy(_)) => true,
1064 TyInfer(FreshFloatTy(_)) => true,
1069 pub fn is_uint(&self) -> bool {
1071 TyInfer(IntVar(_)) | TyUint(ast::UintTy::Us) => true,
1076 pub fn is_char(&self) -> bool {
1083 pub fn is_fp(&self) -> bool {
1085 TyInfer(FloatVar(_)) | TyFloat(_) => true,
1090 pub fn is_numeric(&self) -> bool {
1091 self.is_integral() || self.is_fp()
1094 pub fn is_signed(&self) -> bool {
1101 pub fn is_machine(&self) -> bool {
1103 TyInt(ast::IntTy::Is) | TyUint(ast::UintTy::Us) => false,
1104 TyInt(..) | TyUint(..) | TyFloat(..) => true,
1109 pub fn has_concrete_skeleton(&self) -> bool {
1111 TyParam(_) | TyInfer(_) | TyError => false,
1116 // Returns the type and mutability of *ty.
1118 // The parameter `explicit` indicates if this is an *explicit* dereference.
1119 // Some types---notably unsafe ptrs---can only be dereferenced explicitly.
1120 pub fn builtin_deref(&self, explicit: bool, pref: ty::LvaluePreference)
1121 -> Option<TypeAndMut<'tcx>>
1127 mutbl: if pref == ty::PreferMutLvalue {
1134 TyRef(_, mt) => Some(mt),
1135 TyRawPtr(mt) if explicit => Some(mt),
1140 // Returns the type of ty[i]
1141 pub fn builtin_index(&self) -> Option<Ty<'tcx>> {
1143 TyArray(ty, _) | TySlice(ty) => Some(ty),
1148 pub fn fn_sig(&self) -> &'tcx PolyFnSig<'tcx> {
1150 TyFnDef(_, _, ref f) | TyFnPtr(ref f) => &f.sig,
1151 _ => panic!("Ty::fn_sig() called on non-fn type: {:?}", self)
1155 /// Returns the ABI of the given function.
1156 pub fn fn_abi(&self) -> abi::Abi {
1158 TyFnDef(_, _, ref f) | TyFnPtr(ref f) => f.abi,
1159 _ => panic!("Ty::fn_abi() called on non-fn type"),
1163 // Type accessors for substructures of types
1164 pub fn fn_args(&self) -> ty::Binder<Vec<Ty<'tcx>>> {
1165 self.fn_sig().inputs()
1168 pub fn fn_ret(&self) -> Binder<FnOutput<'tcx>> {
1169 self.fn_sig().output()
1172 pub fn is_fn(&self) -> bool {
1174 TyFnDef(..) | TyFnPtr(_) => true,
1179 pub fn ty_to_def_id(&self) -> Option<DefId> {
1181 TyTrait(ref tt) => Some(tt.principal_def_id()),
1183 TyEnum(def, _) => Some(def.did),
1184 TyClosure(id, _) => Some(id),
1189 pub fn ty_adt_def(&self) -> Option<AdtDef<'tcx>> {
1191 TyStruct(adt, _) | TyEnum(adt, _) => Some(adt),
1196 /// Returns the regions directly referenced from this type (but
1197 /// not types reachable from this type via `walk_tys`). This
1198 /// ignores late-bound regions binders.
1199 pub fn regions(&self) -> Vec<ty::Region> {
1201 TyRef(region, _) => {
1204 TyTrait(ref obj) => {
1205 let mut v = vec![obj.bounds.region_bound];
1206 v.extend_from_slice(obj.principal.skip_binder()
1207 .substs.regions().as_slice());
1211 TyStruct(_, substs) => {
1212 substs.regions().as_slice().to_vec()
1214 TyClosure(_, ref substs) => {
1215 substs.func_substs.regions().as_slice().to_vec()
1217 TyProjection(ref data) => {
1218 data.trait_ref.substs.regions().as_slice().to_vec()