]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/resolve.rs
rollup merge of #17114 : nick29581/dst-type
[rust.git] / src / librustc / middle / resolve.rs
1 // Copyright 2012-2014 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 #![allow(non_camel_case_types)]
12
13 use driver::session::Session;
14 use lint;
15 use metadata::csearch;
16 use metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
17 use middle::def::*;
18 use middle::lang_items::LanguageItems;
19 use middle::pat_util::pat_bindings;
20 use middle::subst::{ParamSpace, FnSpace, TypeSpace};
21 use middle::ty::{ExplicitSelfCategory, StaticExplicitSelfCategory};
22 use util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
23
24 use syntax::ast::{Arm, BindByRef, BindByValue, BindingMode, Block, Crate};
25 use syntax::ast::{DeclItem, DefId, Expr, ExprAgain, ExprBreak, ExprField};
26 use syntax::ast::{ExprFnBlock, ExprForLoop, ExprLoop, ExprWhile, ExprMethodCall};
27 use syntax::ast::{ExprPath, ExprProc, ExprStruct, ExprUnboxedFn, FnDecl};
28 use syntax::ast::{ForeignItem, ForeignItemFn, ForeignItemStatic, Generics};
29 use syntax::ast::{Ident, ImplItem, Item, ItemEnum, ItemFn, ItemForeignMod};
30 use syntax::ast::{ItemImpl, ItemMac, ItemMod, ItemStatic, ItemStruct};
31 use syntax::ast::{ItemTrait, ItemTy, LOCAL_CRATE, Local, Method};
32 use syntax::ast::{MethodImplItem, Mod, Name, NamedField, NodeId};
33 use syntax::ast::{P, Pat, PatEnum, PatIdent, PatLit};
34 use syntax::ast::{PatRange, PatStruct, Path, PathListIdent, PathListMod};
35 use syntax::ast::{PrimTy, Public, SelfExplicit, SelfStatic};
36 use syntax::ast::{RegionTyParamBound, StmtDecl, StructField};
37 use syntax::ast::{StructVariantKind, TraitRef, TraitTyParamBound};
38 use syntax::ast::{TupleVariantKind, Ty, TyBool, TyChar, TyClosure, TyF32};
39 use syntax::ast::{TyF64, TyFloat, TyI, TyI8, TyI16, TyI32, TyI64, TyInt};
40 use syntax::ast::{TyParam, TyParamBound, TyPath, TyPtr, TyProc, TyRptr};
41 use syntax::ast::{TyStr, TyU, TyU8, TyU16, TyU32, TyU64, TyUint};
42 use syntax::ast::{UnboxedFnTyParamBound, UnnamedField, UnsafeFn, Variant};
43 use syntax::ast::{ViewItem, ViewItemExternCrate, ViewItemUse, ViewPathGlob};
44 use syntax::ast::{ViewPathList, ViewPathSimple, Visibility};
45 use syntax::ast;
46 use syntax::ast_util::{PostExpansionMethod, local_def};
47 use syntax::ast_util::{trait_item_to_ty_method, walk_pat};
48 use syntax::attr::AttrMetaMethods;
49 use syntax::ext::mtwt;
50 use syntax::parse::token::special_names;
51 use syntax::parse::token::special_idents;
52 use syntax::parse::token;
53 use syntax::codemap::{Span, DUMMY_SP, Pos};
54 use syntax::owned_slice::OwnedSlice;
55 use syntax::visit;
56 use syntax::visit::Visitor;
57
58 use std::collections::{HashMap, HashSet};
59 use std::cell::{Cell, RefCell};
60 use std::gc::GC;
61 use std::mem::replace;
62 use std::rc::{Rc, Weak};
63 use std::uint;
64
65 // Definition mapping
66 pub type DefMap = RefCell<NodeMap<Def>>;
67
68 struct binding_info {
69     span: Span,
70     binding_mode: BindingMode,
71 }
72
73 // Map from the name in a pattern to its binding mode.
74 type BindingMap = HashMap<Name,binding_info>;
75
76 // Trait method resolution
77 pub type TraitMap = NodeMap<Vec<DefId> >;
78
79 // This is the replacement export map. It maps a module to all of the exports
80 // within.
81 pub type ExportMap2 = RefCell<NodeMap<Vec<Export2> >>;
82
83 pub struct Export2 {
84     pub name: String,        // The name of the target.
85     pub def_id: DefId,     // The definition of the target.
86 }
87
88 // This set contains all exported definitions from external crates. The set does
89 // not contain any entries from local crates.
90 pub type ExternalExports = DefIdSet;
91
92 // FIXME: dox
93 pub type LastPrivateMap = NodeMap<LastPrivate>;
94
95 pub enum LastPrivate {
96     LastMod(PrivateDep),
97     // `use` directives (imports) can refer to two separate definitions in the
98     // type and value namespaces. We record here the last private node for each
99     // and whether the import is in fact used for each.
100     // If the Option<PrivateDep> fields are None, it means there is no definition
101     // in that namespace.
102     LastImport{pub value_priv: Option<PrivateDep>,
103                pub value_used: ImportUse,
104                pub type_priv: Option<PrivateDep>,
105                pub type_used: ImportUse},
106 }
107
108 pub enum PrivateDep {
109     AllPublic,
110     DependsOn(DefId),
111 }
112
113 // How an import is used.
114 #[deriving(PartialEq)]
115 pub enum ImportUse {
116     Unused,       // The import is not used.
117     Used,         // The import is used.
118 }
119
120 impl LastPrivate {
121     fn or(self, other: LastPrivate) -> LastPrivate {
122         match (self, other) {
123             (me, LastMod(AllPublic)) => me,
124             (_, other) => other,
125         }
126     }
127 }
128
129 #[deriving(PartialEq)]
130 enum PatternBindingMode {
131     RefutableMode,
132     LocalIrrefutableMode,
133     ArgumentIrrefutableMode,
134 }
135
136 #[deriving(PartialEq, Eq, Hash)]
137 enum Namespace {
138     TypeNS,
139     ValueNS
140 }
141
142 #[deriving(PartialEq)]
143 enum NamespaceError {
144     NoError,
145     ModuleError,
146     TypeError,
147     ValueError
148 }
149
150 /// A NamespaceResult represents the result of resolving an import in
151 /// a particular namespace. The result is either definitely-resolved,
152 /// definitely- unresolved, or unknown.
153 #[deriving(Clone)]
154 enum NamespaceResult {
155     /// Means that resolve hasn't gathered enough information yet to determine
156     /// whether the name is bound in this namespace. (That is, it hasn't
157     /// resolved all `use` directives yet.)
158     UnknownResult,
159     /// Means that resolve has determined that the name is definitely
160     /// not bound in the namespace.
161     UnboundResult,
162     /// Means that resolve has determined that the name is bound in the Module
163     /// argument, and specified by the NameBindings argument.
164     BoundResult(Rc<Module>, Rc<NameBindings>)
165 }
166
167 impl NamespaceResult {
168     fn is_unknown(&self) -> bool {
169         match *self {
170             UnknownResult => true,
171             _ => false
172         }
173     }
174     fn is_unbound(&self) -> bool {
175         match *self {
176             UnboundResult => true,
177             _ => false
178         }
179     }
180 }
181
182 enum NameDefinition {
183     NoNameDefinition,           //< The name was unbound.
184     ChildNameDefinition(Def, LastPrivate), //< The name identifies an immediate child.
185     ImportNameDefinition(Def, LastPrivate) //< The name identifies an import.
186 }
187
188 impl<'a> Visitor<()> for Resolver<'a> {
189     fn visit_item(&mut self, item: &Item, _: ()) {
190         self.resolve_item(item);
191     }
192     fn visit_arm(&mut self, arm: &Arm, _: ()) {
193         self.resolve_arm(arm);
194     }
195     fn visit_block(&mut self, block: &Block, _: ()) {
196         self.resolve_block(block);
197     }
198     fn visit_expr(&mut self, expr: &Expr, _: ()) {
199         self.resolve_expr(expr);
200     }
201     fn visit_local(&mut self, local: &Local, _: ()) {
202         self.resolve_local(local);
203     }
204     fn visit_ty(&mut self, ty: &Ty, _: ()) {
205         self.resolve_type(ty);
206     }
207 }
208
209 /// Contains data for specific types of import directives.
210 enum ImportDirectiveSubclass {
211     SingleImport(Ident /* target */, Ident /* source */),
212     GlobImport
213 }
214
215 /// The context that we thread through while building the reduced graph.
216 #[deriving(Clone)]
217 enum ReducedGraphParent {
218     ModuleReducedGraphParent(Rc<Module>)
219 }
220
221 impl ReducedGraphParent {
222     fn module(&self) -> Rc<Module> {
223         match *self {
224             ModuleReducedGraphParent(ref m) => {
225                 m.clone()
226             }
227         }
228     }
229 }
230
231 type ErrorMessage = Option<(Span, String)>;
232
233 enum ResolveResult<T> {
234     Failed(ErrorMessage),   // Failed to resolve the name, optional helpful error message.
235     Indeterminate,          // Couldn't determine due to unresolved globs.
236     Success(T)              // Successfully resolved the import.
237 }
238
239 impl<T> ResolveResult<T> {
240     fn indeterminate(&self) -> bool {
241         match *self { Indeterminate => true, _ => false }
242     }
243 }
244
245 enum FallbackSuggestion {
246     NoSuggestion,
247     Field,
248     Method,
249     TraitItem,
250     StaticMethod(String),
251     StaticTraitMethod(String),
252 }
253
254 enum TypeParameters<'a> {
255     NoTypeParameters,
256     HasTypeParameters(
257         // Type parameters.
258         &'a Generics,
259
260         // Identifies the things that these parameters
261         // were declared on (type, fn, etc)
262         ParamSpace,
263
264         // ID of the enclosing item.
265         NodeId,
266
267         // The kind of the rib used for type parameters.
268         RibKind)
269 }
270
271 // The rib kind controls the translation of argument or local definitions
272 // (`def_arg` or `def_local`) to upvars (`def_upvar`).
273
274 enum RibKind {
275     // No translation needs to be applied.
276     NormalRibKind,
277
278     // We passed through a function scope at the given node ID. Translate
279     // upvars as appropriate.
280     FunctionRibKind(NodeId /* func id */, NodeId /* body id */),
281
282     // We passed through an impl or trait and are now in one of its
283     // methods. Allow references to ty params that impl or trait
284     // binds. Disallow any other upvars (including other ty params that are
285     // upvars).
286               // parent;   method itself
287     MethodRibKind(NodeId, MethodSort),
288
289     // We passed through an item scope. Disallow upvars.
290     ItemRibKind,
291
292     // We're in a constant item. Can't refer to dynamic stuff.
293     ConstantItemRibKind
294 }
295
296 // Methods can be required or provided. RequiredMethod methods only occur in traits.
297 enum MethodSort {
298     RequiredMethod,
299     ProvidedMethod(NodeId)
300 }
301
302 enum UseLexicalScopeFlag {
303     DontUseLexicalScope,
304     UseLexicalScope
305 }
306
307 enum ModulePrefixResult {
308     NoPrefixFound,
309     PrefixFound(Rc<Module>, uint)
310 }
311
312 #[deriving(Clone, Eq, PartialEq)]
313 pub enum TraitItemKind {
314     NonstaticMethodTraitItemKind,
315     StaticMethodTraitItemKind,
316 }
317
318 impl TraitItemKind {
319     pub fn from_explicit_self_category(explicit_self_category:
320                                        ExplicitSelfCategory)
321                                        -> TraitItemKind {
322         if explicit_self_category == StaticExplicitSelfCategory {
323             StaticMethodTraitItemKind
324         } else {
325             NonstaticMethodTraitItemKind
326         }
327     }
328 }
329
330 #[deriving(PartialEq)]
331 enum NameSearchType {
332     /// We're doing a name search in order to resolve a `use` directive.
333     ImportSearch,
334
335     /// We're doing a name search in order to resolve a path type, a path
336     /// expression, or a path pattern.
337     PathSearch,
338 }
339
340 enum BareIdentifierPatternResolution {
341     FoundStructOrEnumVariant(Def, LastPrivate),
342     FoundConst(Def, LastPrivate),
343     BareIdentifierPatternUnresolved
344 }
345
346 // Specifies how duplicates should be handled when adding a child item if
347 // another item exists with the same name in some namespace.
348 #[deriving(PartialEq)]
349 enum DuplicateCheckingMode {
350     ForbidDuplicateModules,
351     ForbidDuplicateTypesAndModules,
352     ForbidDuplicateValues,
353     ForbidDuplicateTypesAndValues,
354     OverwriteDuplicates
355 }
356
357 /// One local scope.
358 struct Rib {
359     bindings: RefCell<HashMap<Name, DefLike>>,
360     kind: RibKind,
361 }
362
363 impl Rib {
364     fn new(kind: RibKind) -> Rib {
365         Rib {
366             bindings: RefCell::new(HashMap::new()),
367             kind: kind
368         }
369     }
370 }
371
372 /// One import directive.
373 struct ImportDirective {
374     module_path: Vec<Ident>,
375     subclass: ImportDirectiveSubclass,
376     span: Span,
377     id: NodeId,
378     is_public: bool, // see note in ImportResolution about how to use this
379     shadowable: bool,
380 }
381
382 impl ImportDirective {
383     fn new(module_path: Vec<Ident> ,
384            subclass: ImportDirectiveSubclass,
385            span: Span,
386            id: NodeId,
387            is_public: bool,
388            shadowable: bool)
389            -> ImportDirective {
390         ImportDirective {
391             module_path: module_path,
392             subclass: subclass,
393             span: span,
394             id: id,
395             is_public: is_public,
396             shadowable: shadowable,
397         }
398     }
399 }
400
401 /// The item that an import resolves to.
402 #[deriving(Clone)]
403 struct Target {
404     target_module: Rc<Module>,
405     bindings: Rc<NameBindings>,
406     shadowable: bool,
407 }
408
409 impl Target {
410     fn new(target_module: Rc<Module>,
411            bindings: Rc<NameBindings>,
412            shadowable: bool)
413            -> Target {
414         Target {
415             target_module: target_module,
416             bindings: bindings,
417             shadowable: shadowable,
418         }
419     }
420 }
421
422 /// An ImportResolution represents a particular `use` directive.
423 struct ImportResolution {
424     /// Whether this resolution came from a `use` or a `pub use`. Note that this
425     /// should *not* be used whenever resolution is being performed, this is
426     /// only looked at for glob imports statements currently. Privacy testing
427     /// occurs during a later phase of compilation.
428     is_public: bool,
429
430     // The number of outstanding references to this name. When this reaches
431     // zero, outside modules can count on the targets being correct. Before
432     // then, all bets are off; future imports could override this name.
433     outstanding_references: uint,
434
435     /// The value that this `use` directive names, if there is one.
436     value_target: Option<Target>,
437     /// The source node of the `use` directive leading to the value target
438     /// being non-none
439     value_id: NodeId,
440
441     /// The type that this `use` directive names, if there is one.
442     type_target: Option<Target>,
443     /// The source node of the `use` directive leading to the type target
444     /// being non-none
445     type_id: NodeId,
446 }
447
448 impl ImportResolution {
449     fn new(id: NodeId, is_public: bool) -> ImportResolution {
450         ImportResolution {
451             type_id: id,
452             value_id: id,
453             outstanding_references: 0,
454             value_target: None,
455             type_target: None,
456             is_public: is_public,
457         }
458     }
459
460     fn target_for_namespace(&self, namespace: Namespace)
461                                 -> Option<Target> {
462         match namespace {
463             TypeNS  => self.type_target.clone(),
464             ValueNS => self.value_target.clone(),
465         }
466     }
467
468     fn id(&self, namespace: Namespace) -> NodeId {
469         match namespace {
470             TypeNS  => self.type_id,
471             ValueNS => self.value_id,
472         }
473     }
474 }
475
476 /// The link from a module up to its nearest parent node.
477 #[deriving(Clone)]
478 enum ParentLink {
479     NoParentLink,
480     ModuleParentLink(Weak<Module>, Ident),
481     BlockParentLink(Weak<Module>, NodeId)
482 }
483
484 /// The type of module this is.
485 #[deriving(PartialEq)]
486 enum ModuleKind {
487     NormalModuleKind,
488     ExternModuleKind,
489     TraitModuleKind,
490     ImplModuleKind,
491     AnonymousModuleKind,
492 }
493
494 /// One node in the tree of modules.
495 struct Module {
496     parent_link: ParentLink,
497     def_id: Cell<Option<DefId>>,
498     kind: Cell<ModuleKind>,
499     is_public: bool,
500
501     children: RefCell<HashMap<Name, Rc<NameBindings>>>,
502     imports: RefCell<Vec<ImportDirective>>,
503
504     // The external module children of this node that were declared with
505     // `extern crate`.
506     external_module_children: RefCell<HashMap<Name, Rc<Module>>>,
507
508     // The anonymous children of this node. Anonymous children are pseudo-
509     // modules that are implicitly created around items contained within
510     // blocks.
511     //
512     // For example, if we have this:
513     //
514     //  fn f() {
515     //      fn g() {
516     //          ...
517     //      }
518     //  }
519     //
520     // There will be an anonymous module created around `g` with the ID of the
521     // entry block for `f`.
522     anonymous_children: RefCell<NodeMap<Rc<Module>>>,
523
524     // The status of resolving each import in this module.
525     import_resolutions: RefCell<HashMap<Name, ImportResolution>>,
526
527     // The number of unresolved globs that this module exports.
528     glob_count: Cell<uint>,
529
530     // The index of the import we're resolving.
531     resolved_import_count: Cell<uint>,
532
533     // Whether this module is populated. If not populated, any attempt to
534     // access the children must be preceded with a
535     // `populate_module_if_necessary` call.
536     populated: Cell<bool>,
537 }
538
539 impl Module {
540     fn new(parent_link: ParentLink,
541            def_id: Option<DefId>,
542            kind: ModuleKind,
543            external: bool,
544            is_public: bool)
545            -> Module {
546         Module {
547             parent_link: parent_link,
548             def_id: Cell::new(def_id),
549             kind: Cell::new(kind),
550             is_public: is_public,
551             children: RefCell::new(HashMap::new()),
552             imports: RefCell::new(Vec::new()),
553             external_module_children: RefCell::new(HashMap::new()),
554             anonymous_children: RefCell::new(NodeMap::new()),
555             import_resolutions: RefCell::new(HashMap::new()),
556             glob_count: Cell::new(0),
557             resolved_import_count: Cell::new(0),
558             populated: Cell::new(!external),
559         }
560     }
561
562     fn all_imports_resolved(&self) -> bool {
563         self.imports.borrow().len() == self.resolved_import_count.get()
564     }
565 }
566
567 // Records a possibly-private type definition.
568 #[deriving(Clone)]
569 struct TypeNsDef {
570     is_public: bool, // see note in ImportResolution about how to use this
571     module_def: Option<Rc<Module>>,
572     type_def: Option<Def>,
573     type_span: Option<Span>
574 }
575
576 // Records a possibly-private value definition.
577 #[deriving(Clone)]
578 struct ValueNsDef {
579     is_public: bool, // see note in ImportResolution about how to use this
580     def: Def,
581     value_span: Option<Span>,
582 }
583
584 // Records the definitions (at most one for each namespace) that a name is
585 // bound to.
586 struct NameBindings {
587     type_def: RefCell<Option<TypeNsDef>>,   //< Meaning in type namespace.
588     value_def: RefCell<Option<ValueNsDef>>, //< Meaning in value namespace.
589 }
590
591 /// Ways in which a trait can be referenced
592 enum TraitReferenceType {
593     TraitImplementation,             // impl SomeTrait for T { ... }
594     TraitDerivation,                 // trait T : SomeTrait { ... }
595     TraitBoundingTypeParameter,      // fn f<T:SomeTrait>() { ... }
596 }
597
598 impl NameBindings {
599     fn new() -> NameBindings {
600         NameBindings {
601             type_def: RefCell::new(None),
602             value_def: RefCell::new(None),
603         }
604     }
605
606     /// Creates a new module in this set of name bindings.
607     fn define_module(&self,
608                      parent_link: ParentLink,
609                      def_id: Option<DefId>,
610                      kind: ModuleKind,
611                      external: bool,
612                      is_public: bool,
613                      sp: Span) {
614         // Merges the module with the existing type def or creates a new one.
615         let module_ = Rc::new(Module::new(parent_link, def_id, kind, external,
616                                           is_public));
617         let type_def = self.type_def.borrow().clone();
618         match type_def {
619             None => {
620                 *self.type_def.borrow_mut() = Some(TypeNsDef {
621                     is_public: is_public,
622                     module_def: Some(module_),
623                     type_def: None,
624                     type_span: Some(sp)
625                 });
626             }
627             Some(type_def) => {
628                 *self.type_def.borrow_mut() = Some(TypeNsDef {
629                     is_public: is_public,
630                     module_def: Some(module_),
631                     type_span: Some(sp),
632                     type_def: type_def.type_def
633                 });
634             }
635         }
636     }
637
638     /// Sets the kind of the module, creating a new one if necessary.
639     fn set_module_kind(&self,
640                        parent_link: ParentLink,
641                        def_id: Option<DefId>,
642                        kind: ModuleKind,
643                        external: bool,
644                        is_public: bool,
645                        _sp: Span) {
646         let type_def = self.type_def.borrow().clone();
647         match type_def {
648             None => {
649                 let module = Module::new(parent_link, def_id, kind,
650                                          external, is_public);
651                 *self.type_def.borrow_mut() = Some(TypeNsDef {
652                     is_public: is_public,
653                     module_def: Some(Rc::new(module)),
654                     type_def: None,
655                     type_span: None,
656                 });
657             }
658             Some(type_def) => {
659                 match type_def.module_def {
660                     None => {
661                         let module = Module::new(parent_link,
662                                                  def_id,
663                                                  kind,
664                                                  external,
665                                                  is_public);
666                         *self.type_def.borrow_mut() = Some(TypeNsDef {
667                             is_public: is_public,
668                             module_def: Some(Rc::new(module)),
669                             type_def: type_def.type_def,
670                             type_span: None,
671                         });
672                     }
673                     Some(module_def) => module_def.kind.set(kind),
674                 }
675             }
676         }
677     }
678
679     /// Records a type definition.
680     fn define_type(&self, def: Def, sp: Span, is_public: bool) {
681         // Merges the type with the existing type def or creates a new one.
682         let type_def = self.type_def.borrow().clone();
683         match type_def {
684             None => {
685                 *self.type_def.borrow_mut() = Some(TypeNsDef {
686                     module_def: None,
687                     type_def: Some(def),
688                     type_span: Some(sp),
689                     is_public: is_public,
690                 });
691             }
692             Some(type_def) => {
693                 *self.type_def.borrow_mut() = Some(TypeNsDef {
694                     type_def: Some(def),
695                     type_span: Some(sp),
696                     module_def: type_def.module_def,
697                     is_public: is_public,
698                 });
699             }
700         }
701     }
702
703     /// Records a value definition.
704     fn define_value(&self, def: Def, sp: Span, is_public: bool) {
705         *self.value_def.borrow_mut() = Some(ValueNsDef {
706             def: def,
707             value_span: Some(sp),
708             is_public: is_public,
709         });
710     }
711
712     /// Returns the module node if applicable.
713     fn get_module_if_available(&self) -> Option<Rc<Module>> {
714         match *self.type_def.borrow() {
715             Some(ref type_def) => type_def.module_def.clone(),
716             None => None
717         }
718     }
719
720     /**
721      * Returns the module node. Fails if this node does not have a module
722      * definition.
723      */
724     fn get_module(&self) -> Rc<Module> {
725         match self.get_module_if_available() {
726             None => {
727                 fail!("get_module called on a node with no module \
728                        definition!")
729             }
730             Some(module_def) => module_def
731         }
732     }
733
734     fn defined_in_namespace(&self, namespace: Namespace) -> bool {
735         match namespace {
736             TypeNS   => return self.type_def.borrow().is_some(),
737             ValueNS  => return self.value_def.borrow().is_some()
738         }
739     }
740
741     fn defined_in_public_namespace(&self, namespace: Namespace) -> bool {
742         match namespace {
743             TypeNS => match *self.type_def.borrow() {
744                 Some(ref def) => def.is_public, None => false
745             },
746             ValueNS => match *self.value_def.borrow() {
747                 Some(ref def) => def.is_public, None => false
748             }
749         }
750     }
751
752     fn def_for_namespace(&self, namespace: Namespace) -> Option<Def> {
753         match namespace {
754             TypeNS => {
755                 match *self.type_def.borrow() {
756                     None => None,
757                     Some(ref type_def) => {
758                         match type_def.type_def {
759                             Some(type_def) => Some(type_def),
760                             None => {
761                                 match type_def.module_def {
762                                     Some(ref module) => {
763                                         match module.def_id.get() {
764                                             Some(did) => Some(DefMod(did)),
765                                             None => None,
766                                         }
767                                     }
768                                     None => None,
769                                 }
770                             }
771                         }
772                     }
773                 }
774             }
775             ValueNS => {
776                 match *self.value_def.borrow() {
777                     None => None,
778                     Some(value_def) => Some(value_def.def)
779                 }
780             }
781         }
782     }
783
784     fn span_for_namespace(&self, namespace: Namespace) -> Option<Span> {
785         if self.defined_in_namespace(namespace) {
786             match namespace {
787                 TypeNS  => {
788                     match *self.type_def.borrow() {
789                         None => None,
790                         Some(ref type_def) => type_def.type_span
791                     }
792                 }
793                 ValueNS => {
794                     match *self.value_def.borrow() {
795                         None => None,
796                         Some(ref value_def) => value_def.value_span
797                     }
798                 }
799             }
800         } else {
801             None
802         }
803     }
804 }
805
806 /// Interns the names of the primitive types.
807 struct PrimitiveTypeTable {
808     primitive_types: HashMap<Name, PrimTy>,
809 }
810
811 impl PrimitiveTypeTable {
812     fn new() -> PrimitiveTypeTable {
813         let mut table = PrimitiveTypeTable {
814             primitive_types: HashMap::new()
815         };
816
817         table.intern("bool",    TyBool);
818         table.intern("char",    TyChar);
819         table.intern("f32",     TyFloat(TyF32));
820         table.intern("f64",     TyFloat(TyF64));
821         table.intern("int",     TyInt(TyI));
822         table.intern("i8",      TyInt(TyI8));
823         table.intern("i16",     TyInt(TyI16));
824         table.intern("i32",     TyInt(TyI32));
825         table.intern("i64",     TyInt(TyI64));
826         table.intern("str",     TyStr);
827         table.intern("uint",    TyUint(TyU));
828         table.intern("u8",      TyUint(TyU8));
829         table.intern("u16",     TyUint(TyU16));
830         table.intern("u32",     TyUint(TyU32));
831         table.intern("u64",     TyUint(TyU64));
832
833         table
834     }
835
836     fn intern(&mut self, string: &str, primitive_type: PrimTy) {
837         self.primitive_types.insert(token::intern(string), primitive_type);
838     }
839 }
840
841
842 fn namespace_error_to_string(ns: NamespaceError) -> &'static str {
843     match ns {
844         NoError                 => "",
845         ModuleError | TypeError => "type or module",
846         ValueError              => "value",
847     }
848 }
849
850 /// The main resolver class.
851 struct Resolver<'a> {
852     session: &'a Session,
853
854     graph_root: NameBindings,
855
856     trait_item_map: RefCell<FnvHashMap<(Name, DefId), TraitItemKind>>,
857
858     structs: FnvHashMap<DefId, Vec<Name>>,
859
860     // The number of imports that are currently unresolved.
861     unresolved_imports: uint,
862
863     // The module that represents the current item scope.
864     current_module: Rc<Module>,
865
866     // The current set of local scopes, for values.
867     // FIXME #4948: Reuse ribs to avoid allocation.
868     value_ribs: RefCell<Vec<Rib>>,
869
870     // The current set of local scopes, for types.
871     type_ribs: RefCell<Vec<Rib>>,
872
873     // The current set of local scopes, for labels.
874     label_ribs: RefCell<Vec<Rib>>,
875
876     // The trait that the current context can refer to.
877     current_trait_ref: Option<(DefId, TraitRef)>,
878
879     // The current self type if inside an impl (used for better errors).
880     current_self_type: Option<Ty>,
881
882     // The ident for the keyword "self".
883     self_name: Name,
884     // The ident for the non-keyword "Self".
885     type_self_name: Name,
886
887     // The idents for the primitive types.
888     primitive_type_table: PrimitiveTypeTable,
889
890     def_map: DefMap,
891     export_map2: ExportMap2,
892     trait_map: TraitMap,
893     external_exports: ExternalExports,
894     last_private: LastPrivateMap,
895
896     // Whether or not to print error messages. Can be set to true
897     // when getting additional info for error message suggestions,
898     // so as to avoid printing duplicate errors
899     emit_errors: bool,
900
901     used_imports: HashSet<(NodeId, Namespace)>,
902 }
903
904 struct BuildReducedGraphVisitor<'a, 'b:'a> {
905     resolver: &'a mut Resolver<'b>,
906 }
907
908 impl<'a, 'b> Visitor<ReducedGraphParent> for BuildReducedGraphVisitor<'a, 'b> {
909
910     fn visit_item(&mut self, item: &Item, context: ReducedGraphParent) {
911         let p = self.resolver.build_reduced_graph_for_item(item, context);
912         visit::walk_item(self, item, p);
913     }
914
915     fn visit_foreign_item(&mut self, foreign_item: &ForeignItem,
916                           context: ReducedGraphParent) {
917         self.resolver.build_reduced_graph_for_foreign_item(foreign_item,
918                                                            context.clone(),
919                                                            |r| {
920             let mut v = BuildReducedGraphVisitor{ resolver: r };
921             visit::walk_foreign_item(&mut v, foreign_item, context.clone());
922         })
923     }
924
925     fn visit_view_item(&mut self, view_item: &ViewItem, context: ReducedGraphParent) {
926         self.resolver.build_reduced_graph_for_view_item(view_item, context);
927     }
928
929     fn visit_block(&mut self, block: &Block, context: ReducedGraphParent) {
930         let np = self.resolver.build_reduced_graph_for_block(block, context);
931         visit::walk_block(self, block, np);
932     }
933
934 }
935
936 struct UnusedImportCheckVisitor<'a, 'b:'a> {
937     resolver: &'a mut Resolver<'b>
938 }
939
940 impl<'a, 'b> Visitor<()> for UnusedImportCheckVisitor<'a, 'b> {
941     fn visit_view_item(&mut self, vi: &ViewItem, _: ()) {
942         self.resolver.check_for_item_unused_imports(vi);
943         visit::walk_view_item(self, vi, ());
944     }
945 }
946
947 impl<'a> Resolver<'a> {
948     fn new(session: &'a Session, crate_span: Span) -> Resolver<'a> {
949         let graph_root = NameBindings::new();
950
951         graph_root.define_module(NoParentLink,
952                                  Some(DefId { krate: 0, node: 0 }),
953                                  NormalModuleKind,
954                                  false,
955                                  true,
956                                  crate_span);
957
958         let current_module = graph_root.get_module();
959
960         Resolver {
961             session: session,
962
963             // The outermost module has def ID 0; this is not reflected in the
964             // AST.
965
966             graph_root: graph_root,
967
968             trait_item_map: RefCell::new(FnvHashMap::new()),
969             structs: FnvHashMap::new(),
970
971             unresolved_imports: 0,
972
973             current_module: current_module,
974             value_ribs: RefCell::new(Vec::new()),
975             type_ribs: RefCell::new(Vec::new()),
976             label_ribs: RefCell::new(Vec::new()),
977
978             current_trait_ref: None,
979             current_self_type: None,
980
981             self_name: special_names::self_,
982             type_self_name: special_names::type_self,
983
984             primitive_type_table: PrimitiveTypeTable::new(),
985
986             def_map: RefCell::new(NodeMap::new()),
987             export_map2: RefCell::new(NodeMap::new()),
988             trait_map: NodeMap::new(),
989             used_imports: HashSet::new(),
990             external_exports: DefIdSet::new(),
991             last_private: NodeMap::new(),
992
993             emit_errors: true,
994         }
995     }
996     /// The main name resolution procedure.
997     fn resolve(&mut self, krate: &ast::Crate) {
998         self.build_reduced_graph(krate);
999         self.session.abort_if_errors();
1000
1001         self.resolve_imports();
1002         self.session.abort_if_errors();
1003
1004         self.record_exports();
1005         self.session.abort_if_errors();
1006
1007         self.resolve_crate(krate);
1008         self.session.abort_if_errors();
1009
1010         self.check_for_unused_imports(krate);
1011     }
1012
1013     //
1014     // Reduced graph building
1015     //
1016     // Here we build the "reduced graph": the graph of the module tree without
1017     // any imports resolved.
1018     //
1019
1020     /// Constructs the reduced graph for the entire crate.
1021     fn build_reduced_graph(&mut self, krate: &ast::Crate) {
1022         let initial_parent =
1023             ModuleReducedGraphParent(self.graph_root.get_module());
1024
1025         let mut visitor = BuildReducedGraphVisitor { resolver: self, };
1026         visit::walk_crate(&mut visitor, krate, initial_parent);
1027     }
1028
1029     /**
1030      * Adds a new child item to the module definition of the parent node and
1031      * returns its corresponding name bindings as well as the current parent.
1032      * Or, if we're inside a block, creates (or reuses) an anonymous module
1033      * corresponding to the innermost block ID and returns the name bindings
1034      * as well as the newly-created parent.
1035      *
1036      * If this node does not have a module definition and we are not inside
1037      * a block, fails.
1038      */
1039     fn add_child(&self,
1040                  name: Ident,
1041                  reduced_graph_parent: ReducedGraphParent,
1042                  duplicate_checking_mode: DuplicateCheckingMode,
1043                  // For printing errors
1044                  sp: Span)
1045                  -> Rc<NameBindings> {
1046         // If this is the immediate descendant of a module, then we add the
1047         // child name directly. Otherwise, we create or reuse an anonymous
1048         // module and add the child to that.
1049
1050         let module_ = reduced_graph_parent.module();
1051
1052         self.check_for_conflicts_between_external_crates_and_items(&*module_,
1053                                                                    name.name,
1054                                                                    sp);
1055
1056         // Add or reuse the child.
1057         let child = module_.children.borrow().find_copy(&name.name);
1058         match child {
1059             None => {
1060                 let child = Rc::new(NameBindings::new());
1061                 module_.children.borrow_mut().insert(name.name, child.clone());
1062                 child
1063             }
1064             Some(child) => {
1065                 // Enforce the duplicate checking mode:
1066                 //
1067                 // * If we're requesting duplicate module checking, check that
1068                 //   there isn't a module in the module with the same name.
1069                 //
1070                 // * If we're requesting duplicate type checking, check that
1071                 //   there isn't a type in the module with the same name.
1072                 //
1073                 // * If we're requesting duplicate value checking, check that
1074                 //   there isn't a value in the module with the same name.
1075                 //
1076                 // * If we're requesting duplicate type checking and duplicate
1077                 //   value checking, check that there isn't a duplicate type
1078                 //   and a duplicate value with the same name.
1079                 //
1080                 // * If no duplicate checking was requested at all, do
1081                 //   nothing.
1082
1083                 let mut duplicate_type = NoError;
1084                 let ns = match duplicate_checking_mode {
1085                     ForbidDuplicateModules => {
1086                         if child.get_module_if_available().is_some() {
1087                             duplicate_type = ModuleError;
1088                         }
1089                         Some(TypeNS)
1090                     }
1091                     ForbidDuplicateTypesAndModules => {
1092                         match child.def_for_namespace(TypeNS) {
1093                             None => {}
1094                             Some(_) if child.get_module_if_available()
1095                                             .map(|m| m.kind.get()) ==
1096                                        Some(ImplModuleKind) => {}
1097                             Some(_) => duplicate_type = TypeError
1098                         }
1099                         Some(TypeNS)
1100                     }
1101                     ForbidDuplicateValues => {
1102                         if child.defined_in_namespace(ValueNS) {
1103                             duplicate_type = ValueError;
1104                         }
1105                         Some(ValueNS)
1106                     }
1107                     ForbidDuplicateTypesAndValues => {
1108                         let mut n = None;
1109                         match child.def_for_namespace(TypeNS) {
1110                             Some(DefMod(_)) | None => {}
1111                             Some(_) => {
1112                                 n = Some(TypeNS);
1113                                 duplicate_type = TypeError;
1114                             }
1115                         };
1116                         if child.defined_in_namespace(ValueNS) {
1117                             duplicate_type = ValueError;
1118                             n = Some(ValueNS);
1119                         }
1120                         n
1121                     }
1122                     OverwriteDuplicates => None
1123                 };
1124                 if duplicate_type != NoError {
1125                     // Return an error here by looking up the namespace that
1126                     // had the duplicate.
1127                     let ns = ns.unwrap();
1128                     self.resolve_error(sp,
1129                         format!("duplicate definition of {} `{}`",
1130                              namespace_error_to_string(duplicate_type),
1131                              token::get_ident(name)).as_slice());
1132                     {
1133                         let r = child.span_for_namespace(ns);
1134                         for sp in r.iter() {
1135                             self.session.span_note(*sp,
1136                                  format!("first definition of {} `{}` here",
1137                                       namespace_error_to_string(duplicate_type),
1138                                       token::get_ident(name)).as_slice());
1139                         }
1140                     }
1141                 }
1142                 child
1143             }
1144         }
1145     }
1146
1147     fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
1148         // If the block has view items, we need an anonymous module.
1149         if block.view_items.len() > 0 {
1150             return true;
1151         }
1152
1153         // Check each statement.
1154         for statement in block.stmts.iter() {
1155             match statement.node {
1156                 StmtDecl(declaration, _) => {
1157                     match declaration.node {
1158                         DeclItem(_) => {
1159                             return true;
1160                         }
1161                         _ => {
1162                             // Keep searching.
1163                         }
1164                     }
1165                 }
1166                 _ => {
1167                     // Keep searching.
1168                 }
1169             }
1170         }
1171
1172         // If we found neither view items nor items, we don't need to create
1173         // an anonymous module.
1174
1175         return false;
1176     }
1177
1178     fn get_parent_link(&mut self, parent: ReducedGraphParent, name: Ident)
1179                            -> ParentLink {
1180         match parent {
1181             ModuleReducedGraphParent(module_) => {
1182                 return ModuleParentLink(module_.downgrade(), name);
1183             }
1184         }
1185     }
1186
1187     /// Constructs the reduced graph for one item.
1188     fn build_reduced_graph_for_item(&mut self,
1189                                     item: &Item,
1190                                     parent: ReducedGraphParent)
1191                                     -> ReducedGraphParent
1192     {
1193         let ident = item.ident;
1194         let sp = item.span;
1195         let is_public = item.vis == ast::Public;
1196
1197         match item.node {
1198             ItemMod(..) => {
1199                 let name_bindings =
1200                     self.add_child(ident, parent.clone(), ForbidDuplicateModules, sp);
1201
1202                 let parent_link = self.get_parent_link(parent, ident);
1203                 let def_id = DefId { krate: 0, node: item.id };
1204                 name_bindings.define_module(parent_link,
1205                                             Some(def_id),
1206                                             NormalModuleKind,
1207                                             false,
1208                                             item.vis == ast::Public,
1209                                             sp);
1210
1211                 ModuleReducedGraphParent(name_bindings.get_module())
1212             }
1213
1214             ItemForeignMod(..) => parent,
1215
1216             // These items live in the value namespace.
1217             ItemStatic(_, m, _) => {
1218                 let name_bindings =
1219                     self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1220                 let mutbl = m == ast::MutMutable;
1221
1222                 name_bindings.define_value
1223                     (DefStatic(local_def(item.id), mutbl), sp, is_public);
1224                 parent
1225             }
1226             ItemFn(_, fn_style, _, _, _) => {
1227                 let name_bindings =
1228                     self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1229
1230                 let def = DefFn(local_def(item.id), fn_style);
1231                 name_bindings.define_value(def, sp, is_public);
1232                 parent
1233             }
1234
1235             // These items live in the type namespace.
1236             ItemTy(..) => {
1237                 let name_bindings =
1238                     self.add_child(ident,
1239                                    parent.clone(),
1240                                    ForbidDuplicateTypesAndModules,
1241                                    sp);
1242
1243                 name_bindings.define_type
1244                     (DefTy(local_def(item.id)), sp, is_public);
1245                 parent
1246             }
1247
1248             ItemEnum(ref enum_definition, _) => {
1249                 let name_bindings =
1250                     self.add_child(ident,
1251                                    parent.clone(),
1252                                    ForbidDuplicateTypesAndModules,
1253                                    sp);
1254
1255                 name_bindings.define_type
1256                     (DefTy(local_def(item.id)), sp, is_public);
1257
1258                 for variant in (*enum_definition).variants.iter() {
1259                     self.build_reduced_graph_for_variant(
1260                         &**variant,
1261                         local_def(item.id),
1262                         parent.clone(),
1263                         is_public);
1264                 }
1265                 parent
1266             }
1267
1268             // These items live in both the type and value namespaces.
1269             ItemStruct(struct_def, _) => {
1270                 // Adding to both Type and Value namespaces or just Type?
1271                 let (forbid, ctor_id) = match struct_def.ctor_id {
1272                     Some(ctor_id)   => (ForbidDuplicateTypesAndValues, Some(ctor_id)),
1273                     None            => (ForbidDuplicateTypesAndModules, None)
1274                 };
1275
1276                 let name_bindings = self.add_child(ident, parent.clone(), forbid, sp);
1277
1278                 // Define a name in the type namespace.
1279                 name_bindings.define_type(DefTy(local_def(item.id)), sp, is_public);
1280
1281                 // If this is a newtype or unit-like struct, define a name
1282                 // in the value namespace as well
1283                 ctor_id.while_some(|cid| {
1284                     name_bindings.define_value(DefStruct(local_def(cid)), sp,
1285                                                is_public);
1286                     None
1287                 });
1288
1289                 // Record the def ID and fields of this struct.
1290                 let named_fields = struct_def.fields.iter().filter_map(|f| {
1291                     match f.node.kind {
1292                         NamedField(ident, _) => Some(ident.name),
1293                         UnnamedField(_) => None
1294                     }
1295                 }).collect();
1296                 self.structs.insert(local_def(item.id), named_fields);
1297
1298                 parent
1299             }
1300
1301             ItemImpl(_, None, ty, ref impl_items) => {
1302                 // If this implements an anonymous trait, then add all the
1303                 // methods within to a new module, if the type was defined
1304                 // within this module.
1305                 //
1306                 // FIXME (#3785): This is quite unsatisfactory. Perhaps we
1307                 // should modify anonymous traits to only be implementable in
1308                 // the same module that declared the type.
1309
1310                 // Create the module and add all methods.
1311                 match ty.node {
1312                     TyPath(ref path, _, _) if path.segments.len() == 1 => {
1313                         let name = path.segments.last().unwrap().identifier;
1314
1315                         let parent_opt = parent.module().children.borrow()
1316                                                .find_copy(&name.name);
1317                         let new_parent = match parent_opt {
1318                             // It already exists
1319                             Some(ref child) if child.get_module_if_available()
1320                                                 .is_some() &&
1321                                            child.get_module().kind.get() ==
1322                                                 ImplModuleKind => {
1323                                 ModuleReducedGraphParent(child.get_module())
1324                             }
1325                             // Create the module
1326                             _ => {
1327                                 let name_bindings =
1328                                     self.add_child(name,
1329                                                    parent.clone(),
1330                                                    ForbidDuplicateModules,
1331                                                    sp);
1332
1333                                 let parent_link =
1334                                     self.get_parent_link(parent.clone(), ident);
1335                                 let def_id = local_def(item.id);
1336                                 let ns = TypeNS;
1337                                 let is_public =
1338                                     !name_bindings.defined_in_namespace(ns) ||
1339                                      name_bindings.defined_in_public_namespace(ns);
1340
1341                                 name_bindings.define_module(parent_link,
1342                                                             Some(def_id),
1343                                                             ImplModuleKind,
1344                                                             false,
1345                                                             is_public,
1346                                                             sp);
1347
1348                                 ModuleReducedGraphParent(
1349                                     name_bindings.get_module())
1350                             }
1351                         };
1352
1353                         // For each implementation item...
1354                         for impl_item in impl_items.iter() {
1355                             match *impl_item {
1356                                 MethodImplItem(method) => {
1357                                     // Add the method to the module.
1358                                     let ident = method.pe_ident();
1359                                     let method_name_bindings =
1360                                         self.add_child(ident,
1361                                                        new_parent.clone(),
1362                                                        ForbidDuplicateValues,
1363                                                        method.span);
1364                                     let def = match method.pe_explicit_self()
1365                                                           .node {
1366                                         SelfStatic => {
1367                                             // Static methods become
1368                                             // `def_static_method`s.
1369                                             DefStaticMethod(
1370                                                 local_def(method.id),
1371                                                 FromImpl(local_def(item.id)),
1372                                                          method.pe_fn_style())
1373                                         }
1374                                         _ => {
1375                                             // Non-static methods become
1376                                             // `def_method`s.
1377                                             DefMethod(local_def(method.id),
1378                                                       None)
1379                                         }
1380                                     };
1381
1382                                     let is_public =
1383                                         method.pe_vis() == ast::Public;
1384                                     method_name_bindings.define_value(
1385                                         def,
1386                                         method.span,
1387                                         is_public);
1388                                 }
1389                             }
1390                         }
1391                     }
1392                     _ => {}
1393                 }
1394
1395                 parent
1396             }
1397
1398             ItemImpl(_, Some(_), _, _) => parent,
1399
1400             ItemTrait(_, _, _, ref methods) => {
1401                 let name_bindings =
1402                     self.add_child(ident,
1403                                    parent.clone(),
1404                                    ForbidDuplicateTypesAndModules,
1405                                    sp);
1406
1407                 // Add all the methods within to a new module.
1408                 let parent_link = self.get_parent_link(parent.clone(), ident);
1409                 name_bindings.define_module(parent_link,
1410                                             Some(local_def(item.id)),
1411                                             TraitModuleKind,
1412                                             false,
1413                                             item.vis == ast::Public,
1414                                             sp);
1415                 let module_parent = ModuleReducedGraphParent(name_bindings.
1416                                                              get_module());
1417
1418                 let def_id = local_def(item.id);
1419
1420                 // Add the names of all the methods to the trait info.
1421                 for method in methods.iter() {
1422                     let ty_m = trait_item_to_ty_method(method);
1423
1424                     let ident = ty_m.ident;
1425
1426                     // Add it as a name in the trait module.
1427                     let (def, static_flag) = match ty_m.explicit_self.node {
1428                         SelfStatic => {
1429                             // Static methods become `def_static_method`s.
1430                             (DefStaticMethod(local_def(ty_m.id),
1431                                               FromTrait(local_def(item.id)),
1432                                               ty_m.fn_style),
1433                              StaticMethodTraitItemKind)
1434                         }
1435                         _ => {
1436                             // Non-static methods become `def_method`s.
1437                             (DefMethod(local_def(ty_m.id),
1438                                        Some(local_def(item.id))),
1439                              NonstaticMethodTraitItemKind)
1440                         }
1441                     };
1442
1443                     let method_name_bindings =
1444                         self.add_child(ident,
1445                                        module_parent.clone(),
1446                                        ForbidDuplicateValues,
1447                                        ty_m.span);
1448                     method_name_bindings.define_value(def, ty_m.span, true);
1449
1450                     self.trait_item_map
1451                         .borrow_mut()
1452                         .insert((ident.name, def_id), static_flag);
1453                 }
1454
1455                 name_bindings.define_type(DefTrait(def_id), sp, is_public);
1456                 parent
1457             }
1458             ItemMac(..) => parent
1459         }
1460     }
1461
1462     // Constructs the reduced graph for one variant. Variants exist in the
1463     // type and/or value namespaces.
1464     fn build_reduced_graph_for_variant(&mut self,
1465                                        variant: &Variant,
1466                                        item_id: DefId,
1467                                        parent: ReducedGraphParent,
1468                                        is_public: bool) {
1469         let ident = variant.node.name;
1470
1471         match variant.node.kind {
1472             TupleVariantKind(_) => {
1473                 let child = self.add_child(ident, parent, ForbidDuplicateValues, variant.span);
1474                 child.define_value(DefVariant(item_id,
1475                                               local_def(variant.node.id), false),
1476                                    variant.span, is_public);
1477             }
1478             StructVariantKind(_) => {
1479                 let child = self.add_child(ident, parent,
1480                                            ForbidDuplicateTypesAndValues,
1481                                            variant.span);
1482                 child.define_type(DefVariant(item_id,
1483                                              local_def(variant.node.id), true),
1484                                   variant.span, is_public);
1485
1486                 // Not adding fields for variants as they are not accessed with a self receiver
1487                 self.structs.insert(local_def(variant.node.id), Vec::new());
1488             }
1489         }
1490     }
1491
1492     /// Constructs the reduced graph for one 'view item'. View items consist
1493     /// of imports and use directives.
1494     fn build_reduced_graph_for_view_item(&mut self, view_item: &ViewItem,
1495                                          parent: ReducedGraphParent) {
1496         match view_item.node {
1497             ViewItemUse(ref view_path) => {
1498                 // Extract and intern the module part of the path. For
1499                 // globs and lists, the path is found directly in the AST;
1500                 // for simple paths we have to munge the path a little.
1501                 let module_path = match view_path.node {
1502                     ViewPathSimple(_, ref full_path, _) => {
1503                         full_path.segments
1504                             .as_slice().init()
1505                             .iter().map(|ident| ident.identifier)
1506                             .collect()
1507                     }
1508
1509                     ViewPathGlob(ref module_ident_path, _) |
1510                     ViewPathList(ref module_ident_path, _, _) => {
1511                         module_ident_path.segments
1512                             .iter().map(|ident| ident.identifier).collect()
1513                     }
1514                 };
1515
1516                 // Build up the import directives.
1517                 let module_ = parent.module();
1518                 let is_public = view_item.vis == ast::Public;
1519                 let shadowable =
1520                     view_item.attrs
1521                              .iter()
1522                              .any(|attr| {
1523                                  attr.name() == token::get_ident(
1524                                     special_idents::prelude_import)
1525                              });
1526
1527                 match view_path.node {
1528                     ViewPathSimple(binding, ref full_path, id) => {
1529                         let source_ident =
1530                             full_path.segments.last().unwrap().identifier;
1531                         if token::get_ident(source_ident).get() == "mod" {
1532                             self.resolve_error(view_path.span,
1533                                 "`mod` imports are only allowed within a { } list");
1534                         }
1535
1536                         let subclass = SingleImport(binding,
1537                                                     source_ident);
1538                         self.build_import_directive(&*module_,
1539                                                     module_path,
1540                                                     subclass,
1541                                                     view_path.span,
1542                                                     id,
1543                                                     is_public,
1544                                                     shadowable);
1545                     }
1546                     ViewPathList(_, ref source_items, _) => {
1547                         // Make sure there's at most one `mod` import in the list.
1548                         let mod_spans = source_items.iter().filter_map(|item| match item.node {
1549                             PathListMod { .. } => Some(item.span),
1550                             _ => None
1551                         }).collect::<Vec<Span>>();
1552                         if mod_spans.len() > 1 {
1553                             self.resolve_error(mod_spans[0],
1554                                 "`mod` import can only appear once in the list");
1555                             for other_span in mod_spans.iter().skip(1) {
1556                                 self.session.span_note(*other_span,
1557                                     "another `mod` import appears here");
1558                             }
1559                         }
1560
1561                         for source_item in source_items.iter() {
1562                             let (module_path, name) = match source_item.node {
1563                                 PathListIdent { name, .. } =>
1564                                     (module_path.clone(), name),
1565                                 PathListMod { .. } => {
1566                                     let name = match module_path.last() {
1567                                         Some(ident) => ident.clone(),
1568                                         None => {
1569                                             self.resolve_error(source_item.span,
1570                                                 "`mod` import can only appear in an import list \
1571                                                  with a non-empty prefix");
1572                                             continue;
1573                                         }
1574                                     };
1575                                     let module_path = module_path.as_slice().init();
1576                                     (Vec::from_slice(module_path), name)
1577                                 }
1578                             };
1579                             self.build_import_directive(
1580                                 &*module_,
1581                                 module_path,
1582                                 SingleImport(name, name),
1583                                 source_item.span,
1584                                 source_item.node.id(),
1585                                 is_public,
1586                                 shadowable);
1587                         }
1588                     }
1589                     ViewPathGlob(_, id) => {
1590                         self.build_import_directive(&*module_,
1591                                                     module_path,
1592                                                     GlobImport,
1593                                                     view_path.span,
1594                                                     id,
1595                                                     is_public,
1596                                                     shadowable);
1597                     }
1598                 }
1599             }
1600
1601             ViewItemExternCrate(name, _, node_id) => {
1602                 // n.b. we don't need to look at the path option here, because cstore already did
1603                 for &crate_id in self.session.cstore
1604                                      .find_extern_mod_stmt_cnum(node_id).iter() {
1605                     let def_id = DefId { krate: crate_id, node: 0 };
1606                     self.external_exports.insert(def_id);
1607                     let parent_link =
1608                         ModuleParentLink(parent.module().downgrade(), name);
1609                     let external_module = Rc::new(Module::new(parent_link,
1610                                                               Some(def_id),
1611                                                               NormalModuleKind,
1612                                                               false,
1613                                                               true));
1614                     debug!("(build reduced graph for item) found extern `{}`",
1615                             self.module_to_string(&*external_module));
1616                     self.check_for_conflicts_between_external_crates(
1617                         &*parent.module(),
1618                         name.name,
1619                         view_item.span);
1620                     parent.module().external_module_children.borrow_mut()
1621                                    .insert(name.name, external_module.clone());
1622                     self.build_reduced_graph_for_external_crate(external_module);
1623                 }
1624             }
1625         }
1626     }
1627
1628     /// Constructs the reduced graph for one foreign item.
1629     fn build_reduced_graph_for_foreign_item(&mut self,
1630                                             foreign_item: &ForeignItem,
1631                                             parent: ReducedGraphParent,
1632                                             f: |&mut Resolver|) {
1633         let name = foreign_item.ident;
1634         let is_public = foreign_item.vis == ast::Public;
1635         let name_bindings =
1636             self.add_child(name, parent, ForbidDuplicateValues,
1637                            foreign_item.span);
1638
1639         match foreign_item.node {
1640             ForeignItemFn(_, ref generics) => {
1641                 let def = DefFn(local_def(foreign_item.id), UnsafeFn);
1642                 name_bindings.define_value(def, foreign_item.span, is_public);
1643
1644                 self.with_type_parameter_rib(
1645                     HasTypeParameters(generics,
1646                                       FnSpace,
1647                                       foreign_item.id,
1648                                       NormalRibKind),
1649                     f);
1650             }
1651             ForeignItemStatic(_, m) => {
1652                 let def = DefStatic(local_def(foreign_item.id), m);
1653                 name_bindings.define_value(def, foreign_item.span, is_public);
1654
1655                 f(self)
1656             }
1657         }
1658     }
1659
1660     fn build_reduced_graph_for_block(&mut self,
1661                                          block: &Block,
1662                                          parent: ReducedGraphParent)
1663                                             -> ReducedGraphParent
1664     {
1665         if self.block_needs_anonymous_module(block) {
1666             let block_id = block.id;
1667
1668             debug!("(building reduced graph for block) creating a new \
1669                     anonymous module for block {}",
1670                    block_id);
1671
1672             let parent_module = parent.module();
1673             let new_module = Rc::new(Module::new(
1674                 BlockParentLink(parent_module.downgrade(), block_id),
1675                 None,
1676                 AnonymousModuleKind,
1677                 false,
1678                 false));
1679             parent_module.anonymous_children.borrow_mut()
1680                          .insert(block_id, new_module.clone());
1681             ModuleReducedGraphParent(new_module)
1682         } else {
1683             parent
1684         }
1685     }
1686
1687     fn handle_external_def(&mut self,
1688                            def: Def,
1689                            vis: Visibility,
1690                            child_name_bindings: &NameBindings,
1691                            final_ident: &str,
1692                            ident: Ident,
1693                            new_parent: ReducedGraphParent) {
1694         debug!("(building reduced graph for \
1695                 external crate) building external def, priv {:?}",
1696                vis);
1697         let is_public = vis == ast::Public;
1698         let is_exported = is_public && match new_parent {
1699             ModuleReducedGraphParent(ref module) => {
1700                 match module.def_id.get() {
1701                     None => true,
1702                     Some(did) => self.external_exports.contains(&did)
1703                 }
1704             }
1705         };
1706         if is_exported {
1707             self.external_exports.insert(def.def_id());
1708         }
1709
1710         let kind = match def {
1711             DefStruct(..) | DefTy(..) => ImplModuleKind,
1712             _ => NormalModuleKind
1713         };
1714
1715         match def {
1716           DefMod(def_id) | DefForeignMod(def_id) | DefStruct(def_id) |
1717           DefTy(def_id) => {
1718             let type_def = child_name_bindings.type_def.borrow().clone();
1719             match type_def {
1720               Some(TypeNsDef { module_def: Some(module_def), .. }) => {
1721                 debug!("(building reduced graph for external crate) \
1722                         already created module");
1723                 module_def.def_id.set(Some(def_id));
1724               }
1725               Some(_) | None => {
1726                 debug!("(building reduced graph for \
1727                         external crate) building module \
1728                         {}", final_ident);
1729                 let parent_link = self.get_parent_link(new_parent.clone(), ident);
1730
1731                 child_name_bindings.define_module(parent_link,
1732                                                   Some(def_id),
1733                                                   kind,
1734                                                   true,
1735                                                   is_public,
1736                                                   DUMMY_SP);
1737               }
1738             }
1739           }
1740           _ => {}
1741         }
1742
1743         match def {
1744           DefMod(_) | DefForeignMod(_) => {}
1745           DefVariant(enum_did, variant_id, is_struct) => {
1746             debug!("(building reduced graph for external crate) building \
1747                     variant {}",
1748                    final_ident);
1749             // If this variant is public, then it was publicly reexported,
1750             // otherwise we need to inherit the visibility of the enum
1751             // definition.
1752             let is_exported = is_public ||
1753                               self.external_exports.contains(&enum_did);
1754             if is_struct {
1755                 child_name_bindings.define_type(def, DUMMY_SP, is_exported);
1756                 // Not adding fields for variants as they are not accessed with a self receiver
1757                 self.structs.insert(variant_id, Vec::new());
1758             } else {
1759                 child_name_bindings.define_value(def, DUMMY_SP, is_exported);
1760             }
1761           }
1762           DefFn(..) | DefStaticMethod(..) | DefStatic(..) => {
1763             debug!("(building reduced graph for external \
1764                     crate) building value (fn/static) {}", final_ident);
1765             child_name_bindings.define_value(def, DUMMY_SP, is_public);
1766           }
1767           DefTrait(def_id) => {
1768               debug!("(building reduced graph for external \
1769                       crate) building type {}", final_ident);
1770
1771               // If this is a trait, add all the trait item names to the trait
1772               // info.
1773
1774               let trait_item_def_ids =
1775                 csearch::get_trait_item_def_ids(&self.session.cstore, def_id);
1776               for trait_item_def_id in trait_item_def_ids.iter() {
1777                   let (trait_item_name, trait_item_kind) =
1778                       csearch::get_trait_item_name_and_kind(
1779                           &self.session.cstore,
1780                           trait_item_def_id.def_id());
1781
1782                   debug!("(building reduced graph for external crate) ... \
1783                           adding trait item '{}'",
1784                          token::get_ident(trait_item_name));
1785
1786                   self.trait_item_map
1787                       .borrow_mut()
1788                       .insert((trait_item_name.name, def_id),
1789                               trait_item_kind);
1790
1791                   if is_exported {
1792                       self.external_exports
1793                           .insert(trait_item_def_id.def_id());
1794                   }
1795               }
1796
1797               child_name_bindings.define_type(def, DUMMY_SP, is_public);
1798
1799               // Define a module if necessary.
1800               let parent_link = self.get_parent_link(new_parent, ident);
1801               child_name_bindings.set_module_kind(parent_link,
1802                                                   Some(def_id),
1803                                                   TraitModuleKind,
1804                                                   true,
1805                                                   is_public,
1806                                                   DUMMY_SP)
1807           }
1808           DefTy(_) => {
1809               debug!("(building reduced graph for external \
1810                       crate) building type {}", final_ident);
1811
1812               child_name_bindings.define_type(def, DUMMY_SP, is_public);
1813           }
1814           DefStruct(def_id) => {
1815             debug!("(building reduced graph for external \
1816                     crate) building type and value for {}",
1817                    final_ident);
1818             child_name_bindings.define_type(def, DUMMY_SP, is_public);
1819             let fields = csearch::get_struct_fields(&self.session.cstore, def_id).iter().map(|f| {
1820                 f.name
1821             }).collect::<Vec<_>>();
1822
1823             if fields.len() == 0 {
1824                 child_name_bindings.define_value(def, DUMMY_SP, is_public);
1825             }
1826
1827             // Record the def ID and fields of this struct.
1828             self.structs.insert(def_id, fields);
1829           }
1830           DefMethod(..) => {
1831               debug!("(building reduced graph for external crate) \
1832                       ignoring {:?}", def);
1833               // Ignored; handled elsewhere.
1834           }
1835           DefArg(..) | DefLocal(..) | DefPrimTy(..) |
1836           DefTyParam(..) | DefBinding(..) |
1837           DefUse(..) | DefUpvar(..) | DefRegion(..) |
1838           DefTyParamBinder(..) | DefLabel(..) | DefSelfTy(..) => {
1839             fail!("didn't expect `{:?}`", def);
1840           }
1841         }
1842     }
1843
1844     /// Builds the reduced graph for a single item in an external crate.
1845     fn build_reduced_graph_for_external_crate_def(&mut self,
1846                                                   root: Rc<Module>,
1847                                                   def_like: DefLike,
1848                                                   ident: Ident,
1849                                                   visibility: Visibility) {
1850         match def_like {
1851             DlDef(def) => {
1852                 // Add the new child item, if necessary.
1853                 match def {
1854                     DefForeignMod(def_id) => {
1855                         // Foreign modules have no names. Recur and populate
1856                         // eagerly.
1857                         csearch::each_child_of_item(&self.session.cstore,
1858                                                     def_id,
1859                                                     |def_like,
1860                                                      child_ident,
1861                                                      vis| {
1862                             self.build_reduced_graph_for_external_crate_def(
1863                                 root.clone(),
1864                                 def_like,
1865                                 child_ident,
1866                                 vis)
1867                         });
1868                     }
1869                     _ => {
1870                         let child_name_bindings =
1871                             self.add_child(ident,
1872                                            ModuleReducedGraphParent(root.clone()),
1873                                            OverwriteDuplicates,
1874                                            DUMMY_SP);
1875
1876                         self.handle_external_def(def,
1877                                                  visibility,
1878                                                  &*child_name_bindings,
1879                                                  token::get_ident(ident).get(),
1880                                                  ident,
1881                                                  ModuleReducedGraphParent(root));
1882                     }
1883                 }
1884             }
1885             DlImpl(def) => {
1886                 // We only process static methods of impls here.
1887                 match csearch::get_type_name_if_impl(&self.session.cstore, def) {
1888                     None => {}
1889                     Some(final_ident) => {
1890                         let static_methods_opt =
1891                             csearch::get_static_methods_if_impl(&self.session.cstore, def);
1892                         match static_methods_opt {
1893                             Some(ref static_methods) if
1894                                 static_methods.len() >= 1 => {
1895                                 debug!("(building reduced graph for \
1896                                         external crate) processing \
1897                                         static methods for type name {}",
1898                                         token::get_ident(final_ident));
1899
1900                                 let child_name_bindings =
1901                                     self.add_child(
1902                                         final_ident,
1903                                         ModuleReducedGraphParent(root.clone()),
1904                                         OverwriteDuplicates,
1905                                         DUMMY_SP);
1906
1907                                 // Process the static methods. First,
1908                                 // create the module.
1909                                 let type_module;
1910                                 let type_def = child_name_bindings.type_def.borrow().clone();
1911                                 match type_def {
1912                                     Some(TypeNsDef {
1913                                         module_def: Some(module_def),
1914                                         ..
1915                                     }) => {
1916                                         // We already have a module. This
1917                                         // is OK.
1918                                         type_module = module_def;
1919
1920                                         // Mark it as an impl module if
1921                                         // necessary.
1922                                         type_module.kind.set(ImplModuleKind);
1923                                     }
1924                                     Some(_) | None => {
1925                                         let parent_link =
1926                                             self.get_parent_link(ModuleReducedGraphParent(root),
1927                                                                  final_ident);
1928                                         child_name_bindings.define_module(
1929                                             parent_link,
1930                                             Some(def),
1931                                             ImplModuleKind,
1932                                             true,
1933                                             true,
1934                                             DUMMY_SP);
1935                                         type_module =
1936                                             child_name_bindings.
1937                                                 get_module();
1938                                     }
1939                                 }
1940
1941                                 // Add each static method to the module.
1942                                 let new_parent =
1943                                     ModuleReducedGraphParent(type_module);
1944                                 for static_method_info in
1945                                         static_methods.iter() {
1946                                     let ident = static_method_info.ident;
1947                                     debug!("(building reduced graph for \
1948                                              external crate) creating \
1949                                              static method '{}'",
1950                                            token::get_ident(ident));
1951
1952                                     let method_name_bindings =
1953                                         self.add_child(ident,
1954                                                        new_parent.clone(),
1955                                                        OverwriteDuplicates,
1956                                                        DUMMY_SP);
1957                                     let def = DefFn(
1958                                         static_method_info.def_id,
1959                                         static_method_info.fn_style);
1960
1961                                     method_name_bindings.define_value(
1962                                         def, DUMMY_SP,
1963                                         visibility == ast::Public);
1964                                 }
1965                             }
1966
1967                             // Otherwise, do nothing.
1968                             Some(_) | None => {}
1969                         }
1970                     }
1971                 }
1972             }
1973             DlField => {
1974                 debug!("(building reduced graph for external crate) \
1975                         ignoring field");
1976             }
1977         }
1978     }
1979
1980     /// Builds the reduced graph rooted at the given external module.
1981     fn populate_external_module(&mut self, module: Rc<Module>) {
1982         debug!("(populating external module) attempting to populate {}",
1983                self.module_to_string(&*module));
1984
1985         let def_id = match module.def_id.get() {
1986             None => {
1987                 debug!("(populating external module) ... no def ID!");
1988                 return
1989             }
1990             Some(def_id) => def_id,
1991         };
1992
1993         csearch::each_child_of_item(&self.session.cstore,
1994                                     def_id,
1995                                     |def_like, child_ident, visibility| {
1996             debug!("(populating external module) ... found ident: {}",
1997                    token::get_ident(child_ident));
1998             self.build_reduced_graph_for_external_crate_def(module.clone(),
1999                                                             def_like,
2000                                                             child_ident,
2001                                                             visibility)
2002         });
2003         module.populated.set(true)
2004     }
2005
2006     /// Ensures that the reduced graph rooted at the given external module
2007     /// is built, building it if it is not.
2008     fn populate_module_if_necessary(&mut self, module: &Rc<Module>) {
2009         if !module.populated.get() {
2010             self.populate_external_module(module.clone())
2011         }
2012         assert!(module.populated.get())
2013     }
2014
2015     /// Builds the reduced graph rooted at the 'use' directive for an external
2016     /// crate.
2017     fn build_reduced_graph_for_external_crate(&mut self, root: Rc<Module>) {
2018         csearch::each_top_level_item_of_crate(&self.session.cstore,
2019                                               root.def_id
2020                                                   .get()
2021                                                   .unwrap()
2022                                                   .krate,
2023                                               |def_like, ident, visibility| {
2024             self.build_reduced_graph_for_external_crate_def(root.clone(),
2025                                                             def_like,
2026                                                             ident,
2027                                                             visibility)
2028         });
2029     }
2030
2031     /// Creates and adds an import directive to the given module.
2032     fn build_import_directive(&mut self,
2033                               module_: &Module,
2034                               module_path: Vec<Ident> ,
2035                               subclass: ImportDirectiveSubclass,
2036                               span: Span,
2037                               id: NodeId,
2038                               is_public: bool,
2039                               shadowable: bool) {
2040         module_.imports.borrow_mut().push(ImportDirective::new(module_path,
2041                                                                subclass,
2042                                                                span,
2043                                                                id,
2044                                                                is_public,
2045                                                                shadowable));
2046         self.unresolved_imports += 1;
2047         // Bump the reference count on the name. Or, if this is a glob, set
2048         // the appropriate flag.
2049
2050         match subclass {
2051             SingleImport(target, _) => {
2052                 debug!("(building import directive) building import \
2053                         directive: {}::{}",
2054                        self.idents_to_string(module_.imports.borrow().last().unwrap()
2055                                                  .module_path.as_slice()),
2056                        token::get_ident(target));
2057
2058                 let mut import_resolutions = module_.import_resolutions
2059                                                     .borrow_mut();
2060                 match import_resolutions.find_mut(&target.name) {
2061                     Some(resolution) => {
2062                         debug!("(building import directive) bumping \
2063                                 reference");
2064                         resolution.outstanding_references += 1;
2065
2066                         // the source of this name is different now
2067                         resolution.type_id = id;
2068                         resolution.value_id = id;
2069                         resolution.is_public = is_public;
2070                         return;
2071                     }
2072                     None => {}
2073                 }
2074                 debug!("(building import directive) creating new");
2075                 let mut resolution = ImportResolution::new(id, is_public);
2076                 resolution.outstanding_references = 1;
2077                 import_resolutions.insert(target.name, resolution);
2078             }
2079             GlobImport => {
2080                 // Set the glob flag. This tells us that we don't know the
2081                 // module's exports ahead of time.
2082
2083                 module_.glob_count.set(module_.glob_count.get() + 1);
2084             }
2085         }
2086     }
2087
2088     // Import resolution
2089     //
2090     // This is a fixed-point algorithm. We resolve imports until our efforts
2091     // are stymied by an unresolved import; then we bail out of the current
2092     // module and continue. We terminate successfully once no more imports
2093     // remain or unsuccessfully when no forward progress in resolving imports
2094     // is made.
2095
2096     /// Resolves all imports for the crate. This method performs the fixed-
2097     /// point iteration.
2098     fn resolve_imports(&mut self) {
2099         let mut i = 0u;
2100         let mut prev_unresolved_imports = 0;
2101         loop {
2102             debug!("(resolving imports) iteration {}, {} imports left",
2103                    i, self.unresolved_imports);
2104
2105             let module_root = self.graph_root.get_module();
2106             self.resolve_imports_for_module_subtree(module_root.clone());
2107
2108             if self.unresolved_imports == 0 {
2109                 debug!("(resolving imports) success");
2110                 break;
2111             }
2112
2113             if self.unresolved_imports == prev_unresolved_imports {
2114                 self.report_unresolved_imports(module_root);
2115                 break;
2116             }
2117
2118             i += 1;
2119             prev_unresolved_imports = self.unresolved_imports;
2120         }
2121     }
2122
2123     /// Attempts to resolve imports for the given module and all of its
2124     /// submodules.
2125     fn resolve_imports_for_module_subtree(&mut self, module_: Rc<Module>) {
2126         debug!("(resolving imports for module subtree) resolving {}",
2127                self.module_to_string(&*module_));
2128         let orig_module = replace(&mut self.current_module, module_.clone());
2129         self.resolve_imports_for_module(module_.clone());
2130         self.current_module = orig_module;
2131
2132         self.populate_module_if_necessary(&module_);
2133         for (_, child_node) in module_.children.borrow().iter() {
2134             match child_node.get_module_if_available() {
2135                 None => {
2136                     // Nothing to do.
2137                 }
2138                 Some(child_module) => {
2139                     self.resolve_imports_for_module_subtree(child_module);
2140                 }
2141             }
2142         }
2143
2144         for (_, child_module) in module_.anonymous_children.borrow().iter() {
2145             self.resolve_imports_for_module_subtree(child_module.clone());
2146         }
2147     }
2148
2149     /// Attempts to resolve imports for the given module only.
2150     fn resolve_imports_for_module(&mut self, module: Rc<Module>) {
2151         if module.all_imports_resolved() {
2152             debug!("(resolving imports for module) all imports resolved for \
2153                    {}",
2154                    self.module_to_string(&*module));
2155             return;
2156         }
2157
2158         let imports = module.imports.borrow();
2159         let import_count = imports.len();
2160         while module.resolved_import_count.get() < import_count {
2161             let import_index = module.resolved_import_count.get();
2162             let import_directive = imports.get(import_index);
2163             match self.resolve_import_for_module(module.clone(),
2164                                                  import_directive) {
2165                 Failed(err) => {
2166                     let (span, help) = match err {
2167                         Some((span, msg)) => (span, format!(". {}", msg)),
2168                         None => (import_directive.span, String::new())
2169                     };
2170                     let msg = format!("unresolved import `{}`{}",
2171                                       self.import_path_to_string(
2172                                           import_directive.module_path
2173                                                           .as_slice(),
2174                                           import_directive.subclass),
2175                                       help);
2176                     self.resolve_error(span, msg.as_slice());
2177                 }
2178                 Indeterminate => break, // Bail out. We'll come around next time.
2179                 Success(()) => () // Good. Continue.
2180             }
2181
2182             module.resolved_import_count
2183                   .set(module.resolved_import_count.get() + 1);
2184         }
2185     }
2186
2187     fn idents_to_string(&self, idents: &[Ident]) -> String {
2188         let mut first = true;
2189         let mut result = String::new();
2190         for ident in idents.iter() {
2191             if first {
2192                 first = false
2193             } else {
2194                 result.push_str("::")
2195             }
2196             result.push_str(token::get_ident(*ident).get());
2197         };
2198         result
2199     }
2200
2201     fn path_idents_to_string(&self, path: &Path) -> String {
2202         let identifiers: Vec<ast::Ident> = path.segments
2203                                              .iter()
2204                                              .map(|seg| seg.identifier)
2205                                              .collect();
2206         self.idents_to_string(identifiers.as_slice())
2207     }
2208
2209     fn import_directive_subclass_to_string(&mut self,
2210                                         subclass: ImportDirectiveSubclass)
2211                                         -> String {
2212         match subclass {
2213             SingleImport(_, source) => {
2214                 token::get_ident(source).get().to_string()
2215             }
2216             GlobImport => "*".to_string()
2217         }
2218     }
2219
2220     fn import_path_to_string(&mut self,
2221                           idents: &[Ident],
2222                           subclass: ImportDirectiveSubclass)
2223                           -> String {
2224         if idents.is_empty() {
2225             self.import_directive_subclass_to_string(subclass)
2226         } else {
2227             (format!("{}::{}",
2228                      self.idents_to_string(idents),
2229                      self.import_directive_subclass_to_string(
2230                          subclass))).to_string()
2231         }
2232     }
2233
2234     /// Attempts to resolve the given import. The return value indicates
2235     /// failure if we're certain the name does not exist, indeterminate if we
2236     /// don't know whether the name exists at the moment due to other
2237     /// currently-unresolved imports, or success if we know the name exists.
2238     /// If successful, the resolved bindings are written into the module.
2239     fn resolve_import_for_module(&mut self,
2240                                  module_: Rc<Module>,
2241                                  import_directive: &ImportDirective)
2242                                  -> ResolveResult<()> {
2243         let mut resolution_result = Failed(None);
2244         let module_path = &import_directive.module_path;
2245
2246         debug!("(resolving import for module) resolving import `{}::...` in \
2247                 `{}`",
2248                self.idents_to_string(module_path.as_slice()),
2249                self.module_to_string(&*module_));
2250
2251         // First, resolve the module path for the directive, if necessary.
2252         let container = if module_path.len() == 0 {
2253             // Use the crate root.
2254             Some((self.graph_root.get_module(), LastMod(AllPublic)))
2255         } else {
2256             match self.resolve_module_path(module_.clone(),
2257                                            module_path.as_slice(),
2258                                            DontUseLexicalScope,
2259                                            import_directive.span,
2260                                            ImportSearch) {
2261                 Failed(err) => {
2262                     resolution_result = Failed(err);
2263                     None
2264                 },
2265                 Indeterminate => {
2266                     resolution_result = Indeterminate;
2267                     None
2268                 }
2269                 Success(container) => Some(container),
2270             }
2271         };
2272
2273         match container {
2274             None => {}
2275             Some((containing_module, lp)) => {
2276                 // We found the module that the target is contained
2277                 // within. Attempt to resolve the import within it.
2278
2279                 match import_directive.subclass {
2280                     SingleImport(target, source) => {
2281                         resolution_result =
2282                             self.resolve_single_import(&*module_,
2283                                                        containing_module,
2284                                                        target,
2285                                                        source,
2286                                                        import_directive,
2287                                                        lp);
2288                     }
2289                     GlobImport => {
2290                         resolution_result =
2291                             self.resolve_glob_import(&*module_,
2292                                                      containing_module,
2293                                                      import_directive,
2294                                                      lp);
2295                     }
2296                 }
2297             }
2298         }
2299
2300         // Decrement the count of unresolved imports.
2301         match resolution_result {
2302             Success(()) => {
2303                 assert!(self.unresolved_imports >= 1);
2304                 self.unresolved_imports -= 1;
2305             }
2306             _ => {
2307                 // Nothing to do here; just return the error.
2308             }
2309         }
2310
2311         // Decrement the count of unresolved globs if necessary. But only if
2312         // the resolution result is indeterminate -- otherwise we'll stop
2313         // processing imports here. (See the loop in
2314         // resolve_imports_for_module.)
2315
2316         if !resolution_result.indeterminate() {
2317             match import_directive.subclass {
2318                 GlobImport => {
2319                     assert!(module_.glob_count.get() >= 1);
2320                     module_.glob_count.set(module_.glob_count.get() - 1);
2321                 }
2322                 SingleImport(..) => {
2323                     // Ignore.
2324                 }
2325             }
2326         }
2327
2328         return resolution_result;
2329     }
2330
2331     fn create_name_bindings_from_module(module: Rc<Module>) -> NameBindings {
2332         NameBindings {
2333             type_def: RefCell::new(Some(TypeNsDef {
2334                 is_public: false,
2335                 module_def: Some(module),
2336                 type_def: None,
2337                 type_span: None
2338             })),
2339             value_def: RefCell::new(None),
2340         }
2341     }
2342
2343     fn resolve_single_import(&mut self,
2344                              module_: &Module,
2345                              containing_module: Rc<Module>,
2346                              target: Ident,
2347                              source: Ident,
2348                              directive: &ImportDirective,
2349                              lp: LastPrivate)
2350                                  -> ResolveResult<()> {
2351         debug!("(resolving single import) resolving `{}` = `{}::{}` from \
2352                 `{}` id {}, last private {:?}",
2353                token::get_ident(target),
2354                self.module_to_string(&*containing_module),
2355                token::get_ident(source),
2356                self.module_to_string(module_),
2357                directive.id,
2358                lp);
2359
2360         let lp = match lp {
2361             LastMod(lp) => lp,
2362             LastImport {..} => {
2363                 self.session
2364                     .span_bug(directive.span,
2365                               "not expecting Import here, must be LastMod")
2366             }
2367         };
2368
2369         // We need to resolve both namespaces for this to succeed.
2370         //
2371
2372         let mut value_result = UnknownResult;
2373         let mut type_result = UnknownResult;
2374
2375         // Search for direct children of the containing module.
2376         self.populate_module_if_necessary(&containing_module);
2377
2378         match containing_module.children.borrow().find(&source.name) {
2379             None => {
2380                 // Continue.
2381             }
2382             Some(ref child_name_bindings) => {
2383                 if child_name_bindings.defined_in_namespace(ValueNS) {
2384                     debug!("(resolving single import) found value binding");
2385                     value_result = BoundResult(containing_module.clone(),
2386                                                (*child_name_bindings).clone());
2387                 }
2388                 if child_name_bindings.defined_in_namespace(TypeNS) {
2389                     debug!("(resolving single import) found type binding");
2390                     type_result = BoundResult(containing_module.clone(),
2391                                               (*child_name_bindings).clone());
2392                 }
2393             }
2394         }
2395
2396         // Unless we managed to find a result in both namespaces (unlikely),
2397         // search imports as well.
2398         let mut value_used_reexport = false;
2399         let mut type_used_reexport = false;
2400         match (value_result.clone(), type_result.clone()) {
2401             (BoundResult(..), BoundResult(..)) => {} // Continue.
2402             _ => {
2403                 // If there is an unresolved glob at this point in the
2404                 // containing module, bail out. We don't know enough to be
2405                 // able to resolve this import.
2406
2407                 if containing_module.glob_count.get() > 0 {
2408                     debug!("(resolving single import) unresolved glob; \
2409                             bailing out");
2410                     return Indeterminate;
2411                 }
2412
2413                 // Now search the exported imports within the containing module.
2414                 match containing_module.import_resolutions.borrow().find(&source.name) {
2415                     None => {
2416                         debug!("(resolving single import) no import");
2417                         // The containing module definitely doesn't have an
2418                         // exported import with the name in question. We can
2419                         // therefore accurately report that the names are
2420                         // unbound.
2421
2422                         if value_result.is_unknown() {
2423                             value_result = UnboundResult;
2424                         }
2425                         if type_result.is_unknown() {
2426                             type_result = UnboundResult;
2427                         }
2428                     }
2429                     Some(import_resolution)
2430                             if import_resolution.outstanding_references == 0 => {
2431
2432                         fn get_binding(this: &mut Resolver,
2433                                        import_resolution: &ImportResolution,
2434                                        namespace: Namespace)
2435                                     -> NamespaceResult {
2436
2437                             // Import resolutions must be declared with "pub"
2438                             // in order to be exported.
2439                             if !import_resolution.is_public {
2440                                 return UnboundResult;
2441                             }
2442
2443                             match import_resolution.
2444                                     target_for_namespace(namespace) {
2445                                 None => {
2446                                     return UnboundResult;
2447                                 }
2448                                 Some(Target {
2449                                     target_module,
2450                                     bindings,
2451                                     shadowable: _
2452                                 }) => {
2453                                     debug!("(resolving single import) found \
2454                                             import in ns {:?}", namespace);
2455                                     let id = import_resolution.id(namespace);
2456                                     this.used_imports.insert((id, namespace));
2457                                     return BoundResult(target_module, bindings);
2458                                 }
2459                             }
2460                         }
2461
2462                         // The name is an import which has been fully
2463                         // resolved. We can, therefore, just follow it.
2464                         if value_result.is_unknown() {
2465                             value_result = get_binding(self, import_resolution,
2466                                                        ValueNS);
2467                             value_used_reexport = import_resolution.is_public;
2468                         }
2469                         if type_result.is_unknown() {
2470                             type_result = get_binding(self, import_resolution,
2471                                                       TypeNS);
2472                             type_used_reexport = import_resolution.is_public;
2473                         }
2474
2475                     }
2476                     Some(_) => {
2477                         // The import is unresolved. Bail out.
2478                         debug!("(resolving single import) unresolved import; \
2479                                 bailing out");
2480                         return Indeterminate;
2481                     }
2482                 }
2483             }
2484         }
2485
2486         // If we didn't find a result in the type namespace, search the
2487         // external modules.
2488         let mut value_used_public = false;
2489         let mut type_used_public = false;
2490         match type_result {
2491             BoundResult(..) => {}
2492             _ => {
2493                 match containing_module.external_module_children.borrow_mut()
2494                                        .find_copy(&source.name) {
2495                     None => {} // Continue.
2496                     Some(module) => {
2497                         debug!("(resolving single import) found external \
2498                                 module");
2499                         let name_bindings =
2500                             Rc::new(Resolver::create_name_bindings_from_module(
2501                                 module));
2502                         type_result = BoundResult(containing_module.clone(),
2503                                                   name_bindings);
2504                         type_used_public = true;
2505                     }
2506                 }
2507             }
2508         }
2509
2510         // We've successfully resolved the import. Write the results in.
2511         let mut import_resolutions = module_.import_resolutions.borrow_mut();
2512         let import_resolution = import_resolutions.get_mut(&target.name);
2513
2514         match value_result {
2515             BoundResult(ref target_module, ref name_bindings) => {
2516                 debug!("(resolving single import) found value target");
2517                 self.check_for_conflicting_import(
2518                     &import_resolution.value_target,
2519                     directive.span,
2520                     target.name,
2521                     ValueNS);
2522
2523                 import_resolution.value_target =
2524                     Some(Target::new(target_module.clone(),
2525                                      name_bindings.clone(),
2526                                      directive.shadowable));
2527                 import_resolution.value_id = directive.id;
2528                 import_resolution.is_public = directive.is_public;
2529                 value_used_public = name_bindings.defined_in_public_namespace(ValueNS);
2530             }
2531             UnboundResult => { /* Continue. */ }
2532             UnknownResult => {
2533                 fail!("value result should be known at this point");
2534             }
2535         }
2536         match type_result {
2537             BoundResult(ref target_module, ref name_bindings) => {
2538                 debug!("(resolving single import) found type target: {:?}",
2539                        { name_bindings.type_def.borrow().clone().unwrap().type_def });
2540                 self.check_for_conflicting_import(
2541                     &import_resolution.type_target,
2542                     directive.span,
2543                     target.name,
2544                     TypeNS);
2545
2546                 import_resolution.type_target =
2547                     Some(Target::new(target_module.clone(),
2548                                      name_bindings.clone(),
2549                                      directive.shadowable));
2550                 import_resolution.type_id = directive.id;
2551                 import_resolution.is_public = directive.is_public;
2552                 type_used_public = name_bindings.defined_in_public_namespace(TypeNS);
2553             }
2554             UnboundResult => { /* Continue. */ }
2555             UnknownResult => {
2556                 fail!("type result should be known at this point");
2557             }
2558         }
2559
2560         self.check_for_conflicts_between_imports_and_items(
2561             module_,
2562             import_resolution,
2563             directive.span,
2564             target.name);
2565
2566         if value_result.is_unbound() && type_result.is_unbound() {
2567             let msg = format!("There is no `{}` in `{}`",
2568                               token::get_ident(source),
2569                               self.module_to_string(&*containing_module));
2570             return Failed(Some((directive.span, msg)));
2571         }
2572         let value_used_public = value_used_reexport || value_used_public;
2573         let type_used_public = type_used_reexport || type_used_public;
2574
2575         assert!(import_resolution.outstanding_references >= 1);
2576         import_resolution.outstanding_references -= 1;
2577
2578         // record what this import resolves to for later uses in documentation,
2579         // this may resolve to either a value or a type, but for documentation
2580         // purposes it's good enough to just favor one over the other.
2581         let value_private = match import_resolution.value_target {
2582             Some(ref target) => {
2583                 let def = target.bindings.def_for_namespace(ValueNS).unwrap();
2584                 self.def_map.borrow_mut().insert(directive.id, def);
2585                 let did = def.def_id();
2586                 if value_used_public {Some(lp)} else {Some(DependsOn(did))}
2587             },
2588             // AllPublic here and below is a dummy value, it should never be used because
2589             // _exists is false.
2590             None => None,
2591         };
2592         let type_private = match import_resolution.type_target {
2593             Some(ref target) => {
2594                 let def = target.bindings.def_for_namespace(TypeNS).unwrap();
2595                 self.def_map.borrow_mut().insert(directive.id, def);
2596                 let did = def.def_id();
2597                 if type_used_public {Some(lp)} else {Some(DependsOn(did))}
2598             },
2599             None => None,
2600         };
2601
2602         self.last_private.insert(directive.id, LastImport{value_priv: value_private,
2603                                                           value_used: Used,
2604                                                           type_priv: type_private,
2605                                                           type_used: Used});
2606
2607         debug!("(resolving single import) successfully resolved import");
2608         return Success(());
2609     }
2610
2611     // Resolves a glob import. Note that this function cannot fail; it either
2612     // succeeds or bails out (as importing * from an empty module or a module
2613     // that exports nothing is valid).
2614     fn resolve_glob_import(&mut self,
2615                            module_: &Module,
2616                            containing_module: Rc<Module>,
2617                            import_directive: &ImportDirective,
2618                            lp: LastPrivate)
2619                            -> ResolveResult<()> {
2620         let id = import_directive.id;
2621         let is_public = import_directive.is_public;
2622
2623         // This function works in a highly imperative manner; it eagerly adds
2624         // everything it can to the list of import resolutions of the module
2625         // node.
2626         debug!("(resolving glob import) resolving glob import {}", id);
2627
2628         // We must bail out if the node has unresolved imports of any kind
2629         // (including globs).
2630         if !(*containing_module).all_imports_resolved() {
2631             debug!("(resolving glob import) target module has unresolved \
2632                     imports; bailing out");
2633             return Indeterminate;
2634         }
2635
2636         assert_eq!(containing_module.glob_count.get(), 0);
2637
2638         // Add all resolved imports from the containing module.
2639         let import_resolutions = containing_module.import_resolutions
2640                                                   .borrow();
2641         for (ident, target_import_resolution) in import_resolutions.iter() {
2642             debug!("(resolving glob import) writing module resolution \
2643                     {:?} into `{}`",
2644                    target_import_resolution.type_target.is_none(),
2645                    self.module_to_string(module_));
2646
2647             if !target_import_resolution.is_public {
2648                 debug!("(resolving glob import) nevermind, just kidding");
2649                 continue
2650             }
2651
2652             // Here we merge two import resolutions.
2653             let mut import_resolutions = module_.import_resolutions.borrow_mut();
2654             match import_resolutions.find_mut(ident) {
2655                 Some(dest_import_resolution) => {
2656                     // Merge the two import resolutions at a finer-grained
2657                     // level.
2658
2659                     match target_import_resolution.value_target {
2660                         None => {
2661                             // Continue.
2662                         }
2663                         Some(ref value_target) => {
2664                             dest_import_resolution.value_target =
2665                                 Some(value_target.clone());
2666                         }
2667                     }
2668                     match target_import_resolution.type_target {
2669                         None => {
2670                             // Continue.
2671                         }
2672                         Some(ref type_target) => {
2673                             dest_import_resolution.type_target =
2674                                 Some(type_target.clone());
2675                         }
2676                     }
2677                     dest_import_resolution.is_public = is_public;
2678                     continue;
2679                 }
2680                 None => {}
2681             }
2682
2683             // Simple: just copy the old import resolution.
2684             let mut new_import_resolution = ImportResolution::new(id, is_public);
2685             new_import_resolution.value_target =
2686                 target_import_resolution.value_target.clone();
2687             new_import_resolution.type_target =
2688                 target_import_resolution.type_target.clone();
2689
2690             import_resolutions.insert(*ident, new_import_resolution);
2691         }
2692
2693         // Add all children from the containing module.
2694         self.populate_module_if_necessary(&containing_module);
2695
2696         for (&name, name_bindings) in containing_module.children
2697                                                        .borrow().iter() {
2698             self.merge_import_resolution(module_,
2699                                          containing_module.clone(),
2700                                          import_directive,
2701                                          name,
2702                                          name_bindings.clone());
2703
2704         }
2705
2706         // Add external module children from the containing module.
2707         for (&name, module) in containing_module.external_module_children
2708                                                 .borrow().iter() {
2709             let name_bindings =
2710                 Rc::new(Resolver::create_name_bindings_from_module(module.clone()));
2711             self.merge_import_resolution(module_,
2712                                          containing_module.clone(),
2713                                          import_directive,
2714                                          name,
2715                                          name_bindings);
2716         }
2717
2718         // Record the destination of this import
2719         match containing_module.def_id.get() {
2720             Some(did) => {
2721                 self.def_map.borrow_mut().insert(id, DefMod(did));
2722                 self.last_private.insert(id, lp);
2723             }
2724             None => {}
2725         }
2726
2727         debug!("(resolving glob import) successfully resolved import");
2728         return Success(());
2729     }
2730
2731     fn merge_import_resolution(&mut self,
2732                                module_: &Module,
2733                                containing_module: Rc<Module>,
2734                                import_directive: &ImportDirective,
2735                                name: Name,
2736                                name_bindings: Rc<NameBindings>) {
2737         let id = import_directive.id;
2738         let is_public = import_directive.is_public;
2739
2740         let mut import_resolutions = module_.import_resolutions.borrow_mut();
2741         let dest_import_resolution = import_resolutions.find_or_insert_with(name, |_| {
2742             // Create a new import resolution from this child.
2743             ImportResolution::new(id, is_public)
2744         });
2745
2746         debug!("(resolving glob import) writing resolution `{}` in `{}` \
2747                to `{}`",
2748                token::get_name(name).get().to_string(),
2749                self.module_to_string(&*containing_module),
2750                self.module_to_string(module_));
2751
2752         // Merge the child item into the import resolution.
2753         if name_bindings.defined_in_public_namespace(ValueNS) {
2754             debug!("(resolving glob import) ... for value target");
2755             dest_import_resolution.value_target =
2756                 Some(Target::new(containing_module.clone(),
2757                                  name_bindings.clone(),
2758                                  import_directive.shadowable));
2759             dest_import_resolution.value_id = id;
2760         }
2761         if name_bindings.defined_in_public_namespace(TypeNS) {
2762             debug!("(resolving glob import) ... for type target");
2763             dest_import_resolution.type_target =
2764                 Some(Target::new(containing_module,
2765                                  name_bindings.clone(),
2766                                  import_directive.shadowable));
2767             dest_import_resolution.type_id = id;
2768         }
2769         dest_import_resolution.is_public = is_public;
2770
2771         self.check_for_conflicts_between_imports_and_items(
2772             module_,
2773             dest_import_resolution,
2774             import_directive.span,
2775             name);
2776     }
2777
2778     /// Checks that imported names and items don't have the same name.
2779     fn check_for_conflicting_import(&mut self,
2780                                     target: &Option<Target>,
2781                                     import_span: Span,
2782                                     name: Name,
2783                                     namespace: Namespace) {
2784         if self.session.features.import_shadowing.get() {
2785             return
2786         }
2787
2788         match *target {
2789             Some(ref target) if !target.shadowable => {
2790                 let msg = format!("a {} named `{}` has already been imported \
2791                                    in this module",
2792                                   match namespace {
2793                                     TypeNS => "type",
2794                                     ValueNS => "value",
2795                                   },
2796                                   token::get_name(name).get());
2797                 self.session.span_err(import_span, msg.as_slice());
2798             }
2799             Some(_) | None => {}
2800         }
2801     }
2802
2803     /// Checks that imported names and items don't have the same name.
2804     fn check_for_conflicts_between_imports_and_items(&mut self,
2805                                                      module: &Module,
2806                                                      import_resolution:
2807                                                      &mut ImportResolution,
2808                                                      import_span: Span,
2809                                                      name: Name) {
2810         if self.session.features.import_shadowing.get() {
2811             return
2812         }
2813
2814         // First, check for conflicts between imports and `extern crate`s.
2815         if module.external_module_children
2816                  .borrow()
2817                  .contains_key(&name) {
2818             match import_resolution.type_target {
2819                 Some(ref target) if !target.shadowable => {
2820                     let msg = format!("import `{}` conflicts with imported \
2821                                        crate in this module",
2822                                       token::get_name(name).get());
2823                     self.session.span_err(import_span, msg.as_slice());
2824                 }
2825                 Some(_) | None => {}
2826             }
2827         }
2828
2829         // Check for item conflicts.
2830         let children = module.children.borrow();
2831         let name_bindings = match children.find(&name) {
2832             None => {
2833                 // There can't be any conflicts.
2834                 return
2835             }
2836             Some(ref name_bindings) => (*name_bindings).clone(),
2837         };
2838
2839         match import_resolution.value_target {
2840             Some(ref target) if !target.shadowable => {
2841                 match *name_bindings.value_def.borrow() {
2842                     None => {}
2843                     Some(ref value) => {
2844                         let msg = format!("import `{}` conflicts with value \
2845                                            in this module",
2846                                           token::get_name(name).get());
2847                         self.session.span_err(import_span, msg.as_slice());
2848                         match value.value_span {
2849                             None => {}
2850                             Some(span) => {
2851                                 self.session
2852                                     .span_note(span,
2853                                                "note conflicting value here");
2854                             }
2855                         }
2856                     }
2857                 }
2858             }
2859             Some(_) | None => {}
2860         }
2861
2862         match import_resolution.type_target {
2863             Some(ref target) if !target.shadowable => {
2864                 match *name_bindings.type_def.borrow() {
2865                     None => {}
2866                     Some(ref ty) => {
2867                         let msg = format!("import `{}` conflicts with type in \
2868                                            this module",
2869                                           token::get_name(name).get());
2870                         self.session.span_err(import_span, msg.as_slice());
2871                         match ty.type_span {
2872                             None => {}
2873                             Some(span) => {
2874                                 self.session
2875                                     .span_note(span,
2876                                                "note conflicting type here")
2877                             }
2878                         }
2879                     }
2880                 }
2881             }
2882             Some(_) | None => {}
2883         }
2884     }
2885
2886     /// Checks that the names of external crates don't collide with other
2887     /// external crates.
2888     fn check_for_conflicts_between_external_crates(&self,
2889                                                    module: &Module,
2890                                                    name: Name,
2891                                                    span: Span) {
2892         if self.session.features.import_shadowing.get() {
2893             return
2894         }
2895
2896         if module.external_module_children.borrow().contains_key(&name) {
2897             self.session
2898                 .span_err(span,
2899                           format!("an external crate named `{}` has already \
2900                                    been imported into this module",
2901                                   token::get_name(name).get()).as_slice());
2902         }
2903     }
2904
2905     /// Checks that the names of items don't collide with external crates.
2906     fn check_for_conflicts_between_external_crates_and_items(&self,
2907                                                              module: &Module,
2908                                                              name: Name,
2909                                                              span: Span) {
2910         if self.session.features.import_shadowing.get() {
2911             return
2912         }
2913
2914         if module.external_module_children.borrow().contains_key(&name) {
2915             self.session
2916                 .span_err(span,
2917                           format!("the name `{}` conflicts with an external \
2918                                    crate that has been imported into this \
2919                                    module",
2920                                   token::get_name(name).get()).as_slice());
2921         }
2922     }
2923
2924     /// Resolves the given module path from the given root `module_`.
2925     fn resolve_module_path_from_root(&mut self,
2926                                      module_: Rc<Module>,
2927                                      module_path: &[Ident],
2928                                      index: uint,
2929                                      span: Span,
2930                                      name_search_type: NameSearchType,
2931                                      lp: LastPrivate)
2932                                 -> ResolveResult<(Rc<Module>, LastPrivate)> {
2933         fn search_parent_externals(needle: Name, module: &Rc<Module>)
2934                                 -> Option<Rc<Module>> {
2935             module.external_module_children.borrow()
2936                                             .find_copy(&needle)
2937                                             .map(|_| module.clone())
2938                                             .or_else(|| {
2939                 match module.parent_link.clone() {
2940                     ModuleParentLink(parent, _) => {
2941                         search_parent_externals(needle,
2942                                                 &parent.upgrade().unwrap())
2943                     }
2944                    _ => None
2945                 }
2946             })
2947         }
2948
2949         let mut search_module = module_;
2950         let mut index = index;
2951         let module_path_len = module_path.len();
2952         let mut closest_private = lp;
2953
2954         // Resolve the module part of the path. This does not involve looking
2955         // upward though scope chains; we simply resolve names directly in
2956         // modules as we go.
2957         while index < module_path_len {
2958             let name = module_path[index];
2959             match self.resolve_name_in_module(search_module.clone(),
2960                                               name.name,
2961                                               TypeNS,
2962                                               name_search_type,
2963                                               false) {
2964                 Failed(None) => {
2965                     let segment_name = token::get_ident(name);
2966                     let module_name = self.module_to_string(&*search_module);
2967                     let mut span = span;
2968                     let msg = if "???" == module_name.as_slice() {
2969                         span.hi = span.lo + Pos::from_uint(segment_name.get().len());
2970
2971                         match search_parent_externals(name.name,
2972                                                      &self.current_module) {
2973                             Some(module) => {
2974                                 let path_str = self.idents_to_string(module_path);
2975                                 let target_mod_str = self.module_to_string(&*module);
2976                                 let current_mod_str =
2977                                     self.module_to_string(&*self.current_module);
2978
2979                                 let prefix = if target_mod_str == current_mod_str {
2980                                     "self::".to_string()
2981                                 } else {
2982                                     format!("{}::", target_mod_str)
2983                                 };
2984
2985                                 format!("Did you mean `{}{}`?", prefix, path_str)
2986                             },
2987                             None => format!("Maybe a missing `extern crate {}`?",
2988                                             segment_name),
2989                         }
2990                     } else {
2991                         format!("Could not find `{}` in `{}`.",
2992                                 segment_name,
2993                                 module_name)
2994                     };
2995
2996                     return Failed(Some((span, msg)));
2997                 }
2998                 Failed(err) => return Failed(err),
2999                 Indeterminate => {
3000                     debug!("(resolving module path for import) module \
3001                             resolution is indeterminate: {}",
3002                             token::get_ident(name));
3003                     return Indeterminate;
3004                 }
3005                 Success((target, used_proxy)) => {
3006                     // Check to see whether there are type bindings, and, if
3007                     // so, whether there is a module within.
3008                     match *target.bindings.type_def.borrow() {
3009                         Some(ref type_def) => {
3010                             match type_def.module_def {
3011                                 None => {
3012                                     let msg = format!("Not a module `{}`",
3013                                                         token::get_ident(name));
3014
3015                                     return Failed(Some((span, msg)));
3016                                 }
3017                                 Some(ref module_def) => {
3018                                     // If we're doing the search for an
3019                                     // import, do not allow traits and impls
3020                                     // to be selected.
3021                                     match (name_search_type,
3022                                            module_def.kind.get()) {
3023                                         (ImportSearch, TraitModuleKind) |
3024                                         (ImportSearch, ImplModuleKind) => {
3025                                             let msg =
3026                                                 "Cannot import from a trait or \
3027                                                 type implementation".to_string();
3028                                             return Failed(Some((span, msg)));
3029                                         }
3030                                         (_, _) => {
3031                                             search_module = module_def.clone();
3032
3033                                             // Keep track of the closest
3034                                             // private module used when
3035                                             // resolving this import chain.
3036                                             if !used_proxy &&
3037                                                !search_module.is_public {
3038                                                 match search_module.def_id
3039                                                                    .get() {
3040                                                     Some(did) => {
3041                                                         closest_private =
3042                                                             LastMod(DependsOn(did));
3043                                                     }
3044                                                     None => {}
3045                                                 }
3046                                             }
3047                                         }
3048                                     }
3049                                 }
3050                             }
3051                         }
3052                         None => {
3053                             // There are no type bindings at all.
3054                             let msg = format!("Not a module `{}`",
3055                                               token::get_ident(name));
3056                             return Failed(Some((span, msg)));
3057                         }
3058                     }
3059                 }
3060             }
3061
3062             index += 1;
3063         }
3064
3065         return Success((search_module, closest_private));
3066     }
3067
3068     /// Attempts to resolve the module part of an import directive or path
3069     /// rooted at the given module.
3070     ///
3071     /// On success, returns the resolved module, and the closest *private*
3072     /// module found to the destination when resolving this path.
3073     fn resolve_module_path(&mut self,
3074                            module_: Rc<Module>,
3075                            module_path: &[Ident],
3076                            use_lexical_scope: UseLexicalScopeFlag,
3077                            span: Span,
3078                            name_search_type: NameSearchType)
3079                                -> ResolveResult<(Rc<Module>, LastPrivate)> {
3080         let module_path_len = module_path.len();
3081         assert!(module_path_len > 0);
3082
3083         debug!("(resolving module path for import) processing `{}` rooted at \
3084                `{}`",
3085                self.idents_to_string(module_path),
3086                self.module_to_string(&*module_));
3087
3088         // Resolve the module prefix, if any.
3089         let module_prefix_result = self.resolve_module_prefix(module_.clone(),
3090                                                               module_path);
3091
3092         let search_module;
3093         let start_index;
3094         let last_private;
3095         match module_prefix_result {
3096             Failed(None) => {
3097                 let mpath = self.idents_to_string(module_path);
3098                 let mpath = mpath.as_slice();
3099                 match mpath.rfind(':') {
3100                     Some(idx) => {
3101                         let msg = format!("Could not find `{}` in `{}`",
3102                                             // idx +- 1 to account for the
3103                                             // colons on either side
3104                                             mpath.slice_from(idx + 1),
3105                                             mpath.slice_to(idx - 1));
3106                         return Failed(Some((span, msg)));
3107                     },
3108                     None => return Failed(None),
3109                 }
3110             }
3111             Failed(err) => return Failed(err),
3112             Indeterminate => {
3113                 debug!("(resolving module path for import) indeterminate; \
3114                         bailing");
3115                 return Indeterminate;
3116             }
3117             Success(NoPrefixFound) => {
3118                 // There was no prefix, so we're considering the first element
3119                 // of the path. How we handle this depends on whether we were
3120                 // instructed to use lexical scope or not.
3121                 match use_lexical_scope {
3122                     DontUseLexicalScope => {
3123                         // This is a crate-relative path. We will start the
3124                         // resolution process at index zero.
3125                         search_module = self.graph_root.get_module();
3126                         start_index = 0;
3127                         last_private = LastMod(AllPublic);
3128                     }
3129                     UseLexicalScope => {
3130                         // This is not a crate-relative path. We resolve the
3131                         // first component of the path in the current lexical
3132                         // scope and then proceed to resolve below that.
3133                         match self.resolve_module_in_lexical_scope(
3134                                                             module_,
3135                                                             module_path[0]) {
3136                             Failed(err) => return Failed(err),
3137                             Indeterminate => {
3138                                 debug!("(resolving module path for import) \
3139                                         indeterminate; bailing");
3140                                 return Indeterminate;
3141                             }
3142                             Success(containing_module) => {
3143                                 search_module = containing_module;
3144                                 start_index = 1;
3145                                 last_private = LastMod(AllPublic);
3146                             }
3147                         }
3148                     }
3149                 }
3150             }
3151             Success(PrefixFound(ref containing_module, index)) => {
3152                 search_module = containing_module.clone();
3153                 start_index = index;
3154                 last_private = LastMod(DependsOn(containing_module.def_id
3155                                                                   .get()
3156                                                                   .unwrap()));
3157             }
3158         }
3159
3160         self.resolve_module_path_from_root(search_module,
3161                                            module_path,
3162                                            start_index,
3163                                            span,
3164                                            name_search_type,
3165                                            last_private)
3166     }
3167
3168     /// Invariant: This must only be called during main resolution, not during
3169     /// import resolution.
3170     fn resolve_item_in_lexical_scope(&mut self,
3171                                      module_: Rc<Module>,
3172                                      name: Ident,
3173                                      namespace: Namespace)
3174                                     -> ResolveResult<(Target, bool)> {
3175         debug!("(resolving item in lexical scope) resolving `{}` in \
3176                 namespace {:?} in `{}`",
3177                token::get_ident(name),
3178                namespace,
3179                self.module_to_string(&*module_));
3180
3181         // The current module node is handled specially. First, check for
3182         // its immediate children.
3183         self.populate_module_if_necessary(&module_);
3184
3185         match module_.children.borrow().find(&name.name) {
3186             Some(name_bindings)
3187                     if name_bindings.defined_in_namespace(namespace) => {
3188                 debug!("top name bindings succeeded");
3189                 return Success((Target::new(module_.clone(),
3190                                             name_bindings.clone(),
3191                                             false),
3192                                false));
3193             }
3194             Some(_) | None => { /* Not found; continue. */ }
3195         }
3196
3197         // Now check for its import directives. We don't have to have resolved
3198         // all its imports in the usual way; this is because chains of
3199         // adjacent import statements are processed as though they mutated the
3200         // current scope.
3201         match module_.import_resolutions.borrow().find(&name.name) {
3202             None => {
3203                 // Not found; continue.
3204             }
3205             Some(import_resolution) => {
3206                 match (*import_resolution).target_for_namespace(namespace) {
3207                     None => {
3208                         // Not found; continue.
3209                         debug!("(resolving item in lexical scope) found \
3210                                 import resolution, but not in namespace {:?}",
3211                                namespace);
3212                     }
3213                     Some(target) => {
3214                         debug!("(resolving item in lexical scope) using \
3215                                 import resolution");
3216                         self.used_imports.insert((import_resolution.id(namespace), namespace));
3217                         return Success((target, false));
3218                     }
3219                 }
3220             }
3221         }
3222
3223         // Search for external modules.
3224         if namespace == TypeNS {
3225             match module_.external_module_children.borrow().find_copy(&name.name) {
3226                 None => {}
3227                 Some(module) => {
3228                     let name_bindings =
3229                         Rc::new(Resolver::create_name_bindings_from_module(module));
3230                     debug!("lower name bindings succeeded");
3231                     return Success((Target::new(module_,
3232                                                 name_bindings,
3233                                                 false),
3234                                     false));
3235                 }
3236             }
3237         }
3238
3239         // Finally, proceed up the scope chain looking for parent modules.
3240         let mut search_module = module_;
3241         loop {
3242             // Go to the next parent.
3243             match search_module.parent_link.clone() {
3244                 NoParentLink => {
3245                     // No more parents. This module was unresolved.
3246                     debug!("(resolving item in lexical scope) unresolved \
3247                             module");
3248                     return Failed(None);
3249                 }
3250                 ModuleParentLink(parent_module_node, _) => {
3251                     match search_module.kind.get() {
3252                         NormalModuleKind => {
3253                             // We stop the search here.
3254                             debug!("(resolving item in lexical \
3255                                     scope) unresolved module: not \
3256                                     searching through module \
3257                                     parents");
3258                             return Failed(None);
3259                         }
3260                         ExternModuleKind |
3261                         TraitModuleKind |
3262                         ImplModuleKind |
3263                         AnonymousModuleKind => {
3264                             search_module = parent_module_node.upgrade().unwrap();
3265                         }
3266                     }
3267                 }
3268                 BlockParentLink(ref parent_module_node, _) => {
3269                     search_module = parent_module_node.upgrade().unwrap();
3270                 }
3271             }
3272
3273             // Resolve the name in the parent module.
3274             match self.resolve_name_in_module(search_module.clone(),
3275                                               name.name,
3276                                               namespace,
3277                                               PathSearch,
3278                                               true) {
3279                 Failed(Some((span, msg))) =>
3280                     self.resolve_error(span, format!("failed to resolve. {}",
3281                                                      msg)),
3282                 Failed(None) => (), // Continue up the search chain.
3283                 Indeterminate => {
3284                     // We couldn't see through the higher scope because of an
3285                     // unresolved import higher up. Bail.
3286
3287                     debug!("(resolving item in lexical scope) indeterminate \
3288                             higher scope; bailing");
3289                     return Indeterminate;
3290                 }
3291                 Success((target, used_reexport)) => {
3292                     // We found the module.
3293                     debug!("(resolving item in lexical scope) found name \
3294                             in module, done");
3295                     return Success((target, used_reexport));
3296                 }
3297             }
3298         }
3299     }
3300
3301     /// Resolves a module name in the current lexical scope.
3302     fn resolve_module_in_lexical_scope(&mut self,
3303                                        module_: Rc<Module>,
3304                                        name: Ident)
3305                                 -> ResolveResult<Rc<Module>> {
3306         // If this module is an anonymous module, resolve the item in the
3307         // lexical scope. Otherwise, resolve the item from the crate root.
3308         let resolve_result = self.resolve_item_in_lexical_scope(
3309             module_, name, TypeNS);
3310         match resolve_result {
3311             Success((target, _)) => {
3312                 let bindings = &*target.bindings;
3313                 match *bindings.type_def.borrow() {
3314                     Some(ref type_def) => {
3315                         match type_def.module_def {
3316                             None => {
3317                                 debug!("!!! (resolving module in lexical \
3318                                         scope) module wasn't actually a \
3319                                         module!");
3320                                 return Failed(None);
3321                             }
3322                             Some(ref module_def) => {
3323                                 return Success(module_def.clone());
3324                             }
3325                         }
3326                     }
3327                     None => {
3328                         debug!("!!! (resolving module in lexical scope) module
3329                                 wasn't actually a module!");
3330                         return Failed(None);
3331                     }
3332                 }
3333             }
3334             Indeterminate => {
3335                 debug!("(resolving module in lexical scope) indeterminate; \
3336                         bailing");
3337                 return Indeterminate;
3338             }
3339             Failed(err) => {
3340                 debug!("(resolving module in lexical scope) failed to resolve");
3341                 return Failed(err);
3342             }
3343         }
3344     }
3345
3346     /// Returns the nearest normal module parent of the given module.
3347     fn get_nearest_normal_module_parent(&mut self, module_: Rc<Module>)
3348                                             -> Option<Rc<Module>> {
3349         let mut module_ = module_;
3350         loop {
3351             match module_.parent_link.clone() {
3352                 NoParentLink => return None,
3353                 ModuleParentLink(new_module, _) |
3354                 BlockParentLink(new_module, _) => {
3355                     let new_module = new_module.upgrade().unwrap();
3356                     match new_module.kind.get() {
3357                         NormalModuleKind => return Some(new_module),
3358                         ExternModuleKind |
3359                         TraitModuleKind |
3360                         ImplModuleKind |
3361                         AnonymousModuleKind => module_ = new_module,
3362                     }
3363                 }
3364             }
3365         }
3366     }
3367
3368     /// Returns the nearest normal module parent of the given module, or the
3369     /// module itself if it is a normal module.
3370     fn get_nearest_normal_module_parent_or_self(&mut self, module_: Rc<Module>)
3371                                                 -> Rc<Module> {
3372         match module_.kind.get() {
3373             NormalModuleKind => return module_,
3374             ExternModuleKind |
3375             TraitModuleKind |
3376             ImplModuleKind |
3377             AnonymousModuleKind => {
3378                 match self.get_nearest_normal_module_parent(module_.clone()) {
3379                     None => module_,
3380                     Some(new_module) => new_module
3381                 }
3382             }
3383         }
3384     }
3385
3386     /// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
3387     /// (b) some chain of `super::`.
3388     /// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
3389     fn resolve_module_prefix(&mut self,
3390                              module_: Rc<Module>,
3391                              module_path: &[Ident])
3392                                  -> ResolveResult<ModulePrefixResult> {
3393         // Start at the current module if we see `self` or `super`, or at the
3394         // top of the crate otherwise.
3395         let mut containing_module;
3396         let mut i;
3397         let first_module_path_string = token::get_ident(module_path[0]);
3398         if "self" == first_module_path_string.get() {
3399             containing_module =
3400                 self.get_nearest_normal_module_parent_or_self(module_);
3401             i = 1;
3402         } else if "super" == first_module_path_string.get() {
3403             containing_module =
3404                 self.get_nearest_normal_module_parent_or_self(module_);
3405             i = 0;  // We'll handle `super` below.
3406         } else {
3407             return Success(NoPrefixFound);
3408         }
3409
3410         // Now loop through all the `super`s we find.
3411         while i < module_path.len() {
3412             let string = token::get_ident(module_path[i]);
3413             if "super" != string.get() {
3414                 break
3415             }
3416             debug!("(resolving module prefix) resolving `super` at {}",
3417                    self.module_to_string(&*containing_module));
3418             match self.get_nearest_normal_module_parent(containing_module) {
3419                 None => return Failed(None),
3420                 Some(new_module) => {
3421                     containing_module = new_module;
3422                     i += 1;
3423                 }
3424             }
3425         }
3426
3427         debug!("(resolving module prefix) finished resolving prefix at {}",
3428                self.module_to_string(&*containing_module));
3429
3430         return Success(PrefixFound(containing_module, i));
3431     }
3432
3433     /// Attempts to resolve the supplied name in the given module for the
3434     /// given namespace. If successful, returns the target corresponding to
3435     /// the name.
3436     ///
3437     /// The boolean returned on success is an indicator of whether this lookup
3438     /// passed through a public re-export proxy.
3439     fn resolve_name_in_module(&mut self,
3440                               module_: Rc<Module>,
3441                               name: Name,
3442                               namespace: Namespace,
3443                               name_search_type: NameSearchType,
3444                               allow_private_imports: bool)
3445                               -> ResolveResult<(Target, bool)> {
3446         debug!("(resolving name in module) resolving `{}` in `{}`",
3447                token::get_name(name).get(),
3448                self.module_to_string(&*module_));
3449
3450         // First, check the direct children of the module.
3451         self.populate_module_if_necessary(&module_);
3452
3453         match module_.children.borrow().find(&name) {
3454             Some(name_bindings)
3455                     if name_bindings.defined_in_namespace(namespace) => {
3456                 debug!("(resolving name in module) found node as child");
3457                 return Success((Target::new(module_.clone(),
3458                                             name_bindings.clone(),
3459                                             false),
3460                                false));
3461             }
3462             Some(_) | None => {
3463                 // Continue.
3464             }
3465         }
3466
3467         // Next, check the module's imports if necessary.
3468
3469         // If this is a search of all imports, we should be done with glob
3470         // resolution at this point.
3471         if name_search_type == PathSearch {
3472             assert_eq!(module_.glob_count.get(), 0);
3473         }
3474
3475         // Check the list of resolved imports.
3476         match module_.import_resolutions.borrow().find(&name) {
3477             Some(import_resolution) if allow_private_imports ||
3478                                        import_resolution.is_public => {
3479
3480                 if import_resolution.is_public &&
3481                         import_resolution.outstanding_references != 0 {
3482                     debug!("(resolving name in module) import \
3483                            unresolved; bailing out");
3484                     return Indeterminate;
3485                 }
3486                 match import_resolution.target_for_namespace(namespace) {
3487                     None => {
3488                         debug!("(resolving name in module) name found, \
3489                                 but not in namespace {:?}",
3490                                namespace);
3491                     }
3492                     Some(target) => {
3493                         debug!("(resolving name in module) resolved to \
3494                                 import");
3495                         self.used_imports.insert((import_resolution.id(namespace), namespace));
3496                         return Success((target, true));
3497                     }
3498                 }
3499             }
3500             Some(..) | None => {} // Continue.
3501         }
3502
3503         // Finally, search through external children.
3504         if namespace == TypeNS {
3505             match module_.external_module_children.borrow().find_copy(&name) {
3506                 None => {}
3507                 Some(module) => {
3508                     let name_bindings =
3509                         Rc::new(Resolver::create_name_bindings_from_module(module));
3510                     return Success((Target::new(module_,
3511                                                 name_bindings,
3512                                                 false),
3513                                     false));
3514                 }
3515             }
3516         }
3517
3518         // We're out of luck.
3519         debug!("(resolving name in module) failed to resolve `{}`",
3520                token::get_name(name).get());
3521         return Failed(None);
3522     }
3523
3524     fn report_unresolved_imports(&mut self, module_: Rc<Module>) {
3525         let index = module_.resolved_import_count.get();
3526         let imports = module_.imports.borrow();
3527         let import_count = imports.len();
3528         if index != import_count {
3529             let sn = self.session
3530                          .codemap()
3531                          .span_to_snippet(imports.get(index).span)
3532                          .unwrap();
3533             if sn.as_slice().contains("::") {
3534                 self.resolve_error(imports.get(index).span,
3535                                    "unresolved import");
3536             } else {
3537                 let err = format!("unresolved import (maybe you meant `{}::*`?)",
3538                                   sn.as_slice().slice(0, sn.len()));
3539                 self.resolve_error(imports.get(index).span, err.as_slice());
3540             }
3541         }
3542
3543         // Descend into children and anonymous children.
3544         self.populate_module_if_necessary(&module_);
3545
3546         for (_, child_node) in module_.children.borrow().iter() {
3547             match child_node.get_module_if_available() {
3548                 None => {
3549                     // Continue.
3550                 }
3551                 Some(child_module) => {
3552                     self.report_unresolved_imports(child_module);
3553                 }
3554             }
3555         }
3556
3557         for (_, module_) in module_.anonymous_children.borrow().iter() {
3558             self.report_unresolved_imports(module_.clone());
3559         }
3560     }
3561
3562     // Export recording
3563     //
3564     // This pass simply determines what all "export" keywords refer to and
3565     // writes the results into the export map.
3566     //
3567     // FIXME #4953 This pass will be removed once exports change to per-item.
3568     // Then this operation can simply be performed as part of item (or import)
3569     // processing.
3570
3571     fn record_exports(&mut self) {
3572         let root_module = self.graph_root.get_module();
3573         self.record_exports_for_module_subtree(root_module);
3574     }
3575
3576     fn record_exports_for_module_subtree(&mut self,
3577                                              module_: Rc<Module>) {
3578         // If this isn't a local krate, then bail out. We don't need to record
3579         // exports for nonlocal crates.
3580
3581         match module_.def_id.get() {
3582             Some(def_id) if def_id.krate == LOCAL_CRATE => {
3583                 // OK. Continue.
3584                 debug!("(recording exports for module subtree) recording \
3585                         exports for local module `{}`",
3586                        self.module_to_string(&*module_));
3587             }
3588             None => {
3589                 // Record exports for the root module.
3590                 debug!("(recording exports for module subtree) recording \
3591                         exports for root module `{}`",
3592                        self.module_to_string(&*module_));
3593             }
3594             Some(_) => {
3595                 // Bail out.
3596                 debug!("(recording exports for module subtree) not recording \
3597                         exports for `{}`",
3598                        self.module_to_string(&*module_));
3599                 return;
3600             }
3601         }
3602
3603         self.record_exports_for_module(&*module_);
3604         self.populate_module_if_necessary(&module_);
3605
3606         for (_, child_name_bindings) in module_.children.borrow().iter() {
3607             match child_name_bindings.get_module_if_available() {
3608                 None => {
3609                     // Nothing to do.
3610                 }
3611                 Some(child_module) => {
3612                     self.record_exports_for_module_subtree(child_module);
3613                 }
3614             }
3615         }
3616
3617         for (_, child_module) in module_.anonymous_children.borrow().iter() {
3618             self.record_exports_for_module_subtree(child_module.clone());
3619         }
3620     }
3621
3622     fn record_exports_for_module(&mut self, module_: &Module) {
3623         let mut exports2 = Vec::new();
3624
3625         self.add_exports_for_module(&mut exports2, module_);
3626         match module_.def_id.get() {
3627             Some(def_id) => {
3628                 self.export_map2.borrow_mut().insert(def_id.node, exports2);
3629                 debug!("(computing exports) writing exports for {} (some)",
3630                        def_id.node);
3631             }
3632             None => {}
3633         }
3634     }
3635
3636     fn add_exports_of_namebindings(&mut self,
3637                                    exports2: &mut Vec<Export2> ,
3638                                    name: Name,
3639                                    namebindings: &NameBindings,
3640                                    ns: Namespace) {
3641         match namebindings.def_for_namespace(ns) {
3642             Some(d) => {
3643                 let name = token::get_name(name);
3644                 debug!("(computing exports) YES: export '{}' => {:?}",
3645                        name, d.def_id());
3646                 exports2.push(Export2 {
3647                     name: name.get().to_string(),
3648                     def_id: d.def_id()
3649                 });
3650             }
3651             d_opt => {
3652                 debug!("(computing exports) NO: {:?}", d_opt);
3653             }
3654         }
3655     }
3656
3657     fn add_exports_for_module(&mut self,
3658                               exports2: &mut Vec<Export2> ,
3659                               module_: &Module) {
3660         for (name, importresolution) in module_.import_resolutions.borrow().iter() {
3661             if !importresolution.is_public {
3662                 continue
3663             }
3664             let xs = [TypeNS, ValueNS];
3665             for &ns in xs.iter() {
3666                 match importresolution.target_for_namespace(ns) {
3667                     Some(target) => {
3668                         debug!("(computing exports) maybe export '{}'",
3669                                token::get_name(*name));
3670                         self.add_exports_of_namebindings(exports2,
3671                                                          *name,
3672                                                          &*target.bindings,
3673                                                          ns)
3674                     }
3675                     _ => ()
3676                 }
3677             }
3678         }
3679     }
3680
3681     // AST resolution
3682     //
3683     // We maintain a list of value ribs and type ribs.
3684     //
3685     // Simultaneously, we keep track of the current position in the module
3686     // graph in the `current_module` pointer. When we go to resolve a name in
3687     // the value or type namespaces, we first look through all the ribs and
3688     // then query the module graph. When we resolve a name in the module
3689     // namespace, we can skip all the ribs (since nested modules are not
3690     // allowed within blocks in Rust) and jump straight to the current module
3691     // graph node.
3692     //
3693     // Named implementations are handled separately. When we find a method
3694     // call, we consult the module node to find all of the implementations in
3695     // scope. This information is lazily cached in the module node. We then
3696     // generate a fake "implementation scope" containing all the
3697     // implementations thus found, for compatibility with old resolve pass.
3698
3699     fn with_scope(&mut self, name: Option<Ident>, f: |&mut Resolver|) {
3700         let orig_module = self.current_module.clone();
3701
3702         // Move down in the graph.
3703         match name {
3704             None => {
3705                 // Nothing to do.
3706             }
3707             Some(name) => {
3708                 self.populate_module_if_necessary(&orig_module);
3709
3710                 match orig_module.children.borrow().find(&name.name) {
3711                     None => {
3712                         debug!("!!! (with scope) didn't find `{}` in `{}`",
3713                                token::get_ident(name),
3714                                self.module_to_string(&*orig_module));
3715                     }
3716                     Some(name_bindings) => {
3717                         match (*name_bindings).get_module_if_available() {
3718                             None => {
3719                                 debug!("!!! (with scope) didn't find module \
3720                                         for `{}` in `{}`",
3721                                        token::get_ident(name),
3722                                        self.module_to_string(&*orig_module));
3723                             }
3724                             Some(module_) => {
3725                                 self.current_module = module_;
3726                             }
3727                         }
3728                     }
3729                 }
3730             }
3731         }
3732
3733         f(self);
3734
3735         self.current_module = orig_module;
3736     }
3737
3738     /// Wraps the given definition in the appropriate number of `def_upvar`
3739     /// wrappers.
3740     fn upvarify(&self,
3741                 ribs: &[Rib],
3742                 rib_index: uint,
3743                 def_like: DefLike,
3744                 span: Span)
3745                 -> Option<DefLike> {
3746         let mut def;
3747         let is_ty_param;
3748
3749         match def_like {
3750             DlDef(d @ DefLocal(..)) | DlDef(d @ DefUpvar(..)) |
3751             DlDef(d @ DefArg(..)) | DlDef(d @ DefBinding(..)) => {
3752                 def = d;
3753                 is_ty_param = false;
3754             }
3755             DlDef(d @ DefTyParam(..)) |
3756             DlDef(d @ DefSelfTy(..)) => {
3757                 def = d;
3758                 is_ty_param = true;
3759             }
3760             _ => {
3761                 return Some(def_like);
3762             }
3763         }
3764
3765         let mut rib_index = rib_index + 1;
3766         while rib_index < ribs.len() {
3767             match ribs[rib_index].kind {
3768                 NormalRibKind => {
3769                     // Nothing to do. Continue.
3770                 }
3771                 FunctionRibKind(function_id, body_id) => {
3772                     if !is_ty_param {
3773                         def = DefUpvar(def.def_id().node,
3774                                        box(GC) def,
3775                                        function_id,
3776                                        body_id);
3777                     }
3778                 }
3779                 MethodRibKind(item_id, _) => {
3780                   // If the def is a ty param, and came from the parent
3781                   // item, it's ok
3782                   match def {
3783                     DefTyParam(_, did, _) if {
3784                         self.def_map.borrow().find(&did.node).map(|x| *x)
3785                             == Some(DefTyParamBinder(item_id))
3786                     } => {
3787                       // ok
3788                     }
3789
3790                     DefSelfTy(did) if {
3791                         did == item_id
3792                     } => {
3793                       // ok
3794                     }
3795
3796                     _ => {
3797                     if !is_ty_param {
3798                         // This was an attempt to access an upvar inside a
3799                         // named function item. This is not allowed, so we
3800                         // report an error.
3801
3802                         self.resolve_error(
3803                             span,
3804                             "can't capture dynamic environment in a fn item; \
3805                             use the || { ... } closure form instead");
3806                     } else {
3807                         // This was an attempt to use a type parameter outside
3808                         // its scope.
3809
3810                         self.resolve_error(span,
3811                                               "can't use type parameters from \
3812                                               outer function; try using a local \
3813                                               type parameter instead");
3814                     }
3815
3816                     return None;
3817                     }
3818                   }
3819                 }
3820                 ItemRibKind => {
3821                     if !is_ty_param {
3822                         // This was an attempt to access an upvar inside a
3823                         // named function item. This is not allowed, so we
3824                         // report an error.
3825
3826                         self.resolve_error(
3827                             span,
3828                             "can't capture dynamic environment in a fn item; \
3829                             use the || { ... } closure form instead");
3830                     } else {
3831                         // This was an attempt to use a type parameter outside
3832                         // its scope.
3833
3834                         self.resolve_error(span,
3835                                               "can't use type parameters from \
3836                                               outer function; try using a local \
3837                                               type parameter instead");
3838                     }
3839
3840                     return None;
3841                 }
3842                 ConstantItemRibKind => {
3843                     if is_ty_param {
3844                         // see #9186
3845                         self.resolve_error(span,
3846                                               "cannot use an outer type \
3847                                                parameter in this context");
3848                     } else {
3849                         // Still doesn't deal with upvars
3850                         self.resolve_error(span,
3851                                               "attempt to use a non-constant \
3852                                                value in a constant");
3853                     }
3854
3855                 }
3856             }
3857
3858             rib_index += 1;
3859         }
3860
3861         return Some(DlDef(def));
3862     }
3863
3864     fn search_ribs(&self,
3865                    ribs: &[Rib],
3866                    name: Name,
3867                    span: Span)
3868                    -> Option<DefLike> {
3869         // FIXME #4950: This should not use a while loop.
3870         // FIXME #4950: Try caching?
3871
3872         let mut i = ribs.len();
3873         while i != 0 {
3874             i -= 1;
3875             let binding_opt = ribs[i].bindings.borrow().find_copy(&name);
3876             match binding_opt {
3877                 Some(def_like) => {
3878                     return self.upvarify(ribs, i, def_like, span);
3879                 }
3880                 None => {
3881                     // Continue.
3882                 }
3883             }
3884         }
3885
3886         return None;
3887     }
3888
3889     fn resolve_crate(&mut self, krate: &ast::Crate) {
3890         debug!("(resolving crate) starting");
3891
3892         visit::walk_crate(self, krate, ());
3893     }
3894
3895     fn resolve_item(&mut self, item: &Item) {
3896         debug!("(resolving item) resolving {}",
3897                token::get_ident(item.ident));
3898
3899         match item.node {
3900
3901             // enum item: resolve all the variants' discrs,
3902             // then resolve the ty params
3903             ItemEnum(ref enum_def, ref generics) => {
3904                 for variant in (*enum_def).variants.iter() {
3905                     for dis_expr in variant.node.disr_expr.iter() {
3906                         // resolve the discriminator expr
3907                         // as a constant
3908                         self.with_constant_rib(|this| {
3909                             this.resolve_expr(&**dis_expr);
3910                         });
3911                     }
3912                 }
3913
3914                 // n.b. the discr expr gets visited twice.
3915                 // but maybe it's okay since the first time will signal an
3916                 // error if there is one? -- tjc
3917                 self.with_type_parameter_rib(HasTypeParameters(generics,
3918                                                                TypeSpace,
3919                                                                item.id,
3920                                                                ItemRibKind),
3921                                              |this| {
3922                     this.resolve_type_parameters(&generics.ty_params);
3923                     this.resolve_where_clause(&generics.where_clause);
3924                     visit::walk_item(this, item, ());
3925                 });
3926             }
3927
3928             ItemTy(_, ref generics) => {
3929                 self.with_type_parameter_rib(HasTypeParameters(generics,
3930                                                                TypeSpace,
3931                                                                item.id,
3932                                                                ItemRibKind),
3933                                              |this| {
3934                     this.resolve_type_parameters(&generics.ty_params);
3935                     visit::walk_item(this, item, ());
3936                 });
3937             }
3938
3939             ItemImpl(ref generics,
3940                      ref implemented_traits,
3941                      ref self_type,
3942                      ref impl_items) => {
3943                 self.resolve_implementation(item.id,
3944                                             generics,
3945                                             implemented_traits,
3946                                             &**self_type,
3947                                             impl_items.as_slice());
3948             }
3949
3950             ItemTrait(ref generics, ref unbound, ref bounds, ref methods) => {
3951                 // Create a new rib for the self type.
3952                 let self_type_rib = Rib::new(ItemRibKind);
3953
3954                 // plain insert (no renaming, types are not currently hygienic....)
3955                 let name = self.type_self_name;
3956                 self_type_rib.bindings.borrow_mut()
3957                              .insert(name, DlDef(DefSelfTy(item.id)));
3958                 self.type_ribs.borrow_mut().push(self_type_rib);
3959
3960                 // Create a new rib for the trait-wide type parameters.
3961                 self.with_type_parameter_rib(HasTypeParameters(generics,
3962                                                                TypeSpace,
3963                                                                item.id,
3964                                                                NormalRibKind),
3965                                              |this| {
3966                     this.resolve_type_parameters(&generics.ty_params);
3967                     this.resolve_where_clause(&generics.where_clause);
3968
3969                     this.resolve_type_parameter_bounds(item.id, bounds,
3970                                                        TraitDerivation);
3971
3972                     match unbound {
3973                         &Some(ast::TraitTyParamBound(ref tpb)) => {
3974                             this.resolve_trait_reference(item.id, tpb, TraitDerivation);
3975                         }
3976                         _ => {}
3977                     }
3978
3979                     for method in (*methods).iter() {
3980                         // Create a new rib for the method-specific type
3981                         // parameters.
3982                         //
3983                         // FIXME #4951: Do we need a node ID here?
3984
3985                         match *method {
3986                           ast::RequiredMethod(ref ty_m) => {
3987                             this.with_type_parameter_rib
3988                                 (HasTypeParameters(&ty_m.generics,
3989                                                    FnSpace,
3990                                                    item.id,
3991                                         MethodRibKind(item.id, RequiredMethod)),
3992                                  |this| {
3993
3994                                 // Resolve the method-specific type
3995                                 // parameters.
3996                                 this.resolve_type_parameters(
3997                                     &ty_m.generics.ty_params);
3998                                 this.resolve_where_clause(&ty_m.generics
3999                                                                .where_clause);
4000
4001                                 for argument in ty_m.decl.inputs.iter() {
4002                                     this.resolve_type(&*argument.ty);
4003                                 }
4004
4005                                 match ty_m.explicit_self.node {
4006                                     SelfExplicit(ref typ, _) => {
4007                                         this.resolve_type(&**typ)
4008                                     }
4009                                     _ => {}
4010                                 }
4011
4012                                 this.resolve_type(&*ty_m.decl.output);
4013                             });
4014                           }
4015                           ast::ProvidedMethod(ref m) => {
4016                               this.resolve_method(MethodRibKind(item.id,
4017                                                                 ProvidedMethod(m.id)),
4018                                                   &**m)
4019                           }
4020                         }
4021                     }
4022                 });
4023
4024                 self.type_ribs.borrow_mut().pop();
4025             }
4026
4027             ItemStruct(ref struct_def, ref generics) => {
4028                 self.resolve_struct(item.id,
4029                                     generics,
4030                                     struct_def.super_struct,
4031                                     struct_def.fields.as_slice());
4032             }
4033
4034             ItemMod(ref module_) => {
4035                 self.with_scope(Some(item.ident), |this| {
4036                     this.resolve_module(module_, item.span, item.ident,
4037                                         item.id);
4038                 });
4039             }
4040
4041             ItemForeignMod(ref foreign_module) => {
4042                 self.with_scope(Some(item.ident), |this| {
4043                     for foreign_item in foreign_module.items.iter() {
4044                         match foreign_item.node {
4045                             ForeignItemFn(_, ref generics) => {
4046                                 this.with_type_parameter_rib(
4047                                     HasTypeParameters(
4048                                         generics, FnSpace, foreign_item.id,
4049                                         ItemRibKind),
4050                                     |this| visit::walk_foreign_item(this,
4051                                                                 &**foreign_item,
4052                                                                 ()));
4053                             }
4054                             ForeignItemStatic(..) => {
4055                                 visit::walk_foreign_item(this,
4056                                                          &**foreign_item,
4057                                                          ());
4058                             }
4059                         }
4060                     }
4061                 });
4062             }
4063
4064             ItemFn(fn_decl, _, _, ref generics, block) => {
4065                 self.resolve_function(ItemRibKind,
4066                                       Some(fn_decl),
4067                                       HasTypeParameters
4068                                         (generics,
4069                                          FnSpace,
4070                                          item.id,
4071                                          ItemRibKind),
4072                                       block);
4073             }
4074
4075             ItemStatic(..) => {
4076                 self.with_constant_rib(|this| {
4077                     visit::walk_item(this, item, ());
4078                 });
4079             }
4080
4081            ItemMac(..) => {
4082                 // do nothing, these are just around to be encoded
4083            }
4084         }
4085     }
4086
4087     fn with_type_parameter_rib(&mut self,
4088                                type_parameters: TypeParameters,
4089                                f: |&mut Resolver|) {
4090         match type_parameters {
4091             HasTypeParameters(generics, space, node_id,
4092                               rib_kind) => {
4093
4094                 let function_type_rib = Rib::new(rib_kind);
4095
4096                 for (index, type_parameter) in generics.ty_params.iter().enumerate() {
4097                     let ident = type_parameter.ident;
4098                     debug!("with_type_parameter_rib: {} {}", node_id,
4099                            type_parameter.id);
4100                     let def_like = DlDef(DefTyParam(space,
4101                                                     local_def(type_parameter.id),
4102                                                     index));
4103                     // Associate this type parameter with
4104                     // the item that bound it
4105                     self.record_def(type_parameter.id,
4106                                     (DefTyParamBinder(node_id), LastMod(AllPublic)));
4107                     // plain insert (no renaming)
4108                     function_type_rib.bindings.borrow_mut()
4109                                      .insert(ident.name, def_like);
4110                 }
4111                 self.type_ribs.borrow_mut().push(function_type_rib);
4112             }
4113
4114             NoTypeParameters => {
4115                 // Nothing to do.
4116             }
4117         }
4118
4119         f(self);
4120
4121         match type_parameters {
4122             HasTypeParameters(..) => { self.type_ribs.borrow_mut().pop(); }
4123             NoTypeParameters => { }
4124         }
4125     }
4126
4127     fn with_label_rib(&mut self, f: |&mut Resolver|) {
4128         self.label_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4129         f(self);
4130         self.label_ribs.borrow_mut().pop();
4131     }
4132
4133     fn with_constant_rib(&mut self, f: |&mut Resolver|) {
4134         self.value_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind));
4135         self.type_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind));
4136         f(self);
4137         self.type_ribs.borrow_mut().pop();
4138         self.value_ribs.borrow_mut().pop();
4139     }
4140
4141     fn resolve_function(&mut self,
4142                         rib_kind: RibKind,
4143                         optional_declaration: Option<P<FnDecl>>,
4144                         type_parameters: TypeParameters,
4145                         block: P<Block>) {
4146         // Create a value rib for the function.
4147         let function_value_rib = Rib::new(rib_kind);
4148         self.value_ribs.borrow_mut().push(function_value_rib);
4149
4150         // Create a label rib for the function.
4151         let function_label_rib = Rib::new(rib_kind);
4152         self.label_ribs.borrow_mut().push(function_label_rib);
4153
4154         // If this function has type parameters, add them now.
4155         self.with_type_parameter_rib(type_parameters, |this| {
4156             // Resolve the type parameters.
4157             match type_parameters {
4158                 NoTypeParameters => {
4159                     // Continue.
4160                 }
4161                 HasTypeParameters(ref generics, _, _, _) => {
4162                     this.resolve_type_parameters(&generics.ty_params);
4163                     this.resolve_where_clause(&generics.where_clause);
4164                 }
4165             }
4166
4167             // Add each argument to the rib.
4168             match optional_declaration {
4169                 None => {
4170                     // Nothing to do.
4171                 }
4172                 Some(declaration) => {
4173                     for argument in declaration.inputs.iter() {
4174                         let mut bindings_list = HashMap::new();
4175                         this.resolve_pattern(&*argument.pat,
4176                                              ArgumentIrrefutableMode,
4177                                              &mut bindings_list);
4178
4179                         this.resolve_type(&*argument.ty);
4180
4181                         debug!("(resolving function) recorded argument");
4182                     }
4183
4184                     this.resolve_type(&*declaration.output);
4185                 }
4186             }
4187
4188             // Resolve the function body.
4189             this.resolve_block(&*block);
4190
4191             debug!("(resolving function) leaving function");
4192         });
4193
4194         self.label_ribs.borrow_mut().pop();
4195         self.value_ribs.borrow_mut().pop();
4196     }
4197
4198     fn resolve_type_parameters(&mut self,
4199                                type_parameters: &OwnedSlice<TyParam>) {
4200         for type_parameter in type_parameters.iter() {
4201             for bound in type_parameter.bounds.iter() {
4202                 self.resolve_type_parameter_bound(type_parameter.id, bound,
4203                                                   TraitBoundingTypeParameter);
4204             }
4205             match &type_parameter.unbound {
4206                 &Some(ref unbound) =>
4207                     self.resolve_type_parameter_bound(
4208                         type_parameter.id, unbound, TraitBoundingTypeParameter),
4209                 &None => {}
4210             }
4211             match type_parameter.default {
4212                 Some(ref ty) => self.resolve_type(&**ty),
4213                 None => {}
4214             }
4215         }
4216     }
4217
4218     fn resolve_type_parameter_bounds(&mut self,
4219                                      id: NodeId,
4220                                      type_parameter_bounds: &OwnedSlice<TyParamBound>,
4221                                      reference_type: TraitReferenceType) {
4222         for type_parameter_bound in type_parameter_bounds.iter() {
4223             self.resolve_type_parameter_bound(id, type_parameter_bound,
4224                                               reference_type);
4225         }
4226     }
4227
4228     fn resolve_type_parameter_bound(&mut self,
4229                                     id: NodeId,
4230                                     type_parameter_bound: &TyParamBound,
4231                                     reference_type: TraitReferenceType) {
4232         match *type_parameter_bound {
4233             TraitTyParamBound(ref tref) => {
4234                 self.resolve_trait_reference(id, tref, reference_type)
4235             }
4236             UnboxedFnTyParamBound(ref unboxed_function) => {
4237                 for argument in unboxed_function.decl.inputs.iter() {
4238                     self.resolve_type(&*argument.ty);
4239                 }
4240
4241                 self.resolve_type(&*unboxed_function.decl.output);
4242             }
4243             RegionTyParamBound(..) => {}
4244         }
4245     }
4246
4247     fn resolve_trait_reference(&mut self,
4248                                id: NodeId,
4249                                trait_reference: &TraitRef,
4250                                reference_type: TraitReferenceType) {
4251         match self.resolve_path(id, &trait_reference.path, TypeNS, true) {
4252             None => {
4253                 let path_str = self.path_idents_to_string(&trait_reference.path);
4254                 let usage_str = match reference_type {
4255                     TraitBoundingTypeParameter => "bound type parameter with",
4256                     TraitImplementation        => "implement",
4257                     TraitDerivation            => "derive",
4258                 };
4259
4260                 let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str);
4261                 self.resolve_error(trait_reference.path.span, msg.as_slice());
4262             }
4263             Some(def) => {
4264                 match def {
4265                     (DefTrait(_), _) => {
4266                         debug!("(resolving trait) found trait def: {:?}", def);
4267                         self.record_def(trait_reference.ref_id, def);
4268                     }
4269                     (def, _) => {
4270                         self.resolve_error(trait_reference.path.span,
4271                                            format!("`{}` is not a trait",
4272                                                    self.path_idents_to_string(
4273                                                         &trait_reference.path)));
4274
4275                         // If it's a typedef, give a note
4276                         match def {
4277                             DefTy(_) => {
4278                                 self.session.span_note(
4279                                                 trait_reference.path.span,
4280                                                 format!("`type` aliases cannot \
4281                                                         be used for traits")
4282                                                         .as_slice());
4283                             }
4284                             _ => {}
4285                         }
4286                     }
4287                 }
4288
4289             }
4290         }
4291     }
4292
4293     fn resolve_where_clause(&mut self, where_clause: &ast::WhereClause) {
4294         for predicate in where_clause.predicates.iter() {
4295             match self.resolve_identifier(predicate.ident,
4296                                           TypeNS,
4297                                           true,
4298                                           predicate.span) {
4299                 Some((def @ DefTyParam(_, _, _), last_private)) => {
4300                     self.record_def(predicate.id, (def, last_private));
4301                 }
4302                 _ => {
4303                     self.resolve_error(
4304                         predicate.span,
4305                         format!("undeclared type parameter `{}`",
4306                                 token::get_ident(
4307                                     predicate.ident)).as_slice());
4308                 }
4309             }
4310
4311             for bound in predicate.bounds.iter() {
4312                 self.resolve_type_parameter_bound(predicate.id, bound,
4313                                                   TraitBoundingTypeParameter);
4314             }
4315         }
4316     }
4317
4318     fn resolve_struct(&mut self,
4319                       id: NodeId,
4320                       generics: &Generics,
4321                       super_struct: Option<P<Ty>>,
4322                       fields: &[StructField]) {
4323         // If applicable, create a rib for the type parameters.
4324         self.with_type_parameter_rib(HasTypeParameters(generics,
4325                                                        TypeSpace,
4326                                                        id,
4327                                                        ItemRibKind),
4328                                      |this| {
4329             // Resolve the type parameters.
4330             this.resolve_type_parameters(&generics.ty_params);
4331             this.resolve_where_clause(&generics.where_clause);
4332
4333             // Resolve the super struct.
4334             match super_struct {
4335                 Some(t) => match t.node {
4336                     TyPath(ref path, None, path_id) => {
4337                         match this.resolve_path(id, path, TypeNS, true) {
4338                             Some((DefTy(def_id), lp)) if this.structs.contains_key(&def_id) => {
4339                                 let def = DefStruct(def_id);
4340                                 debug!("(resolving struct) resolved `{}` to type {:?}",
4341                                        token::get_ident(path.segments
4342                                                             .last().unwrap()
4343                                                             .identifier),
4344                                        def);
4345                                 debug!("(resolving struct) writing resolution for `{}` (id {})",
4346                                        this.path_idents_to_string(path),
4347                                        path_id);
4348                                 this.record_def(path_id, (def, lp));
4349                             }
4350                             Some((DefStruct(_), _)) => {
4351                                 span_err!(this.session, t.span, E0154,
4352                                     "super-struct is defined in a different crate");
4353                             },
4354                             Some(_) => {
4355                                 span_err!(this.session, t.span, E0155,
4356                                     "super-struct is not a struct type");
4357                             }
4358                             None => {
4359                                 span_err!(this.session, t.span, E0156,
4360                                     "super-struct could not be resolved");
4361                             }
4362                         }
4363                     },
4364                     _ => this.session.span_bug(t.span, "path not mapped to a TyPath")
4365                 },
4366                 None => {}
4367             }
4368
4369             // Resolve fields.
4370             for field in fields.iter() {
4371                 this.resolve_type(&*field.node.ty);
4372             }
4373         });
4374     }
4375
4376     // Does this really need to take a RibKind or is it always going
4377     // to be NormalRibKind?
4378     fn resolve_method(&mut self,
4379                       rib_kind: RibKind,
4380                       method: &Method) {
4381         let method_generics = method.pe_generics();
4382         let type_parameters = HasTypeParameters(method_generics,
4383                                                 FnSpace,
4384                                                 method.id,
4385                                                 rib_kind);
4386
4387         match method.pe_explicit_self().node {
4388             SelfExplicit(ref typ, _) => self.resolve_type(&**typ),
4389             _ => {}
4390         }
4391
4392         self.resolve_function(rib_kind,
4393                               Some(method.pe_fn_decl()),
4394                               type_parameters,
4395                               method.pe_body());
4396     }
4397
4398     fn with_current_self_type<T>(&mut self, self_type: &Ty, f: |&mut Resolver| -> T) -> T {
4399         // Handle nested impls (inside fn bodies)
4400         let previous_value = replace(&mut self.current_self_type, Some(self_type.clone()));
4401         let result = f(self);
4402         self.current_self_type = previous_value;
4403         result
4404     }
4405
4406     fn with_optional_trait_ref<T>(&mut self, id: NodeId,
4407                                   opt_trait_ref: &Option<TraitRef>,
4408                                   f: |&mut Resolver| -> T) -> T {
4409         let new_val = match *opt_trait_ref {
4410             Some(ref trait_ref) => {
4411                 self.resolve_trait_reference(id, trait_ref, TraitImplementation);
4412
4413                 match self.def_map.borrow().find(&trait_ref.ref_id) {
4414                     Some(def) => {
4415                         let did = def.def_id();
4416                         Some((did, trait_ref.clone()))
4417                     }
4418                     None => None
4419                 }
4420             }
4421             None => None
4422         };
4423         let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
4424         let result = f(self);
4425         self.current_trait_ref = original_trait_ref;
4426         result
4427     }
4428
4429     fn resolve_implementation(&mut self,
4430                               id: NodeId,
4431                               generics: &Generics,
4432                               opt_trait_reference: &Option<TraitRef>,
4433                               self_type: &Ty,
4434                               impl_items: &[ImplItem]) {
4435         // If applicable, create a rib for the type parameters.
4436         self.with_type_parameter_rib(HasTypeParameters(generics,
4437                                                        TypeSpace,
4438                                                        id,
4439                                                        NormalRibKind),
4440                                      |this| {
4441             // Resolve the type parameters.
4442             this.resolve_type_parameters(&generics.ty_params);
4443             this.resolve_where_clause(&generics.where_clause);
4444
4445             // Resolve the trait reference, if necessary.
4446             this.with_optional_trait_ref(id, opt_trait_reference, |this| {
4447                 // Resolve the self type.
4448                 this.resolve_type(self_type);
4449
4450                 this.with_current_self_type(self_type, |this| {
4451                     for impl_item in impl_items.iter() {
4452                         match *impl_item {
4453                             MethodImplItem(method) => {
4454                                 // If this is a trait impl, ensure the method
4455                                 // exists in trait
4456                                 this.check_trait_item(method.pe_ident(),
4457                                                       method.span);
4458
4459                                 // We also need a new scope for the method-
4460                                 // specific type parameters.
4461                                 this.resolve_method(
4462                                     MethodRibKind(id,
4463                                                   ProvidedMethod(method.id)),
4464                                     &*method);
4465                             }
4466                         }
4467                     }
4468                 });
4469             });
4470         });
4471     }
4472
4473     fn check_trait_item(&self, ident: Ident, span: Span) {
4474         // If there is a TraitRef in scope for an impl, then the method must be in the trait.
4475         for &(did, ref trait_ref) in self.current_trait_ref.iter() {
4476             let method_name = ident.name;
4477
4478             if self.trait_item_map.borrow().find(&(method_name, did)).is_none() {
4479                 let path_str = self.path_idents_to_string(&trait_ref.path);
4480                 self.resolve_error(span,
4481                                     format!("method `{}` is not a member of trait `{}`",
4482                                             token::get_name(method_name),
4483                                             path_str).as_slice());
4484             }
4485         }
4486     }
4487
4488     fn resolve_module(&mut self, module: &Mod, _span: Span,
4489                       _name: Ident, id: NodeId) {
4490         // Write the implementations in scope into the module metadata.
4491         debug!("(resolving module) resolving module ID {}", id);
4492         visit::walk_mod(self, module, ());
4493     }
4494
4495     fn resolve_local(&mut self, local: &Local) {
4496         // Resolve the type.
4497         self.resolve_type(&*local.ty);
4498
4499         // Resolve the initializer, if necessary.
4500         match local.init {
4501             None => {
4502                 // Nothing to do.
4503             }
4504             Some(ref initializer) => {
4505                 self.resolve_expr(&**initializer);
4506             }
4507         }
4508
4509         // Resolve the pattern.
4510         let mut bindings_list = HashMap::new();
4511         self.resolve_pattern(&*local.pat,
4512                              LocalIrrefutableMode,
4513                              &mut bindings_list);
4514     }
4515
4516     // build a map from pattern identifiers to binding-info's.
4517     // this is done hygienically. This could arise for a macro
4518     // that expands into an or-pattern where one 'x' was from the
4519     // user and one 'x' came from the macro.
4520     fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap {
4521         let mut result = HashMap::new();
4522         pat_bindings(&self.def_map, pat, |binding_mode, _id, sp, path1| {
4523             let name = mtwt::resolve(path1.node);
4524             result.insert(name,
4525                           binding_info {span: sp,
4526                                         binding_mode: binding_mode});
4527         });
4528         return result;
4529     }
4530
4531     // check that all of the arms in an or-pattern have exactly the
4532     // same set of bindings, with the same binding modes for each.
4533     fn check_consistent_bindings(&mut self, arm: &Arm) {
4534         if arm.pats.len() == 0 {
4535             return
4536         }
4537         let map_0 = self.binding_mode_map(&**arm.pats.get(0));
4538         for (i, p) in arm.pats.iter().enumerate() {
4539             let map_i = self.binding_mode_map(&**p);
4540
4541             for (&key, &binding_0) in map_0.iter() {
4542                 match map_i.find(&key) {
4543                   None => {
4544                     self.resolve_error(
4545                         p.span,
4546                         format!("variable `{}` from pattern #1 is \
4547                                   not bound in pattern #{}",
4548                                 token::get_name(key),
4549                                 i + 1).as_slice());
4550                   }
4551                   Some(binding_i) => {
4552                     if binding_0.binding_mode != binding_i.binding_mode {
4553                         self.resolve_error(
4554                             binding_i.span,
4555                             format!("variable `{}` is bound with different \
4556                                       mode in pattern #{} than in pattern #1",
4557                                     token::get_name(key),
4558                                     i + 1).as_slice());
4559                     }
4560                   }
4561                 }
4562             }
4563
4564             for (&key, &binding) in map_i.iter() {
4565                 if !map_0.contains_key(&key) {
4566                     self.resolve_error(
4567                         binding.span,
4568                         format!("variable `{}` from pattern {}{} is \
4569                                   not bound in pattern {}1",
4570                                 token::get_name(key),
4571                                 "#", i + 1, "#").as_slice());
4572                 }
4573             }
4574         }
4575     }
4576
4577     fn resolve_arm(&mut self, arm: &Arm) {
4578         self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4579
4580         let mut bindings_list = HashMap::new();
4581         for pattern in arm.pats.iter() {
4582             self.resolve_pattern(&**pattern, RefutableMode, &mut bindings_list);
4583         }
4584
4585         // This has to happen *after* we determine which
4586         // pat_idents are variants
4587         self.check_consistent_bindings(arm);
4588
4589         visit::walk_expr_opt(self, arm.guard, ());
4590         self.resolve_expr(&*arm.body);
4591
4592         self.value_ribs.borrow_mut().pop();
4593     }
4594
4595     fn resolve_block(&mut self, block: &Block) {
4596         debug!("(resolving block) entering block");
4597         self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4598
4599         // Move down in the graph, if there's an anonymous module rooted here.
4600         let orig_module = self.current_module.clone();
4601         match orig_module.anonymous_children.borrow().find(&block.id) {
4602             None => { /* Nothing to do. */ }
4603             Some(anonymous_module) => {
4604                 debug!("(resolving block) found anonymous module, moving \
4605                         down");
4606                 self.current_module = anonymous_module.clone();
4607             }
4608         }
4609
4610         // Descend into the block.
4611         visit::walk_block(self, block, ());
4612
4613         // Move back up.
4614         self.current_module = orig_module;
4615
4616         self.value_ribs.borrow_mut().pop();
4617         debug!("(resolving block) leaving block");
4618     }
4619
4620     fn resolve_type(&mut self, ty: &Ty) {
4621         match ty.node {
4622             // Like path expressions, the interpretation of path types depends
4623             // on whether the path has multiple elements in it or not.
4624
4625             TyPath(ref path, ref bounds, path_id) => {
4626                 // This is a path in the type namespace. Walk through scopes
4627                 // looking for it.
4628                 let mut result_def = None;
4629
4630                 // First, check to see whether the name is a primitive type.
4631                 if path.segments.len() == 1 {
4632                     let id = path.segments.last().unwrap().identifier;
4633
4634                     match self.primitive_type_table
4635                             .primitive_types
4636                             .find(&id.name) {
4637
4638                         Some(&primitive_type) => {
4639                             result_def =
4640                                 Some((DefPrimTy(primitive_type), LastMod(AllPublic)));
4641
4642                             if path.segments
4643                                    .iter()
4644                                    .any(|s| !s.lifetimes.is_empty()) {
4645                                 span_err!(self.session, path.span, E0157,
4646                                     "lifetime parameters are not allowed on this type");
4647                             } else if path.segments
4648                                           .iter()
4649                                           .any(|s| s.types.len() > 0) {
4650                                 span_err!(self.session, path.span, E0153,
4651                                     "type parameters are not allowed on this type");
4652                             }
4653                         }
4654                         None => {
4655                             // Continue.
4656                         }
4657                     }
4658                 }
4659
4660                 match result_def {
4661                     None => {
4662                         match self.resolve_path(ty.id, path, TypeNS, true) {
4663                             Some(def) => {
4664                                 debug!("(resolving type) resolved `{}` to \
4665                                         type {:?}",
4666                                        token::get_ident(path.segments
4667                                                             .last().unwrap()
4668                                                             .identifier),
4669                                        def);
4670                                 result_def = Some(def);
4671                             }
4672                             None => {
4673                                 result_def = None;
4674                             }
4675                         }
4676                     }
4677                     Some(_) => {}   // Continue.
4678                 }
4679
4680                 match result_def {
4681                     Some(def) => {
4682                         // Write the result into the def map.
4683                         debug!("(resolving type) writing resolution for `{}` \
4684                                 (id {})",
4685                                self.path_idents_to_string(path),
4686                                path_id);
4687                         self.record_def(path_id, def);
4688                     }
4689                     None => {
4690                         let msg = format!("use of undeclared type name `{}`",
4691                                           self.path_idents_to_string(path));
4692                         self.resolve_error(ty.span, msg.as_slice());
4693                     }
4694                 }
4695
4696                 bounds.as_ref().map(|bound_vec| {
4697                     self.resolve_type_parameter_bounds(ty.id, bound_vec,
4698                                                        TraitBoundingTypeParameter);
4699                 });
4700             }
4701
4702             TyClosure(c) | TyProc(c) => {
4703                 self.resolve_type_parameter_bounds(ty.id, &c.bounds,
4704                                                    TraitBoundingTypeParameter);
4705                 visit::walk_ty(self, ty, ());
4706             }
4707
4708             _ => {
4709                 // Just resolve embedded types.
4710                 visit::walk_ty(self, ty, ());
4711             }
4712         }
4713     }
4714
4715     fn resolve_pattern(&mut self,
4716                        pattern: &Pat,
4717                        mode: PatternBindingMode,
4718                        // Maps idents to the node ID for the (outermost)
4719                        // pattern that binds them
4720                        bindings_list: &mut HashMap<Name,NodeId>) {
4721         let pat_id = pattern.id;
4722         walk_pat(pattern, |pattern| {
4723             match pattern.node {
4724                 PatIdent(binding_mode, ref path1, _) => {
4725
4726                     // The meaning of pat_ident with no type parameters
4727                     // depends on whether an enum variant or unit-like struct
4728                     // with that name is in scope. The probing lookup has to
4729                     // be careful not to emit spurious errors. Only matching
4730                     // patterns (match) can match nullary variants or
4731                     // unit-like structs. For binding patterns (let), matching
4732                     // such a value is simply disallowed (since it's rarely
4733                     // what you want).
4734
4735                     let ident = path1.node;
4736                     let renamed = mtwt::resolve(ident);
4737
4738                     match self.resolve_bare_identifier_pattern(ident, pattern.span) {
4739                         FoundStructOrEnumVariant(def, lp)
4740                                 if mode == RefutableMode => {
4741                             debug!("(resolving pattern) resolving `{}` to \
4742                                     struct or enum variant",
4743                                    token::get_name(renamed));
4744
4745                             self.enforce_default_binding_mode(
4746                                 pattern,
4747                                 binding_mode,
4748                                 "an enum variant");
4749                             self.record_def(pattern.id, (def, lp));
4750                         }
4751                         FoundStructOrEnumVariant(..) => {
4752                             self.resolve_error(
4753                                 pattern.span,
4754                                 format!("declaration of `{}` shadows an enum \
4755                                          variant or unit-like struct in \
4756                                          scope",
4757                                         token::get_name(renamed)).as_slice());
4758                         }
4759                         FoundConst(def, lp) if mode == RefutableMode => {
4760                             debug!("(resolving pattern) resolving `{}` to \
4761                                     constant",
4762                                    token::get_name(renamed));
4763
4764                             self.enforce_default_binding_mode(
4765                                 pattern,
4766                                 binding_mode,
4767                                 "a constant");
4768                             self.record_def(pattern.id, (def, lp));
4769                         }
4770                         FoundConst(..) => {
4771                             self.resolve_error(pattern.span,
4772                                                   "only irrefutable patterns \
4773                                                    allowed here");
4774                         }
4775                         BareIdentifierPatternUnresolved => {
4776                             debug!("(resolving pattern) binding `{}`",
4777                                    token::get_name(renamed));
4778
4779                             let def = match mode {
4780                                 RefutableMode => {
4781                                     // For pattern arms, we must use
4782                                     // `def_binding` definitions.
4783
4784                                     DefBinding(pattern.id, binding_mode)
4785                                 }
4786                                 LocalIrrefutableMode => {
4787                                     // But for locals, we use `def_local`.
4788                                     DefLocal(pattern.id, binding_mode)
4789                                 }
4790                                 ArgumentIrrefutableMode => {
4791                                     // And for function arguments, `def_arg`.
4792                                     DefArg(pattern.id, binding_mode)
4793                                 }
4794                             };
4795
4796                             // Record the definition so that later passes
4797                             // will be able to distinguish variants from
4798                             // locals in patterns.
4799
4800                             self.record_def(pattern.id, (def, LastMod(AllPublic)));
4801
4802                             // Add the binding to the local ribs, if it
4803                             // doesn't already exist in the bindings list. (We
4804                             // must not add it if it's in the bindings list
4805                             // because that breaks the assumptions later
4806                             // passes make about or-patterns.)
4807
4808                             if !bindings_list.contains_key(&renamed) {
4809                                 let this = &mut *self;
4810                                 let value_ribs = this.value_ribs.borrow();
4811                                 let length = value_ribs.len();
4812                                 let last_rib = value_ribs.get(
4813                                     length - 1);
4814                                 last_rib.bindings.borrow_mut()
4815                                         .insert(renamed, DlDef(def));
4816                                 bindings_list.insert(renamed, pat_id);
4817                             } else if bindings_list.find(&renamed) ==
4818                                     Some(&pat_id) {
4819                                 // Then this is a duplicate variable in the
4820                                 // same disjunction, which is an error.
4821                                 self.resolve_error(pattern.span,
4822                                     format!("identifier `{}` is bound \
4823                                              more than once in the same \
4824                                              pattern",
4825                                             token::get_ident(ident)).as_slice());
4826                             }
4827                             // Else, not bound in the same pattern: do
4828                             // nothing.
4829                         }
4830                     }
4831                 }
4832
4833                 PatEnum(ref path, _) => {
4834                     // This must be an enum variant, struct or const.
4835                     match self.resolve_path(pat_id, path, ValueNS, false) {
4836                         Some(def @ (DefFn(..), _))      |
4837                         Some(def @ (DefVariant(..), _)) |
4838                         Some(def @ (DefStruct(..), _))  |
4839                         Some(def @ (DefStatic(..), _)) => {
4840                             self.record_def(pattern.id, def);
4841                         }
4842                         Some(_) => {
4843                             self.resolve_error(path.span,
4844                                 format!("`{}` is not an enum variant, struct or const",
4845                                     token::get_ident(
4846                                         path.segments
4847                                             .last()
4848                                             .unwrap()
4849                                             .identifier)).as_slice());
4850                         }
4851                         None => {
4852                             self.resolve_error(path.span,
4853                                 format!("unresolved enum variant, struct or const `{}`",
4854                                     token::get_ident(
4855                                         path.segments
4856                                             .last()
4857                                             .unwrap()
4858                                             .identifier)).as_slice());
4859                         }
4860                     }
4861
4862                     // Check the types in the path pattern.
4863                     for ty in path.segments
4864                                   .iter()
4865                                   .flat_map(|s| s.types.iter()) {
4866                         self.resolve_type(&**ty);
4867                     }
4868                 }
4869
4870                 PatLit(ref expr) => {
4871                     self.resolve_expr(&**expr);
4872                 }
4873
4874                 PatRange(ref first_expr, ref last_expr) => {
4875                     self.resolve_expr(&**first_expr);
4876                     self.resolve_expr(&**last_expr);
4877                 }
4878
4879                 PatStruct(ref path, _, _) => {
4880                     match self.resolve_path(pat_id, path, TypeNS, false) {
4881                         Some(definition) => {
4882                             self.record_def(pattern.id, definition);
4883                         }
4884                         result => {
4885                             debug!("(resolving pattern) didn't find struct \
4886                                     def: {:?}", result);
4887                             let msg = format!("`{}` does not name a structure",
4888                                               self.path_idents_to_string(path));
4889                             self.resolve_error(path.span, msg.as_slice());
4890                         }
4891                     }
4892                 }
4893
4894                 _ => {
4895                     // Nothing to do.
4896                 }
4897             }
4898             true
4899         });
4900     }
4901
4902     fn resolve_bare_identifier_pattern(&mut self, name: Ident, span: Span)
4903                                        -> BareIdentifierPatternResolution {
4904         let module = self.current_module.clone();
4905         match self.resolve_item_in_lexical_scope(module,
4906                                                  name,
4907                                                  ValueNS) {
4908             Success((target, _)) => {
4909                 debug!("(resolve bare identifier pattern) succeeded in \
4910                          finding {} at {:?}",
4911                         token::get_ident(name),
4912                         target.bindings.value_def.borrow());
4913                 match *target.bindings.value_def.borrow() {
4914                     None => {
4915                         fail!("resolved name in the value namespace to a \
4916                               set of name bindings with no def?!");
4917                     }
4918                     Some(def) => {
4919                         // For the two success cases, this lookup can be
4920                         // considered as not having a private component because
4921                         // the lookup happened only within the current module.
4922                         match def.def {
4923                             def @ DefVariant(..) | def @ DefStruct(..) => {
4924                                 return FoundStructOrEnumVariant(def, LastMod(AllPublic));
4925                             }
4926                             def @ DefStatic(_, false) => {
4927                                 return FoundConst(def, LastMod(AllPublic));
4928                             }
4929                             DefStatic(_, true) => {
4930                                 self.resolve_error(span,
4931                                     "mutable static variables cannot be referenced in a pattern");
4932                                 return BareIdentifierPatternUnresolved;
4933                             }
4934                             _ => {
4935                                 return BareIdentifierPatternUnresolved;
4936                             }
4937                         }
4938                     }
4939                 }
4940             }
4941
4942             Indeterminate => {
4943                 fail!("unexpected indeterminate result");
4944             }
4945             Failed(err) => {
4946                 match err {
4947                     Some((span, msg)) => {
4948                         self.resolve_error(span, format!("failed to resolve: {}",
4949                                                          msg));
4950                     }
4951                     None => ()
4952                 }
4953
4954                 debug!("(resolve bare identifier pattern) failed to find {}",
4955                         token::get_ident(name));
4956                 return BareIdentifierPatternUnresolved;
4957             }
4958         }
4959     }
4960
4961     /// If `check_ribs` is true, checks the local definitions first; i.e.
4962     /// doesn't skip straight to the containing module.
4963     fn resolve_path(&mut self,
4964                     id: NodeId,
4965                     path: &Path,
4966                     namespace: Namespace,
4967                     check_ribs: bool) -> Option<(Def, LastPrivate)> {
4968         // First, resolve the types.
4969         for ty in path.segments.iter().flat_map(|s| s.types.iter()) {
4970             self.resolve_type(&**ty);
4971         }
4972
4973         if path.global {
4974             return self.resolve_crate_relative_path(path, namespace);
4975         }
4976
4977         let unqualified_def =
4978                 self.resolve_identifier(path.segments
4979                                             .last().unwrap()
4980                                             .identifier,
4981                                         namespace,
4982                                         check_ribs,
4983                                         path.span);
4984
4985         if path.segments.len() > 1 {
4986             let def = self.resolve_module_relative_path(path, namespace);
4987             match (def, unqualified_def) {
4988                 (Some((d, _)), Some((ud, _))) if d == ud => {
4989                     self.session
4990                         .add_lint(lint::builtin::UNNECESSARY_QUALIFICATION,
4991                                   id,
4992                                   path.span,
4993                                   "unnecessary qualification".to_string());
4994                 }
4995                 _ => ()
4996             }
4997
4998             return def;
4999         }
5000
5001         return unqualified_def;
5002     }
5003
5004     // resolve a single identifier (used as a varref)
5005     fn resolve_identifier(&mut self,
5006                               identifier: Ident,
5007                               namespace: Namespace,
5008                               check_ribs: bool,
5009                               span: Span)
5010                               -> Option<(Def, LastPrivate)> {
5011         if check_ribs {
5012             match self.resolve_identifier_in_local_ribs(identifier,
5013                                                       namespace,
5014                                                       span) {
5015                 Some(def) => {
5016                     return Some((def, LastMod(AllPublic)));
5017                 }
5018                 None => {
5019                     // Continue.
5020                 }
5021             }
5022         }
5023
5024         return self.resolve_item_by_identifier_in_lexical_scope(identifier,
5025                                                                 namespace);
5026     }
5027
5028     // FIXME #4952: Merge me with resolve_name_in_module?
5029     fn resolve_definition_of_name_in_module(&mut self,
5030                                             containing_module: Rc<Module>,
5031                                             name: Name,
5032                                             namespace: Namespace)
5033                                                 -> NameDefinition {
5034         // First, search children.
5035         self.populate_module_if_necessary(&containing_module);
5036
5037         match containing_module.children.borrow().find(&name) {
5038             Some(child_name_bindings) => {
5039                 match child_name_bindings.def_for_namespace(namespace) {
5040                     Some(def) => {
5041                         // Found it. Stop the search here.
5042                         let p = child_name_bindings.defined_in_public_namespace(
5043                                         namespace);
5044                         let lp = if p {LastMod(AllPublic)} else {
5045                             LastMod(DependsOn(def.def_id()))
5046                         };
5047                         return ChildNameDefinition(def, lp);
5048                     }
5049                     None => {}
5050                 }
5051             }
5052             None => {}
5053         }
5054
5055         // Next, search import resolutions.
5056         match containing_module.import_resolutions.borrow().find(&name) {
5057             Some(import_resolution) if import_resolution.is_public => {
5058                 match (*import_resolution).target_for_namespace(namespace) {
5059                     Some(target) => {
5060                         match target.bindings.def_for_namespace(namespace) {
5061                             Some(def) => {
5062                                 // Found it.
5063                                 let id = import_resolution.id(namespace);
5064                                 self.used_imports.insert((id, namespace));
5065                                 return ImportNameDefinition(def, LastMod(AllPublic));
5066                             }
5067                             None => {
5068                                 // This can happen with external impls, due to
5069                                 // the imperfect way we read the metadata.
5070                             }
5071                         }
5072                     }
5073                     None => {}
5074                 }
5075             }
5076             Some(..) | None => {} // Continue.
5077         }
5078
5079         // Finally, search through external children.
5080         if namespace == TypeNS {
5081             match containing_module.external_module_children.borrow()
5082                                    .find_copy(&name) {
5083                 None => {}
5084                 Some(module) => {
5085                     match module.def_id.get() {
5086                         None => {} // Continue.
5087                         Some(def_id) => {
5088                             let lp = if module.is_public {LastMod(AllPublic)} else {
5089                                 LastMod(DependsOn(def_id))
5090                             };
5091                             return ChildNameDefinition(DefMod(def_id), lp);
5092                         }
5093                     }
5094                 }
5095             }
5096         }
5097
5098         return NoNameDefinition;
5099     }
5100
5101     // resolve a "module-relative" path, e.g. a::b::c
5102     fn resolve_module_relative_path(&mut self,
5103                                         path: &Path,
5104                                         namespace: Namespace)
5105                                         -> Option<(Def, LastPrivate)> {
5106         let module_path_idents = path.segments.init().iter()
5107                                                      .map(|ps| ps.identifier)
5108                                                      .collect::<Vec<_>>();
5109
5110         let containing_module;
5111         let last_private;
5112         let module = self.current_module.clone();
5113         match self.resolve_module_path(module,
5114                                        module_path_idents.as_slice(),
5115                                        UseLexicalScope,
5116                                        path.span,
5117                                        PathSearch) {
5118             Failed(err) => {
5119                 let (span, msg) = match err {
5120                     Some((span, msg)) => (span, msg),
5121                     None => {
5122                         let msg = format!("Use of undeclared module `{}`",
5123                                           self.idents_to_string(
5124                                                module_path_idents.as_slice()));
5125                         (path.span, msg)
5126                     }
5127                 };
5128
5129                 self.resolve_error(span, format!("failed to resolve. {}",
5130                                                  msg.as_slice()));
5131                 return None;
5132             }
5133             Indeterminate => fail!("indeterminate unexpected"),
5134             Success((resulting_module, resulting_last_private)) => {
5135                 containing_module = resulting_module;
5136                 last_private = resulting_last_private;
5137             }
5138         }
5139
5140         let ident = path.segments.last().unwrap().identifier;
5141         let def = match self.resolve_definition_of_name_in_module(containing_module.clone(),
5142                                                         ident.name,
5143                                                         namespace) {
5144             NoNameDefinition => {
5145                 // We failed to resolve the name. Report an error.
5146                 return None;
5147             }
5148             ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
5149                 (def, last_private.or(lp))
5150             }
5151         };
5152         match containing_module.kind.get() {
5153             TraitModuleKind | ImplModuleKind => {
5154                 match containing_module.def_id.get() {
5155                     Some(def_id) => {
5156                         match self.trait_item_map.borrow().find(&(ident.name, def_id)) {
5157                             Some(&StaticMethodTraitItemKind) => (),
5158                             None => (),
5159                             _ => {
5160                                 debug!("containing module was a trait or impl \
5161                                 and name was a method -> not resolved");
5162                                 return None;
5163                             }
5164                         }
5165                     },
5166                     _ => (),
5167                 }
5168             },
5169             _ => (),
5170         }
5171         return Some(def);
5172     }
5173
5174     /// Invariant: This must be called only during main resolution, not during
5175     /// import resolution.
5176     fn resolve_crate_relative_path(&mut self,
5177                                    path: &Path,
5178                                    namespace: Namespace)
5179                                        -> Option<(Def, LastPrivate)> {
5180         let module_path_idents = path.segments.init().iter()
5181                                                      .map(|ps| ps.identifier)
5182                                                      .collect::<Vec<_>>();
5183
5184         let root_module = self.graph_root.get_module();
5185
5186         let containing_module;
5187         let last_private;
5188         match self.resolve_module_path_from_root(root_module,
5189                                                  module_path_idents.as_slice(),
5190                                                  0,
5191                                                  path.span,
5192                                                  PathSearch,
5193                                                  LastMod(AllPublic)) {
5194             Failed(err) => {
5195                 let (span, msg) = match err {
5196                     Some((span, msg)) => (span, msg),
5197                     None => {
5198                         let msg = format!("Use of undeclared module `::{}`",
5199                                           self.idents_to_string(
5200                                                module_path_idents.as_slice()));
5201                         (path.span, msg)
5202                     }
5203                 };
5204
5205                 self.resolve_error(span, format!("failed to resolve. {}",
5206                                                  msg.as_slice()));
5207                 return None;
5208             }
5209
5210             Indeterminate => {
5211                 fail!("indeterminate unexpected");
5212             }
5213
5214             Success((resulting_module, resulting_last_private)) => {
5215                 containing_module = resulting_module;
5216                 last_private = resulting_last_private;
5217             }
5218         }
5219
5220         let name = path.segments.last().unwrap().identifier.name;
5221         match self.resolve_definition_of_name_in_module(containing_module,
5222                                                         name,
5223                                                         namespace) {
5224             NoNameDefinition => {
5225                 // We failed to resolve the name. Report an error.
5226                 return None;
5227             }
5228             ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
5229                 return Some((def, last_private.or(lp)));
5230             }
5231         }
5232     }
5233
5234     fn resolve_identifier_in_local_ribs(&mut self,
5235                                             ident: Ident,
5236                                             namespace: Namespace,
5237                                             span: Span)
5238                                             -> Option<Def> {
5239         // Check the local set of ribs.
5240         let search_result = match namespace {
5241             ValueNS => {
5242                 let renamed = mtwt::resolve(ident);
5243                 self.search_ribs(self.value_ribs.borrow().as_slice(),
5244                                  renamed, span)
5245             }
5246             TypeNS => {
5247                 let name = ident.name;
5248                 self.search_ribs(self.type_ribs.borrow().as_slice(), name, span)
5249             }
5250         };
5251
5252         match search_result {
5253             Some(DlDef(def)) => {
5254                 debug!("(resolving path in local ribs) resolved `{}` to \
5255                         local: {:?}",
5256                        token::get_ident(ident),
5257                        def);
5258                 return Some(def);
5259             }
5260             Some(DlField) | Some(DlImpl(_)) | None => {
5261                 return None;
5262             }
5263         }
5264     }
5265
5266     fn resolve_item_by_identifier_in_lexical_scope(&mut self,
5267                                                    ident: Ident,
5268                                                    namespace: Namespace)
5269                                                 -> Option<(Def, LastPrivate)> {
5270         // Check the items.
5271         let module = self.current_module.clone();
5272         match self.resolve_item_in_lexical_scope(module,
5273                                                  ident,
5274                                                  namespace) {
5275             Success((target, _)) => {
5276                 match (*target.bindings).def_for_namespace(namespace) {
5277                     None => {
5278                         // This can happen if we were looking for a type and
5279                         // found a module instead. Modules don't have defs.
5280                         debug!("(resolving item path by identifier in lexical \
5281                                  scope) failed to resolve {} after success...",
5282                                  token::get_ident(ident));
5283                         return None;
5284                     }
5285                     Some(def) => {
5286                         debug!("(resolving item path in lexical scope) \
5287                                 resolved `{}` to item",
5288                                token::get_ident(ident));
5289                         // This lookup is "all public" because it only searched
5290                         // for one identifier in the current module (couldn't
5291                         // have passed through reexports or anything like that.
5292                         return Some((def, LastMod(AllPublic)));
5293                     }
5294                 }
5295             }
5296             Indeterminate => {
5297                 fail!("unexpected indeterminate result");
5298             }
5299             Failed(err) => {
5300                 match err {
5301                     Some((span, msg)) =>
5302                         self.resolve_error(span, format!("failed to resolve. {}", msg)),
5303                     None => ()
5304                 }
5305
5306                 debug!("(resolving item path by identifier in lexical scope) \
5307                          failed to resolve {}", token::get_ident(ident));
5308                 return None;
5309             }
5310         }
5311     }
5312
5313     fn with_no_errors<T>(&mut self, f: |&mut Resolver| -> T) -> T {
5314         self.emit_errors = false;
5315         let rs = f(self);
5316         self.emit_errors = true;
5317         rs
5318     }
5319
5320     fn resolve_error<T: Str>(&self, span: Span, s: T) {
5321         if self.emit_errors {
5322             self.session.span_err(span, s.as_slice());
5323         }
5324     }
5325
5326     fn find_fallback_in_self_type(&mut self, name: Name) -> FallbackSuggestion {
5327         #[deriving(PartialEq)]
5328         enum FallbackChecks {
5329             Everything,
5330             OnlyTraitAndStatics
5331         }
5332
5333         fn extract_path_and_node_id(t: &Ty, allow: FallbackChecks)
5334                                                     -> Option<(Path, NodeId, FallbackChecks)> {
5335             match t.node {
5336                 TyPath(ref path, _, node_id) => Some((path.clone(), node_id, allow)),
5337                 TyPtr(mut_ty) => extract_path_and_node_id(&*mut_ty.ty, OnlyTraitAndStatics),
5338                 TyRptr(_, mut_ty) => extract_path_and_node_id(&*mut_ty.ty, allow),
5339                 // This doesn't handle the remaining `Ty` variants as they are not
5340                 // that commonly the self_type, it might be interesting to provide
5341                 // support for those in future.
5342                 _ => None,
5343             }
5344         }
5345
5346         fn get_module(this: &mut Resolver, span: Span, ident_path: &[ast::Ident])
5347                             -> Option<Rc<Module>> {
5348             let root = this.current_module.clone();
5349             let last_name = ident_path.last().unwrap().name;
5350
5351             if ident_path.len() == 1 {
5352                 match this.primitive_type_table.primitive_types.find(&last_name) {
5353                     Some(_) => None,
5354                     None => {
5355                         match this.current_module.children.borrow().find(&last_name) {
5356                             Some(child) => child.get_module_if_available(),
5357                             None => None
5358                         }
5359                     }
5360                 }
5361             } else {
5362                 match this.resolve_module_path(root,
5363                                                 ident_path.as_slice(),
5364                                                 UseLexicalScope,
5365                                                 span,
5366                                                 PathSearch) {
5367                     Success((module, _)) => Some(module),
5368                     _ => None
5369                 }
5370             }
5371         }
5372
5373         let (path, node_id, allowed) = match self.current_self_type {
5374             Some(ref ty) => match extract_path_and_node_id(ty, Everything) {
5375                 Some(x) => x,
5376                 None => return NoSuggestion,
5377             },
5378             None => return NoSuggestion,
5379         };
5380
5381         if allowed == Everything {
5382             // Look for a field with the same name in the current self_type.
5383             match self.def_map.borrow().find(&node_id) {
5384                  Some(&DefTy(did))
5385                 | Some(&DefStruct(did))
5386                 | Some(&DefVariant(_, did, _)) => match self.structs.find(&did) {
5387                     None => {}
5388                     Some(fields) => {
5389                         if fields.iter().any(|&field_name| name == field_name) {
5390                             return Field;
5391                         }
5392                     }
5393                 },
5394                 _ => {} // Self type didn't resolve properly
5395             }
5396         }
5397
5398         let ident_path = path.segments.iter().map(|seg| seg.identifier).collect::<Vec<_>>();
5399
5400         // Look for a method in the current self type's impl module.
5401         match get_module(self, path.span, ident_path.as_slice()) {
5402             Some(module) => match module.children.borrow().find(&name) {
5403                 Some(binding) => {
5404                     let p_str = self.path_idents_to_string(&path);
5405                     match binding.def_for_namespace(ValueNS) {
5406                         Some(DefStaticMethod(_, provenance, _)) => {
5407                             match provenance {
5408                                 FromImpl(_) => return StaticMethod(p_str),
5409                                 FromTrait(_) => unreachable!()
5410                             }
5411                         }
5412                         Some(DefMethod(_, None)) if allowed == Everything => return Method,
5413                         Some(DefMethod(_, Some(_))) => return TraitItem,
5414                         _ => ()
5415                     }
5416                 }
5417                 None => {}
5418             },
5419             None => {}
5420         }
5421
5422         // Look for a method in the current trait.
5423         let trait_item_map = self.trait_item_map.borrow();
5424         match self.current_trait_ref {
5425             Some((did, ref trait_ref)) => {
5426                 let path_str = self.path_idents_to_string(&trait_ref.path);
5427
5428                 match trait_item_map.find(&(name, did)) {
5429                     Some(&StaticMethodTraitItemKind) => return StaticTraitMethod(path_str),
5430                     Some(_) => return TraitItem,
5431                     None => {}
5432                 }
5433             }
5434             None => {}
5435         }
5436
5437         NoSuggestion
5438     }
5439
5440     fn find_best_match_for_name(&mut self, name: &str, max_distance: uint)
5441                                 -> Option<String> {
5442         let this = &mut *self;
5443
5444         let mut maybes: Vec<token::InternedString> = Vec::new();
5445         let mut values: Vec<uint> = Vec::new();
5446
5447         let mut j = this.value_ribs.borrow().len();
5448         while j != 0 {
5449             j -= 1;
5450             let value_ribs = this.value_ribs.borrow();
5451             let bindings = value_ribs.get(j).bindings.borrow();
5452             for (&k, _) in bindings.iter() {
5453                 maybes.push(token::get_name(k));
5454                 values.push(uint::MAX);
5455             }
5456         }
5457
5458         let mut smallest = 0;
5459         for (i, other) in maybes.iter().enumerate() {
5460             *values.get_mut(i) = name.lev_distance(other.get());
5461
5462             if *values.get(i) <= *values.get(smallest) {
5463                 smallest = i;
5464             }
5465         }
5466
5467         if values.len() > 0 &&
5468             *values.get(smallest) != uint::MAX &&
5469             *values.get(smallest) < name.len() + 2 &&
5470             *values.get(smallest) <= max_distance &&
5471             name != maybes.get(smallest).get() {
5472
5473             Some(maybes.get(smallest).get().to_string())
5474
5475         } else {
5476             None
5477         }
5478     }
5479
5480     fn resolve_expr(&mut self, expr: &Expr) {
5481         // First, record candidate traits for this expression if it could
5482         // result in the invocation of a method call.
5483
5484         self.record_candidate_traits_for_expr_if_necessary(expr);
5485
5486         // Next, resolve the node.
5487         match expr.node {
5488             // The interpretation of paths depends on whether the path has
5489             // multiple elements in it or not.
5490
5491             ExprPath(ref path) => {
5492                 // This is a local path in the value namespace. Walk through
5493                 // scopes looking for it.
5494
5495                 match self.resolve_path(expr.id, path, ValueNS, true) {
5496                     Some(def) => {
5497                         // Write the result into the def map.
5498                         debug!("(resolving expr) resolved `{}`",
5499                                self.path_idents_to_string(path));
5500
5501                         // First-class methods are not supported yet; error
5502                         // out here.
5503                         match def {
5504                             (DefMethod(..), _) => {
5505                                 self.resolve_error(expr.span,
5506                                                       "first-class methods \
5507                                                        are not supported");
5508                                 self.session.span_note(expr.span,
5509                                                        "call the method \
5510                                                         using the `.` \
5511                                                         syntax");
5512                             }
5513                             _ => {}
5514                         }
5515
5516                         self.record_def(expr.id, def);
5517                     }
5518                     None => {
5519                         let wrong_name = self.path_idents_to_string(path);
5520                         // Be helpful if the name refers to a struct
5521                         // (The pattern matching def_tys where the id is in self.structs
5522                         // matches on regular structs while excluding tuple- and enum-like
5523                         // structs, which wouldn't result in this error.)
5524                         match self.with_no_errors(|this|
5525                             this.resolve_path(expr.id, path, TypeNS, false)) {
5526                             Some((DefTy(struct_id), _))
5527                               if self.structs.contains_key(&struct_id) => {
5528                                 self.resolve_error(expr.span,
5529                                         format!("`{}` is a structure name, but \
5530                                                  this expression \
5531                                                  uses it like a function name",
5532                                                 wrong_name).as_slice());
5533
5534                                 self.session.span_note(expr.span,
5535                                     format!("Did you mean to write: \
5536                                             `{} {{ /* fields */ }}`?",
5537                                             wrong_name).as_slice());
5538
5539                             }
5540                             _ => {
5541                                 let mut method_scope = false;
5542                                 self.value_ribs.borrow().iter().rev().all(|rib| {
5543                                     let res = match *rib {
5544                                         Rib { bindings: _, kind: MethodRibKind(_, _) } => true,
5545                                         Rib { bindings: _, kind: ItemRibKind } => false,
5546                                         _ => return true, // Keep advancing
5547                                     };
5548
5549                                     method_scope = res;
5550                                     false // Stop advancing
5551                                 });
5552
5553                                 if method_scope && token::get_name(self.self_name).get()
5554                                                                    == wrong_name.as_slice() {
5555                                         self.resolve_error(
5556                                             expr.span,
5557                                             "`self` is not available \
5558                                              in a static method. Maybe a \
5559                                              `self` argument is missing?");
5560                                 } else {
5561                                     let last_name = path.segments.last().unwrap().identifier.name;
5562                                     let mut msg = match self.find_fallback_in_self_type(last_name) {
5563                                         NoSuggestion => {
5564                                             // limit search to 5 to reduce the number
5565                                             // of stupid suggestions
5566                                             self.find_best_match_for_name(wrong_name.as_slice(), 5)
5567                                                                 .map_or("".to_string(),
5568                                                                         |x| format!("`{}`", x))
5569                                         }
5570                                         Field =>
5571                                             format!("`self.{}`", wrong_name),
5572                                         Method
5573                                         | TraitItem =>
5574                                             format!("to call `self.{}`", wrong_name),
5575                                         StaticTraitMethod(path_str)
5576                                         | StaticMethod(path_str) =>
5577                                             format!("to call `{}::{}`", path_str, wrong_name)
5578                                     };
5579
5580                                     if msg.len() > 0 {
5581                                         msg = format!(" Did you mean {}?", msg)
5582                                     }
5583
5584                                     self.resolve_error(
5585                                         expr.span,
5586                                         format!("unresolved name `{}`.{}",
5587                                                 wrong_name,
5588                                                 msg).as_slice());
5589                                 }
5590                             }
5591                         }
5592                     }
5593                 }
5594
5595                 visit::walk_expr(self, expr, ());
5596             }
5597
5598             ExprFnBlock(_, fn_decl, block) |
5599             ExprProc(fn_decl, block) |
5600             ExprUnboxedFn(_, _, fn_decl, block) => {
5601                 self.resolve_function(FunctionRibKind(expr.id, block.id),
5602                                       Some(fn_decl), NoTypeParameters,
5603                                       block);
5604             }
5605
5606             ExprStruct(ref path, _, _) => {
5607                 // Resolve the path to the structure it goes to. We don't
5608                 // check to ensure that the path is actually a structure; that
5609                 // is checked later during typeck.
5610                 match self.resolve_path(expr.id, path, TypeNS, false) {
5611                     Some(definition) => self.record_def(expr.id, definition),
5612                     result => {
5613                         debug!("(resolving expression) didn't find struct \
5614                                 def: {:?}", result);
5615                         let msg = format!("`{}` does not name a structure",
5616                                           self.path_idents_to_string(path));
5617                         self.resolve_error(path.span, msg.as_slice());
5618                     }
5619                 }
5620
5621                 visit::walk_expr(self, expr, ());
5622             }
5623
5624             ExprLoop(_, Some(label)) | ExprWhile(_, _, Some(label)) => {
5625                 self.with_label_rib(|this| {
5626                     let def_like = DlDef(DefLabel(expr.id));
5627
5628                     {
5629                         let label_ribs = this.label_ribs.borrow();
5630                         let length = label_ribs.len();
5631                         let rib = label_ribs.get(length - 1);
5632                         let renamed = mtwt::resolve(label);
5633                         rib.bindings.borrow_mut().insert(renamed, def_like);
5634                     }
5635
5636                     visit::walk_expr(this, expr, ());
5637                 })
5638             }
5639
5640             ExprForLoop(ref pattern, ref head, ref body, optional_label) => {
5641                 self.resolve_expr(&**head);
5642
5643                 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
5644
5645                 self.resolve_pattern(&**pattern,
5646                                      LocalIrrefutableMode,
5647                                      &mut HashMap::new());
5648
5649                 match optional_label {
5650                     None => {}
5651                     Some(label) => {
5652                         self.label_ribs
5653                             .borrow_mut()
5654                             .push(Rib::new(NormalRibKind));
5655                         let def_like = DlDef(DefLabel(expr.id));
5656
5657                         {
5658                             let label_ribs = self.label_ribs.borrow();
5659                             let length = label_ribs.len();
5660                             let rib = label_ribs.get(length - 1);
5661                             let renamed = mtwt::resolve(label);
5662                             rib.bindings.borrow_mut().insert(renamed,
5663                                                              def_like);
5664                         }
5665                     }
5666                 }
5667
5668                 self.resolve_block(&**body);
5669
5670                 if optional_label.is_some() {
5671                     drop(self.label_ribs.borrow_mut().pop())
5672                 }
5673
5674                 self.value_ribs.borrow_mut().pop();
5675             }
5676
5677             ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
5678                 let renamed = mtwt::resolve(label);
5679                 match self.search_ribs(self.label_ribs.borrow().as_slice(),
5680                                        renamed, expr.span) {
5681                     None => {
5682                         self.resolve_error(
5683                             expr.span,
5684                             format!("use of undeclared label `{}`",
5685                                     token::get_ident(label)).as_slice())
5686                     }
5687                     Some(DlDef(def @ DefLabel(_))) => {
5688                         // Since this def is a label, it is never read.
5689                         self.record_def(expr.id, (def, LastMod(AllPublic)))
5690                     }
5691                     Some(_) => {
5692                         self.session.span_bug(expr.span,
5693                                               "label wasn't mapped to a \
5694                                                label def!")
5695                     }
5696                 }
5697             }
5698
5699             _ => {
5700                 visit::walk_expr(self, expr, ());
5701             }
5702         }
5703     }
5704
5705     fn record_candidate_traits_for_expr_if_necessary(&mut self, expr: &Expr) {
5706         match expr.node {
5707             ExprField(_, ident, _) => {
5708                 // FIXME(#6890): Even though you can't treat a method like a
5709                 // field, we need to add any trait methods we find that match
5710                 // the field name so that we can do some nice error reporting
5711                 // later on in typeck.
5712                 let traits = self.search_for_traits_containing_method(ident.node.name);
5713                 self.trait_map.insert(expr.id, traits);
5714             }
5715             ExprMethodCall(ident, _, _) => {
5716                 debug!("(recording candidate traits for expr) recording \
5717                         traits for {}",
5718                        expr.id);
5719                 let traits = self.search_for_traits_containing_method(ident.node.name);
5720                 self.trait_map.insert(expr.id, traits);
5721             }
5722             _ => {
5723                 // Nothing to do.
5724             }
5725         }
5726     }
5727
5728     fn search_for_traits_containing_method(&mut self, name: Name) -> Vec<DefId> {
5729         debug!("(searching for traits containing method) looking for '{}'",
5730                token::get_name(name));
5731
5732         fn add_trait_info(found_traits: &mut Vec<DefId>,
5733                           trait_def_id: DefId,
5734                           name: Name) {
5735             debug!("(adding trait info) found trait {}:{} for method '{}'",
5736                 trait_def_id.krate,
5737                 trait_def_id.node,
5738                 token::get_name(name));
5739             found_traits.push(trait_def_id);
5740         }
5741
5742         let mut found_traits = Vec::new();
5743         let mut search_module = self.current_module.clone();
5744         loop {
5745             // Look for the current trait.
5746             match self.current_trait_ref {
5747                 Some((trait_def_id, _)) => {
5748                     let trait_item_map = self.trait_item_map.borrow();
5749
5750                     if trait_item_map.contains_key(&(name, trait_def_id)) {
5751                         add_trait_info(&mut found_traits, trait_def_id, name);
5752                     }
5753                 }
5754                 None => {} // Nothing to do.
5755             }
5756
5757             // Look for trait children.
5758             self.populate_module_if_necessary(&search_module);
5759
5760             {
5761                 let trait_item_map = self.trait_item_map.borrow();
5762                 for (_, child_names) in search_module.children.borrow().iter() {
5763                     let def = match child_names.def_for_namespace(TypeNS) {
5764                         Some(def) => def,
5765                         None => continue
5766                     };
5767                     let trait_def_id = match def {
5768                         DefTrait(trait_def_id) => trait_def_id,
5769                         _ => continue,
5770                     };
5771                     if trait_item_map.contains_key(&(name, trait_def_id)) {
5772                         add_trait_info(&mut found_traits, trait_def_id, name);
5773                     }
5774                 }
5775             }
5776
5777             // Look for imports.
5778             for (_, import) in search_module.import_resolutions.borrow().iter() {
5779                 let target = match import.target_for_namespace(TypeNS) {
5780                     None => continue,
5781                     Some(target) => target,
5782                 };
5783                 let did = match target.bindings.def_for_namespace(TypeNS) {
5784                     Some(DefTrait(trait_def_id)) => trait_def_id,
5785                     Some(..) | None => continue,
5786                 };
5787                 if self.trait_item_map.borrow().contains_key(&(name, did)) {
5788                     add_trait_info(&mut found_traits, did, name);
5789                     self.used_imports.insert((import.type_id, TypeNS));
5790                 }
5791             }
5792
5793             match search_module.parent_link.clone() {
5794                 NoParentLink | ModuleParentLink(..) => break,
5795                 BlockParentLink(parent_module, _) => {
5796                     search_module = parent_module.upgrade().unwrap();
5797                 }
5798             }
5799         }
5800
5801         found_traits
5802     }
5803
5804     fn record_def(&mut self, node_id: NodeId, (def, lp): (Def, LastPrivate)) {
5805         debug!("(recording def) recording {:?} for {:?}, last private {:?}",
5806                 def, node_id, lp);
5807         assert!(match lp {LastImport{..} => false, _ => true},
5808                 "Import should only be used for `use` directives");
5809         self.last_private.insert(node_id, lp);
5810         self.def_map.borrow_mut().insert_or_update_with(node_id, def, |_, old_value| {
5811             // Resolve appears to "resolve" the same ID multiple
5812             // times, so here is a sanity check it at least comes to
5813             // the same conclusion! - nmatsakis
5814             if def != *old_value {
5815                 self.session
5816                     .bug(format!("node_id {:?} resolved first to {:?} and \
5817                                   then {:?}",
5818                                  node_id,
5819                                  *old_value,
5820                                  def).as_slice());
5821             }
5822         });
5823     }
5824
5825     fn enforce_default_binding_mode(&mut self,
5826                                         pat: &Pat,
5827                                         pat_binding_mode: BindingMode,
5828                                         descr: &str) {
5829         match pat_binding_mode {
5830             BindByValue(_) => {}
5831             BindByRef(..) => {
5832                 self.resolve_error(pat.span,
5833                                    format!("cannot use `ref` binding mode \
5834                                             with {}",
5835                                            descr).as_slice());
5836             }
5837         }
5838     }
5839
5840     //
5841     // Unused import checking
5842     //
5843     // Although this is mostly a lint pass, it lives in here because it depends on
5844     // resolve data structures and because it finalises the privacy information for
5845     // `use` directives.
5846     //
5847
5848     fn check_for_unused_imports(&mut self, krate: &ast::Crate) {
5849         let mut visitor = UnusedImportCheckVisitor{ resolver: self };
5850         visit::walk_crate(&mut visitor, krate, ());
5851     }
5852
5853     fn check_for_item_unused_imports(&mut self, vi: &ViewItem) {
5854         // Ignore is_public import statements because there's no way to be sure
5855         // whether they're used or not. Also ignore imports with a dummy span
5856         // because this means that they were generated in some fashion by the
5857         // compiler and we don't need to consider them.
5858         if vi.vis == Public { return }
5859         if vi.span == DUMMY_SP { return }
5860
5861         match vi.node {
5862             ViewItemExternCrate(..) => {} // ignore
5863             ViewItemUse(ref p) => {
5864                 match p.node {
5865                     ViewPathSimple(_, _, id) => self.finalize_import(id, p.span),
5866                     ViewPathList(_, ref list, _) => {
5867                         for i in list.iter() {
5868                             self.finalize_import(i.node.id(), i.span);
5869                         }
5870                     },
5871                     ViewPathGlob(_, id) => {
5872                         if !self.used_imports.contains(&(id, TypeNS)) &&
5873                            !self.used_imports.contains(&(id, ValueNS)) {
5874                             self.session
5875                                 .add_lint(lint::builtin::UNUSED_IMPORTS,
5876                                           id,
5877                                           p.span,
5878                                           "unused import".to_string());
5879                         }
5880                     },
5881                 }
5882             }
5883         }
5884     }
5885
5886     // We have information about whether `use` (import) directives are actually used now.
5887     // If an import is not used at all, we signal a lint error. If an import is only used
5888     // for a single namespace, we remove the other namespace from the recorded privacy
5889     // information. That means in privacy.rs, we will only check imports and namespaces
5890     // which are used. In particular, this means that if an import could name either a
5891     // public or private item, we will check the correct thing, dependent on how the import
5892     // is used.
5893     fn finalize_import(&mut self, id: NodeId, span: Span) {
5894         debug!("finalizing import uses for {}",
5895                self.session.codemap().span_to_snippet(span));
5896
5897         if !self.used_imports.contains(&(id, TypeNS)) &&
5898            !self.used_imports.contains(&(id, ValueNS)) {
5899             self.session.add_lint(lint::builtin::UNUSED_IMPORTS,
5900                                   id,
5901                                   span,
5902                                   "unused import".to_string());
5903         }
5904
5905         let (v_priv, t_priv) = match self.last_private.find(&id) {
5906             Some(&LastImport {
5907                 value_priv: v,
5908                 value_used: _,
5909                 type_priv: t,
5910                 type_used: _
5911             }) => (v, t),
5912             Some(_) => {
5913                 fail!("we should only have LastImport for `use` directives")
5914             }
5915             _ => return,
5916         };
5917
5918         let mut v_used = if self.used_imports.contains(&(id, ValueNS)) {
5919             Used
5920         } else {
5921             Unused
5922         };
5923         let t_used = if self.used_imports.contains(&(id, TypeNS)) {
5924             Used
5925         } else {
5926             Unused
5927         };
5928
5929         match (v_priv, t_priv) {
5930             // Since some items may be both in the value _and_ type namespaces (e.g., structs)
5931             // we might have two LastPrivates pointing at the same thing. There is no point
5932             // checking both, so lets not check the value one.
5933             (Some(DependsOn(def_v)), Some(DependsOn(def_t))) if def_v == def_t => v_used = Unused,
5934             _ => {},
5935         }
5936
5937         self.last_private.insert(id, LastImport{value_priv: v_priv,
5938                                                 value_used: v_used,
5939                                                 type_priv: t_priv,
5940                                                 type_used: t_used});
5941     }
5942
5943     //
5944     // Diagnostics
5945     //
5946     // Diagnostics are not particularly efficient, because they're rarely
5947     // hit.
5948     //
5949
5950     /// A somewhat inefficient routine to obtain the name of a module.
5951     fn module_to_string(&self, module: &Module) -> String {
5952         let mut idents = Vec::new();
5953
5954         fn collect_mod(idents: &mut Vec<ast::Ident>, module: &Module) {
5955             match module.parent_link {
5956                 NoParentLink => {}
5957                 ModuleParentLink(ref module, name) => {
5958                     idents.push(name);
5959                     collect_mod(idents, &*module.upgrade().unwrap());
5960                 }
5961                 BlockParentLink(ref module, _) => {
5962                     // danger, shouldn't be ident?
5963                     idents.push(special_idents::opaque);
5964                     collect_mod(idents, &*module.upgrade().unwrap());
5965                 }
5966             }
5967         }
5968         collect_mod(&mut idents, module);
5969
5970         if idents.len() == 0 {
5971             return "???".to_string();
5972         }
5973         self.idents_to_string(idents.move_iter().rev()
5974                                  .collect::<Vec<ast::Ident>>()
5975                                  .as_slice())
5976     }
5977
5978     #[allow(dead_code)]   // useful for debugging
5979     fn dump_module(&mut self, module_: Rc<Module>) {
5980         debug!("Dump of module `{}`:", self.module_to_string(&*module_));
5981
5982         debug!("Children:");
5983         self.populate_module_if_necessary(&module_);
5984         for (&name, _) in module_.children.borrow().iter() {
5985             debug!("* {}", token::get_name(name));
5986         }
5987
5988         debug!("Import resolutions:");
5989         let import_resolutions = module_.import_resolutions.borrow();
5990         for (&name, import_resolution) in import_resolutions.iter() {
5991             let value_repr;
5992             match import_resolution.target_for_namespace(ValueNS) {
5993                 None => { value_repr = "".to_string(); }
5994                 Some(_) => {
5995                     value_repr = " value:?".to_string();
5996                     // FIXME #4954
5997                 }
5998             }
5999
6000             let type_repr;
6001             match import_resolution.target_for_namespace(TypeNS) {
6002                 None => { type_repr = "".to_string(); }
6003                 Some(_) => {
6004                     type_repr = " type:?".to_string();
6005                     // FIXME #4954
6006                 }
6007             }
6008
6009             debug!("* {}:{}{}", token::get_name(name), value_repr, type_repr);
6010         }
6011     }
6012 }
6013
6014 pub struct CrateMap {
6015     pub def_map: DefMap,
6016     pub exp_map2: ExportMap2,
6017     pub trait_map: TraitMap,
6018     pub external_exports: ExternalExports,
6019     pub last_private_map: LastPrivateMap,
6020 }
6021
6022 /// Entry point to crate resolution.
6023 pub fn resolve_crate(session: &Session,
6024                      _: &LanguageItems,
6025                      krate: &Crate)
6026                   -> CrateMap {
6027     let mut resolver = Resolver::new(session, krate.span);
6028     resolver.resolve(krate);
6029     let Resolver { def_map, export_map2, trait_map, last_private,
6030                    external_exports, .. } = resolver;
6031     CrateMap {
6032         def_map: def_map,
6033         exp_map2: export_map2,
6034         trait_map: trait_map,
6035         external_exports: external_exports,
6036         last_private_map: last_private,
6037     }
6038 }