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