]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/ty/mod.rs
Remove unused imports
[rust.git] / src / librustc / middle / ty / mod.rs
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.
4 //
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.
10
11 pub use self::ImplOrTraitItemId::*;
12 pub use self::ClosureKind::*;
13 pub use self::Variance::*;
14 pub use self::DtorKind::*;
15 pub use self::ExplicitSelfCategory::*;
16 pub use self::ImplOrTraitItemContainer::*;
17 pub use self::BorrowKind::*;
18 pub use self::ImplOrTraitItem::*;
19 pub use self::IntVarValue::*;
20 pub use self::LvaluePreference::*;
21
22 use front::map as ast_map;
23 use front::map::LinkedPath;
24 use middle;
25 use middle::cstore::{self, CrateStore, LOCAL_CRATE};
26 use middle::def::{self, ExportMap};
27 use middle::def_id::DefId;
28 use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
29 use middle::region::{CodeExtent};
30 use middle::subst::{self, Subst, Substs, VecPerParamSpace};
31 use middle::traits;
32 use middle::ty;
33 use middle::ty::fold::TypeFolder;
34 use middle::ty::walk::TypeWalker;
35 use util::common::memoized;
36 use util::nodemap::{NodeMap, NodeSet, DefIdMap};
37 use util::nodemap::FnvHashMap;
38
39 use serialize::{Encodable, Encoder, Decodable, Decoder};
40 use std::borrow::{Borrow, Cow};
41 use std::cell::{Cell, RefCell};
42 use std::hash::{Hash, Hasher};
43 use std::iter;
44 use std::rc::Rc;
45 use std::slice;
46 use std::vec::IntoIter;
47 use std::collections::{HashMap, HashSet};
48 use syntax::ast::{self, CrateNum, Name, NodeId};
49 use syntax::attr::{self, AttrMetaMethods};
50 use syntax::codemap::Span;
51 use syntax::parse::token::{InternedString, special_idents};
52
53 use rustc_front::hir;
54 use rustc_front::hir::{ItemImpl, ItemTrait};
55
56 pub use self::sty::{Binder, DebruijnIndex};
57 pub use self::sty::{BuiltinBound, BuiltinBounds, ExistentialBounds};
58 pub use self::sty::{BareFnTy, FnSig, PolyFnSig, FnOutput, PolyFnOutput};
59 pub use self::sty::{ClosureTy, InferTy, ParamTy, ProjectionTy, TraitTy};
60 pub use self::sty::{ClosureSubsts, TypeAndMut};
61 pub use self::sty::{TraitRef, TypeVariants, PolyTraitRef};
62 pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region};
63 pub use self::sty::{TyVid, IntVid, FloatVid, RegionVid, SkolemizedRegionVid};
64 pub use self::sty::BoundRegion::*;
65 pub use self::sty::FnOutput::*;
66 pub use self::sty::InferTy::*;
67 pub use self::sty::Region::*;
68 pub use self::sty::TypeVariants::*;
69
70 pub use self::sty::BuiltinBound::Send as BoundSend;
71 pub use self::sty::BuiltinBound::Sized as BoundSized;
72 pub use self::sty::BuiltinBound::Copy as BoundCopy;
73 pub use self::sty::BuiltinBound::Sync as BoundSync;
74
75 pub use self::contents::TypeContents;
76 pub use self::context::{ctxt, tls};
77 pub use self::context::{CtxtArenas, Lift, Tables};
78
79 pub mod adjustment;
80 pub mod cast;
81 pub mod error;
82 pub mod fast_reject;
83 pub mod fold;
84 pub mod _match;
85 pub mod outlives;
86 pub mod relate;
87 pub mod walk;
88 pub mod wf;
89 pub mod util;
90
91 mod contents;
92 mod context;
93 mod flags;
94 mod ivar;
95 mod structural_impls;
96 mod sty;
97
98 pub type Disr = u64;
99 pub const INITIAL_DISCRIMINANT_VALUE: Disr = 0;
100
101 // Data types
102
103 /// The complete set of all analyses described in this module. This is
104 /// produced by the driver and fed to trans and later passes.
105 pub struct CrateAnalysis<'a> {
106     pub export_map: ExportMap,
107     pub access_levels: middle::privacy::AccessLevels,
108     pub reachable: NodeSet,
109     pub name: &'a str,
110     pub glob_map: Option<GlobMap>,
111 }
112
113 #[derive(Copy, Clone)]
114 pub enum DtorKind {
115     NoDtor,
116     TraitDtor(bool)
117 }
118
119 impl DtorKind {
120     pub fn is_present(&self) -> bool {
121         match *self {
122             TraitDtor(..) => true,
123             _ => false
124         }
125     }
126
127     pub fn has_drop_flag(&self) -> bool {
128         match self {
129             &NoDtor => false,
130             &TraitDtor(flag) => flag
131         }
132     }
133 }
134
135 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
136 pub enum ImplOrTraitItemContainer {
137     TraitContainer(DefId),
138     ImplContainer(DefId),
139 }
140
141 impl ImplOrTraitItemContainer {
142     pub fn id(&self) -> DefId {
143         match *self {
144             TraitContainer(id) => id,
145             ImplContainer(id) => id,
146         }
147     }
148 }
149
150 #[derive(Clone)]
151 pub enum ImplOrTraitItem<'tcx> {
152     ConstTraitItem(Rc<AssociatedConst<'tcx>>),
153     MethodTraitItem(Rc<Method<'tcx>>),
154     TypeTraitItem(Rc<AssociatedType<'tcx>>),
155 }
156
157 impl<'tcx> ImplOrTraitItem<'tcx> {
158     fn id(&self) -> ImplOrTraitItemId {
159         match *self {
160             ConstTraitItem(ref associated_const) => {
161                 ConstTraitItemId(associated_const.def_id)
162             }
163             MethodTraitItem(ref method) => MethodTraitItemId(method.def_id),
164             TypeTraitItem(ref associated_type) => {
165                 TypeTraitItemId(associated_type.def_id)
166             }
167         }
168     }
169
170     pub fn def_id(&self) -> DefId {
171         match *self {
172             ConstTraitItem(ref associated_const) => associated_const.def_id,
173             MethodTraitItem(ref method) => method.def_id,
174             TypeTraitItem(ref associated_type) => associated_type.def_id,
175         }
176     }
177
178     pub fn name(&self) -> Name {
179         match *self {
180             ConstTraitItem(ref associated_const) => associated_const.name,
181             MethodTraitItem(ref method) => method.name,
182             TypeTraitItem(ref associated_type) => associated_type.name,
183         }
184     }
185
186     pub fn vis(&self) -> hir::Visibility {
187         match *self {
188             ConstTraitItem(ref associated_const) => associated_const.vis,
189             MethodTraitItem(ref method) => method.vis,
190             TypeTraitItem(ref associated_type) => associated_type.vis,
191         }
192     }
193
194     pub fn container(&self) -> ImplOrTraitItemContainer {
195         match *self {
196             ConstTraitItem(ref associated_const) => associated_const.container,
197             MethodTraitItem(ref method) => method.container,
198             TypeTraitItem(ref associated_type) => associated_type.container,
199         }
200     }
201
202     pub fn as_opt_method(&self) -> Option<Rc<Method<'tcx>>> {
203         match *self {
204             MethodTraitItem(ref m) => Some((*m).clone()),
205             _ => None,
206         }
207     }
208 }
209
210 #[derive(Clone, Copy, Debug)]
211 pub enum ImplOrTraitItemId {
212     ConstTraitItemId(DefId),
213     MethodTraitItemId(DefId),
214     TypeTraitItemId(DefId),
215 }
216
217 impl ImplOrTraitItemId {
218     pub fn def_id(&self) -> DefId {
219         match *self {
220             ConstTraitItemId(def_id) => def_id,
221             MethodTraitItemId(def_id) => def_id,
222             TypeTraitItemId(def_id) => def_id,
223         }
224     }
225 }
226
227 #[derive(Clone, Debug)]
228 pub struct Method<'tcx> {
229     pub name: Name,
230     pub generics: Generics<'tcx>,
231     pub predicates: GenericPredicates<'tcx>,
232     pub fty: BareFnTy<'tcx>,
233     pub explicit_self: ExplicitSelfCategory,
234     pub vis: hir::Visibility,
235     pub def_id: DefId,
236     pub container: ImplOrTraitItemContainer,
237 }
238
239 impl<'tcx> Method<'tcx> {
240     pub fn new(name: Name,
241                generics: ty::Generics<'tcx>,
242                predicates: GenericPredicates<'tcx>,
243                fty: BareFnTy<'tcx>,
244                explicit_self: ExplicitSelfCategory,
245                vis: hir::Visibility,
246                def_id: DefId,
247                container: ImplOrTraitItemContainer)
248                -> Method<'tcx> {
249        Method {
250             name: name,
251             generics: generics,
252             predicates: predicates,
253             fty: fty,
254             explicit_self: explicit_self,
255             vis: vis,
256             def_id: def_id,
257             container: container,
258         }
259     }
260
261     pub fn container_id(&self) -> DefId {
262         match self.container {
263             TraitContainer(id) => id,
264             ImplContainer(id) => id,
265         }
266     }
267 }
268
269 impl<'tcx> PartialEq for Method<'tcx> {
270     #[inline]
271     fn eq(&self, other: &Self) -> bool { self.def_id == other.def_id }
272 }
273
274 impl<'tcx> Eq for Method<'tcx> {}
275
276 impl<'tcx> Hash for Method<'tcx> {
277     #[inline]
278     fn hash<H: Hasher>(&self, s: &mut H) {
279         self.def_id.hash(s)
280     }
281 }
282
283 #[derive(Clone, Copy, Debug)]
284 pub struct AssociatedConst<'tcx> {
285     pub name: Name,
286     pub ty: Ty<'tcx>,
287     pub vis: hir::Visibility,
288     pub def_id: DefId,
289     pub container: ImplOrTraitItemContainer,
290     pub has_value: bool
291 }
292
293 #[derive(Clone, Copy, Debug)]
294 pub struct AssociatedType<'tcx> {
295     pub name: Name,
296     pub ty: Option<Ty<'tcx>>,
297     pub vis: hir::Visibility,
298     pub def_id: DefId,
299     pub container: ImplOrTraitItemContainer,
300 }
301
302 #[derive(Clone, PartialEq, RustcDecodable, RustcEncodable)]
303 pub struct ItemVariances {
304     pub types: VecPerParamSpace<Variance>,
305     pub regions: VecPerParamSpace<Variance>,
306 }
307
308 #[derive(Clone, PartialEq, RustcDecodable, RustcEncodable, Copy)]
309 pub enum Variance {
310     Covariant,      // T<A> <: T<B> iff A <: B -- e.g., function return type
311     Invariant,      // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
312     Contravariant,  // T<A> <: T<B> iff B <: A -- e.g., function param type
313     Bivariant,      // T<A> <: T<B>            -- e.g., unused type parameter
314 }
315
316 #[derive(Clone, Copy, Debug)]
317 pub struct MethodCallee<'tcx> {
318     /// Impl method ID, for inherent methods, or trait method ID, otherwise.
319     pub def_id: DefId,
320     pub ty: Ty<'tcx>,
321     pub substs: &'tcx subst::Substs<'tcx>
322 }
323
324 /// With method calls, we store some extra information in
325 /// side tables (i.e method_map). We use
326 /// MethodCall as a key to index into these tables instead of
327 /// just directly using the expression's NodeId. The reason
328 /// for this being that we may apply adjustments (coercions)
329 /// with the resulting expression also needing to use the
330 /// side tables. The problem with this is that we don't
331 /// assign a separate NodeId to this new expression
332 /// and so it would clash with the base expression if both
333 /// needed to add to the side tables. Thus to disambiguate
334 /// we also keep track of whether there's an adjustment in
335 /// our key.
336 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
337 pub struct MethodCall {
338     pub expr_id: NodeId,
339     pub autoderef: u32
340 }
341
342 impl MethodCall {
343     pub fn expr(id: NodeId) -> MethodCall {
344         MethodCall {
345             expr_id: id,
346             autoderef: 0
347         }
348     }
349
350     pub fn autoderef(expr_id: NodeId, autoderef: u32) -> MethodCall {
351         MethodCall {
352             expr_id: expr_id,
353             autoderef: 1 + autoderef
354         }
355     }
356 }
357
358 // maps from an expression id that corresponds to a method call to the details
359 // of the method to be invoked
360 pub type MethodMap<'tcx> = FnvHashMap<MethodCall, MethodCallee<'tcx>>;
361
362 // Contains information needed to resolve types and (in the future) look up
363 // the types of AST nodes.
364 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
365 pub struct CReaderCacheKey {
366     pub cnum: CrateNum,
367     pub pos: usize,
368 }
369
370 /// A restriction that certain types must be the same size. The use of
371 /// `transmute` gives rise to these restrictions. These generally
372 /// cannot be checked until trans; therefore, each call to `transmute`
373 /// will push one or more such restriction into the
374 /// `transmute_restrictions` vector during `intrinsicck`. They are
375 /// then checked during `trans` by the fn `check_intrinsics`.
376 #[derive(Copy, Clone)]
377 pub struct TransmuteRestriction<'tcx> {
378     /// The span whence the restriction comes.
379     pub span: Span,
380
381     /// The type being transmuted from.
382     pub original_from: Ty<'tcx>,
383
384     /// The type being transmuted to.
385     pub original_to: Ty<'tcx>,
386
387     /// The type being transmuted from, with all type parameters
388     /// substituted for an arbitrary representative. Not to be shown
389     /// to the end user.
390     pub substituted_from: Ty<'tcx>,
391
392     /// The type being transmuted to, with all type parameters
393     /// substituted for an arbitrary representative. Not to be shown
394     /// to the end user.
395     pub substituted_to: Ty<'tcx>,
396
397     /// NodeId of the transmute intrinsic.
398     pub id: NodeId,
399 }
400
401 /// Describes the fragment-state associated with a NodeId.
402 ///
403 /// Currently only unfragmented paths have entries in the table,
404 /// but longer-term this enum is expected to expand to also
405 /// include data for fragmented paths.
406 #[derive(Copy, Clone, Debug)]
407 pub enum FragmentInfo {
408     Moved { var: NodeId, move_expr: NodeId },
409     Assigned { var: NodeId, assign_expr: NodeId, assignee_id: NodeId },
410 }
411
412 // Flags that we track on types. These flags are propagated upwards
413 // through the type during type construction, so that we can quickly
414 // check whether the type has various kinds of types in it without
415 // recursing over the type itself.
416 bitflags! {
417     flags TypeFlags: u32 {
418         const HAS_PARAMS         = 1 << 0,
419         const HAS_SELF           = 1 << 1,
420         const HAS_TY_INFER       = 1 << 2,
421         const HAS_RE_INFER       = 1 << 3,
422         const HAS_RE_EARLY_BOUND = 1 << 4,
423         const HAS_FREE_REGIONS   = 1 << 5,
424         const HAS_TY_ERR         = 1 << 6,
425         const HAS_PROJECTION     = 1 << 7,
426         const HAS_TY_CLOSURE     = 1 << 8,
427
428         // true if there are "names" of types and regions and so forth
429         // that are local to a particular fn
430         const HAS_LOCAL_NAMES   = 1 << 9,
431
432         const NEEDS_SUBST        = TypeFlags::HAS_PARAMS.bits |
433                                    TypeFlags::HAS_SELF.bits |
434                                    TypeFlags::HAS_RE_EARLY_BOUND.bits,
435
436         // Flags representing the nominal content of a type,
437         // computed by FlagsComputation. If you add a new nominal
438         // flag, it should be added here too.
439         const NOMINAL_FLAGS     = TypeFlags::HAS_PARAMS.bits |
440                                   TypeFlags::HAS_SELF.bits |
441                                   TypeFlags::HAS_TY_INFER.bits |
442                                   TypeFlags::HAS_RE_INFER.bits |
443                                   TypeFlags::HAS_RE_EARLY_BOUND.bits |
444                                   TypeFlags::HAS_FREE_REGIONS.bits |
445                                   TypeFlags::HAS_TY_ERR.bits |
446                                   TypeFlags::HAS_PROJECTION.bits |
447                                   TypeFlags::HAS_TY_CLOSURE.bits |
448                                   TypeFlags::HAS_LOCAL_NAMES.bits,
449
450         // Caches for type_is_sized, type_moves_by_default
451         const SIZEDNESS_CACHED  = 1 << 16,
452         const IS_SIZED          = 1 << 17,
453         const MOVENESS_CACHED   = 1 << 18,
454         const MOVES_BY_DEFAULT  = 1 << 19,
455     }
456 }
457
458 pub struct TyS<'tcx> {
459     pub sty: TypeVariants<'tcx>,
460     pub flags: Cell<TypeFlags>,
461
462     // the maximal depth of any bound regions appearing in this type.
463     region_depth: u32,
464 }
465
466 impl<'tcx> PartialEq for TyS<'tcx> {
467     #[inline]
468     fn eq(&self, other: &TyS<'tcx>) -> bool {
469         // (self as *const _) == (other as *const _)
470         (self as *const TyS<'tcx>) == (other as *const TyS<'tcx>)
471     }
472 }
473 impl<'tcx> Eq for TyS<'tcx> {}
474
475 impl<'tcx> Hash for TyS<'tcx> {
476     fn hash<H: Hasher>(&self, s: &mut H) {
477         (self as *const TyS).hash(s)
478     }
479 }
480
481 pub type Ty<'tcx> = &'tcx TyS<'tcx>;
482
483 impl<'tcx> Encodable for Ty<'tcx> {
484     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
485         cstore::tls::with_encoding_context(s, |ecx, rbml_w| {
486             ecx.encode_ty(rbml_w, *self);
487             Ok(())
488         })
489     }
490 }
491
492 impl<'tcx> Decodable for Ty<'tcx> {
493     fn decode<D: Decoder>(d: &mut D) -> Result<Ty<'tcx>, D::Error> {
494         cstore::tls::with_decoding_context(d, |dcx, rbml_r| {
495             Ok(dcx.decode_ty(rbml_r))
496         })
497     }
498 }
499
500
501 /// Upvars do not get their own node-id. Instead, we use the pair of
502 /// the original var id (that is, the root variable that is referenced
503 /// by the upvar) and the id of the closure expression.
504 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
505 pub struct UpvarId {
506     pub var_id: NodeId,
507     pub closure_expr_id: NodeId,
508 }
509
510 #[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, Copy)]
511 pub enum BorrowKind {
512     /// Data must be immutable and is aliasable.
513     ImmBorrow,
514
515     /// Data must be immutable but not aliasable.  This kind of borrow
516     /// cannot currently be expressed by the user and is used only in
517     /// implicit closure bindings. It is needed when you the closure
518     /// is borrowing or mutating a mutable referent, e.g.:
519     ///
520     ///    let x: &mut isize = ...;
521     ///    let y = || *x += 5;
522     ///
523     /// If we were to try to translate this closure into a more explicit
524     /// form, we'd encounter an error with the code as written:
525     ///
526     ///    struct Env { x: & &mut isize }
527     ///    let x: &mut isize = ...;
528     ///    let y = (&mut Env { &x }, fn_ptr);  // Closure is pair of env and fn
529     ///    fn fn_ptr(env: &mut Env) { **env.x += 5; }
530     ///
531     /// This is then illegal because you cannot mutate a `&mut` found
532     /// in an aliasable location. To solve, you'd have to translate with
533     /// an `&mut` borrow:
534     ///
535     ///    struct Env { x: & &mut isize }
536     ///    let x: &mut isize = ...;
537     ///    let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
538     ///    fn fn_ptr(env: &mut Env) { **env.x += 5; }
539     ///
540     /// Now the assignment to `**env.x` is legal, but creating a
541     /// mutable pointer to `x` is not because `x` is not mutable. We
542     /// could fix this by declaring `x` as `let mut x`. This is ok in
543     /// user code, if awkward, but extra weird for closures, since the
544     /// borrow is hidden.
545     ///
546     /// So we introduce a "unique imm" borrow -- the referent is
547     /// immutable, but not aliasable. This solves the problem. For
548     /// simplicity, we don't give users the way to express this
549     /// borrow, it's just used when translating closures.
550     UniqueImmBorrow,
551
552     /// Data is mutable and not aliasable.
553     MutBorrow
554 }
555
556 /// Information describing the capture of an upvar. This is computed
557 /// during `typeck`, specifically by `regionck`.
558 #[derive(PartialEq, Clone, Debug, Copy)]
559 pub enum UpvarCapture {
560     /// Upvar is captured by value. This is always true when the
561     /// closure is labeled `move`, but can also be true in other cases
562     /// depending on inference.
563     ByValue,
564
565     /// Upvar is captured by reference.
566     ByRef(UpvarBorrow),
567 }
568
569 #[derive(PartialEq, Clone, Copy)]
570 pub struct UpvarBorrow {
571     /// The kind of borrow: by-ref upvars have access to shared
572     /// immutable borrows, which are not part of the normal language
573     /// syntax.
574     pub kind: BorrowKind,
575
576     /// Region of the resulting reference.
577     pub region: ty::Region,
578 }
579
580 pub type UpvarCaptureMap = FnvHashMap<UpvarId, UpvarCapture>;
581
582 #[derive(Copy, Clone)]
583 pub struct ClosureUpvar<'tcx> {
584     pub def: def::Def,
585     pub span: Span,
586     pub ty: Ty<'tcx>,
587 }
588
589 #[derive(Clone, Copy, PartialEq)]
590 pub enum IntVarValue {
591     IntType(ast::IntTy),
592     UintType(ast::UintTy),
593 }
594
595 /// Default region to use for the bound of objects that are
596 /// supplied as the value for this type parameter. This is derived
597 /// from `T:'a` annotations appearing in the type definition.  If
598 /// this is `None`, then the default is inherited from the
599 /// surrounding context. See RFC #599 for details.
600 #[derive(Copy, Clone)]
601 pub enum ObjectLifetimeDefault {
602     /// Require an explicit annotation. Occurs when multiple
603     /// `T:'a` constraints are found.
604     Ambiguous,
605
606     /// Use the base default, typically 'static, but in a fn body it is a fresh variable
607     BaseDefault,
608
609     /// Use the given region as the default.
610     Specific(Region),
611 }
612
613 #[derive(Clone)]
614 pub struct TypeParameterDef<'tcx> {
615     pub name: Name,
616     pub def_id: DefId,
617     pub space: subst::ParamSpace,
618     pub index: u32,
619     pub default_def_id: DefId, // for use in error reporing about defaults
620     pub default: Option<Ty<'tcx>>,
621     pub object_lifetime_default: ObjectLifetimeDefault,
622 }
623
624 #[derive(Clone)]
625 pub struct RegionParameterDef {
626     pub name: Name,
627     pub def_id: DefId,
628     pub space: subst::ParamSpace,
629     pub index: u32,
630     pub bounds: Vec<ty::Region>,
631 }
632
633 impl RegionParameterDef {
634     pub fn to_early_bound_region(&self) -> ty::Region {
635         ty::ReEarlyBound(ty::EarlyBoundRegion {
636             def_id: self.def_id,
637             space: self.space,
638             index: self.index,
639             name: self.name,
640         })
641     }
642     pub fn to_bound_region(&self) -> ty::BoundRegion {
643         ty::BoundRegion::BrNamed(self.def_id, self.name)
644     }
645 }
646
647 /// Information about the formal type/lifetime parameters associated
648 /// with an item or method. Analogous to hir::Generics.
649 #[derive(Clone, Debug)]
650 pub struct Generics<'tcx> {
651     pub types: VecPerParamSpace<TypeParameterDef<'tcx>>,
652     pub regions: VecPerParamSpace<RegionParameterDef>,
653 }
654
655 impl<'tcx> Generics<'tcx> {
656     pub fn empty() -> Generics<'tcx> {
657         Generics {
658             types: VecPerParamSpace::empty(),
659             regions: VecPerParamSpace::empty(),
660         }
661     }
662
663     pub fn is_empty(&self) -> bool {
664         self.types.is_empty() && self.regions.is_empty()
665     }
666
667     pub fn has_type_params(&self, space: subst::ParamSpace) -> bool {
668         !self.types.is_empty_in(space)
669     }
670
671     pub fn has_region_params(&self, space: subst::ParamSpace) -> bool {
672         !self.regions.is_empty_in(space)
673     }
674 }
675
676 /// Bounds on generics.
677 #[derive(Clone)]
678 pub struct GenericPredicates<'tcx> {
679     pub predicates: VecPerParamSpace<Predicate<'tcx>>,
680 }
681
682 impl<'tcx> GenericPredicates<'tcx> {
683     pub fn empty() -> GenericPredicates<'tcx> {
684         GenericPredicates {
685             predicates: VecPerParamSpace::empty(),
686         }
687     }
688
689     pub fn instantiate(&self, tcx: &ctxt<'tcx>, substs: &Substs<'tcx>)
690                        -> InstantiatedPredicates<'tcx> {
691         InstantiatedPredicates {
692             predicates: self.predicates.subst(tcx, substs),
693         }
694     }
695
696     pub fn instantiate_supertrait(&self,
697                                   tcx: &ctxt<'tcx>,
698                                   poly_trait_ref: &ty::PolyTraitRef<'tcx>)
699                                   -> InstantiatedPredicates<'tcx>
700     {
701         InstantiatedPredicates {
702             predicates: self.predicates.map(|pred| pred.subst_supertrait(tcx, poly_trait_ref))
703         }
704     }
705 }
706
707 #[derive(Clone, PartialEq, Eq, Hash)]
708 pub enum Predicate<'tcx> {
709     /// Corresponds to `where Foo : Bar<A,B,C>`. `Foo` here would be
710     /// the `Self` type of the trait reference and `A`, `B`, and `C`
711     /// would be the parameters in the `TypeSpace`.
712     Trait(PolyTraitPredicate<'tcx>),
713
714     /// where `T1 == T2`.
715     Equate(PolyEquatePredicate<'tcx>),
716
717     /// where 'a : 'b
718     RegionOutlives(PolyRegionOutlivesPredicate),
719
720     /// where T : 'a
721     TypeOutlives(PolyTypeOutlivesPredicate<'tcx>),
722
723     /// where <T as TraitRef>::Name == X, approximately.
724     /// See `ProjectionPredicate` struct for details.
725     Projection(PolyProjectionPredicate<'tcx>),
726
727     /// no syntax: T WF
728     WellFormed(Ty<'tcx>),
729
730     /// trait must be object-safe
731     ObjectSafe(DefId),
732 }
733
734 impl<'tcx> Predicate<'tcx> {
735     /// Performs a substitution suitable for going from a
736     /// poly-trait-ref to supertraits that must hold if that
737     /// poly-trait-ref holds. This is slightly different from a normal
738     /// substitution in terms of what happens with bound regions.  See
739     /// lengthy comment below for details.
740     pub fn subst_supertrait(&self,
741                             tcx: &ctxt<'tcx>,
742                             trait_ref: &ty::PolyTraitRef<'tcx>)
743                             -> ty::Predicate<'tcx>
744     {
745         // The interaction between HRTB and supertraits is not entirely
746         // obvious. Let me walk you (and myself) through an example.
747         //
748         // Let's start with an easy case. Consider two traits:
749         //
750         //     trait Foo<'a> : Bar<'a,'a> { }
751         //     trait Bar<'b,'c> { }
752         //
753         // Now, if we have a trait reference `for<'x> T : Foo<'x>`, then
754         // we can deduce that `for<'x> T : Bar<'x,'x>`. Basically, if we
755         // knew that `Foo<'x>` (for any 'x) then we also know that
756         // `Bar<'x,'x>` (for any 'x). This more-or-less falls out from
757         // normal substitution.
758         //
759         // In terms of why this is sound, the idea is that whenever there
760         // is an impl of `T:Foo<'a>`, it must show that `T:Bar<'a,'a>`
761         // holds.  So if there is an impl of `T:Foo<'a>` that applies to
762         // all `'a`, then we must know that `T:Bar<'a,'a>` holds for all
763         // `'a`.
764         //
765         // Another example to be careful of is this:
766         //
767         //     trait Foo1<'a> : for<'b> Bar1<'a,'b> { }
768         //     trait Bar1<'b,'c> { }
769         //
770         // Here, if we have `for<'x> T : Foo1<'x>`, then what do we know?
771         // The answer is that we know `for<'x,'b> T : Bar1<'x,'b>`. The
772         // reason is similar to the previous example: any impl of
773         // `T:Foo1<'x>` must show that `for<'b> T : Bar1<'x, 'b>`.  So
774         // basically we would want to collapse the bound lifetimes from
775         // the input (`trait_ref`) and the supertraits.
776         //
777         // To achieve this in practice is fairly straightforward. Let's
778         // consider the more complicated scenario:
779         //
780         // - We start out with `for<'x> T : Foo1<'x>`. In this case, `'x`
781         //   has a De Bruijn index of 1. We want to produce `for<'x,'b> T : Bar1<'x,'b>`,
782         //   where both `'x` and `'b` would have a DB index of 1.
783         //   The substitution from the input trait-ref is therefore going to be
784         //   `'a => 'x` (where `'x` has a DB index of 1).
785         // - The super-trait-ref is `for<'b> Bar1<'a,'b>`, where `'a` is an
786         //   early-bound parameter and `'b' is a late-bound parameter with a
787         //   DB index of 1.
788         // - If we replace `'a` with `'x` from the input, it too will have
789         //   a DB index of 1, and thus we'll have `for<'x,'b> Bar1<'x,'b>`
790         //   just as we wanted.
791         //
792         // There is only one catch. If we just apply the substitution `'a
793         // => 'x` to `for<'b> Bar1<'a,'b>`, the substitution code will
794         // adjust the DB index because we substituting into a binder (it
795         // tries to be so smart...) resulting in `for<'x> for<'b>
796         // Bar1<'x,'b>` (we have no syntax for this, so use your
797         // imagination). Basically the 'x will have DB index of 2 and 'b
798         // will have DB index of 1. Not quite what we want. So we apply
799         // the substitution to the *contents* of the trait reference,
800         // rather than the trait reference itself (put another way, the
801         // substitution code expects equal binding levels in the values
802         // from the substitution and the value being substituted into, and
803         // this trick achieves that).
804
805         let substs = &trait_ref.0.substs;
806         match *self {
807             Predicate::Trait(ty::Binder(ref data)) =>
808                 Predicate::Trait(ty::Binder(data.subst(tcx, substs))),
809             Predicate::Equate(ty::Binder(ref data)) =>
810                 Predicate::Equate(ty::Binder(data.subst(tcx, substs))),
811             Predicate::RegionOutlives(ty::Binder(ref data)) =>
812                 Predicate::RegionOutlives(ty::Binder(data.subst(tcx, substs))),
813             Predicate::TypeOutlives(ty::Binder(ref data)) =>
814                 Predicate::TypeOutlives(ty::Binder(data.subst(tcx, substs))),
815             Predicate::Projection(ty::Binder(ref data)) =>
816                 Predicate::Projection(ty::Binder(data.subst(tcx, substs))),
817             Predicate::WellFormed(data) =>
818                 Predicate::WellFormed(data.subst(tcx, substs)),
819             Predicate::ObjectSafe(trait_def_id) =>
820                 Predicate::ObjectSafe(trait_def_id),
821         }
822     }
823 }
824
825 #[derive(Clone, PartialEq, Eq, Hash)]
826 pub struct TraitPredicate<'tcx> {
827     pub trait_ref: TraitRef<'tcx>
828 }
829 pub type PolyTraitPredicate<'tcx> = ty::Binder<TraitPredicate<'tcx>>;
830
831 impl<'tcx> TraitPredicate<'tcx> {
832     pub fn def_id(&self) -> DefId {
833         self.trait_ref.def_id
834     }
835
836     pub fn input_types(&self) -> &[Ty<'tcx>] {
837         self.trait_ref.substs.types.as_slice()
838     }
839
840     pub fn self_ty(&self) -> Ty<'tcx> {
841         self.trait_ref.self_ty()
842     }
843 }
844
845 impl<'tcx> PolyTraitPredicate<'tcx> {
846     pub fn def_id(&self) -> DefId {
847         self.0.def_id()
848     }
849 }
850
851 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
852 pub struct EquatePredicate<'tcx>(pub Ty<'tcx>, pub Ty<'tcx>); // `0 == 1`
853 pub type PolyEquatePredicate<'tcx> = ty::Binder<EquatePredicate<'tcx>>;
854
855 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
856 pub struct OutlivesPredicate<A,B>(pub A, pub B); // `A : B`
857 pub type PolyOutlivesPredicate<A,B> = ty::Binder<OutlivesPredicate<A,B>>;
858 pub type PolyRegionOutlivesPredicate = PolyOutlivesPredicate<ty::Region, ty::Region>;
859 pub type PolyTypeOutlivesPredicate<'tcx> = PolyOutlivesPredicate<Ty<'tcx>, ty::Region>;
860
861 /// This kind of predicate has no *direct* correspondent in the
862 /// syntax, but it roughly corresponds to the syntactic forms:
863 ///
864 /// 1. `T : TraitRef<..., Item=Type>`
865 /// 2. `<T as TraitRef<...>>::Item == Type` (NYI)
866 ///
867 /// In particular, form #1 is "desugared" to the combination of a
868 /// normal trait predicate (`T : TraitRef<...>`) and one of these
869 /// predicates. Form #2 is a broader form in that it also permits
870 /// equality between arbitrary types. Processing an instance of Form
871 /// #2 eventually yields one of these `ProjectionPredicate`
872 /// instances to normalize the LHS.
873 #[derive(Clone, PartialEq, Eq, Hash)]
874 pub struct ProjectionPredicate<'tcx> {
875     pub projection_ty: ProjectionTy<'tcx>,
876     pub ty: Ty<'tcx>,
877 }
878
879 pub type PolyProjectionPredicate<'tcx> = Binder<ProjectionPredicate<'tcx>>;
880
881 impl<'tcx> PolyProjectionPredicate<'tcx> {
882     pub fn item_name(&self) -> Name {
883         self.0.projection_ty.item_name // safe to skip the binder to access a name
884     }
885
886     pub fn sort_key(&self) -> (DefId, Name) {
887         self.0.projection_ty.sort_key()
888     }
889 }
890
891 pub trait ToPolyTraitRef<'tcx> {
892     fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
893 }
894
895 impl<'tcx> ToPolyTraitRef<'tcx> for TraitRef<'tcx> {
896     fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
897         assert!(!self.has_escaping_regions());
898         ty::Binder(self.clone())
899     }
900 }
901
902 impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
903     fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
904         self.map_bound_ref(|trait_pred| trait_pred.trait_ref.clone())
905     }
906 }
907
908 impl<'tcx> ToPolyTraitRef<'tcx> for PolyProjectionPredicate<'tcx> {
909     fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
910         // Note: unlike with TraitRef::to_poly_trait_ref(),
911         // self.0.trait_ref is permitted to have escaping regions.
912         // This is because here `self` has a `Binder` and so does our
913         // return value, so we are preserving the number of binding
914         // levels.
915         ty::Binder(self.0.projection_ty.trait_ref.clone())
916     }
917 }
918
919 pub trait ToPredicate<'tcx> {
920     fn to_predicate(&self) -> Predicate<'tcx>;
921 }
922
923 impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
924     fn to_predicate(&self) -> Predicate<'tcx> {
925         // we're about to add a binder, so let's check that we don't
926         // accidentally capture anything, or else that might be some
927         // weird debruijn accounting.
928         assert!(!self.has_escaping_regions());
929
930         ty::Predicate::Trait(ty::Binder(ty::TraitPredicate {
931             trait_ref: self.clone()
932         }))
933     }
934 }
935
936 impl<'tcx> ToPredicate<'tcx> for PolyTraitRef<'tcx> {
937     fn to_predicate(&self) -> Predicate<'tcx> {
938         ty::Predicate::Trait(self.to_poly_trait_predicate())
939     }
940 }
941
942 impl<'tcx> ToPredicate<'tcx> for PolyEquatePredicate<'tcx> {
943     fn to_predicate(&self) -> Predicate<'tcx> {
944         Predicate::Equate(self.clone())
945     }
946 }
947
948 impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate {
949     fn to_predicate(&self) -> Predicate<'tcx> {
950         Predicate::RegionOutlives(self.clone())
951     }
952 }
953
954 impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
955     fn to_predicate(&self) -> Predicate<'tcx> {
956         Predicate::TypeOutlives(self.clone())
957     }
958 }
959
960 impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
961     fn to_predicate(&self) -> Predicate<'tcx> {
962         Predicate::Projection(self.clone())
963     }
964 }
965
966 impl<'tcx> Predicate<'tcx> {
967     /// Iterates over the types in this predicate. Note that in all
968     /// cases this is skipping over a binder, so late-bound regions
969     /// with depth 0 are bound by the predicate.
970     pub fn walk_tys(&self) -> IntoIter<Ty<'tcx>> {
971         let vec: Vec<_> = match *self {
972             ty::Predicate::Trait(ref data) => {
973                 data.0.trait_ref.substs.types.as_slice().to_vec()
974             }
975             ty::Predicate::Equate(ty::Binder(ref data)) => {
976                 vec![data.0, data.1]
977             }
978             ty::Predicate::TypeOutlives(ty::Binder(ref data)) => {
979                 vec![data.0]
980             }
981             ty::Predicate::RegionOutlives(..) => {
982                 vec![]
983             }
984             ty::Predicate::Projection(ref data) => {
985                 let trait_inputs = data.0.projection_ty.trait_ref.substs.types.as_slice();
986                 trait_inputs.iter()
987                             .cloned()
988                             .chain(Some(data.0.ty))
989                             .collect()
990             }
991             ty::Predicate::WellFormed(data) => {
992                 vec![data]
993             }
994             ty::Predicate::ObjectSafe(_trait_def_id) => {
995                 vec![]
996             }
997         };
998
999         // The only reason to collect into a vector here is that I was
1000         // too lazy to make the full (somewhat complicated) iterator
1001         // type that would be needed here. But I wanted this fn to
1002         // return an iterator conceptually, rather than a `Vec`, so as
1003         // to be closer to `Ty::walk`.
1004         vec.into_iter()
1005     }
1006
1007     pub fn to_opt_poly_trait_ref(&self) -> Option<PolyTraitRef<'tcx>> {
1008         match *self {
1009             Predicate::Trait(ref t) => {
1010                 Some(t.to_poly_trait_ref())
1011             }
1012             Predicate::Projection(..) |
1013             Predicate::Equate(..) |
1014             Predicate::RegionOutlives(..) |
1015             Predicate::WellFormed(..) |
1016             Predicate::ObjectSafe(..) |
1017             Predicate::TypeOutlives(..) => {
1018                 None
1019             }
1020         }
1021     }
1022 }
1023
1024 /// Represents the bounds declared on a particular set of type
1025 /// parameters.  Should eventually be generalized into a flag list of
1026 /// where clauses.  You can obtain a `InstantiatedPredicates` list from a
1027 /// `GenericPredicates` by using the `instantiate` method. Note that this method
1028 /// reflects an important semantic invariant of `InstantiatedPredicates`: while
1029 /// the `GenericPredicates` are expressed in terms of the bound type
1030 /// parameters of the impl/trait/whatever, an `InstantiatedPredicates` instance
1031 /// represented a set of bounds for some particular instantiation,
1032 /// meaning that the generic parameters have been substituted with
1033 /// their values.
1034 ///
1035 /// Example:
1036 ///
1037 ///     struct Foo<T,U:Bar<T>> { ... }
1038 ///
1039 /// Here, the `GenericPredicates` for `Foo` would contain a list of bounds like
1040 /// `[[], [U:Bar<T>]]`.  Now if there were some particular reference
1041 /// like `Foo<isize,usize>`, then the `InstantiatedPredicates` would be `[[],
1042 /// [usize:Bar<isize>]]`.
1043 #[derive(Clone)]
1044 pub struct InstantiatedPredicates<'tcx> {
1045     pub predicates: VecPerParamSpace<Predicate<'tcx>>,
1046 }
1047
1048 impl<'tcx> InstantiatedPredicates<'tcx> {
1049     pub fn empty() -> InstantiatedPredicates<'tcx> {
1050         InstantiatedPredicates { predicates: VecPerParamSpace::empty() }
1051     }
1052
1053     pub fn is_empty(&self) -> bool {
1054         self.predicates.is_empty()
1055     }
1056 }
1057
1058 impl<'tcx> TraitRef<'tcx> {
1059     pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>) -> TraitRef<'tcx> {
1060         TraitRef { def_id: def_id, substs: substs }
1061     }
1062
1063     pub fn self_ty(&self) -> Ty<'tcx> {
1064         self.substs.self_ty().unwrap()
1065     }
1066
1067     pub fn input_types(&self) -> &[Ty<'tcx>] {
1068         // Select only the "input types" from a trait-reference. For
1069         // now this is all the types that appear in the
1070         // trait-reference, but it should eventually exclude
1071         // associated types.
1072         self.substs.types.as_slice()
1073     }
1074 }
1075
1076 /// When type checking, we use the `ParameterEnvironment` to track
1077 /// details about the type/lifetime parameters that are in scope.
1078 /// It primarily stores the bounds information.
1079 ///
1080 /// Note: This information might seem to be redundant with the data in
1081 /// `tcx.ty_param_defs`, but it is not. That table contains the
1082 /// parameter definitions from an "outside" perspective, but this
1083 /// struct will contain the bounds for a parameter as seen from inside
1084 /// the function body. Currently the only real distinction is that
1085 /// bound lifetime parameters are replaced with free ones, but in the
1086 /// future I hope to refine the representation of types so as to make
1087 /// more distinctions clearer.
1088 #[derive(Clone)]
1089 pub struct ParameterEnvironment<'a, 'tcx:'a> {
1090     pub tcx: &'a ctxt<'tcx>,
1091
1092     /// See `construct_free_substs` for details.
1093     pub free_substs: Substs<'tcx>,
1094
1095     /// Each type parameter has an implicit region bound that
1096     /// indicates it must outlive at least the function body (the user
1097     /// may specify stronger requirements). This field indicates the
1098     /// region of the callee.
1099     pub implicit_region_bound: ty::Region,
1100
1101     /// Obligations that the caller must satisfy. This is basically
1102     /// the set of bounds on the in-scope type parameters, translated
1103     /// into Obligations, and elaborated and normalized.
1104     pub caller_bounds: Vec<ty::Predicate<'tcx>>,
1105
1106     /// Caches the results of trait selection. This cache is used
1107     /// for things that have to do with the parameters in scope.
1108     pub selection_cache: traits::SelectionCache<'tcx>,
1109
1110     /// Caches the results of trait evaluation.
1111     pub evaluation_cache: traits::EvaluationCache<'tcx>,
1112
1113     /// Scope that is attached to free regions for this scope. This
1114     /// is usually the id of the fn body, but for more abstract scopes
1115     /// like structs we often use the node-id of the struct.
1116     ///
1117     /// FIXME(#3696). It would be nice to refactor so that free
1118     /// regions don't have this implicit scope and instead introduce
1119     /// relationships in the environment.
1120     pub free_id_outlive: CodeExtent,
1121 }
1122
1123 impl<'a, 'tcx> ParameterEnvironment<'a, 'tcx> {
1124     pub fn with_caller_bounds(&self,
1125                               caller_bounds: Vec<ty::Predicate<'tcx>>)
1126                               -> ParameterEnvironment<'a,'tcx>
1127     {
1128         ParameterEnvironment {
1129             tcx: self.tcx,
1130             free_substs: self.free_substs.clone(),
1131             implicit_region_bound: self.implicit_region_bound,
1132             caller_bounds: caller_bounds,
1133             selection_cache: traits::SelectionCache::new(),
1134             evaluation_cache: traits::EvaluationCache::new(),
1135             free_id_outlive: self.free_id_outlive,
1136         }
1137     }
1138
1139     pub fn for_item(cx: &'a ctxt<'tcx>, id: NodeId) -> ParameterEnvironment<'a, 'tcx> {
1140         match cx.map.find(id) {
1141             Some(ast_map::NodeImplItem(ref impl_item)) => {
1142                 match impl_item.node {
1143                     hir::ImplItemKind::Type(_) => {
1144                         // associated types don't have their own entry (for some reason),
1145                         // so for now just grab environment for the impl
1146                         let impl_id = cx.map.get_parent(id);
1147                         let impl_def_id = cx.map.local_def_id(impl_id);
1148                         let scheme = cx.lookup_item_type(impl_def_id);
1149                         let predicates = cx.lookup_predicates(impl_def_id);
1150                         cx.construct_parameter_environment(impl_item.span,
1151                                                            &scheme.generics,
1152                                                            &predicates,
1153                                                            cx.region_maps.item_extent(id))
1154                     }
1155                     hir::ImplItemKind::Const(_, _) => {
1156                         let def_id = cx.map.local_def_id(id);
1157                         let scheme = cx.lookup_item_type(def_id);
1158                         let predicates = cx.lookup_predicates(def_id);
1159                         cx.construct_parameter_environment(impl_item.span,
1160                                                            &scheme.generics,
1161                                                            &predicates,
1162                                                            cx.region_maps.item_extent(id))
1163                     }
1164                     hir::ImplItemKind::Method(_, ref body) => {
1165                         let method_def_id = cx.map.local_def_id(id);
1166                         match cx.impl_or_trait_item(method_def_id) {
1167                             MethodTraitItem(ref method_ty) => {
1168                                 let method_generics = &method_ty.generics;
1169                                 let method_bounds = &method_ty.predicates;
1170                                 cx.construct_parameter_environment(
1171                                     impl_item.span,
1172                                     method_generics,
1173                                     method_bounds,
1174                                     cx.region_maps.call_site_extent(id, body.id))
1175                             }
1176                             _ => {
1177                                 cx.sess
1178                                   .bug("ParameterEnvironment::for_item(): \
1179                                         got non-method item from impl method?!")
1180                             }
1181                         }
1182                     }
1183                 }
1184             }
1185             Some(ast_map::NodeTraitItem(trait_item)) => {
1186                 match trait_item.node {
1187                     hir::TypeTraitItem(..) => {
1188                         // associated types don't have their own entry (for some reason),
1189                         // so for now just grab environment for the trait
1190                         let trait_id = cx.map.get_parent(id);
1191                         let trait_def_id = cx.map.local_def_id(trait_id);
1192                         let trait_def = cx.lookup_trait_def(trait_def_id);
1193                         let predicates = cx.lookup_predicates(trait_def_id);
1194                         cx.construct_parameter_environment(trait_item.span,
1195                                                            &trait_def.generics,
1196                                                            &predicates,
1197                                                            cx.region_maps.item_extent(id))
1198                     }
1199                     hir::ConstTraitItem(..) => {
1200                         let def_id = cx.map.local_def_id(id);
1201                         let scheme = cx.lookup_item_type(def_id);
1202                         let predicates = cx.lookup_predicates(def_id);
1203                         cx.construct_parameter_environment(trait_item.span,
1204                                                            &scheme.generics,
1205                                                            &predicates,
1206                                                            cx.region_maps.item_extent(id))
1207                     }
1208                     hir::MethodTraitItem(_, ref body) => {
1209                         // Use call-site for extent (unless this is a
1210                         // trait method with no default; then fallback
1211                         // to the method id).
1212                         let method_def_id = cx.map.local_def_id(id);
1213                         match cx.impl_or_trait_item(method_def_id) {
1214                             MethodTraitItem(ref method_ty) => {
1215                                 let method_generics = &method_ty.generics;
1216                                 let method_bounds = &method_ty.predicates;
1217                                 let extent = if let Some(ref body) = *body {
1218                                     // default impl: use call_site extent as free_id_outlive bound.
1219                                     cx.region_maps.call_site_extent(id, body.id)
1220                                 } else {
1221                                     // no default impl: use item extent as free_id_outlive bound.
1222                                     cx.region_maps.item_extent(id)
1223                                 };
1224                                 cx.construct_parameter_environment(
1225                                     trait_item.span,
1226                                     method_generics,
1227                                     method_bounds,
1228                                     extent)
1229                             }
1230                             _ => {
1231                                 cx.sess
1232                                   .bug("ParameterEnvironment::for_item(): \
1233                                         got non-method item from provided \
1234                                         method?!")
1235                             }
1236                         }
1237                     }
1238                 }
1239             }
1240             Some(ast_map::NodeItem(item)) => {
1241                 match item.node {
1242                     hir::ItemFn(_, _, _, _, _, ref body) => {
1243                         // We assume this is a function.
1244                         let fn_def_id = cx.map.local_def_id(id);
1245                         let fn_scheme = cx.lookup_item_type(fn_def_id);
1246                         let fn_predicates = cx.lookup_predicates(fn_def_id);
1247
1248                         cx.construct_parameter_environment(item.span,
1249                                                            &fn_scheme.generics,
1250                                                            &fn_predicates,
1251                                                            cx.region_maps.call_site_extent(id,
1252                                                                                            body.id))
1253                     }
1254                     hir::ItemEnum(..) |
1255                     hir::ItemStruct(..) |
1256                     hir::ItemImpl(..) |
1257                     hir::ItemConst(..) |
1258                     hir::ItemStatic(..) => {
1259                         let def_id = cx.map.local_def_id(id);
1260                         let scheme = cx.lookup_item_type(def_id);
1261                         let predicates = cx.lookup_predicates(def_id);
1262                         cx.construct_parameter_environment(item.span,
1263                                                            &scheme.generics,
1264                                                            &predicates,
1265                                                            cx.region_maps.item_extent(id))
1266                     }
1267                     hir::ItemTrait(..) => {
1268                         let def_id = cx.map.local_def_id(id);
1269                         let trait_def = cx.lookup_trait_def(def_id);
1270                         let predicates = cx.lookup_predicates(def_id);
1271                         cx.construct_parameter_environment(item.span,
1272                                                            &trait_def.generics,
1273                                                            &predicates,
1274                                                            cx.region_maps.item_extent(id))
1275                     }
1276                     _ => {
1277                         cx.sess.span_bug(item.span,
1278                                          "ParameterEnvironment::from_item():
1279                                           can't create a parameter \
1280                                           environment for this kind of item")
1281                     }
1282                 }
1283             }
1284             Some(ast_map::NodeExpr(..)) => {
1285                 // This is a convenience to allow closures to work.
1286                 ParameterEnvironment::for_item(cx, cx.map.get_parent(id))
1287             }
1288             _ => {
1289                 cx.sess.bug(&format!("ParameterEnvironment::from_item(): \
1290                                      `{}` is not an item",
1291                                     cx.map.node_to_string(id)))
1292             }
1293         }
1294     }
1295 }
1296
1297 /// A "type scheme", in ML terminology, is a type combined with some
1298 /// set of generic types that the type is, well, generic over. In Rust
1299 /// terms, it is the "type" of a fn item or struct -- this type will
1300 /// include various generic parameters that must be substituted when
1301 /// the item/struct is referenced. That is called converting the type
1302 /// scheme to a monotype.
1303 ///
1304 /// - `generics`: the set of type parameters and their bounds
1305 /// - `ty`: the base types, which may reference the parameters defined
1306 ///   in `generics`
1307 ///
1308 /// Note that TypeSchemes are also sometimes called "polytypes" (and
1309 /// in fact this struct used to carry that name, so you may find some
1310 /// stray references in a comment or something). We try to reserve the
1311 /// "poly" prefix to refer to higher-ranked things, as in
1312 /// `PolyTraitRef`.
1313 ///
1314 /// Note that each item also comes with predicates, see
1315 /// `lookup_predicates`.
1316 #[derive(Clone, Debug)]
1317 pub struct TypeScheme<'tcx> {
1318     pub generics: Generics<'tcx>,
1319     pub ty: Ty<'tcx>,
1320 }
1321
1322 bitflags! {
1323     flags TraitFlags: u32 {
1324         const NO_TRAIT_FLAGS        = 0,
1325         const HAS_DEFAULT_IMPL      = 1 << 0,
1326         const IS_OBJECT_SAFE        = 1 << 1,
1327         const OBJECT_SAFETY_VALID   = 1 << 2,
1328         const IMPLS_VALID           = 1 << 3,
1329     }
1330 }
1331
1332 /// As `TypeScheme` but for a trait ref.
1333 pub struct TraitDef<'tcx> {
1334     pub unsafety: hir::Unsafety,
1335
1336     /// If `true`, then this trait had the `#[rustc_paren_sugar]`
1337     /// attribute, indicating that it should be used with `Foo()`
1338     /// sugar. This is a temporary thing -- eventually any trait wil
1339     /// be usable with the sugar (or without it).
1340     pub paren_sugar: bool,
1341
1342     /// Generic type definitions. Note that `Self` is listed in here
1343     /// as having a single bound, the trait itself (e.g., in the trait
1344     /// `Eq`, there is a single bound `Self : Eq`). This is so that
1345     /// default methods get to assume that the `Self` parameters
1346     /// implements the trait.
1347     pub generics: Generics<'tcx>,
1348
1349     pub trait_ref: TraitRef<'tcx>,
1350
1351     /// A list of the associated types defined in this trait. Useful
1352     /// for resolving `X::Foo` type markers.
1353     pub associated_type_names: Vec<Name>,
1354
1355     // Impls of this trait. To allow for quicker lookup, the impls are indexed
1356     // by a simplified version of their Self type: impls with a simplifiable
1357     // Self are stored in nonblanket_impls keyed by it, while all other impls
1358     // are stored in blanket_impls.
1359
1360     /// Impls of the trait.
1361     pub nonblanket_impls: RefCell<
1362         FnvHashMap<fast_reject::SimplifiedType, Vec<DefId>>
1363     >,
1364
1365     /// Blanket impls associated with the trait.
1366     pub blanket_impls: RefCell<Vec<DefId>>,
1367
1368     /// Various flags
1369     pub flags: Cell<TraitFlags>
1370 }
1371
1372 impl<'tcx> TraitDef<'tcx> {
1373     // returns None if not yet calculated
1374     pub fn object_safety(&self) -> Option<bool> {
1375         if self.flags.get().intersects(TraitFlags::OBJECT_SAFETY_VALID) {
1376             Some(self.flags.get().intersects(TraitFlags::IS_OBJECT_SAFE))
1377         } else {
1378             None
1379         }
1380     }
1381
1382     pub fn set_object_safety(&self, is_safe: bool) {
1383         assert!(self.object_safety().map(|cs| cs == is_safe).unwrap_or(true));
1384         self.flags.set(
1385             self.flags.get() | if is_safe {
1386                 TraitFlags::OBJECT_SAFETY_VALID | TraitFlags::IS_OBJECT_SAFE
1387             } else {
1388                 TraitFlags::OBJECT_SAFETY_VALID
1389             }
1390         );
1391     }
1392
1393     /// Records a trait-to-implementation mapping.
1394     pub fn record_impl(&self,
1395                        tcx: &ctxt<'tcx>,
1396                        impl_def_id: DefId,
1397                        impl_trait_ref: TraitRef<'tcx>) {
1398         debug!("TraitDef::record_impl for {:?}, from {:?}",
1399                self, impl_trait_ref);
1400
1401         // We don't want to borrow_mut after we already populated all impls,
1402         // so check if an impl is present with an immutable borrow first.
1403         if let Some(sty) = fast_reject::simplify_type(tcx,
1404                                                       impl_trait_ref.self_ty(), false) {
1405             if let Some(is) = self.nonblanket_impls.borrow().get(&sty) {
1406                 if is.contains(&impl_def_id) {
1407                     return // duplicate - skip
1408                 }
1409             }
1410
1411             self.nonblanket_impls.borrow_mut().entry(sty).or_insert(vec![]).push(impl_def_id)
1412         } else {
1413             if self.blanket_impls.borrow().contains(&impl_def_id) {
1414                 return // duplicate - skip
1415             }
1416             self.blanket_impls.borrow_mut().push(impl_def_id)
1417         }
1418     }
1419
1420
1421     pub fn for_each_impl<F: FnMut(DefId)>(&self, tcx: &ctxt<'tcx>, mut f: F)  {
1422         tcx.populate_implementations_for_trait_if_necessary(self.trait_ref.def_id);
1423
1424         for &impl_def_id in self.blanket_impls.borrow().iter() {
1425             f(impl_def_id);
1426         }
1427
1428         for v in self.nonblanket_impls.borrow().values() {
1429             for &impl_def_id in v {
1430                 f(impl_def_id);
1431             }
1432         }
1433     }
1434
1435     /// Iterate over every impl that could possibly match the
1436     /// self-type `self_ty`.
1437     pub fn for_each_relevant_impl<F: FnMut(DefId)>(&self,
1438                                                    tcx: &ctxt<'tcx>,
1439                                                    self_ty: Ty<'tcx>,
1440                                                    mut f: F)
1441     {
1442         tcx.populate_implementations_for_trait_if_necessary(self.trait_ref.def_id);
1443
1444         for &impl_def_id in self.blanket_impls.borrow().iter() {
1445             f(impl_def_id);
1446         }
1447
1448         // simplify_type(.., false) basically replaces type parameters and
1449         // projections with infer-variables. This is, of course, done on
1450         // the impl trait-ref when it is instantiated, but not on the
1451         // predicate trait-ref which is passed here.
1452         //
1453         // for example, if we match `S: Copy` against an impl like
1454         // `impl<T:Copy> Copy for Option<T>`, we replace the type variable
1455         // in `Option<T>` with an infer variable, to `Option<_>` (this
1456         // doesn't actually change fast_reject output), but we don't
1457         // replace `S` with anything - this impl of course can't be
1458         // selected, and as there are hundreds of similar impls,
1459         // considering them would significantly harm performance.
1460         if let Some(simp) = fast_reject::simplify_type(tcx, self_ty, true) {
1461             if let Some(impls) = self.nonblanket_impls.borrow().get(&simp) {
1462                 for &impl_def_id in impls {
1463                     f(impl_def_id);
1464                 }
1465             }
1466         } else {
1467             for v in self.nonblanket_impls.borrow().values() {
1468                 for &impl_def_id in v {
1469                     f(impl_def_id);
1470                 }
1471             }
1472         }
1473     }
1474
1475 }
1476
1477 bitflags! {
1478     flags AdtFlags: u32 {
1479         const NO_ADT_FLAGS        = 0,
1480         const IS_ENUM             = 1 << 0,
1481         const IS_DTORCK           = 1 << 1, // is this a dtorck type?
1482         const IS_DTORCK_VALID     = 1 << 2,
1483         const IS_PHANTOM_DATA     = 1 << 3,
1484         const IS_SIMD             = 1 << 4,
1485         const IS_FUNDAMENTAL      = 1 << 5,
1486         const IS_NO_DROP_FLAG     = 1 << 6,
1487     }
1488 }
1489
1490 pub type AdtDef<'tcx> = &'tcx AdtDefData<'tcx, 'static>;
1491 pub type VariantDef<'tcx> = &'tcx VariantDefData<'tcx, 'static>;
1492 pub type FieldDef<'tcx> = &'tcx FieldDefData<'tcx, 'static>;
1493
1494 // See comment on AdtDefData for explanation
1495 pub type AdtDefMaster<'tcx> = &'tcx AdtDefData<'tcx, 'tcx>;
1496 pub type VariantDefMaster<'tcx> = &'tcx VariantDefData<'tcx, 'tcx>;
1497 pub type FieldDefMaster<'tcx> = &'tcx FieldDefData<'tcx, 'tcx>;
1498
1499 pub struct VariantDefData<'tcx, 'container: 'tcx> {
1500     /// The variant's DefId. If this is a tuple-like struct,
1501     /// this is the DefId of the struct's ctor.
1502     pub did: DefId,
1503     pub name: Name, // struct's name if this is a struct
1504     pub disr_val: Disr,
1505     pub fields: Vec<FieldDefData<'tcx, 'container>>,
1506 }
1507
1508 pub struct FieldDefData<'tcx, 'container: 'tcx> {
1509     /// The field's DefId. NOTE: the fields of tuple-like enum variants
1510     /// are not real items, and don't have entries in tcache etc.
1511     pub did: DefId,
1512     /// special_idents::unnamed_field.name
1513     /// if this is a tuple-like field
1514     pub name: Name,
1515     pub vis: hir::Visibility,
1516     /// TyIVar is used here to allow for variance (see the doc at
1517     /// AdtDefData).
1518     ty: ivar::TyIVar<'tcx, 'container>
1519 }
1520
1521 /// The definition of an abstract data type - a struct or enum.
1522 ///
1523 /// These are all interned (by intern_adt_def) into the adt_defs
1524 /// table.
1525 ///
1526 /// Because of the possibility of nested tcx-s, this type
1527 /// needs 2 lifetimes: the traditional variant lifetime ('tcx)
1528 /// bounding the lifetime of the inner types is of course necessary.
1529 /// However, it is not sufficient - types from a child tcx must
1530 /// not be leaked into the master tcx by being stored in an AdtDefData.
1531 ///
1532 /// The 'container lifetime ensures that by outliving the container
1533 /// tcx and preventing shorter-lived types from being inserted. When
1534 /// write access is not needed, the 'container lifetime can be
1535 /// erased to 'static, which can be done by the AdtDef wrapper.
1536 pub struct AdtDefData<'tcx, 'container: 'tcx> {
1537     pub did: DefId,
1538     pub variants: Vec<VariantDefData<'tcx, 'container>>,
1539     destructor: Cell<Option<DefId>>,
1540     flags: Cell<AdtFlags>,
1541 }
1542
1543 impl<'tcx, 'container> PartialEq for AdtDefData<'tcx, 'container> {
1544     // AdtDefData are always interned and this is part of TyS equality
1545     #[inline]
1546     fn eq(&self, other: &Self) -> bool { self as *const _ == other as *const _ }
1547 }
1548
1549 impl<'tcx, 'container> Eq for AdtDefData<'tcx, 'container> {}
1550
1551 impl<'tcx, 'container> Hash for AdtDefData<'tcx, 'container> {
1552     #[inline]
1553     fn hash<H: Hasher>(&self, s: &mut H) {
1554         (self as *const AdtDefData).hash(s)
1555     }
1556 }
1557
1558 impl<'tcx> Encodable for AdtDef<'tcx> {
1559     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1560         self.did.encode(s)
1561     }
1562 }
1563
1564 impl<'tcx> Decodable for AdtDef<'tcx> {
1565     fn decode<D: Decoder>(d: &mut D) -> Result<AdtDef<'tcx>, D::Error> {
1566         let def_id: DefId = try!{ Decodable::decode(d) };
1567
1568         cstore::tls::with_decoding_context(d, |dcx, _| {
1569             let def_id = dcx.translate_def_id(def_id);
1570             Ok(dcx.tcx().lookup_adt_def(def_id))
1571         })
1572     }
1573 }
1574
1575
1576 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
1577 pub enum AdtKind { Struct, Enum }
1578
1579 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
1580 pub enum VariantKind { Struct, Tuple, Unit }
1581
1582 impl<'tcx, 'container> AdtDefData<'tcx, 'container> {
1583     fn new(tcx: &ctxt<'tcx>,
1584            did: DefId,
1585            kind: AdtKind,
1586            variants: Vec<VariantDefData<'tcx, 'container>>) -> Self {
1587         let mut flags = AdtFlags::NO_ADT_FLAGS;
1588         let attrs = tcx.get_attrs(did);
1589         if attr::contains_name(&attrs, "fundamental") {
1590             flags = flags | AdtFlags::IS_FUNDAMENTAL;
1591         }
1592         if attr::contains_name(&attrs, "unsafe_no_drop_flag") {
1593             flags = flags | AdtFlags::IS_NO_DROP_FLAG;
1594         }
1595         if tcx.lookup_simd(did) {
1596             flags = flags | AdtFlags::IS_SIMD;
1597         }
1598         if Some(did) == tcx.lang_items.phantom_data() {
1599             flags = flags | AdtFlags::IS_PHANTOM_DATA;
1600         }
1601         if let AdtKind::Enum = kind {
1602             flags = flags | AdtFlags::IS_ENUM;
1603         }
1604         AdtDefData {
1605             did: did,
1606             variants: variants,
1607             flags: Cell::new(flags),
1608             destructor: Cell::new(None)
1609         }
1610     }
1611
1612     fn calculate_dtorck(&'tcx self, tcx: &ctxt<'tcx>) {
1613         if tcx.is_adt_dtorck(self) {
1614             self.flags.set(self.flags.get() | AdtFlags::IS_DTORCK);
1615         }
1616         self.flags.set(self.flags.get() | AdtFlags::IS_DTORCK_VALID)
1617     }
1618
1619     /// Returns the kind of the ADT - Struct or Enum.
1620     #[inline]
1621     pub fn adt_kind(&self) -> AdtKind {
1622         if self.flags.get().intersects(AdtFlags::IS_ENUM) {
1623             AdtKind::Enum
1624         } else {
1625             AdtKind::Struct
1626         }
1627     }
1628
1629     /// Returns whether this is a dtorck type. If this returns
1630     /// true, this type being safe for destruction requires it to be
1631     /// alive; Otherwise, only the contents are required to be.
1632     #[inline]
1633     pub fn is_dtorck(&'tcx self, tcx: &ctxt<'tcx>) -> bool {
1634         if !self.flags.get().intersects(AdtFlags::IS_DTORCK_VALID) {
1635             self.calculate_dtorck(tcx)
1636         }
1637         self.flags.get().intersects(AdtFlags::IS_DTORCK)
1638     }
1639
1640     /// Returns whether this type is #[fundamental] for the purposes
1641     /// of coherence checking.
1642     #[inline]
1643     pub fn is_fundamental(&self) -> bool {
1644         self.flags.get().intersects(AdtFlags::IS_FUNDAMENTAL)
1645     }
1646
1647     #[inline]
1648     pub fn is_simd(&self) -> bool {
1649         self.flags.get().intersects(AdtFlags::IS_SIMD)
1650     }
1651
1652     /// Returns true if this is PhantomData<T>.
1653     #[inline]
1654     pub fn is_phantom_data(&self) -> bool {
1655         self.flags.get().intersects(AdtFlags::IS_PHANTOM_DATA)
1656     }
1657
1658     /// Returns whether this type has a destructor.
1659     pub fn has_dtor(&self) -> bool {
1660         match self.dtor_kind() {
1661             NoDtor => false,
1662             TraitDtor(..) => true
1663         }
1664     }
1665
1666     /// Asserts this is a struct and returns the struct's unique
1667     /// variant.
1668     pub fn struct_variant(&self) -> &VariantDefData<'tcx, 'container> {
1669         assert!(self.adt_kind() == AdtKind::Struct);
1670         &self.variants[0]
1671     }
1672
1673     #[inline]
1674     pub fn type_scheme(&self, tcx: &ctxt<'tcx>) -> TypeScheme<'tcx> {
1675         tcx.lookup_item_type(self.did)
1676     }
1677
1678     #[inline]
1679     pub fn predicates(&self, tcx: &ctxt<'tcx>) -> GenericPredicates<'tcx> {
1680         tcx.lookup_predicates(self.did)
1681     }
1682
1683     /// Returns an iterator over all fields contained
1684     /// by this ADT.
1685     #[inline]
1686     pub fn all_fields(&self) ->
1687             iter::FlatMap<
1688                 slice::Iter<VariantDefData<'tcx, 'container>>,
1689                 slice::Iter<FieldDefData<'tcx, 'container>>,
1690                 for<'s> fn(&'s VariantDefData<'tcx, 'container>)
1691                     -> slice::Iter<'s, FieldDefData<'tcx, 'container>>
1692             > {
1693         self.variants.iter().flat_map(VariantDefData::fields_iter)
1694     }
1695
1696     #[inline]
1697     pub fn is_empty(&self) -> bool {
1698         self.variants.is_empty()
1699     }
1700
1701     #[inline]
1702     pub fn is_univariant(&self) -> bool {
1703         self.variants.len() == 1
1704     }
1705
1706     pub fn is_payloadfree(&self) -> bool {
1707         !self.variants.is_empty() &&
1708             self.variants.iter().all(|v| v.fields.is_empty())
1709     }
1710
1711     pub fn variant_with_id(&self, vid: DefId) -> &VariantDefData<'tcx, 'container> {
1712         self.variants
1713             .iter()
1714             .find(|v| v.did == vid)
1715             .expect("variant_with_id: unknown variant")
1716     }
1717
1718     pub fn variant_index_with_id(&self, vid: DefId) -> usize {
1719         self.variants
1720             .iter()
1721             .position(|v| v.did == vid)
1722             .expect("variant_index_with_id: unknown variant")
1723     }
1724
1725     pub fn variant_of_def(&self, def: def::Def) -> &VariantDefData<'tcx, 'container> {
1726         match def {
1727             def::DefVariant(_, vid, _) => self.variant_with_id(vid),
1728             def::DefStruct(..) | def::DefTy(..) => self.struct_variant(),
1729             _ => panic!("unexpected def {:?} in variant_of_def", def)
1730         }
1731     }
1732
1733     pub fn destructor(&self) -> Option<DefId> {
1734         self.destructor.get()
1735     }
1736
1737     pub fn set_destructor(&self, dtor: DefId) {
1738         self.destructor.set(Some(dtor));
1739     }
1740
1741     pub fn dtor_kind(&self) -> DtorKind {
1742         match self.destructor.get() {
1743             Some(_) => {
1744                 TraitDtor(!self.flags.get().intersects(AdtFlags::IS_NO_DROP_FLAG))
1745             }
1746             None => NoDtor,
1747         }
1748     }
1749 }
1750
1751 impl<'tcx, 'container> VariantDefData<'tcx, 'container> {
1752     #[inline]
1753     fn fields_iter(&self) -> slice::Iter<FieldDefData<'tcx, 'container>> {
1754         self.fields.iter()
1755     }
1756
1757     pub fn kind(&self) -> VariantKind {
1758         match self.fields.get(0) {
1759             None => VariantKind::Unit,
1760             Some(&FieldDefData { name, .. }) if name == special_idents::unnamed_field.name => {
1761                 VariantKind::Tuple
1762             }
1763             Some(_) => VariantKind::Struct
1764         }
1765     }
1766
1767     pub fn is_tuple_struct(&self) -> bool {
1768         self.kind() == VariantKind::Tuple
1769     }
1770
1771     #[inline]
1772     pub fn find_field_named(&self,
1773                             name: ast::Name)
1774                             -> Option<&FieldDefData<'tcx, 'container>> {
1775         self.fields.iter().find(|f| f.name == name)
1776     }
1777
1778     #[inline]
1779     pub fn index_of_field_named(&self,
1780                                 name: ast::Name)
1781                                 -> Option<usize> {
1782         self.fields.iter().position(|f| f.name == name)
1783     }
1784
1785     #[inline]
1786     pub fn field_named(&self, name: ast::Name) -> &FieldDefData<'tcx, 'container> {
1787         self.find_field_named(name).unwrap()
1788     }
1789 }
1790
1791 impl<'tcx, 'container> FieldDefData<'tcx, 'container> {
1792     pub fn new(did: DefId,
1793                name: Name,
1794                vis: hir::Visibility) -> Self {
1795         FieldDefData {
1796             did: did,
1797             name: name,
1798             vis: vis,
1799             ty: ivar::TyIVar::new()
1800         }
1801     }
1802
1803     pub fn ty(&self, tcx: &ctxt<'tcx>, subst: &Substs<'tcx>) -> Ty<'tcx> {
1804         self.unsubst_ty().subst(tcx, subst)
1805     }
1806
1807     pub fn unsubst_ty(&self) -> Ty<'tcx> {
1808         self.ty.unwrap()
1809     }
1810
1811     pub fn fulfill_ty(&self, ty: Ty<'container>) {
1812         self.ty.fulfill(ty);
1813     }
1814 }
1815
1816 /// Records the substitutions used to translate the polytype for an
1817 /// item into the monotype of an item reference.
1818 #[derive(Clone)]
1819 pub struct ItemSubsts<'tcx> {
1820     pub substs: Substs<'tcx>,
1821 }
1822
1823 #[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, RustcEncodable, RustcDecodable)]
1824 pub enum ClosureKind {
1825     // Warning: Ordering is significant here! The ordering is chosen
1826     // because the trait Fn is a subtrait of FnMut and so in turn, and
1827     // hence we order it so that Fn < FnMut < FnOnce.
1828     FnClosureKind,
1829     FnMutClosureKind,
1830     FnOnceClosureKind,
1831 }
1832
1833 impl ClosureKind {
1834     pub fn trait_did(&self, cx: &ctxt) -> DefId {
1835         let result = match *self {
1836             FnClosureKind => cx.lang_items.require(FnTraitLangItem),
1837             FnMutClosureKind => {
1838                 cx.lang_items.require(FnMutTraitLangItem)
1839             }
1840             FnOnceClosureKind => {
1841                 cx.lang_items.require(FnOnceTraitLangItem)
1842             }
1843         };
1844         match result {
1845             Ok(trait_did) => trait_did,
1846             Err(err) => cx.sess.fatal(&err[..]),
1847         }
1848     }
1849
1850     /// True if this a type that impls this closure kind
1851     /// must also implement `other`.
1852     pub fn extends(self, other: ty::ClosureKind) -> bool {
1853         match (self, other) {
1854             (FnClosureKind, FnClosureKind) => true,
1855             (FnClosureKind, FnMutClosureKind) => true,
1856             (FnClosureKind, FnOnceClosureKind) => true,
1857             (FnMutClosureKind, FnMutClosureKind) => true,
1858             (FnMutClosureKind, FnOnceClosureKind) => true,
1859             (FnOnceClosureKind, FnOnceClosureKind) => true,
1860             _ => false,
1861         }
1862     }
1863 }
1864
1865 impl<'tcx> TyS<'tcx> {
1866     /// Iterator that walks `self` and any types reachable from
1867     /// `self`, in depth-first order. Note that just walks the types
1868     /// that appear in `self`, it does not descend into the fields of
1869     /// structs or variants. For example:
1870     ///
1871     /// ```notrust
1872     /// isize => { isize }
1873     /// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
1874     /// [isize] => { [isize], isize }
1875     /// ```
1876     pub fn walk(&'tcx self) -> TypeWalker<'tcx> {
1877         TypeWalker::new(self)
1878     }
1879
1880     /// Iterator that walks the immediate children of `self`.  Hence
1881     /// `Foo<Bar<i32>, u32>` yields the sequence `[Bar<i32>, u32]`
1882     /// (but not `i32`, like `walk`).
1883     pub fn walk_shallow(&'tcx self) -> IntoIter<Ty<'tcx>> {
1884         walk::walk_shallow(self)
1885     }
1886
1887     /// Walks `ty` and any types appearing within `ty`, invoking the
1888     /// callback `f` on each type. If the callback returns false, then the
1889     /// children of the current type are ignored.
1890     ///
1891     /// Note: prefer `ty.walk()` where possible.
1892     pub fn maybe_walk<F>(&'tcx self, mut f: F)
1893         where F : FnMut(Ty<'tcx>) -> bool
1894     {
1895         let mut walker = self.walk();
1896         while let Some(ty) = walker.next() {
1897             if !f(ty) {
1898                 walker.skip_current_subtree();
1899             }
1900         }
1901     }
1902 }
1903
1904 impl<'tcx> ItemSubsts<'tcx> {
1905     pub fn empty() -> ItemSubsts<'tcx> {
1906         ItemSubsts { substs: Substs::empty() }
1907     }
1908
1909     pub fn is_noop(&self) -> bool {
1910         self.substs.is_noop()
1911     }
1912 }
1913
1914 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1915 pub enum LvaluePreference {
1916     PreferMutLvalue,
1917     NoPreference
1918 }
1919
1920 impl LvaluePreference {
1921     pub fn from_mutbl(m: hir::Mutability) -> Self {
1922         match m {
1923             hir::MutMutable => PreferMutLvalue,
1924             hir::MutImmutable => NoPreference,
1925         }
1926     }
1927 }
1928
1929 /// Helper for looking things up in the various maps that are populated during
1930 /// typeck::collect (e.g., `cx.impl_or_trait_items`, `cx.tcache`, etc).  All of
1931 /// these share the pattern that if the id is local, it should have been loaded
1932 /// into the map by the `typeck::collect` phase.  If the def-id is external,
1933 /// then we have to go consult the crate loading code (and cache the result for
1934 /// the future).
1935 fn lookup_locally_or_in_crate_store<V, F>(descr: &str,
1936                                           def_id: DefId,
1937                                           map: &RefCell<DefIdMap<V>>,
1938                                           load_external: F) -> V where
1939     V: Clone,
1940     F: FnOnce() -> V,
1941 {
1942     match map.borrow().get(&def_id).cloned() {
1943         Some(v) => { return v; }
1944         None => { }
1945     }
1946
1947     if def_id.is_local() {
1948         panic!("No def'n found for {:?} in tcx.{}", def_id, descr);
1949     }
1950     let v = load_external();
1951     map.borrow_mut().insert(def_id, v.clone());
1952     v
1953 }
1954
1955 impl BorrowKind {
1956     pub fn from_mutbl(m: hir::Mutability) -> BorrowKind {
1957         match m {
1958             hir::MutMutable => MutBorrow,
1959             hir::MutImmutable => ImmBorrow,
1960         }
1961     }
1962
1963     /// Returns a mutability `m` such that an `&m T` pointer could be used to obtain this borrow
1964     /// kind. Because borrow kinds are richer than mutabilities, we sometimes have to pick a
1965     /// mutability that is stronger than necessary so that it at least *would permit* the borrow in
1966     /// question.
1967     pub fn to_mutbl_lossy(self) -> hir::Mutability {
1968         match self {
1969             MutBorrow => hir::MutMutable,
1970             ImmBorrow => hir::MutImmutable,
1971
1972             // We have no type corresponding to a unique imm borrow, so
1973             // use `&mut`. It gives all the capabilities of an `&uniq`
1974             // and hence is a safe "over approximation".
1975             UniqueImmBorrow => hir::MutMutable,
1976         }
1977     }
1978
1979     pub fn to_user_str(&self) -> &'static str {
1980         match *self {
1981             MutBorrow => "mutable",
1982             ImmBorrow => "immutable",
1983             UniqueImmBorrow => "uniquely immutable",
1984         }
1985     }
1986 }
1987
1988 impl<'tcx> ctxt<'tcx> {
1989     pub fn node_id_to_type(&self, id: NodeId) -> Ty<'tcx> {
1990         match self.node_id_to_type_opt(id) {
1991            Some(ty) => ty,
1992            None => self.sess.bug(
1993                &format!("node_id_to_type: no type for node `{}`",
1994                         self.map.node_to_string(id)))
1995         }
1996     }
1997
1998     pub fn node_id_to_type_opt(&self, id: NodeId) -> Option<Ty<'tcx>> {
1999         self.tables.borrow().node_types.get(&id).cloned()
2000     }
2001
2002     pub fn node_id_item_substs(&self, id: NodeId) -> ItemSubsts<'tcx> {
2003         match self.tables.borrow().item_substs.get(&id) {
2004             None => ItemSubsts::empty(),
2005             Some(ts) => ts.clone(),
2006         }
2007     }
2008
2009     // Returns the type of a pattern as a monotype. Like @expr_ty, this function
2010     // doesn't provide type parameter substitutions.
2011     pub fn pat_ty(&self, pat: &hir::Pat) -> Ty<'tcx> {
2012         self.node_id_to_type(pat.id)
2013     }
2014     pub fn pat_ty_opt(&self, pat: &hir::Pat) -> Option<Ty<'tcx>> {
2015         self.node_id_to_type_opt(pat.id)
2016     }
2017
2018     // Returns the type of an expression as a monotype.
2019     //
2020     // NB (1): This is the PRE-ADJUSTMENT TYPE for the expression.  That is, in
2021     // some cases, we insert `AutoAdjustment` annotations such as auto-deref or
2022     // auto-ref.  The type returned by this function does not consider such
2023     // adjustments.  See `expr_ty_adjusted()` instead.
2024     //
2025     // NB (2): This type doesn't provide type parameter substitutions; e.g. if you
2026     // ask for the type of "id" in "id(3)", it will return "fn(&isize) -> isize"
2027     // instead of "fn(ty) -> T with T = isize".
2028     pub fn expr_ty(&self, expr: &hir::Expr) -> Ty<'tcx> {
2029         self.node_id_to_type(expr.id)
2030     }
2031
2032     pub fn expr_ty_opt(&self, expr: &hir::Expr) -> Option<Ty<'tcx>> {
2033         self.node_id_to_type_opt(expr.id)
2034     }
2035
2036     /// Returns the type of `expr`, considering any `AutoAdjustment`
2037     /// entry recorded for that expression.
2038     ///
2039     /// It would almost certainly be better to store the adjusted ty in with
2040     /// the `AutoAdjustment`, but I opted not to do this because it would
2041     /// require serializing and deserializing the type and, although that's not
2042     /// hard to do, I just hate that code so much I didn't want to touch it
2043     /// unless it was to fix it properly, which seemed a distraction from the
2044     /// thread at hand! -nmatsakis
2045     pub fn expr_ty_adjusted(&self, expr: &hir::Expr) -> Ty<'tcx> {
2046         self.expr_ty(expr)
2047             .adjust(self, expr.span, expr.id,
2048                     self.tables.borrow().adjustments.get(&expr.id),
2049                     |method_call| {
2050             self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
2051         })
2052     }
2053
2054     pub fn expr_span(&self, id: NodeId) -> Span {
2055         match self.map.find(id) {
2056             Some(ast_map::NodeExpr(e)) => {
2057                 e.span
2058             }
2059             Some(f) => {
2060                 self.sess.bug(&format!("Node id {} is not an expr: {:?}",
2061                                        id, f));
2062             }
2063             None => {
2064                 self.sess.bug(&format!("Node id {} is not present \
2065                                         in the node map", id));
2066             }
2067         }
2068     }
2069
2070     pub fn local_var_name_str(&self, id: NodeId) -> InternedString {
2071         match self.map.find(id) {
2072             Some(ast_map::NodeLocal(pat)) => {
2073                 match pat.node {
2074                     hir::PatIdent(_, ref path1, _) => path1.node.name.as_str(),
2075                     _ => {
2076                         self.sess.bug(&format!("Variable id {} maps to {:?}, not local", id, pat));
2077                     },
2078                 }
2079             },
2080             r => self.sess.bug(&format!("Variable id {} maps to {:?}, not local", id, r)),
2081         }
2082     }
2083
2084     pub fn resolve_expr(&self, expr: &hir::Expr) -> def::Def {
2085         match self.def_map.borrow().get(&expr.id) {
2086             Some(def) => def.full_def(),
2087             None => {
2088                 self.sess.span_bug(expr.span, &format!(
2089                     "no def-map entry for expr {}", expr.id));
2090             }
2091         }
2092     }
2093
2094     pub fn expr_is_lval(&self, expr: &hir::Expr) -> bool {
2095          match expr.node {
2096             hir::ExprPath(..) => {
2097                 // We can't use resolve_expr here, as this needs to run on broken
2098                 // programs. We don't need to through - associated items are all
2099                 // rvalues.
2100                 match self.def_map.borrow().get(&expr.id) {
2101                     Some(&def::PathResolution {
2102                         base_def: def::DefStatic(..), ..
2103                     }) | Some(&def::PathResolution {
2104                         base_def: def::DefUpvar(..), ..
2105                     }) | Some(&def::PathResolution {
2106                         base_def: def::DefLocal(..), ..
2107                     }) => {
2108                         true
2109                     }
2110                     Some(&def::PathResolution { base_def: def::DefErr, .. })=> true,
2111                     Some(..) => false,
2112                     None => self.sess.span_bug(expr.span, &format!(
2113                         "no def for path {}", expr.id))
2114                 }
2115             }
2116
2117             hir::ExprUnary(hir::UnDeref, _) |
2118             hir::ExprField(..) |
2119             hir::ExprTupField(..) |
2120             hir::ExprIndex(..) => {
2121                 true
2122             }
2123
2124             hir::ExprCall(..) |
2125             hir::ExprMethodCall(..) |
2126             hir::ExprStruct(..) |
2127             hir::ExprRange(..) |
2128             hir::ExprTup(..) |
2129             hir::ExprIf(..) |
2130             hir::ExprMatch(..) |
2131             hir::ExprClosure(..) |
2132             hir::ExprBlock(..) |
2133             hir::ExprRepeat(..) |
2134             hir::ExprVec(..) |
2135             hir::ExprBreak(..) |
2136             hir::ExprAgain(..) |
2137             hir::ExprRet(..) |
2138             hir::ExprWhile(..) |
2139             hir::ExprLoop(..) |
2140             hir::ExprAssign(..) |
2141             hir::ExprInlineAsm(..) |
2142             hir::ExprAssignOp(..) |
2143             hir::ExprLit(_) |
2144             hir::ExprUnary(..) |
2145             hir::ExprBox(..) |
2146             hir::ExprAddrOf(..) |
2147             hir::ExprBinary(..) |
2148             hir::ExprCast(..) => {
2149                 false
2150             }
2151         }
2152     }
2153
2154     pub fn provided_trait_methods(&self, id: DefId) -> Vec<Rc<Method<'tcx>>> {
2155         if let Some(id) = self.map.as_local_node_id(id) {
2156             if let ItemTrait(_, _, _, ref ms) = self.map.expect_item(id).node {
2157                 ms.iter().filter_map(|ti| {
2158                     if let hir::MethodTraitItem(_, Some(_)) = ti.node {
2159                         match self.impl_or_trait_item(self.map.local_def_id(ti.id)) {
2160                             MethodTraitItem(m) => Some(m),
2161                             _ => {
2162                                 self.sess.bug("provided_trait_methods(): \
2163                                                non-method item found from \
2164                                                looking up provided method?!")
2165                             }
2166                         }
2167                     } else {
2168                         None
2169                     }
2170                 }).collect()
2171             } else {
2172                 self.sess.bug(&format!("provided_trait_methods: `{:?}` is not a trait", id))
2173             }
2174         } else {
2175             self.sess.cstore.provided_trait_methods(self, id)
2176         }
2177     }
2178
2179     pub fn associated_consts(&self, id: DefId) -> Vec<Rc<AssociatedConst<'tcx>>> {
2180         if let Some(id) = self.map.as_local_node_id(id) {
2181             match self.map.expect_item(id).node {
2182                 ItemTrait(_, _, _, ref tis) => {
2183                     tis.iter().filter_map(|ti| {
2184                         if let hir::ConstTraitItem(_, _) = ti.node {
2185                             match self.impl_or_trait_item(self.map.local_def_id(ti.id)) {
2186                                 ConstTraitItem(ac) => Some(ac),
2187                                 _ => {
2188                                     self.sess.bug("associated_consts(): \
2189                                                    non-const item found from \
2190                                                    looking up a constant?!")
2191                                 }
2192                             }
2193                         } else {
2194                             None
2195                         }
2196                     }).collect()
2197                 }
2198                 ItemImpl(_, _, _, _, _, ref iis) => {
2199                     iis.iter().filter_map(|ii| {
2200                         if let hir::ImplItemKind::Const(_, _) = ii.node {
2201                             match self.impl_or_trait_item(self.map.local_def_id(ii.id)) {
2202                                 ConstTraitItem(ac) => Some(ac),
2203                                 _ => {
2204                                     self.sess.bug("associated_consts(): \
2205                                                    non-const item found from \
2206                                                    looking up a constant?!")
2207                                 }
2208                             }
2209                         } else {
2210                             None
2211                         }
2212                     }).collect()
2213                 }
2214                 _ => {
2215                     self.sess.bug(&format!("associated_consts: `{:?}` is not a trait \
2216                                             or impl", id))
2217                 }
2218             }
2219         } else {
2220             self.sess.cstore.associated_consts(self, id)
2221         }
2222     }
2223
2224     pub fn trait_items(&self, trait_did: DefId) -> Rc<Vec<ImplOrTraitItem<'tcx>>> {
2225         let mut trait_items = self.trait_items_cache.borrow_mut();
2226         match trait_items.get(&trait_did).cloned() {
2227             Some(trait_items) => trait_items,
2228             None => {
2229                 let def_ids = self.trait_item_def_ids(trait_did);
2230                 let items: Rc<Vec<ImplOrTraitItem>> =
2231                     Rc::new(def_ids.iter()
2232                                    .map(|d| self.impl_or_trait_item(d.def_id()))
2233                                    .collect());
2234                 trait_items.insert(trait_did, items.clone());
2235                 items
2236             }
2237         }
2238     }
2239
2240     pub fn trait_impl_polarity(&self, id: DefId) -> Option<hir::ImplPolarity> {
2241         if let Some(id) = self.map.as_local_node_id(id) {
2242             match self.map.find(id) {
2243                 Some(ast_map::NodeItem(item)) => {
2244                     match item.node {
2245                         hir::ItemImpl(_, polarity, _, _, _, _) => Some(polarity),
2246                         _ => None
2247                     }
2248                 }
2249                 _ => None
2250             }
2251         } else {
2252             self.sess.cstore.impl_polarity(id)
2253         }
2254     }
2255
2256     pub fn custom_coerce_unsized_kind(&self, did: DefId) -> adjustment::CustomCoerceUnsized {
2257         memoized(&self.custom_coerce_unsized_kinds, did, |did: DefId| {
2258             let (kind, src) = if did.krate != LOCAL_CRATE {
2259                 (self.sess.cstore.custom_coerce_unsized_kind(did), "external")
2260             } else {
2261                 (None, "local")
2262             };
2263
2264             match kind {
2265                 Some(kind) => kind,
2266                 None => {
2267                     self.sess.bug(&format!("custom_coerce_unsized_kind: \
2268                                             {} impl `{}` is missing its kind",
2269                                            src, self.item_path_str(did)));
2270                 }
2271             }
2272         })
2273     }
2274
2275     pub fn impl_or_trait_item(&self, id: DefId) -> ImplOrTraitItem<'tcx> {
2276         lookup_locally_or_in_crate_store(
2277             "impl_or_trait_items", id, &self.impl_or_trait_items,
2278             || self.sess.cstore.impl_or_trait_item(self, id))
2279     }
2280
2281     pub fn trait_item_def_ids(&self, id: DefId) -> Rc<Vec<ImplOrTraitItemId>> {
2282         lookup_locally_or_in_crate_store(
2283             "trait_item_def_ids", id, &self.trait_item_def_ids,
2284             || Rc::new(self.sess.cstore.trait_item_def_ids(id)))
2285     }
2286
2287     /// Returns the trait-ref corresponding to a given impl, or None if it is
2288     /// an inherent impl.
2289     pub fn impl_trait_ref(&self, id: DefId) -> Option<TraitRef<'tcx>> {
2290         lookup_locally_or_in_crate_store(
2291             "impl_trait_refs", id, &self.impl_trait_refs,
2292             || self.sess.cstore.impl_trait_ref(self, id))
2293     }
2294
2295     /// Returns whether this DefId refers to an impl
2296     pub fn is_impl(&self, id: DefId) -> bool {
2297         if let Some(id) = self.map.as_local_node_id(id) {
2298             if let Some(ast_map::NodeItem(
2299                 &hir::Item { node: hir::ItemImpl(..), .. })) = self.map.find(id) {
2300                 true
2301             } else {
2302                 false
2303             }
2304         } else {
2305             self.sess.cstore.is_impl(id)
2306         }
2307     }
2308
2309     pub fn trait_ref_to_def_id(&self, tr: &hir::TraitRef) -> DefId {
2310         self.def_map.borrow().get(&tr.ref_id).expect("no def-map entry for trait").def_id()
2311     }
2312
2313     pub fn item_path_str(&self, id: DefId) -> String {
2314         self.with_path(id, |path| ast_map::path_to_string(path))
2315     }
2316
2317     pub fn def_path(&self, id: DefId) -> ast_map::DefPath {
2318         if id.is_local() {
2319             self.map.def_path(id)
2320         } else {
2321             self.sess.cstore.def_path(id)
2322         }
2323     }
2324
2325     pub fn with_path<T, F>(&self, id: DefId, f: F) -> T where
2326         F: FnOnce(ast_map::PathElems) -> T,
2327     {
2328         if let Some(id) = self.map.as_local_node_id(id) {
2329             self.map.with_path(id, f)
2330         } else {
2331             f(self.sess.cstore.item_path(id).iter().cloned().chain(LinkedPath::empty()))
2332         }
2333     }
2334
2335     pub fn item_name(&self, id: DefId) -> ast::Name {
2336         if let Some(id) = self.map.as_local_node_id(id) {
2337             self.map.get_path_elem(id).name()
2338         } else {
2339             self.sess.cstore.item_name(id)
2340         }
2341     }
2342
2343     // Register a given item type
2344     pub fn register_item_type(&self, did: DefId, ty: TypeScheme<'tcx>) {
2345         self.tcache.borrow_mut().insert(did, ty);
2346     }
2347
2348     // If the given item is in an external crate, looks up its type and adds it to
2349     // the type cache. Returns the type parameters and type.
2350     pub fn lookup_item_type(&self, did: DefId) -> TypeScheme<'tcx> {
2351         lookup_locally_or_in_crate_store(
2352             "tcache", did, &self.tcache,
2353             || self.sess.cstore.item_type(self, did))
2354     }
2355
2356     /// Given the did of a trait, returns its canonical trait ref.
2357     pub fn lookup_trait_def(&self, did: DefId) -> &'tcx TraitDef<'tcx> {
2358         lookup_locally_or_in_crate_store(
2359             "trait_defs", did, &self.trait_defs,
2360             || self.alloc_trait_def(self.sess.cstore.trait_def(self, did))
2361         )
2362     }
2363
2364     /// Given the did of an ADT, return a master reference to its
2365     /// definition. Unless you are planning on fulfilling the ADT's fields,
2366     /// use lookup_adt_def instead.
2367     pub fn lookup_adt_def_master(&self, did: DefId) -> AdtDefMaster<'tcx> {
2368         lookup_locally_or_in_crate_store(
2369             "adt_defs", did, &self.adt_defs,
2370             || self.sess.cstore.adt_def(self, did)
2371         )
2372     }
2373
2374     /// Given the did of an ADT, return a reference to its definition.
2375     pub fn lookup_adt_def(&self, did: DefId) -> AdtDef<'tcx> {
2376         // when reverse-variance goes away, a transmute::<AdtDefMaster,AdtDef>
2377         // woud be needed here.
2378         self.lookup_adt_def_master(did)
2379     }
2380
2381     /// Given the did of an item, returns its full set of predicates.
2382     pub fn lookup_predicates(&self, did: DefId) -> GenericPredicates<'tcx> {
2383         lookup_locally_or_in_crate_store(
2384             "predicates", did, &self.predicates,
2385             || self.sess.cstore.item_predicates(self, did))
2386     }
2387
2388     /// Given the did of a trait, returns its superpredicates.
2389     pub fn lookup_super_predicates(&self, did: DefId) -> GenericPredicates<'tcx> {
2390         lookup_locally_or_in_crate_store(
2391             "super_predicates", did, &self.super_predicates,
2392             || self.sess.cstore.item_super_predicates(self, did))
2393     }
2394
2395     /// Get the attributes of a definition.
2396     pub fn get_attrs(&self, did: DefId) -> Cow<'tcx, [ast::Attribute]> {
2397         if let Some(id) = self.map.as_local_node_id(did) {
2398             Cow::Borrowed(self.map.attrs(id))
2399         } else {
2400             Cow::Owned(self.sess.cstore.item_attrs(did))
2401         }
2402     }
2403
2404     /// Determine whether an item is annotated with an attribute
2405     pub fn has_attr(&self, did: DefId, attr: &str) -> bool {
2406         self.get_attrs(did).iter().any(|item| item.check_name(attr))
2407     }
2408
2409     /// Determine whether an item is annotated with `#[repr(packed)]`
2410     pub fn lookup_packed(&self, did: DefId) -> bool {
2411         self.lookup_repr_hints(did).contains(&attr::ReprPacked)
2412     }
2413
2414     /// Determine whether an item is annotated with `#[simd]`
2415     pub fn lookup_simd(&self, did: DefId) -> bool {
2416         self.has_attr(did, "simd")
2417             || self.lookup_repr_hints(did).contains(&attr::ReprSimd)
2418     }
2419
2420     /// Obtain the representation annotation for a struct definition.
2421     pub fn lookup_repr_hints(&self, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
2422         memoized(&self.repr_hint_cache, did, |did: DefId| {
2423             Rc::new(if did.is_local() {
2424                 self.get_attrs(did).iter().flat_map(|meta| {
2425                     attr::find_repr_attrs(self.sess.diagnostic(), meta).into_iter()
2426                 }).collect()
2427             } else {
2428                 self.sess.cstore.repr_attrs(did)
2429             })
2430         })
2431     }
2432
2433     pub fn item_variances(&self, item_id: DefId) -> Rc<ItemVariances> {
2434         lookup_locally_or_in_crate_store(
2435             "item_variance_map", item_id, &self.item_variance_map,
2436             || Rc::new(self.sess.cstore.item_variances(item_id)))
2437     }
2438
2439     pub fn trait_has_default_impl(&self, trait_def_id: DefId) -> bool {
2440         self.populate_implementations_for_trait_if_necessary(trait_def_id);
2441
2442         let def = self.lookup_trait_def(trait_def_id);
2443         def.flags.get().intersects(TraitFlags::HAS_DEFAULT_IMPL)
2444     }
2445
2446     /// Records a trait-to-implementation mapping.
2447     pub fn record_trait_has_default_impl(&self, trait_def_id: DefId) {
2448         let def = self.lookup_trait_def(trait_def_id);
2449         def.flags.set(def.flags.get() | TraitFlags::HAS_DEFAULT_IMPL)
2450     }
2451
2452     /// Load primitive inherent implementations if necessary
2453     pub fn populate_implementations_for_primitive_if_necessary(&self,
2454                                                                primitive_def_id: DefId) {
2455         if primitive_def_id.is_local() {
2456             return
2457         }
2458
2459         if self.populated_external_primitive_impls.borrow().contains(&primitive_def_id) {
2460             return
2461         }
2462
2463         debug!("populate_implementations_for_primitive_if_necessary: searching for {:?}",
2464                primitive_def_id);
2465
2466         let impl_items = self.sess.cstore.impl_items(primitive_def_id);
2467
2468         // Store the implementation info.
2469         self.impl_items.borrow_mut().insert(primitive_def_id, impl_items);
2470         self.populated_external_primitive_impls.borrow_mut().insert(primitive_def_id);
2471     }
2472
2473     /// Populates the type context with all the inherent implementations for
2474     /// the given type if necessary.
2475     pub fn populate_inherent_implementations_for_type_if_necessary(&self,
2476                                                                    type_id: DefId) {
2477         if type_id.is_local() {
2478             return
2479         }
2480
2481         if self.populated_external_types.borrow().contains(&type_id) {
2482             return
2483         }
2484
2485         debug!("populate_inherent_implementations_for_type_if_necessary: searching for {:?}",
2486                type_id);
2487
2488         let inherent_impls = self.sess.cstore.inherent_implementations_for_type(type_id);
2489         for &impl_def_id in &inherent_impls {
2490             // Store the implementation info.
2491             let impl_items = self.sess.cstore.impl_items(impl_def_id);
2492             self.impl_items.borrow_mut().insert(impl_def_id, impl_items);
2493         }
2494
2495         self.inherent_impls.borrow_mut().insert(type_id, Rc::new(inherent_impls));
2496         self.populated_external_types.borrow_mut().insert(type_id);
2497     }
2498
2499     /// Populates the type context with all the implementations for the given
2500     /// trait if necessary.
2501     pub fn populate_implementations_for_trait_if_necessary(&self, trait_id: DefId) {
2502         if trait_id.is_local() {
2503             return
2504         }
2505
2506         let def = self.lookup_trait_def(trait_id);
2507         if def.flags.get().intersects(TraitFlags::IMPLS_VALID) {
2508             return;
2509         }
2510
2511         debug!("populate_implementations_for_trait_if_necessary: searching for {:?}", def);
2512
2513         if self.sess.cstore.is_defaulted_trait(trait_id) {
2514             self.record_trait_has_default_impl(trait_id);
2515         }
2516
2517         for impl_def_id in self.sess.cstore.implementations_of_trait(trait_id) {
2518             let impl_items = self.sess.cstore.impl_items(impl_def_id);
2519             let trait_ref = self.impl_trait_ref(impl_def_id).unwrap();
2520             // Record the trait->implementation mapping.
2521             def.record_impl(self, impl_def_id, trait_ref);
2522
2523             // For any methods that use a default implementation, add them to
2524             // the map. This is a bit unfortunate.
2525             for impl_item_def_id in &impl_items {
2526                 let method_def_id = impl_item_def_id.def_id();
2527                 // load impl items eagerly for convenience
2528                 // FIXME: we may want to load these lazily
2529                 self.impl_or_trait_item(method_def_id);
2530             }
2531
2532             // Store the implementation info.
2533             self.impl_items.borrow_mut().insert(impl_def_id, impl_items);
2534         }
2535
2536         def.flags.set(def.flags.get() | TraitFlags::IMPLS_VALID);
2537     }
2538
2539     pub fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind {
2540         Tables::closure_kind(&self.tables, self, def_id)
2541     }
2542
2543     pub fn closure_type(&self,
2544                         def_id: DefId,
2545                         substs: &ClosureSubsts<'tcx>)
2546                         -> ty::ClosureTy<'tcx>
2547     {
2548         Tables::closure_type(&self.tables, self, def_id, substs)
2549     }
2550
2551     /// Given the def_id of an impl, return the def_id of the trait it implements.
2552     /// If it implements no trait, return `None`.
2553     pub fn trait_id_of_impl(&self, def_id: DefId) -> Option<DefId> {
2554         self.impl_trait_ref(def_id).map(|tr| tr.def_id)
2555     }
2556
2557     /// If the given def ID describes a method belonging to an impl, return the
2558     /// ID of the impl that the method belongs to. Otherwise, return `None`.
2559     pub fn impl_of_method(&self, def_id: DefId) -> Option<DefId> {
2560         if def_id.krate != LOCAL_CRATE {
2561             return match self.sess.cstore.impl_or_trait_item(self, def_id).container() {
2562                 TraitContainer(_) => None,
2563                 ImplContainer(def_id) => Some(def_id),
2564             };
2565         }
2566         match self.impl_or_trait_items.borrow().get(&def_id).cloned() {
2567             Some(trait_item) => {
2568                 match trait_item.container() {
2569                     TraitContainer(_) => None,
2570                     ImplContainer(def_id) => Some(def_id),
2571                 }
2572             }
2573             None => None
2574         }
2575     }
2576
2577     /// If the given def ID describes an item belonging to a trait (either a
2578     /// default method or an implementation of a trait method), return the ID of
2579     /// the trait that the method belongs to. Otherwise, return `None`.
2580     pub fn trait_of_item(&self, def_id: DefId) -> Option<DefId> {
2581         if def_id.krate != LOCAL_CRATE {
2582             return self.sess.cstore.trait_of_item(self, def_id);
2583         }
2584         match self.impl_or_trait_items.borrow().get(&def_id).cloned() {
2585             Some(impl_or_trait_item) => {
2586                 match impl_or_trait_item.container() {
2587                     TraitContainer(def_id) => Some(def_id),
2588                     ImplContainer(def_id) => self.trait_id_of_impl(def_id),
2589                 }
2590             }
2591             None => None
2592         }
2593     }
2594
2595     /// If the given def ID describes an item belonging to a trait, (either a
2596     /// default method or an implementation of a trait method), return the ID of
2597     /// the method inside trait definition (this means that if the given def ID
2598     /// is already that of the original trait method, then the return value is
2599     /// the same).
2600     /// Otherwise, return `None`.
2601     pub fn trait_item_of_item(&self, def_id: DefId) -> Option<ImplOrTraitItemId> {
2602         let impl_item = match self.impl_or_trait_items.borrow().get(&def_id) {
2603             Some(m) => m.clone(),
2604             None => return None,
2605         };
2606         let name = impl_item.name();
2607         match self.trait_of_item(def_id) {
2608             Some(trait_did) => {
2609                 self.trait_items(trait_did).iter()
2610                     .find(|item| item.name() == name)
2611                     .map(|item| item.id())
2612             }
2613             None => None
2614         }
2615     }
2616
2617     /// Construct a parameter environment suitable for static contexts or other contexts where there
2618     /// are no free type/lifetime parameters in scope.
2619     pub fn empty_parameter_environment<'a>(&'a self)
2620                                            -> ParameterEnvironment<'a,'tcx> {
2621
2622         // for an empty parameter environment, there ARE no free
2623         // regions, so it shouldn't matter what we use for the free id
2624         let free_id_outlive = self.region_maps.node_extent(ast::DUMMY_NODE_ID);
2625         ty::ParameterEnvironment { tcx: self,
2626                                    free_substs: Substs::empty(),
2627                                    caller_bounds: Vec::new(),
2628                                    implicit_region_bound: ty::ReEmpty,
2629                                    selection_cache: traits::SelectionCache::new(),
2630                                    evaluation_cache: traits::EvaluationCache::new(),
2631                                    free_id_outlive: free_id_outlive }
2632     }
2633
2634     /// Constructs and returns a substitution that can be applied to move from
2635     /// the "outer" view of a type or method to the "inner" view.
2636     /// In general, this means converting from bound parameters to
2637     /// free parameters. Since we currently represent bound/free type
2638     /// parameters in the same way, this only has an effect on regions.
2639     pub fn construct_free_substs(&self, generics: &Generics<'tcx>,
2640                                  free_id_outlive: CodeExtent) -> Substs<'tcx> {
2641         // map T => T
2642         let mut types = VecPerParamSpace::empty();
2643         for def in generics.types.as_slice() {
2644             debug!("construct_parameter_environment(): push_types_from_defs: def={:?}",
2645                     def);
2646             types.push(def.space, self.mk_param_from_def(def));
2647         }
2648
2649         // map bound 'a => free 'a
2650         let mut regions = VecPerParamSpace::empty();
2651         for def in generics.regions.as_slice() {
2652             let region =
2653                 ReFree(FreeRegion { scope: free_id_outlive,
2654                                     bound_region: BrNamed(def.def_id, def.name) });
2655             debug!("push_region_params {:?}", region);
2656             regions.push(def.space, region);
2657         }
2658
2659         Substs {
2660             types: types,
2661             regions: subst::NonerasedRegions(regions)
2662         }
2663     }
2664
2665     /// See `ParameterEnvironment` struct def'n for details.
2666     /// If you were using `free_id: NodeId`, you might try `self.region_maps.item_extent(free_id)`
2667     /// for the `free_id_outlive` parameter. (But note that that is not always quite right.)
2668     pub fn construct_parameter_environment<'a>(&'a self,
2669                                                span: Span,
2670                                                generics: &ty::Generics<'tcx>,
2671                                                generic_predicates: &ty::GenericPredicates<'tcx>,
2672                                                free_id_outlive: CodeExtent)
2673                                                -> ParameterEnvironment<'a, 'tcx>
2674     {
2675         //
2676         // Construct the free substs.
2677         //
2678
2679         let free_substs = self.construct_free_substs(generics, free_id_outlive);
2680
2681         //
2682         // Compute the bounds on Self and the type parameters.
2683         //
2684
2685         let bounds = generic_predicates.instantiate(self, &free_substs);
2686         let bounds = self.liberate_late_bound_regions(free_id_outlive, &ty::Binder(bounds));
2687         let predicates = bounds.predicates.into_vec();
2688
2689         // Finally, we have to normalize the bounds in the environment, in
2690         // case they contain any associated type projections. This process
2691         // can yield errors if the put in illegal associated types, like
2692         // `<i32 as Foo>::Bar` where `i32` does not implement `Foo`. We
2693         // report these errors right here; this doesn't actually feel
2694         // right to me, because constructing the environment feels like a
2695         // kind of a "idempotent" action, but I'm not sure where would be
2696         // a better place. In practice, we construct environments for
2697         // every fn once during type checking, and we'll abort if there
2698         // are any errors at that point, so after type checking you can be
2699         // sure that this will succeed without errors anyway.
2700         //
2701
2702         let unnormalized_env = ty::ParameterEnvironment {
2703             tcx: self,
2704             free_substs: free_substs,
2705             implicit_region_bound: ty::ReScope(free_id_outlive),
2706             caller_bounds: predicates,
2707             selection_cache: traits::SelectionCache::new(),
2708             evaluation_cache: traits::EvaluationCache::new(),
2709             free_id_outlive: free_id_outlive,
2710         };
2711
2712         let cause = traits::ObligationCause::misc(span, free_id_outlive.node_id(&self.region_maps));
2713         traits::normalize_param_env_or_error(unnormalized_env, cause)
2714     }
2715
2716     pub fn is_method_call(&self, expr_id: NodeId) -> bool {
2717         self.tables.borrow().method_map.contains_key(&MethodCall::expr(expr_id))
2718     }
2719
2720     pub fn is_overloaded_autoderef(&self, expr_id: NodeId, autoderefs: u32) -> bool {
2721         self.tables.borrow().method_map.contains_key(&MethodCall::autoderef(expr_id,
2722                                                                             autoderefs))
2723     }
2724
2725     pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture> {
2726         Some(self.tables.borrow().upvar_capture_map.get(&upvar_id).unwrap().clone())
2727     }
2728 }
2729
2730 /// The category of explicit self.
2731 #[derive(Clone, Copy, Eq, PartialEq, Debug)]
2732 pub enum ExplicitSelfCategory {
2733     StaticExplicitSelfCategory,
2734     ByValueExplicitSelfCategory,
2735     ByReferenceExplicitSelfCategory(Region, hir::Mutability),
2736     ByBoxExplicitSelfCategory,
2737 }
2738
2739 /// A free variable referred to in a function.
2740 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
2741 pub struct Freevar {
2742     /// The variable being accessed free.
2743     pub def: def::Def,
2744
2745     // First span where it is accessed (there can be multiple).
2746     pub span: Span
2747 }
2748
2749 pub type FreevarMap = NodeMap<Vec<Freevar>>;
2750
2751 pub type CaptureModeMap = NodeMap<hir::CaptureClause>;
2752
2753 // Trait method resolution
2754 pub type TraitMap = NodeMap<Vec<DefId>>;
2755
2756 // Map from the NodeId of a glob import to a list of items which are actually
2757 // imported.
2758 pub type GlobMap = HashMap<NodeId, HashSet<Name>>;
2759
2760 impl<'tcx> ctxt<'tcx> {
2761     pub fn with_freevars<T, F>(&self, fid: NodeId, f: F) -> T where
2762         F: FnOnce(&[Freevar]) -> T,
2763     {
2764         match self.freevars.borrow().get(&fid) {
2765             None => f(&[]),
2766             Some(d) => f(&d[..])
2767         }
2768     }
2769
2770     pub fn make_substs_for_receiver_types(&self,
2771                                           trait_ref: &ty::TraitRef<'tcx>,
2772                                           method: &ty::Method<'tcx>)
2773                                           -> subst::Substs<'tcx>
2774     {
2775         /*!
2776          * Substitutes the values for the receiver's type parameters
2777          * that are found in method, leaving the method's type parameters
2778          * intact.
2779          */
2780
2781         let meth_tps: Vec<Ty> =
2782             method.generics.types.get_slice(subst::FnSpace)
2783                   .iter()
2784                   .map(|def| self.mk_param_from_def(def))
2785                   .collect();
2786         let meth_regions: Vec<ty::Region> =
2787             method.generics.regions.get_slice(subst::FnSpace)
2788                   .iter()
2789                   .map(|def| def.to_early_bound_region())
2790                   .collect();
2791         trait_ref.substs.clone().with_method(meth_tps, meth_regions)
2792     }
2793 }
2794
2795 /// An "escaping region" is a bound region whose binder is not part of `t`.
2796 ///
2797 /// So, for example, consider a type like the following, which has two binders:
2798 ///
2799 ///    for<'a> fn(x: for<'b> fn(&'a isize, &'b isize))
2800 ///    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ outer scope
2801 ///                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~  inner scope
2802 ///
2803 /// This type has *bound regions* (`'a`, `'b`), but it does not have escaping regions, because the
2804 /// binders of both `'a` and `'b` are part of the type itself. However, if we consider the *inner
2805 /// fn type*, that type has an escaping region: `'a`.
2806 ///
2807 /// Note that what I'm calling an "escaping region" is often just called a "free region". However,
2808 /// we already use the term "free region". It refers to the regions that we use to represent bound
2809 /// regions on a fn definition while we are typechecking its body.
2810 ///
2811 /// To clarify, conceptually there is no particular difference between an "escaping" region and a
2812 /// "free" region. However, there is a big difference in practice. Basically, when "entering" a
2813 /// binding level, one is generally required to do some sort of processing to a bound region, such
2814 /// as replacing it with a fresh/skolemized region, or making an entry in the environment to
2815 /// represent the scope to which it is attached, etc. An escaping region represents a bound region
2816 /// for which this processing has not yet been done.
2817 pub trait RegionEscape {
2818     fn has_escaping_regions(&self) -> bool {
2819         self.has_regions_escaping_depth(0)
2820     }
2821
2822     fn has_regions_escaping_depth(&self, depth: u32) -> bool;
2823 }
2824
2825 pub trait HasTypeFlags {
2826     fn has_type_flags(&self, flags: TypeFlags) -> bool;
2827     fn has_projection_types(&self) -> bool {
2828         self.has_type_flags(TypeFlags::HAS_PROJECTION)
2829     }
2830     fn references_error(&self) -> bool {
2831         self.has_type_flags(TypeFlags::HAS_TY_ERR)
2832     }
2833     fn has_param_types(&self) -> bool {
2834         self.has_type_flags(TypeFlags::HAS_PARAMS)
2835     }
2836     fn has_self_ty(&self) -> bool {
2837         self.has_type_flags(TypeFlags::HAS_SELF)
2838     }
2839     fn has_infer_types(&self) -> bool {
2840         self.has_type_flags(TypeFlags::HAS_TY_INFER)
2841     }
2842     fn needs_infer(&self) -> bool {
2843         self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_RE_INFER)
2844     }
2845     fn needs_subst(&self) -> bool {
2846         self.has_type_flags(TypeFlags::NEEDS_SUBST)
2847     }
2848     fn has_closure_types(&self) -> bool {
2849         self.has_type_flags(TypeFlags::HAS_TY_CLOSURE)
2850     }
2851     fn has_erasable_regions(&self) -> bool {
2852         self.has_type_flags(TypeFlags::HAS_RE_EARLY_BOUND |
2853                             TypeFlags::HAS_RE_INFER |
2854                             TypeFlags::HAS_FREE_REGIONS)
2855     }
2856     /// Indicates whether this value references only 'global'
2857     /// types/lifetimes that are the same regardless of what fn we are
2858     /// in. This is used for caching. Errs on the side of returning
2859     /// false.
2860     fn is_global(&self) -> bool {
2861         !self.has_type_flags(TypeFlags::HAS_LOCAL_NAMES)
2862     }
2863 }