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.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_camel_case_types)]
13 use driver::session::Session;
15 use metadata::csearch;
16 use metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
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};
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};
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;
56 use syntax::visit::Visitor;
58 use std::collections::{HashMap, HashSet};
59 use std::cell::{Cell, RefCell};
61 use std::mem::replace;
62 use std::rc::{Rc, Weak};
66 pub type DefMap = RefCell<NodeMap<Def>>;
70 binding_mode: BindingMode,
73 // Map from the name in a pattern to its binding mode.
74 type BindingMap = HashMap<Name,binding_info>;
76 // Trait method resolution
77 pub type TraitMap = NodeMap<Vec<DefId> >;
79 // This is the replacement export map. It maps a module to all of the exports
81 pub type ExportMap2 = RefCell<NodeMap<Vec<Export2> >>;
84 pub name: String, // The name of the target.
85 pub def_id: DefId, // The definition of the target.
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;
93 pub type LastPrivateMap = NodeMap<LastPrivate>;
95 pub enum LastPrivate {
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},
108 pub enum PrivateDep {
113 // How an import is used.
114 #[deriving(PartialEq)]
116 Unused, // The import is not used.
117 Used, // The import is used.
121 fn or(self, other: LastPrivate) -> LastPrivate {
122 match (self, other) {
123 (me, LastMod(AllPublic)) => me,
129 #[deriving(PartialEq)]
130 enum PatternBindingMode {
132 LocalIrrefutableMode,
133 ArgumentIrrefutableMode,
136 #[deriving(PartialEq, Eq, Hash)]
142 #[deriving(PartialEq)]
143 enum NamespaceError {
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.
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.)
159 /// Means that resolve has determined that the name is definitely
160 /// not bound in the namespace.
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>)
167 impl NamespaceResult {
168 fn is_unknown(&self) -> bool {
170 UnknownResult => true,
174 fn is_unbound(&self) -> bool {
176 UnboundResult => true,
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.
188 impl<'a> Visitor<()> for Resolver<'a> {
189 fn visit_item(&mut self, item: &Item, _: ()) {
190 self.resolve_item(item);
192 fn visit_arm(&mut self, arm: &Arm, _: ()) {
193 self.resolve_arm(arm);
195 fn visit_block(&mut self, block: &Block, _: ()) {
196 self.resolve_block(block);
198 fn visit_expr(&mut self, expr: &Expr, _: ()) {
199 self.resolve_expr(expr);
201 fn visit_local(&mut self, local: &Local, _: ()) {
202 self.resolve_local(local);
204 fn visit_ty(&mut self, ty: &Ty, _: ()) {
205 self.resolve_type(ty);
209 /// Contains data for specific types of import directives.
210 enum ImportDirectiveSubclass {
211 SingleImport(Ident /* target */, Ident /* source */),
215 /// The context that we thread through while building the reduced graph.
217 enum ReducedGraphParent {
218 ModuleReducedGraphParent(Rc<Module>)
221 impl ReducedGraphParent {
222 fn module(&self) -> Rc<Module> {
224 ModuleReducedGraphParent(ref m) => {
231 type ErrorMessage = Option<(Span, String)>;
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.
239 impl<T> ResolveResult<T> {
240 fn indeterminate(&self) -> bool {
241 match *self { Indeterminate => true, _ => false }
245 enum FallbackSuggestion {
250 StaticMethod(String),
251 StaticTraitMethod(String),
254 enum TypeParameters<'a> {
260 // Identifies the things that these parameters
261 // were declared on (type, fn, etc)
264 // ID of the enclosing item.
267 // The kind of the rib used for type parameters.
271 // The rib kind controls the translation of argument or local definitions
272 // (`def_arg` or `def_local`) to upvars (`def_upvar`).
275 // No translation needs to be applied.
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 */),
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
286 // parent; method itself
287 MethodRibKind(NodeId, MethodSort),
289 // We passed through an item scope. Disallow upvars.
292 // We're in a constant item. Can't refer to dynamic stuff.
296 // Methods can be required or provided. RequiredMethod methods only occur in traits.
299 ProvidedMethod(NodeId)
302 enum UseLexicalScopeFlag {
307 enum ModulePrefixResult {
309 PrefixFound(Rc<Module>, uint)
312 #[deriving(Clone, Eq, PartialEq)]
313 pub enum TraitItemKind {
314 NonstaticMethodTraitItemKind,
315 StaticMethodTraitItemKind,
319 pub fn from_explicit_self_category(explicit_self_category:
320 ExplicitSelfCategory)
322 if explicit_self_category == StaticExplicitSelfCategory {
323 StaticMethodTraitItemKind
325 NonstaticMethodTraitItemKind
330 #[deriving(PartialEq)]
331 enum NameSearchType {
332 /// We're doing a name search in order to resolve a `use` directive.
335 /// We're doing a name search in order to resolve a path type, a path
336 /// expression, or a path pattern.
340 enum BareIdentifierPatternResolution {
341 FoundStructOrEnumVariant(Def, LastPrivate),
342 FoundConst(Def, LastPrivate),
343 BareIdentifierPatternUnresolved
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,
359 bindings: RefCell<HashMap<Name, DefLike>>,
364 fn new(kind: RibKind) -> Rib {
366 bindings: RefCell::new(HashMap::new()),
372 /// One import directive.
373 struct ImportDirective {
374 module_path: Vec<Ident>,
375 subclass: ImportDirectiveSubclass,
378 is_public: bool, // see note in ImportResolution about how to use this
382 impl ImportDirective {
383 fn new(module_path: Vec<Ident> ,
384 subclass: ImportDirectiveSubclass,
391 module_path: module_path,
395 is_public: is_public,
396 shadowable: shadowable,
401 /// The item that an import resolves to.
404 target_module: Rc<Module>,
405 bindings: Rc<NameBindings>,
410 fn new(target_module: Rc<Module>,
411 bindings: Rc<NameBindings>,
415 target_module: target_module,
417 shadowable: shadowable,
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.
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,
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
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
448 impl ImportResolution {
449 fn new(id: NodeId, is_public: bool) -> ImportResolution {
453 outstanding_references: 0,
456 is_public: is_public,
460 fn target_for_namespace(&self, namespace: Namespace)
463 TypeNS => self.type_target.clone(),
464 ValueNS => self.value_target.clone(),
468 fn id(&self, namespace: Namespace) -> NodeId {
470 TypeNS => self.type_id,
471 ValueNS => self.value_id,
476 /// The link from a module up to its nearest parent node.
480 ModuleParentLink(Weak<Module>, Ident),
481 BlockParentLink(Weak<Module>, NodeId)
484 /// The type of module this is.
485 #[deriving(PartialEq)]
494 /// One node in the tree of modules.
496 parent_link: ParentLink,
497 def_id: Cell<Option<DefId>>,
498 kind: Cell<ModuleKind>,
501 children: RefCell<HashMap<Name, Rc<NameBindings>>>,
502 imports: RefCell<Vec<ImportDirective>>,
504 // The external module children of this node that were declared with
506 external_module_children: RefCell<HashMap<Name, Rc<Module>>>,
508 // The anonymous children of this node. Anonymous children are pseudo-
509 // modules that are implicitly created around items contained within
512 // For example, if we have this:
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>>>,
524 // The status of resolving each import in this module.
525 import_resolutions: RefCell<HashMap<Name, ImportResolution>>,
527 // The number of unresolved globs that this module exports.
528 glob_count: Cell<uint>,
530 // The index of the import we're resolving.
531 resolved_import_count: Cell<uint>,
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>,
540 fn new(parent_link: ParentLink,
541 def_id: Option<DefId>,
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),
562 fn all_imports_resolved(&self) -> bool {
563 self.imports.borrow().len() == self.resolved_import_count.get()
567 // Records a possibly-private type definition.
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>
576 // Records a possibly-private value definition.
579 is_public: bool, // see note in ImportResolution about how to use this
581 value_span: Option<Span>,
584 // Records the definitions (at most one for each namespace) that a name is
586 struct NameBindings {
587 type_def: RefCell<Option<TypeNsDef>>, //< Meaning in type namespace.
588 value_def: RefCell<Option<ValueNsDef>>, //< Meaning in value namespace.
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>() { ... }
599 fn new() -> NameBindings {
601 type_def: RefCell::new(None),
602 value_def: RefCell::new(None),
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>,
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,
617 let type_def = self.type_def.borrow().clone();
620 *self.type_def.borrow_mut() = Some(TypeNsDef {
621 is_public: is_public,
622 module_def: Some(module_),
628 *self.type_def.borrow_mut() = Some(TypeNsDef {
629 is_public: is_public,
630 module_def: Some(module_),
632 type_def: type_def.type_def
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>,
646 let type_def = self.type_def.borrow().clone();
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)),
659 match type_def.module_def {
661 let module = Module::new(parent_link,
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,
673 Some(module_def) => module_def.kind.set(kind),
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();
685 *self.type_def.borrow_mut() = Some(TypeNsDef {
689 is_public: is_public,
693 *self.type_def.borrow_mut() = Some(TypeNsDef {
696 module_def: type_def.module_def,
697 is_public: is_public,
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 {
707 value_span: Some(sp),
708 is_public: is_public,
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(),
721 * Returns the module node. Fails if this node does not have a module
724 fn get_module(&self) -> Rc<Module> {
725 match self.get_module_if_available() {
727 fail!("get_module called on a node with no module \
730 Some(module_def) => module_def
734 fn defined_in_namespace(&self, namespace: Namespace) -> bool {
736 TypeNS => return self.type_def.borrow().is_some(),
737 ValueNS => return self.value_def.borrow().is_some()
741 fn defined_in_public_namespace(&self, namespace: Namespace) -> bool {
743 TypeNS => match *self.type_def.borrow() {
744 Some(ref def) => def.is_public, None => false
746 ValueNS => match *self.value_def.borrow() {
747 Some(ref def) => def.is_public, None => false
752 fn def_for_namespace(&self, namespace: Namespace) -> Option<Def> {
755 match *self.type_def.borrow() {
757 Some(ref type_def) => {
758 match type_def.type_def {
759 Some(type_def) => Some(type_def),
761 match type_def.module_def {
762 Some(ref module) => {
763 match module.def_id.get() {
764 Some(did) => Some(DefMod(did)),
776 match *self.value_def.borrow() {
778 Some(value_def) => Some(value_def.def)
784 fn span_for_namespace(&self, namespace: Namespace) -> Option<Span> {
785 if self.defined_in_namespace(namespace) {
788 match *self.type_def.borrow() {
790 Some(ref type_def) => type_def.type_span
794 match *self.value_def.borrow() {
796 Some(ref value_def) => value_def.value_span
806 /// Interns the names of the primitive types.
807 struct PrimitiveTypeTable {
808 primitive_types: HashMap<Name, PrimTy>,
811 impl PrimitiveTypeTable {
812 fn new() -> PrimitiveTypeTable {
813 let mut table = PrimitiveTypeTable {
814 primitive_types: HashMap::new()
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));
836 fn intern(&mut self, string: &str, primitive_type: PrimTy) {
837 self.primitive_types.insert(token::intern(string), primitive_type);
842 fn namespace_error_to_string(ns: NamespaceError) -> &'static str {
845 ModuleError | TypeError => "type or module",
846 ValueError => "value",
850 /// The main resolver class.
851 struct Resolver<'a> {
852 session: &'a Session,
854 graph_root: NameBindings,
856 trait_item_map: RefCell<FnvHashMap<(Name, DefId), TraitItemKind>>,
858 structs: FnvHashMap<DefId, Vec<Name>>,
860 // The number of imports that are currently unresolved.
861 unresolved_imports: uint,
863 // The module that represents the current item scope.
864 current_module: Rc<Module>,
866 // The current set of local scopes, for values.
867 // FIXME #4948: Reuse ribs to avoid allocation.
868 value_ribs: RefCell<Vec<Rib>>,
870 // The current set of local scopes, for types.
871 type_ribs: RefCell<Vec<Rib>>,
873 // The current set of local scopes, for labels.
874 label_ribs: RefCell<Vec<Rib>>,
876 // The trait that the current context can refer to.
877 current_trait_ref: Option<(DefId, TraitRef)>,
879 // The current self type if inside an impl (used for better errors).
880 current_self_type: Option<Ty>,
882 // The ident for the keyword "self".
884 // The ident for the non-keyword "Self".
885 type_self_name: Name,
887 // The idents for the primitive types.
888 primitive_type_table: PrimitiveTypeTable,
891 export_map2: ExportMap2,
893 external_exports: ExternalExports,
894 last_private: LastPrivateMap,
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
901 used_imports: HashSet<(NodeId, Namespace)>,
904 struct BuildReducedGraphVisitor<'a, 'b:'a> {
905 resolver: &'a mut Resolver<'b>,
908 impl<'a, 'b> Visitor<ReducedGraphParent> for BuildReducedGraphVisitor<'a, 'b> {
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);
915 fn visit_foreign_item(&mut self, foreign_item: &ForeignItem,
916 context: ReducedGraphParent) {
917 self.resolver.build_reduced_graph_for_foreign_item(foreign_item,
920 let mut v = BuildReducedGraphVisitor{ resolver: r };
921 visit::walk_foreign_item(&mut v, foreign_item, context.clone());
925 fn visit_view_item(&mut self, view_item: &ViewItem, context: ReducedGraphParent) {
926 self.resolver.build_reduced_graph_for_view_item(view_item, context);
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);
936 struct UnusedImportCheckVisitor<'a, 'b:'a> {
937 resolver: &'a mut Resolver<'b>
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, ());
947 impl<'a> Resolver<'a> {
948 fn new(session: &'a Session, crate_span: Span) -> Resolver<'a> {
949 let graph_root = NameBindings::new();
951 graph_root.define_module(NoParentLink,
952 Some(DefId { krate: 0, node: 0 }),
958 let current_module = graph_root.get_module();
963 // The outermost module has def ID 0; this is not reflected in the
966 graph_root: graph_root,
968 trait_item_map: RefCell::new(FnvHashMap::new()),
969 structs: FnvHashMap::new(),
971 unresolved_imports: 0,
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()),
978 current_trait_ref: None,
979 current_self_type: None,
981 self_name: special_names::self_,
982 type_self_name: special_names::type_self,
984 primitive_type_table: PrimitiveTypeTable::new(),
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(),
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();
1001 self.resolve_imports();
1002 self.session.abort_if_errors();
1004 self.record_exports();
1005 self.session.abort_if_errors();
1007 self.resolve_crate(krate);
1008 self.session.abort_if_errors();
1010 self.check_for_unused_imports(krate);
1014 // Reduced graph building
1016 // Here we build the "reduced graph": the graph of the module tree without
1017 // any imports resolved.
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());
1025 let mut visitor = BuildReducedGraphVisitor { resolver: self, };
1026 visit::walk_crate(&mut visitor, krate, initial_parent);
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.
1036 * If this node does not have a module definition and we are not inside
1041 reduced_graph_parent: ReducedGraphParent,
1042 duplicate_checking_mode: DuplicateCheckingMode,
1043 // For printing errors
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.
1050 let module_ = reduced_graph_parent.module();
1052 self.check_for_conflicts_between_external_crates_and_items(&*module_,
1056 // Add or reuse the child.
1057 let child = module_.children.borrow().find_copy(&name.name);
1060 let child = Rc::new(NameBindings::new());
1061 module_.children.borrow_mut().insert(name.name, child.clone());
1065 // Enforce the duplicate checking mode:
1067 // * If we're requesting duplicate module checking, check that
1068 // there isn't a module in the module with the same name.
1070 // * If we're requesting duplicate type checking, check that
1071 // there isn't a type in the module with the same name.
1073 // * If we're requesting duplicate value checking, check that
1074 // there isn't a value in the module with the same name.
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.
1080 // * If no duplicate checking was requested at all, do
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;
1091 ForbidDuplicateTypesAndModules => {
1092 match child.def_for_namespace(TypeNS) {
1094 Some(_) if child.get_module_if_available()
1095 .map(|m| m.kind.get()) ==
1096 Some(ImplModuleKind) => {}
1097 Some(_) => duplicate_type = TypeError
1101 ForbidDuplicateValues => {
1102 if child.defined_in_namespace(ValueNS) {
1103 duplicate_type = ValueError;
1107 ForbidDuplicateTypesAndValues => {
1109 match child.def_for_namespace(TypeNS) {
1110 Some(DefMod(_)) | None => {}
1113 duplicate_type = TypeError;
1116 if child.defined_in_namespace(ValueNS) {
1117 duplicate_type = ValueError;
1122 OverwriteDuplicates => None
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());
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());
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 {
1153 // Check each statement.
1154 for statement in block.stmts.iter() {
1155 match statement.node {
1156 StmtDecl(declaration, _) => {
1157 match declaration.node {
1172 // If we found neither view items nor items, we don't need to create
1173 // an anonymous module.
1178 fn get_parent_link(&mut self, parent: ReducedGraphParent, name: Ident)
1181 ModuleReducedGraphParent(module_) => {
1182 return ModuleParentLink(module_.downgrade(), name);
1187 /// Constructs the reduced graph for one item.
1188 fn build_reduced_graph_for_item(&mut self,
1190 parent: ReducedGraphParent)
1191 -> ReducedGraphParent
1193 let ident = item.ident;
1195 let is_public = item.vis == ast::Public;
1200 self.add_child(ident, parent.clone(), ForbidDuplicateModules, sp);
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,
1208 item.vis == ast::Public,
1211 ModuleReducedGraphParent(name_bindings.get_module())
1214 ItemForeignMod(..) => parent,
1216 // These items live in the value namespace.
1217 ItemStatic(_, m, _) => {
1219 self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1220 let mutbl = m == ast::MutMutable;
1222 name_bindings.define_value
1223 (DefStatic(local_def(item.id), mutbl), sp, is_public);
1226 ItemFn(_, fn_style, _, _, _) => {
1228 self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1230 let def = DefFn(local_def(item.id), fn_style);
1231 name_bindings.define_value(def, sp, is_public);
1235 // These items live in the type namespace.
1238 self.add_child(ident,
1240 ForbidDuplicateTypesAndModules,
1243 name_bindings.define_type
1244 (DefTy(local_def(item.id)), sp, is_public);
1248 ItemEnum(ref enum_definition, _) => {
1250 self.add_child(ident,
1252 ForbidDuplicateTypesAndModules,
1255 name_bindings.define_type
1256 (DefTy(local_def(item.id)), sp, is_public);
1258 for variant in (*enum_definition).variants.iter() {
1259 self.build_reduced_graph_for_variant(
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)
1276 let name_bindings = self.add_child(ident, parent.clone(), forbid, sp);
1278 // Define a name in the type namespace.
1279 name_bindings.define_type(DefTy(local_def(item.id)), sp, is_public);
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,
1289 // Record the def ID and fields of this struct.
1290 let named_fields = struct_def.fields.iter().filter_map(|f| {
1292 NamedField(ident, _) => Some(ident.name),
1293 UnnamedField(_) => None
1296 self.structs.insert(local_def(item.id), named_fields);
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.
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.
1310 // Create the module and add all methods.
1312 TyPath(ref path, _, _) if path.segments.len() == 1 => {
1313 let name = path.segments.last().unwrap().identifier;
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()
1321 child.get_module().kind.get() ==
1323 ModuleReducedGraphParent(child.get_module())
1325 // Create the module
1328 self.add_child(name,
1330 ForbidDuplicateModules,
1334 self.get_parent_link(parent.clone(), ident);
1335 let def_id = local_def(item.id);
1338 !name_bindings.defined_in_namespace(ns) ||
1339 name_bindings.defined_in_public_namespace(ns);
1341 name_bindings.define_module(parent_link,
1348 ModuleReducedGraphParent(
1349 name_bindings.get_module())
1353 // For each implementation item...
1354 for impl_item in impl_items.iter() {
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,
1362 ForbidDuplicateValues,
1364 let def = match method.pe_explicit_self()
1367 // Static methods become
1368 // `def_static_method`s.
1370 local_def(method.id),
1371 FromImpl(local_def(item.id)),
1372 method.pe_fn_style())
1375 // Non-static methods become
1377 DefMethod(local_def(method.id),
1383 method.pe_vis() == ast::Public;
1384 method_name_bindings.define_value(
1398 ItemImpl(_, Some(_), _, _) => parent,
1400 ItemTrait(_, _, _, ref methods) => {
1402 self.add_child(ident,
1404 ForbidDuplicateTypesAndModules,
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)),
1413 item.vis == ast::Public,
1415 let module_parent = ModuleReducedGraphParent(name_bindings.
1418 let def_id = local_def(item.id);
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);
1424 let ident = ty_m.ident;
1426 // Add it as a name in the trait module.
1427 let (def, static_flag) = match ty_m.explicit_self.node {
1429 // Static methods become `def_static_method`s.
1430 (DefStaticMethod(local_def(ty_m.id),
1431 FromTrait(local_def(item.id)),
1433 StaticMethodTraitItemKind)
1436 // Non-static methods become `def_method`s.
1437 (DefMethod(local_def(ty_m.id),
1438 Some(local_def(item.id))),
1439 NonstaticMethodTraitItemKind)
1443 let method_name_bindings =
1444 self.add_child(ident,
1445 module_parent.clone(),
1446 ForbidDuplicateValues,
1448 method_name_bindings.define_value(def, ty_m.span, true);
1452 .insert((ident.name, def_id), static_flag);
1455 name_bindings.define_type(DefTrait(def_id), sp, is_public);
1458 ItemMac(..) => parent
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,
1467 parent: ReducedGraphParent,
1469 let ident = variant.node.name;
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);
1478 StructVariantKind(_) => {
1479 let child = self.add_child(ident, parent,
1480 ForbidDuplicateTypesAndValues,
1482 child.define_type(DefVariant(item_id,
1483 local_def(variant.node.id), true),
1484 variant.span, is_public);
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());
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, _) => {
1505 .iter().map(|ident| ident.identifier)
1509 ViewPathGlob(ref module_ident_path, _) |
1510 ViewPathList(ref module_ident_path, _, _) => {
1511 module_ident_path.segments
1512 .iter().map(|ident| ident.identifier).collect()
1516 // Build up the import directives.
1517 let module_ = parent.module();
1518 let is_public = view_item.vis == ast::Public;
1523 attr.name() == token::get_ident(
1524 special_idents::prelude_import)
1527 match view_path.node {
1528 ViewPathSimple(binding, ref full_path, id) => {
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");
1536 let subclass = SingleImport(binding,
1538 self.build_import_directive(&*module_,
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),
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");
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(),
1569 self.resolve_error(source_item.span,
1570 "`mod` import can only appear in an import list \
1571 with a non-empty prefix");
1575 let module_path = module_path.as_slice().init();
1576 (Vec::from_slice(module_path), name)
1579 self.build_import_directive(
1582 SingleImport(name, name),
1584 source_item.node.id(),
1589 ViewPathGlob(_, id) => {
1590 self.build_import_directive(&*module_,
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);
1608 ModuleParentLink(parent.module().downgrade(), name);
1609 let external_module = Rc::new(Module::new(parent_link,
1614 debug!("(build reduced graph for item) found extern `{}`",
1615 self.module_to_string(&*external_module));
1616 self.check_for_conflicts_between_external_crates(
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);
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;
1636 self.add_child(name, parent, ForbidDuplicateValues,
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);
1644 self.with_type_parameter_rib(
1645 HasTypeParameters(generics,
1651 ForeignItemStatic(_, m) => {
1652 let def = DefStatic(local_def(foreign_item.id), m);
1653 name_bindings.define_value(def, foreign_item.span, is_public);
1660 fn build_reduced_graph_for_block(&mut self,
1662 parent: ReducedGraphParent)
1663 -> ReducedGraphParent
1665 if self.block_needs_anonymous_module(block) {
1666 let block_id = block.id;
1668 debug!("(building reduced graph for block) creating a new \
1669 anonymous module for block {}",
1672 let parent_module = parent.module();
1673 let new_module = Rc::new(Module::new(
1674 BlockParentLink(parent_module.downgrade(), block_id),
1676 AnonymousModuleKind,
1679 parent_module.anonymous_children.borrow_mut()
1680 .insert(block_id, new_module.clone());
1681 ModuleReducedGraphParent(new_module)
1687 fn handle_external_def(&mut self,
1690 child_name_bindings: &NameBindings,
1693 new_parent: ReducedGraphParent) {
1694 debug!("(building reduced graph for \
1695 external crate) building external def, priv {:?}",
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() {
1702 Some(did) => self.external_exports.contains(&did)
1707 self.external_exports.insert(def.def_id());
1710 let kind = match def {
1711 DefStruct(..) | DefTy(..) => ImplModuleKind,
1712 _ => NormalModuleKind
1716 DefMod(def_id) | DefForeignMod(def_id) | DefStruct(def_id) |
1718 let type_def = child_name_bindings.type_def.borrow().clone();
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));
1726 debug!("(building reduced graph for \
1727 external crate) building module \
1729 let parent_link = self.get_parent_link(new_parent.clone(), ident);
1731 child_name_bindings.define_module(parent_link,
1744 DefMod(_) | DefForeignMod(_) => {}
1745 DefVariant(enum_did, variant_id, is_struct) => {
1746 debug!("(building reduced graph for external crate) building \
1749 // If this variant is public, then it was publicly reexported,
1750 // otherwise we need to inherit the visibility of the enum
1752 let is_exported = is_public ||
1753 self.external_exports.contains(&enum_did);
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());
1759 child_name_bindings.define_value(def, DUMMY_SP, is_exported);
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);
1767 DefTrait(def_id) => {
1768 debug!("(building reduced graph for external \
1769 crate) building type {}", final_ident);
1771 // If this is a trait, add all the trait item names to the trait
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());
1782 debug!("(building reduced graph for external crate) ... \
1783 adding trait item '{}'",
1784 token::get_ident(trait_item_name));
1788 .insert((trait_item_name.name, def_id),
1792 self.external_exports
1793 .insert(trait_item_def_id.def_id());
1797 child_name_bindings.define_type(def, DUMMY_SP, is_public);
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,
1809 debug!("(building reduced graph for external \
1810 crate) building type {}", final_ident);
1812 child_name_bindings.define_type(def, DUMMY_SP, is_public);
1814 DefStruct(def_id) => {
1815 debug!("(building reduced graph for external \
1816 crate) building type and value for {}",
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| {
1821 }).collect::<Vec<_>>();
1823 if fields.len() == 0 {
1824 child_name_bindings.define_value(def, DUMMY_SP, is_public);
1827 // Record the def ID and fields of this struct.
1828 self.structs.insert(def_id, fields);
1831 debug!("(building reduced graph for external crate) \
1832 ignoring {:?}", def);
1833 // Ignored; handled elsewhere.
1835 DefArg(..) | DefLocal(..) | DefPrimTy(..) |
1836 DefTyParam(..) | DefBinding(..) |
1837 DefUse(..) | DefUpvar(..) | DefRegion(..) |
1838 DefTyParamBinder(..) | DefLabel(..) | DefSelfTy(..) => {
1839 fail!("didn't expect `{:?}`", def);
1844 /// Builds the reduced graph for a single item in an external crate.
1845 fn build_reduced_graph_for_external_crate_def(&mut self,
1849 visibility: Visibility) {
1852 // Add the new child item, if necessary.
1854 DefForeignMod(def_id) => {
1855 // Foreign modules have no names. Recur and populate
1857 csearch::each_child_of_item(&self.session.cstore,
1862 self.build_reduced_graph_for_external_crate_def(
1870 let child_name_bindings =
1871 self.add_child(ident,
1872 ModuleReducedGraphParent(root.clone()),
1873 OverwriteDuplicates,
1876 self.handle_external_def(def,
1878 &*child_name_bindings,
1879 token::get_ident(ident).get(),
1881 ModuleReducedGraphParent(root));
1886 // We only process static methods of impls here.
1887 match csearch::get_type_name_if_impl(&self.session.cstore, def) {
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));
1900 let child_name_bindings =
1903 ModuleReducedGraphParent(root.clone()),
1904 OverwriteDuplicates,
1907 // Process the static methods. First,
1908 // create the module.
1910 let type_def = child_name_bindings.type_def.borrow().clone();
1913 module_def: Some(module_def),
1916 // We already have a module. This
1918 type_module = module_def;
1920 // Mark it as an impl module if
1922 type_module.kind.set(ImplModuleKind);
1926 self.get_parent_link(ModuleReducedGraphParent(root),
1928 child_name_bindings.define_module(
1936 child_name_bindings.
1941 // Add each static method to the module.
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));
1952 let method_name_bindings =
1953 self.add_child(ident,
1955 OverwriteDuplicates,
1958 static_method_info.def_id,
1959 static_method_info.fn_style);
1961 method_name_bindings.define_value(
1963 visibility == ast::Public);
1967 // Otherwise, do nothing.
1968 Some(_) | None => {}
1974 debug!("(building reduced graph for external crate) \
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));
1985 let def_id = match module.def_id.get() {
1987 debug!("(populating external module) ... no def ID!");
1990 Some(def_id) => def_id,
1993 csearch::each_child_of_item(&self.session.cstore,
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(),
2003 module.populated.set(true)
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())
2012 assert!(module.populated.get())
2015 /// Builds the reduced graph rooted at the 'use' directive for an external
2017 fn build_reduced_graph_for_external_crate(&mut self, root: Rc<Module>) {
2018 csearch::each_top_level_item_of_crate(&self.session.cstore,
2023 |def_like, ident, visibility| {
2024 self.build_reduced_graph_for_external_crate_def(root.clone(),
2031 /// Creates and adds an import directive to the given module.
2032 fn build_import_directive(&mut self,
2034 module_path: Vec<Ident> ,
2035 subclass: ImportDirectiveSubclass,
2040 module_.imports.borrow_mut().push(ImportDirective::new(module_path,
2046 self.unresolved_imports += 1;
2047 // Bump the reference count on the name. Or, if this is a glob, set
2048 // the appropriate flag.
2051 SingleImport(target, _) => {
2052 debug!("(building import directive) building import \
2054 self.idents_to_string(module_.imports.borrow().last().unwrap()
2055 .module_path.as_slice()),
2056 token::get_ident(target));
2058 let mut import_resolutions = module_.import_resolutions
2060 match import_resolutions.find_mut(&target.name) {
2061 Some(resolution) => {
2062 debug!("(building import directive) bumping \
2064 resolution.outstanding_references += 1;
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;
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);
2080 // Set the glob flag. This tells us that we don't know the
2081 // module's exports ahead of time.
2083 module_.glob_count.set(module_.glob_count.get() + 1);
2088 // Import resolution
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
2096 /// Resolves all imports for the crate. This method performs the fixed-
2097 /// point iteration.
2098 fn resolve_imports(&mut self) {
2100 let mut prev_unresolved_imports = 0;
2102 debug!("(resolving imports) iteration {}, {} imports left",
2103 i, self.unresolved_imports);
2105 let module_root = self.graph_root.get_module();
2106 self.resolve_imports_for_module_subtree(module_root.clone());
2108 if self.unresolved_imports == 0 {
2109 debug!("(resolving imports) success");
2113 if self.unresolved_imports == prev_unresolved_imports {
2114 self.report_unresolved_imports(module_root);
2119 prev_unresolved_imports = self.unresolved_imports;
2123 /// Attempts to resolve imports for the given module and all of its
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;
2132 self.populate_module_if_necessary(&module_);
2133 for (_, child_node) in module_.children.borrow().iter() {
2134 match child_node.get_module_if_available() {
2138 Some(child_module) => {
2139 self.resolve_imports_for_module_subtree(child_module);
2144 for (_, child_module) in module_.anonymous_children.borrow().iter() {
2145 self.resolve_imports_for_module_subtree(child_module.clone());
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 \
2154 self.module_to_string(&*module));
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(),
2166 let (span, help) = match err {
2167 Some((span, msg)) => (span, format!(". {}", msg)),
2168 None => (import_directive.span, String::new())
2170 let msg = format!("unresolved import `{}`{}",
2171 self.import_path_to_string(
2172 import_directive.module_path
2174 import_directive.subclass),
2176 self.resolve_error(span, msg.as_slice());
2178 Indeterminate => break, // Bail out. We'll come around next time.
2179 Success(()) => () // Good. Continue.
2182 module.resolved_import_count
2183 .set(module.resolved_import_count.get() + 1);
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() {
2194 result.push_str("::")
2196 result.push_str(token::get_ident(*ident).get());
2201 fn path_idents_to_string(&self, path: &Path) -> String {
2202 let identifiers: Vec<ast::Ident> = path.segments
2204 .map(|seg| seg.identifier)
2206 self.idents_to_string(identifiers.as_slice())
2209 fn import_directive_subclass_to_string(&mut self,
2210 subclass: ImportDirectiveSubclass)
2213 SingleImport(_, source) => {
2214 token::get_ident(source).get().to_string()
2216 GlobImport => "*".to_string()
2220 fn import_path_to_string(&mut self,
2222 subclass: ImportDirectiveSubclass)
2224 if idents.is_empty() {
2225 self.import_directive_subclass_to_string(subclass)
2228 self.idents_to_string(idents),
2229 self.import_directive_subclass_to_string(
2230 subclass))).to_string()
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;
2246 debug!("(resolving import for module) resolving import `{}::...` in \
2248 self.idents_to_string(module_path.as_slice()),
2249 self.module_to_string(&*module_));
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)))
2256 match self.resolve_module_path(module_.clone(),
2257 module_path.as_slice(),
2258 DontUseLexicalScope,
2259 import_directive.span,
2262 resolution_result = Failed(err);
2266 resolution_result = Indeterminate;
2269 Success(container) => Some(container),
2275 Some((containing_module, lp)) => {
2276 // We found the module that the target is contained
2277 // within. Attempt to resolve the import within it.
2279 match import_directive.subclass {
2280 SingleImport(target, source) => {
2282 self.resolve_single_import(&*module_,
2291 self.resolve_glob_import(&*module_,
2300 // Decrement the count of unresolved imports.
2301 match resolution_result {
2303 assert!(self.unresolved_imports >= 1);
2304 self.unresolved_imports -= 1;
2307 // Nothing to do here; just return the error.
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.)
2316 if !resolution_result.indeterminate() {
2317 match import_directive.subclass {
2319 assert!(module_.glob_count.get() >= 1);
2320 module_.glob_count.set(module_.glob_count.get() - 1);
2322 SingleImport(..) => {
2328 return resolution_result;
2331 fn create_name_bindings_from_module(module: Rc<Module>) -> NameBindings {
2333 type_def: RefCell::new(Some(TypeNsDef {
2335 module_def: Some(module),
2339 value_def: RefCell::new(None),
2343 fn resolve_single_import(&mut self,
2345 containing_module: Rc<Module>,
2348 directive: &ImportDirective,
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_),
2362 LastImport {..} => {
2364 .span_bug(directive.span,
2365 "not expecting Import here, must be LastMod")
2369 // We need to resolve both namespaces for this to succeed.
2372 let mut value_result = UnknownResult;
2373 let mut type_result = UnknownResult;
2375 // Search for direct children of the containing module.
2376 self.populate_module_if_necessary(&containing_module);
2378 match containing_module.children.borrow().find(&source.name) {
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());
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());
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.
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.
2407 if containing_module.glob_count.get() > 0 {
2408 debug!("(resolving single import) unresolved glob; \
2410 return Indeterminate;
2413 // Now search the exported imports within the containing module.
2414 match containing_module.import_resolutions.borrow().find(&source.name) {
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
2422 if value_result.is_unknown() {
2423 value_result = UnboundResult;
2425 if type_result.is_unknown() {
2426 type_result = UnboundResult;
2429 Some(import_resolution)
2430 if import_resolution.outstanding_references == 0 => {
2432 fn get_binding(this: &mut Resolver,
2433 import_resolution: &ImportResolution,
2434 namespace: Namespace)
2435 -> NamespaceResult {
2437 // Import resolutions must be declared with "pub"
2438 // in order to be exported.
2439 if !import_resolution.is_public {
2440 return UnboundResult;
2443 match import_resolution.
2444 target_for_namespace(namespace) {
2446 return UnboundResult;
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);
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,
2467 value_used_reexport = import_resolution.is_public;
2469 if type_result.is_unknown() {
2470 type_result = get_binding(self, import_resolution,
2472 type_used_reexport = import_resolution.is_public;
2477 // The import is unresolved. Bail out.
2478 debug!("(resolving single import) unresolved import; \
2480 return Indeterminate;
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;
2491 BoundResult(..) => {}
2493 match containing_module.external_module_children.borrow_mut()
2494 .find_copy(&source.name) {
2495 None => {} // Continue.
2497 debug!("(resolving single import) found external \
2500 Rc::new(Resolver::create_name_bindings_from_module(
2502 type_result = BoundResult(containing_module.clone(),
2504 type_used_public = true;
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);
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,
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);
2531 UnboundResult => { /* Continue. */ }
2533 fail!("value result should be known at this point");
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,
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);
2554 UnboundResult => { /* Continue. */ }
2556 fail!("type result should be known at this point");
2560 self.check_for_conflicts_between_imports_and_items(
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)));
2572 let value_used_public = value_used_reexport || value_used_public;
2573 let type_used_public = type_used_reexport || type_used_public;
2575 assert!(import_resolution.outstanding_references >= 1);
2576 import_resolution.outstanding_references -= 1;
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))}
2588 // AllPublic here and below is a dummy value, it should never be used because
2589 // _exists is false.
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))}
2602 self.last_private.insert(directive.id, LastImport{value_priv: value_private,
2604 type_priv: type_private,
2607 debug!("(resolving single import) successfully resolved import");
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,
2616 containing_module: Rc<Module>,
2617 import_directive: &ImportDirective,
2619 -> ResolveResult<()> {
2620 let id = import_directive.id;
2621 let is_public = import_directive.is_public;
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
2626 debug!("(resolving glob import) resolving glob import {}", id);
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;
2636 assert_eq!(containing_module.glob_count.get(), 0);
2638 // Add all resolved imports from the containing module.
2639 let import_resolutions = containing_module.import_resolutions
2641 for (ident, target_import_resolution) in import_resolutions.iter() {
2642 debug!("(resolving glob import) writing module resolution \
2644 target_import_resolution.type_target.is_none(),
2645 self.module_to_string(module_));
2647 if !target_import_resolution.is_public {
2648 debug!("(resolving glob import) nevermind, just kidding");
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
2659 match target_import_resolution.value_target {
2663 Some(ref value_target) => {
2664 dest_import_resolution.value_target =
2665 Some(value_target.clone());
2668 match target_import_resolution.type_target {
2672 Some(ref type_target) => {
2673 dest_import_resolution.type_target =
2674 Some(type_target.clone());
2677 dest_import_resolution.is_public = is_public;
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();
2690 import_resolutions.insert(*ident, new_import_resolution);
2693 // Add all children from the containing module.
2694 self.populate_module_if_necessary(&containing_module);
2696 for (&name, name_bindings) in containing_module.children
2698 self.merge_import_resolution(module_,
2699 containing_module.clone(),
2702 name_bindings.clone());
2706 // Add external module children from the containing module.
2707 for (&name, module) in containing_module.external_module_children
2710 Rc::new(Resolver::create_name_bindings_from_module(module.clone()));
2711 self.merge_import_resolution(module_,
2712 containing_module.clone(),
2718 // Record the destination of this import
2719 match containing_module.def_id.get() {
2721 self.def_map.borrow_mut().insert(id, DefMod(did));
2722 self.last_private.insert(id, lp);
2727 debug!("(resolving glob import) successfully resolved import");
2731 fn merge_import_resolution(&mut self,
2733 containing_module: Rc<Module>,
2734 import_directive: &ImportDirective,
2736 name_bindings: Rc<NameBindings>) {
2737 let id = import_directive.id;
2738 let is_public = import_directive.is_public;
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)
2746 debug!("(resolving glob import) writing resolution `{}` in `{}` \
2748 token::get_name(name).get().to_string(),
2749 self.module_to_string(&*containing_module),
2750 self.module_to_string(module_));
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;
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;
2769 dest_import_resolution.is_public = is_public;
2771 self.check_for_conflicts_between_imports_and_items(
2773 dest_import_resolution,
2774 import_directive.span,
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>,
2783 namespace: Namespace) {
2784 if self.session.features.import_shadowing.get() {
2789 Some(ref target) if !target.shadowable => {
2790 let msg = format!("a {} named `{}` has already been imported \
2796 token::get_name(name).get());
2797 self.session.span_err(import_span, msg.as_slice());
2799 Some(_) | None => {}
2803 /// Checks that imported names and items don't have the same name.
2804 fn check_for_conflicts_between_imports_and_items(&mut self,
2807 &mut ImportResolution,
2810 if self.session.features.import_shadowing.get() {
2814 // First, check for conflicts between imports and `extern crate`s.
2815 if module.external_module_children
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());
2825 Some(_) | None => {}
2829 // Check for item conflicts.
2830 let children = module.children.borrow();
2831 let name_bindings = match children.find(&name) {
2833 // There can't be any conflicts.
2836 Some(ref name_bindings) => (*name_bindings).clone(),
2839 match import_resolution.value_target {
2840 Some(ref target) if !target.shadowable => {
2841 match *name_bindings.value_def.borrow() {
2843 Some(ref value) => {
2844 let msg = format!("import `{}` conflicts with value \
2846 token::get_name(name).get());
2847 self.session.span_err(import_span, msg.as_slice());
2848 match value.value_span {
2853 "note conflicting value here");
2859 Some(_) | None => {}
2862 match import_resolution.type_target {
2863 Some(ref target) if !target.shadowable => {
2864 match *name_bindings.type_def.borrow() {
2867 let msg = format!("import `{}` conflicts with type in \
2869 token::get_name(name).get());
2870 self.session.span_err(import_span, msg.as_slice());
2871 match ty.type_span {
2876 "note conflicting type here")
2882 Some(_) | None => {}
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,
2892 if self.session.features.import_shadowing.get() {
2896 if module.external_module_children.borrow().contains_key(&name) {
2899 format!("an external crate named `{}` has already \
2900 been imported into this module",
2901 token::get_name(name).get()).as_slice());
2905 /// Checks that the names of items don't collide with external crates.
2906 fn check_for_conflicts_between_external_crates_and_items(&self,
2910 if self.session.features.import_shadowing.get() {
2914 if module.external_module_children.borrow().contains_key(&name) {
2917 format!("the name `{}` conflicts with an external \
2918 crate that has been imported into this \
2920 token::get_name(name).get()).as_slice());
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],
2930 name_search_type: NameSearchType,
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()
2937 .map(|_| module.clone())
2939 match module.parent_link.clone() {
2940 ModuleParentLink(parent, _) => {
2941 search_parent_externals(needle,
2942 &parent.upgrade().unwrap())
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;
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(),
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());
2971 match search_parent_externals(name.name,
2972 &self.current_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);
2979 let prefix = if target_mod_str == current_mod_str {
2980 "self::".to_string()
2982 format!("{}::", target_mod_str)
2985 format!("Did you mean `{}{}`?", prefix, path_str)
2987 None => format!("Maybe a missing `extern crate {}`?",
2991 format!("Could not find `{}` in `{}`.",
2996 return Failed(Some((span, msg)));
2998 Failed(err) => return Failed(err),
3000 debug!("(resolving module path for import) module \
3001 resolution is indeterminate: {}",
3002 token::get_ident(name));
3003 return Indeterminate;
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 {
3012 let msg = format!("Not a module `{}`",
3013 token::get_ident(name));
3015 return Failed(Some((span, msg)));
3017 Some(ref module_def) => {
3018 // If we're doing the search for an
3019 // import, do not allow traits and impls
3021 match (name_search_type,
3022 module_def.kind.get()) {
3023 (ImportSearch, TraitModuleKind) |
3024 (ImportSearch, ImplModuleKind) => {
3026 "Cannot import from a trait or \
3027 type implementation".to_string();
3028 return Failed(Some((span, msg)));
3031 search_module = module_def.clone();
3033 // Keep track of the closest
3034 // private module used when
3035 // resolving this import chain.
3037 !search_module.is_public {
3038 match search_module.def_id
3042 LastMod(DependsOn(did));
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)));
3065 return Success((search_module, closest_private));
3068 /// Attempts to resolve the module part of an import directive or path
3069 /// rooted at the given module.
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,
3078 name_search_type: NameSearchType)
3079 -> ResolveResult<(Rc<Module>, LastPrivate)> {
3080 let module_path_len = module_path.len();
3081 assert!(module_path_len > 0);
3083 debug!("(resolving module path for import) processing `{}` rooted at \
3085 self.idents_to_string(module_path),
3086 self.module_to_string(&*module_));
3088 // Resolve the module prefix, if any.
3089 let module_prefix_result = self.resolve_module_prefix(module_.clone(),
3095 match module_prefix_result {
3097 let mpath = self.idents_to_string(module_path);
3098 let mpath = mpath.as_slice();
3099 match mpath.rfind(':') {
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)));
3108 None => return Failed(None),
3111 Failed(err) => return Failed(err),
3113 debug!("(resolving module path for import) indeterminate; \
3115 return Indeterminate;
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();
3127 last_private = LastMod(AllPublic);
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(
3136 Failed(err) => return Failed(err),
3138 debug!("(resolving module path for import) \
3139 indeterminate; bailing");
3140 return Indeterminate;
3142 Success(containing_module) => {
3143 search_module = containing_module;
3145 last_private = LastMod(AllPublic);
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
3160 self.resolve_module_path_from_root(search_module,
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>,
3173 namespace: Namespace)
3174 -> ResolveResult<(Target, bool)> {
3175 debug!("(resolving item in lexical scope) resolving `{}` in \
3176 namespace {:?} in `{}`",
3177 token::get_ident(name),
3179 self.module_to_string(&*module_));
3181 // The current module node is handled specially. First, check for
3182 // its immediate children.
3183 self.populate_module_if_necessary(&module_);
3185 match module_.children.borrow().find(&name.name) {
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(),
3194 Some(_) | None => { /* Not found; continue. */ }
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
3201 match module_.import_resolutions.borrow().find(&name.name) {
3203 // Not found; continue.
3205 Some(import_resolution) => {
3206 match (*import_resolution).target_for_namespace(namespace) {
3208 // Not found; continue.
3209 debug!("(resolving item in lexical scope) found \
3210 import resolution, but not in namespace {:?}",
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));
3223 // Search for external modules.
3224 if namespace == TypeNS {
3225 match module_.external_module_children.borrow().find_copy(&name.name) {
3229 Rc::new(Resolver::create_name_bindings_from_module(module));
3230 debug!("lower name bindings succeeded");
3231 return Success((Target::new(module_,
3239 // Finally, proceed up the scope chain looking for parent modules.
3240 let mut search_module = module_;
3242 // Go to the next parent.
3243 match search_module.parent_link.clone() {
3245 // No more parents. This module was unresolved.
3246 debug!("(resolving item in lexical scope) unresolved \
3248 return Failed(None);
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 \
3258 return Failed(None);
3263 AnonymousModuleKind => {
3264 search_module = parent_module_node.upgrade().unwrap();
3268 BlockParentLink(ref parent_module_node, _) => {
3269 search_module = parent_module_node.upgrade().unwrap();
3273 // Resolve the name in the parent module.
3274 match self.resolve_name_in_module(search_module.clone(),
3279 Failed(Some((span, msg))) =>
3280 self.resolve_error(span, format!("failed to resolve. {}",
3282 Failed(None) => (), // Continue up the search chain.
3284 // We couldn't see through the higher scope because of an
3285 // unresolved import higher up. Bail.
3287 debug!("(resolving item in lexical scope) indeterminate \
3288 higher scope; bailing");
3289 return Indeterminate;
3291 Success((target, used_reexport)) => {
3292 // We found the module.
3293 debug!("(resolving item in lexical scope) found name \
3295 return Success((target, used_reexport));
3301 /// Resolves a module name in the current lexical scope.
3302 fn resolve_module_in_lexical_scope(&mut self,
3303 module_: Rc<Module>,
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 {
3317 debug!("!!! (resolving module in lexical \
3318 scope) module wasn't actually a \
3320 return Failed(None);
3322 Some(ref module_def) => {
3323 return Success(module_def.clone());
3328 debug!("!!! (resolving module in lexical scope) module
3329 wasn't actually a module!");
3330 return Failed(None);
3335 debug!("(resolving module in lexical scope) indeterminate; \
3337 return Indeterminate;
3340 debug!("(resolving module in lexical scope) failed to resolve");
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_;
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),
3361 AnonymousModuleKind => module_ = new_module,
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>)
3372 match module_.kind.get() {
3373 NormalModuleKind => return module_,
3377 AnonymousModuleKind => {
3378 match self.get_nearest_normal_module_parent(module_.clone()) {
3380 Some(new_module) => new_module
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;
3397 let first_module_path_string = token::get_ident(module_path[0]);
3398 if "self" == first_module_path_string.get() {
3400 self.get_nearest_normal_module_parent_or_self(module_);
3402 } else if "super" == first_module_path_string.get() {
3404 self.get_nearest_normal_module_parent_or_self(module_);
3405 i = 0; // We'll handle `super` below.
3407 return Success(NoPrefixFound);
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() {
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;
3427 debug!("(resolving module prefix) finished resolving prefix at {}",
3428 self.module_to_string(&*containing_module));
3430 return Success(PrefixFound(containing_module, i));
3433 /// Attempts to resolve the supplied name in the given module for the
3434 /// given namespace. If successful, returns the target corresponding to
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>,
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_));
3450 // First, check the direct children of the module.
3451 self.populate_module_if_necessary(&module_);
3453 match module_.children.borrow().find(&name) {
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(),
3467 // Next, check the module's imports if necessary.
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);
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 => {
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;
3486 match import_resolution.target_for_namespace(namespace) {
3488 debug!("(resolving name in module) name found, \
3489 but not in namespace {:?}",
3493 debug!("(resolving name in module) resolved to \
3495 self.used_imports.insert((import_resolution.id(namespace), namespace));
3496 return Success((target, true));
3500 Some(..) | None => {} // Continue.
3503 // Finally, search through external children.
3504 if namespace == TypeNS {
3505 match module_.external_module_children.borrow().find_copy(&name) {
3509 Rc::new(Resolver::create_name_bindings_from_module(module));
3510 return Success((Target::new(module_,
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);
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
3531 .span_to_snippet(imports.get(index).span)
3533 if sn.as_slice().contains("::") {
3534 self.resolve_error(imports.get(index).span,
3535 "unresolved import");
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());
3543 // Descend into children and anonymous children.
3544 self.populate_module_if_necessary(&module_);
3546 for (_, child_node) in module_.children.borrow().iter() {
3547 match child_node.get_module_if_available() {
3551 Some(child_module) => {
3552 self.report_unresolved_imports(child_module);
3557 for (_, module_) in module_.anonymous_children.borrow().iter() {
3558 self.report_unresolved_imports(module_.clone());
3564 // This pass simply determines what all "export" keywords refer to and
3565 // writes the results into the export map.
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)
3571 fn record_exports(&mut self) {
3572 let root_module = self.graph_root.get_module();
3573 self.record_exports_for_module_subtree(root_module);
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.
3581 match module_.def_id.get() {
3582 Some(def_id) if def_id.krate == LOCAL_CRATE => {
3584 debug!("(recording exports for module subtree) recording \
3585 exports for local module `{}`",
3586 self.module_to_string(&*module_));
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_));
3596 debug!("(recording exports for module subtree) not recording \
3598 self.module_to_string(&*module_));
3603 self.record_exports_for_module(&*module_);
3604 self.populate_module_if_necessary(&module_);
3606 for (_, child_name_bindings) in module_.children.borrow().iter() {
3607 match child_name_bindings.get_module_if_available() {
3611 Some(child_module) => {
3612 self.record_exports_for_module_subtree(child_module);
3617 for (_, child_module) in module_.anonymous_children.borrow().iter() {
3618 self.record_exports_for_module_subtree(child_module.clone());
3622 fn record_exports_for_module(&mut self, module_: &Module) {
3623 let mut exports2 = Vec::new();
3625 self.add_exports_for_module(&mut exports2, module_);
3626 match module_.def_id.get() {
3628 self.export_map2.borrow_mut().insert(def_id.node, exports2);
3629 debug!("(computing exports) writing exports for {} (some)",
3636 fn add_exports_of_namebindings(&mut self,
3637 exports2: &mut Vec<Export2> ,
3639 namebindings: &NameBindings,
3641 match namebindings.def_for_namespace(ns) {
3643 let name = token::get_name(name);
3644 debug!("(computing exports) YES: export '{}' => {:?}",
3646 exports2.push(Export2 {
3647 name: name.get().to_string(),
3652 debug!("(computing exports) NO: {:?}", d_opt);
3657 fn add_exports_for_module(&mut self,
3658 exports2: &mut Vec<Export2> ,
3660 for (name, importresolution) in module_.import_resolutions.borrow().iter() {
3661 if !importresolution.is_public {
3664 let xs = [TypeNS, ValueNS];
3665 for &ns in xs.iter() {
3666 match importresolution.target_for_namespace(ns) {
3668 debug!("(computing exports) maybe export '{}'",
3669 token::get_name(*name));
3670 self.add_exports_of_namebindings(exports2,
3683 // We maintain a list of value ribs and type ribs.
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
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.
3699 fn with_scope(&mut self, name: Option<Ident>, f: |&mut Resolver|) {
3700 let orig_module = self.current_module.clone();
3702 // Move down in the graph.
3708 self.populate_module_if_necessary(&orig_module);
3710 match orig_module.children.borrow().find(&name.name) {
3712 debug!("!!! (with scope) didn't find `{}` in `{}`",
3713 token::get_ident(name),
3714 self.module_to_string(&*orig_module));
3716 Some(name_bindings) => {
3717 match (*name_bindings).get_module_if_available() {
3719 debug!("!!! (with scope) didn't find module \
3721 token::get_ident(name),
3722 self.module_to_string(&*orig_module));
3725 self.current_module = module_;
3735 self.current_module = orig_module;
3738 /// Wraps the given definition in the appropriate number of `def_upvar`
3745 -> Option<DefLike> {
3750 DlDef(d @ DefLocal(..)) | DlDef(d @ DefUpvar(..)) |
3751 DlDef(d @ DefArg(..)) | DlDef(d @ DefBinding(..)) => {
3753 is_ty_param = false;
3755 DlDef(d @ DefTyParam(..)) |
3756 DlDef(d @ DefSelfTy(..)) => {
3761 return Some(def_like);
3765 let mut rib_index = rib_index + 1;
3766 while rib_index < ribs.len() {
3767 match ribs[rib_index].kind {
3769 // Nothing to do. Continue.
3771 FunctionRibKind(function_id, body_id) => {
3773 def = DefUpvar(def.def_id().node,
3779 MethodRibKind(item_id, _) => {
3780 // If the def is a ty param, and came from the parent
3783 DefTyParam(_, did, _) if {
3784 self.def_map.borrow().find(&did.node).map(|x| *x)
3785 == Some(DefTyParamBinder(item_id))
3798 // This was an attempt to access an upvar inside a
3799 // named function item. This is not allowed, so we
3804 "can't capture dynamic environment in a fn item; \
3805 use the || { ... } closure form instead");
3807 // This was an attempt to use a type parameter outside
3810 self.resolve_error(span,
3811 "can't use type parameters from \
3812 outer function; try using a local \
3813 type parameter instead");
3822 // This was an attempt to access an upvar inside a
3823 // named function item. This is not allowed, so we
3828 "can't capture dynamic environment in a fn item; \
3829 use the || { ... } closure form instead");
3831 // This was an attempt to use a type parameter outside
3834 self.resolve_error(span,
3835 "can't use type parameters from \
3836 outer function; try using a local \
3837 type parameter instead");
3842 ConstantItemRibKind => {
3845 self.resolve_error(span,
3846 "cannot use an outer type \
3847 parameter in this context");
3849 // Still doesn't deal with upvars
3850 self.resolve_error(span,
3851 "attempt to use a non-constant \
3852 value in a constant");
3861 return Some(DlDef(def));
3864 fn search_ribs(&self,
3868 -> Option<DefLike> {
3869 // FIXME #4950: This should not use a while loop.
3870 // FIXME #4950: Try caching?
3872 let mut i = ribs.len();
3875 let binding_opt = ribs[i].bindings.borrow().find_copy(&name);
3878 return self.upvarify(ribs, i, def_like, span);
3889 fn resolve_crate(&mut self, krate: &ast::Crate) {
3890 debug!("(resolving crate) starting");
3892 visit::walk_crate(self, krate, ());
3895 fn resolve_item(&mut self, item: &Item) {
3896 debug!("(resolving item) resolving {}",
3897 token::get_ident(item.ident));
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
3908 self.with_constant_rib(|this| {
3909 this.resolve_expr(&**dis_expr);
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,
3922 this.resolve_type_parameters(&generics.ty_params);
3923 this.resolve_where_clause(&generics.where_clause);
3924 visit::walk_item(this, item, ());
3928 ItemTy(_, ref generics) => {
3929 self.with_type_parameter_rib(HasTypeParameters(generics,
3934 this.resolve_type_parameters(&generics.ty_params);
3935 visit::walk_item(this, item, ());
3939 ItemImpl(ref generics,
3940 ref implemented_traits,
3942 ref impl_items) => {
3943 self.resolve_implementation(item.id,
3947 impl_items.as_slice());
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);
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);
3960 // Create a new rib for the trait-wide type parameters.
3961 self.with_type_parameter_rib(HasTypeParameters(generics,
3966 this.resolve_type_parameters(&generics.ty_params);
3967 this.resolve_where_clause(&generics.where_clause);
3969 this.resolve_type_parameter_bounds(item.id, bounds,
3973 &Some(ast::TraitTyParamBound(ref tpb)) => {
3974 this.resolve_trait_reference(item.id, tpb, TraitDerivation);
3979 for method in (*methods).iter() {
3980 // Create a new rib for the method-specific type
3983 // FIXME #4951: Do we need a node ID here?
3986 ast::RequiredMethod(ref ty_m) => {
3987 this.with_type_parameter_rib
3988 (HasTypeParameters(&ty_m.generics,
3991 MethodRibKind(item.id, RequiredMethod)),
3994 // Resolve the method-specific type
3996 this.resolve_type_parameters(
3997 &ty_m.generics.ty_params);
3998 this.resolve_where_clause(&ty_m.generics
4001 for argument in ty_m.decl.inputs.iter() {
4002 this.resolve_type(&*argument.ty);
4005 match ty_m.explicit_self.node {
4006 SelfExplicit(ref typ, _) => {
4007 this.resolve_type(&**typ)
4012 this.resolve_type(&*ty_m.decl.output);
4015 ast::ProvidedMethod(ref m) => {
4016 this.resolve_method(MethodRibKind(item.id,
4017 ProvidedMethod(m.id)),
4024 self.type_ribs.borrow_mut().pop();
4027 ItemStruct(ref struct_def, ref generics) => {
4028 self.resolve_struct(item.id,
4030 struct_def.super_struct,
4031 struct_def.fields.as_slice());
4034 ItemMod(ref module_) => {
4035 self.with_scope(Some(item.ident), |this| {
4036 this.resolve_module(module_, item.span, item.ident,
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(
4048 generics, FnSpace, foreign_item.id,
4050 |this| visit::walk_foreign_item(this,
4054 ForeignItemStatic(..) => {
4055 visit::walk_foreign_item(this,
4064 ItemFn(fn_decl, _, _, ref generics, block) => {
4065 self.resolve_function(ItemRibKind,
4076 self.with_constant_rib(|this| {
4077 visit::walk_item(this, item, ());
4082 // do nothing, these are just around to be encoded
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,
4094 let function_type_rib = Rib::new(rib_kind);
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,
4100 let def_like = DlDef(DefTyParam(space,
4101 local_def(type_parameter.id),
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);
4111 self.type_ribs.borrow_mut().push(function_type_rib);
4114 NoTypeParameters => {
4121 match type_parameters {
4122 HasTypeParameters(..) => { self.type_ribs.borrow_mut().pop(); }
4123 NoTypeParameters => { }
4127 fn with_label_rib(&mut self, f: |&mut Resolver|) {
4128 self.label_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4130 self.label_ribs.borrow_mut().pop();
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));
4137 self.type_ribs.borrow_mut().pop();
4138 self.value_ribs.borrow_mut().pop();
4141 fn resolve_function(&mut self,
4143 optional_declaration: Option<P<FnDecl>>,
4144 type_parameters: TypeParameters,
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);
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);
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 => {
4161 HasTypeParameters(ref generics, _, _, _) => {
4162 this.resolve_type_parameters(&generics.ty_params);
4163 this.resolve_where_clause(&generics.where_clause);
4167 // Add each argument to the rib.
4168 match optional_declaration {
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);
4179 this.resolve_type(&*argument.ty);
4181 debug!("(resolving function) recorded argument");
4184 this.resolve_type(&*declaration.output);
4188 // Resolve the function body.
4189 this.resolve_block(&*block);
4191 debug!("(resolving function) leaving function");
4194 self.label_ribs.borrow_mut().pop();
4195 self.value_ribs.borrow_mut().pop();
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);
4205 match &type_parameter.unbound {
4206 &Some(ref unbound) =>
4207 self.resolve_type_parameter_bound(
4208 type_parameter.id, unbound, TraitBoundingTypeParameter),
4211 match type_parameter.default {
4212 Some(ref ty) => self.resolve_type(&**ty),
4218 fn resolve_type_parameter_bounds(&mut self,
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,
4228 fn resolve_type_parameter_bound(&mut self,
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)
4236 UnboxedFnTyParamBound(ref unboxed_function) => {
4237 for argument in unboxed_function.decl.inputs.iter() {
4238 self.resolve_type(&*argument.ty);
4241 self.resolve_type(&*unboxed_function.decl.output);
4243 RegionTyParamBound(..) => {}
4247 fn resolve_trait_reference(&mut self,
4249 trait_reference: &TraitRef,
4250 reference_type: TraitReferenceType) {
4251 match self.resolve_path(id, &trait_reference.path, TypeNS, true) {
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",
4260 let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str);
4261 self.resolve_error(trait_reference.path.span, msg.as_slice());
4265 (DefTrait(_), _) => {
4266 debug!("(resolving trait) found trait def: {:?}", def);
4267 self.record_def(trait_reference.ref_id, 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)));
4275 // If it's a typedef, give a note
4278 self.session.span_note(
4279 trait_reference.path.span,
4280 format!("`type` aliases cannot \
4281 be used for traits")
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,
4299 Some((def @ DefTyParam(_, _, _), last_private)) => {
4300 self.record_def(predicate.id, (def, last_private));
4305 format!("undeclared type parameter `{}`",
4307 predicate.ident)).as_slice());
4311 for bound in predicate.bounds.iter() {
4312 self.resolve_type_parameter_bound(predicate.id, bound,
4313 TraitBoundingTypeParameter);
4318 fn resolve_struct(&mut self,
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,
4329 // Resolve the type parameters.
4330 this.resolve_type_parameters(&generics.ty_params);
4331 this.resolve_where_clause(&generics.where_clause);
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
4345 debug!("(resolving struct) writing resolution for `{}` (id {})",
4346 this.path_idents_to_string(path),
4348 this.record_def(path_id, (def, lp));
4350 Some((DefStruct(_), _)) => {
4351 span_err!(this.session, t.span, E0154,
4352 "super-struct is defined in a different crate");
4355 span_err!(this.session, t.span, E0155,
4356 "super-struct is not a struct type");
4359 span_err!(this.session, t.span, E0156,
4360 "super-struct could not be resolved");
4364 _ => this.session.span_bug(t.span, "path not mapped to a TyPath")
4370 for field in fields.iter() {
4371 this.resolve_type(&*field.node.ty);
4376 // Does this really need to take a RibKind or is it always going
4377 // to be NormalRibKind?
4378 fn resolve_method(&mut self,
4381 let method_generics = method.pe_generics();
4382 let type_parameters = HasTypeParameters(method_generics,
4387 match method.pe_explicit_self().node {
4388 SelfExplicit(ref typ, _) => self.resolve_type(&**typ),
4392 self.resolve_function(rib_kind,
4393 Some(method.pe_fn_decl()),
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;
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);
4413 match self.def_map.borrow().find(&trait_ref.ref_id) {
4415 let did = def.def_id();
4416 Some((did, trait_ref.clone()))
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;
4429 fn resolve_implementation(&mut self,
4431 generics: &Generics,
4432 opt_trait_reference: &Option<TraitRef>,
4434 impl_items: &[ImplItem]) {
4435 // If applicable, create a rib for the type parameters.
4436 self.with_type_parameter_rib(HasTypeParameters(generics,
4441 // Resolve the type parameters.
4442 this.resolve_type_parameters(&generics.ty_params);
4443 this.resolve_where_clause(&generics.where_clause);
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);
4450 this.with_current_self_type(self_type, |this| {
4451 for impl_item in impl_items.iter() {
4453 MethodImplItem(method) => {
4454 // If this is a trait impl, ensure the method
4456 this.check_trait_item(method.pe_ident(),
4459 // We also need a new scope for the method-
4460 // specific type parameters.
4461 this.resolve_method(
4463 ProvidedMethod(method.id)),
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;
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());
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, ());
4495 fn resolve_local(&mut self, local: &Local) {
4496 // Resolve the type.
4497 self.resolve_type(&*local.ty);
4499 // Resolve the initializer, if necessary.
4504 Some(ref initializer) => {
4505 self.resolve_expr(&**initializer);
4509 // Resolve the pattern.
4510 let mut bindings_list = HashMap::new();
4511 self.resolve_pattern(&*local.pat,
4512 LocalIrrefutableMode,
4513 &mut bindings_list);
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);
4525 binding_info {span: sp,
4526 binding_mode: binding_mode});
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 {
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);
4541 for (&key, &binding_0) in map_0.iter() {
4542 match map_i.find(&key) {
4546 format!("variable `{}` from pattern #1 is \
4547 not bound in pattern #{}",
4548 token::get_name(key),
4551 Some(binding_i) => {
4552 if binding_0.binding_mode != binding_i.binding_mode {
4555 format!("variable `{}` is bound with different \
4556 mode in pattern #{} than in pattern #1",
4557 token::get_name(key),
4564 for (&key, &binding) in map_i.iter() {
4565 if !map_0.contains_key(&key) {
4568 format!("variable `{}` from pattern {}{} is \
4569 not bound in pattern {}1",
4570 token::get_name(key),
4571 "#", i + 1, "#").as_slice());
4577 fn resolve_arm(&mut self, arm: &Arm) {
4578 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4580 let mut bindings_list = HashMap::new();
4581 for pattern in arm.pats.iter() {
4582 self.resolve_pattern(&**pattern, RefutableMode, &mut bindings_list);
4585 // This has to happen *after* we determine which
4586 // pat_idents are variants
4587 self.check_consistent_bindings(arm);
4589 visit::walk_expr_opt(self, arm.guard, ());
4590 self.resolve_expr(&*arm.body);
4592 self.value_ribs.borrow_mut().pop();
4595 fn resolve_block(&mut self, block: &Block) {
4596 debug!("(resolving block) entering block");
4597 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
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 \
4606 self.current_module = anonymous_module.clone();
4610 // Descend into the block.
4611 visit::walk_block(self, block, ());
4614 self.current_module = orig_module;
4616 self.value_ribs.borrow_mut().pop();
4617 debug!("(resolving block) leaving block");
4620 fn resolve_type(&mut self, ty: &Ty) {
4622 // Like path expressions, the interpretation of path types depends
4623 // on whether the path has multiple elements in it or not.
4625 TyPath(ref path, ref bounds, path_id) => {
4626 // This is a path in the type namespace. Walk through scopes
4628 let mut result_def = None;
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;
4634 match self.primitive_type_table
4638 Some(&primitive_type) => {
4640 Some((DefPrimTy(primitive_type), LastMod(AllPublic)));
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
4649 .any(|s| s.types.len() > 0) {
4650 span_err!(self.session, path.span, E0153,
4651 "type parameters are not allowed on this type");
4662 match self.resolve_path(ty.id, path, TypeNS, true) {
4664 debug!("(resolving type) resolved `{}` to \
4666 token::get_ident(path.segments
4670 result_def = Some(def);
4677 Some(_) => {} // Continue.
4682 // Write the result into the def map.
4683 debug!("(resolving type) writing resolution for `{}` \
4685 self.path_idents_to_string(path),
4687 self.record_def(path_id, def);
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());
4696 bounds.as_ref().map(|bound_vec| {
4697 self.resolve_type_parameter_bounds(ty.id, bound_vec,
4698 TraitBoundingTypeParameter);
4702 TyClosure(c) | TyProc(c) => {
4703 self.resolve_type_parameter_bounds(ty.id, &c.bounds,
4704 TraitBoundingTypeParameter);
4705 visit::walk_ty(self, ty, ());
4709 // Just resolve embedded types.
4710 visit::walk_ty(self, ty, ());
4715 fn resolve_pattern(&mut self,
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, _) => {
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
4735 let ident = path1.node;
4736 let renamed = mtwt::resolve(ident);
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));
4745 self.enforce_default_binding_mode(
4749 self.record_def(pattern.id, (def, lp));
4751 FoundStructOrEnumVariant(..) => {
4754 format!("declaration of `{}` shadows an enum \
4755 variant or unit-like struct in \
4757 token::get_name(renamed)).as_slice());
4759 FoundConst(def, lp) if mode == RefutableMode => {
4760 debug!("(resolving pattern) resolving `{}` to \
4762 token::get_name(renamed));
4764 self.enforce_default_binding_mode(
4768 self.record_def(pattern.id, (def, lp));
4771 self.resolve_error(pattern.span,
4772 "only irrefutable patterns \
4775 BareIdentifierPatternUnresolved => {
4776 debug!("(resolving pattern) binding `{}`",
4777 token::get_name(renamed));
4779 let def = match mode {
4781 // For pattern arms, we must use
4782 // `def_binding` definitions.
4784 DefBinding(pattern.id, binding_mode)
4786 LocalIrrefutableMode => {
4787 // But for locals, we use `def_local`.
4788 DefLocal(pattern.id, binding_mode)
4790 ArgumentIrrefutableMode => {
4791 // And for function arguments, `def_arg`.
4792 DefArg(pattern.id, binding_mode)
4796 // Record the definition so that later passes
4797 // will be able to distinguish variants from
4798 // locals in patterns.
4800 self.record_def(pattern.id, (def, LastMod(AllPublic)));
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.)
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(
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) ==
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 \
4825 token::get_ident(ident)).as_slice());
4827 // Else, not bound in the same pattern: do
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);
4843 self.resolve_error(path.span,
4844 format!("`{}` is not an enum variant, struct or const",
4849 .identifier)).as_slice());
4852 self.resolve_error(path.span,
4853 format!("unresolved enum variant, struct or const `{}`",
4858 .identifier)).as_slice());
4862 // Check the types in the path pattern.
4863 for ty in path.segments
4865 .flat_map(|s| s.types.iter()) {
4866 self.resolve_type(&**ty);
4870 PatLit(ref expr) => {
4871 self.resolve_expr(&**expr);
4874 PatRange(ref first_expr, ref last_expr) => {
4875 self.resolve_expr(&**first_expr);
4876 self.resolve_expr(&**last_expr);
4879 PatStruct(ref path, _, _) => {
4880 match self.resolve_path(pat_id, path, TypeNS, false) {
4881 Some(definition) => {
4882 self.record_def(pattern.id, definition);
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());
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,
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() {
4915 fail!("resolved name in the value namespace to a \
4916 set of name bindings with no 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.
4923 def @ DefVariant(..) | def @ DefStruct(..) => {
4924 return FoundStructOrEnumVariant(def, LastMod(AllPublic));
4926 def @ DefStatic(_, false) => {
4927 return FoundConst(def, LastMod(AllPublic));
4929 DefStatic(_, true) => {
4930 self.resolve_error(span,
4931 "mutable static variables cannot be referenced in a pattern");
4932 return BareIdentifierPatternUnresolved;
4935 return BareIdentifierPatternUnresolved;
4943 fail!("unexpected indeterminate result");
4947 Some((span, msg)) => {
4948 self.resolve_error(span, format!("failed to resolve: {}",
4954 debug!("(resolve bare identifier pattern) failed to find {}",
4955 token::get_ident(name));
4956 return BareIdentifierPatternUnresolved;
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,
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);
4974 return self.resolve_crate_relative_path(path, namespace);
4977 let unqualified_def =
4978 self.resolve_identifier(path.segments
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 => {
4990 .add_lint(lint::builtin::UNNECESSARY_QUALIFICATION,
4993 "unnecessary qualification".to_string());
5001 return unqualified_def;
5004 // resolve a single identifier (used as a varref)
5005 fn resolve_identifier(&mut self,
5007 namespace: Namespace,
5010 -> Option<(Def, LastPrivate)> {
5012 match self.resolve_identifier_in_local_ribs(identifier,
5016 return Some((def, LastMod(AllPublic)));
5024 return self.resolve_item_by_identifier_in_lexical_scope(identifier,
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>,
5032 namespace: Namespace)
5034 // First, search children.
5035 self.populate_module_if_necessary(&containing_module);
5037 match containing_module.children.borrow().find(&name) {
5038 Some(child_name_bindings) => {
5039 match child_name_bindings.def_for_namespace(namespace) {
5041 // Found it. Stop the search here.
5042 let p = child_name_bindings.defined_in_public_namespace(
5044 let lp = if p {LastMod(AllPublic)} else {
5045 LastMod(DependsOn(def.def_id()))
5047 return ChildNameDefinition(def, lp);
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) {
5060 match target.bindings.def_for_namespace(namespace) {
5063 let id = import_resolution.id(namespace);
5064 self.used_imports.insert((id, namespace));
5065 return ImportNameDefinition(def, LastMod(AllPublic));
5068 // This can happen with external impls, due to
5069 // the imperfect way we read the metadata.
5076 Some(..) | None => {} // Continue.
5079 // Finally, search through external children.
5080 if namespace == TypeNS {
5081 match containing_module.external_module_children.borrow()
5085 match module.def_id.get() {
5086 None => {} // Continue.
5088 let lp = if module.is_public {LastMod(AllPublic)} else {
5089 LastMod(DependsOn(def_id))
5091 return ChildNameDefinition(DefMod(def_id), lp);
5098 return NoNameDefinition;
5101 // resolve a "module-relative" path, e.g. a::b::c
5102 fn resolve_module_relative_path(&mut self,
5104 namespace: Namespace)
5105 -> Option<(Def, LastPrivate)> {
5106 let module_path_idents = path.segments.init().iter()
5107 .map(|ps| ps.identifier)
5108 .collect::<Vec<_>>();
5110 let containing_module;
5112 let module = self.current_module.clone();
5113 match self.resolve_module_path(module,
5114 module_path_idents.as_slice(),
5119 let (span, msg) = match err {
5120 Some((span, msg)) => (span, msg),
5122 let msg = format!("Use of undeclared module `{}`",
5123 self.idents_to_string(
5124 module_path_idents.as_slice()));
5129 self.resolve_error(span, format!("failed to resolve. {}",
5133 Indeterminate => fail!("indeterminate unexpected"),
5134 Success((resulting_module, resulting_last_private)) => {
5135 containing_module = resulting_module;
5136 last_private = resulting_last_private;
5140 let ident = path.segments.last().unwrap().identifier;
5141 let def = match self.resolve_definition_of_name_in_module(containing_module.clone(),
5144 NoNameDefinition => {
5145 // We failed to resolve the name. Report an error.
5148 ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
5149 (def, last_private.or(lp))
5152 match containing_module.kind.get() {
5153 TraitModuleKind | ImplModuleKind => {
5154 match containing_module.def_id.get() {
5156 match self.trait_item_map.borrow().find(&(ident.name, def_id)) {
5157 Some(&StaticMethodTraitItemKind) => (),
5160 debug!("containing module was a trait or impl \
5161 and name was a method -> not resolved");
5174 /// Invariant: This must be called only during main resolution, not during
5175 /// import resolution.
5176 fn resolve_crate_relative_path(&mut self,
5178 namespace: Namespace)
5179 -> Option<(Def, LastPrivate)> {
5180 let module_path_idents = path.segments.init().iter()
5181 .map(|ps| ps.identifier)
5182 .collect::<Vec<_>>();
5184 let root_module = self.graph_root.get_module();
5186 let containing_module;
5188 match self.resolve_module_path_from_root(root_module,
5189 module_path_idents.as_slice(),
5193 LastMod(AllPublic)) {
5195 let (span, msg) = match err {
5196 Some((span, msg)) => (span, msg),
5198 let msg = format!("Use of undeclared module `::{}`",
5199 self.idents_to_string(
5200 module_path_idents.as_slice()));
5205 self.resolve_error(span, format!("failed to resolve. {}",
5211 fail!("indeterminate unexpected");
5214 Success((resulting_module, resulting_last_private)) => {
5215 containing_module = resulting_module;
5216 last_private = resulting_last_private;
5220 let name = path.segments.last().unwrap().identifier.name;
5221 match self.resolve_definition_of_name_in_module(containing_module,
5224 NoNameDefinition => {
5225 // We failed to resolve the name. Report an error.
5228 ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
5229 return Some((def, last_private.or(lp)));
5234 fn resolve_identifier_in_local_ribs(&mut self,
5236 namespace: Namespace,
5239 // Check the local set of ribs.
5240 let search_result = match namespace {
5242 let renamed = mtwt::resolve(ident);
5243 self.search_ribs(self.value_ribs.borrow().as_slice(),
5247 let name = ident.name;
5248 self.search_ribs(self.type_ribs.borrow().as_slice(), name, span)
5252 match search_result {
5253 Some(DlDef(def)) => {
5254 debug!("(resolving path in local ribs) resolved `{}` to \
5256 token::get_ident(ident),
5260 Some(DlField) | Some(DlImpl(_)) | None => {
5266 fn resolve_item_by_identifier_in_lexical_scope(&mut self,
5268 namespace: Namespace)
5269 -> Option<(Def, LastPrivate)> {
5271 let module = self.current_module.clone();
5272 match self.resolve_item_in_lexical_scope(module,
5275 Success((target, _)) => {
5276 match (*target.bindings).def_for_namespace(namespace) {
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));
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)));
5297 fail!("unexpected indeterminate result");
5301 Some((span, msg)) =>
5302 self.resolve_error(span, format!("failed to resolve. {}", msg)),
5306 debug!("(resolving item path by identifier in lexical scope) \
5307 failed to resolve {}", token::get_ident(ident));
5313 fn with_no_errors<T>(&mut self, f: |&mut Resolver| -> T) -> T {
5314 self.emit_errors = false;
5316 self.emit_errors = true;
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());
5326 fn find_fallback_in_self_type(&mut self, name: Name) -> FallbackSuggestion {
5327 #[deriving(PartialEq)]
5328 enum FallbackChecks {
5333 fn extract_path_and_node_id(t: &Ty, allow: FallbackChecks)
5334 -> Option<(Path, NodeId, FallbackChecks)> {
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.
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;
5351 if ident_path.len() == 1 {
5352 match this.primitive_type_table.primitive_types.find(&last_name) {
5355 match this.current_module.children.borrow().find(&last_name) {
5356 Some(child) => child.get_module_if_available(),
5362 match this.resolve_module_path(root,
5363 ident_path.as_slice(),
5367 Success((module, _)) => Some(module),
5373 let (path, node_id, allowed) = match self.current_self_type {
5374 Some(ref ty) => match extract_path_and_node_id(ty, Everything) {
5376 None => return NoSuggestion,
5378 None => return NoSuggestion,
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) {
5385 | Some(&DefStruct(did))
5386 | Some(&DefVariant(_, did, _)) => match self.structs.find(&did) {
5389 if fields.iter().any(|&field_name| name == field_name) {
5394 _ => {} // Self type didn't resolve properly
5398 let ident_path = path.segments.iter().map(|seg| seg.identifier).collect::<Vec<_>>();
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) {
5404 let p_str = self.path_idents_to_string(&path);
5405 match binding.def_for_namespace(ValueNS) {
5406 Some(DefStaticMethod(_, provenance, _)) => {
5408 FromImpl(_) => return StaticMethod(p_str),
5409 FromTrait(_) => unreachable!()
5412 Some(DefMethod(_, None)) if allowed == Everything => return Method,
5413 Some(DefMethod(_, Some(_))) => return TraitItem,
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);
5428 match trait_item_map.find(&(name, did)) {
5429 Some(&StaticMethodTraitItemKind) => return StaticTraitMethod(path_str),
5430 Some(_) => return TraitItem,
5440 fn find_best_match_for_name(&mut self, name: &str, max_distance: uint)
5442 let this = &mut *self;
5444 let mut maybes: Vec<token::InternedString> = Vec::new();
5445 let mut values: Vec<uint> = Vec::new();
5447 let mut j = this.value_ribs.borrow().len();
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);
5458 let mut smallest = 0;
5459 for (i, other) in maybes.iter().enumerate() {
5460 *values.get_mut(i) = name.lev_distance(other.get());
5462 if *values.get(i) <= *values.get(smallest) {
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() {
5473 Some(maybes.get(smallest).get().to_string())
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.
5484 self.record_candidate_traits_for_expr_if_necessary(expr);
5486 // Next, resolve the node.
5488 // The interpretation of paths depends on whether the path has
5489 // multiple elements in it or not.
5491 ExprPath(ref path) => {
5492 // This is a local path in the value namespace. Walk through
5493 // scopes looking for it.
5495 match self.resolve_path(expr.id, path, ValueNS, true) {
5497 // Write the result into the def map.
5498 debug!("(resolving expr) resolved `{}`",
5499 self.path_idents_to_string(path));
5501 // First-class methods are not supported yet; error
5504 (DefMethod(..), _) => {
5505 self.resolve_error(expr.span,
5506 "first-class methods \
5507 are not supported");
5508 self.session.span_note(expr.span,
5516 self.record_def(expr.id, def);
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 \
5531 uses it like a function name",
5532 wrong_name).as_slice());
5534 self.session.span_note(expr.span,
5535 format!("Did you mean to write: \
5536 `{} {{ /* fields */ }}`?",
5537 wrong_name).as_slice());
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
5550 false // Stop advancing
5553 if method_scope && token::get_name(self.self_name).get()
5554 == wrong_name.as_slice() {
5557 "`self` is not available \
5558 in a static method. Maybe a \
5559 `self` argument is missing?");
5561 let last_name = path.segments.last().unwrap().identifier.name;
5562 let mut msg = match self.find_fallback_in_self_type(last_name) {
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))
5571 format!("`self.{}`", wrong_name),
5574 format!("to call `self.{}`", wrong_name),
5575 StaticTraitMethod(path_str)
5576 | StaticMethod(path_str) =>
5577 format!("to call `{}::{}`", path_str, wrong_name)
5581 msg = format!(" Did you mean {}?", msg)
5586 format!("unresolved name `{}`.{}",
5595 visit::walk_expr(self, expr, ());
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,
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),
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());
5621 visit::walk_expr(self, expr, ());
5624 ExprLoop(_, Some(label)) | ExprWhile(_, _, Some(label)) => {
5625 self.with_label_rib(|this| {
5626 let def_like = DlDef(DefLabel(expr.id));
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);
5636 visit::walk_expr(this, expr, ());
5640 ExprForLoop(ref pattern, ref head, ref body, optional_label) => {
5641 self.resolve_expr(&**head);
5643 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
5645 self.resolve_pattern(&**pattern,
5646 LocalIrrefutableMode,
5647 &mut HashMap::new());
5649 match optional_label {
5654 .push(Rib::new(NormalRibKind));
5655 let def_like = DlDef(DefLabel(expr.id));
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,
5668 self.resolve_block(&**body);
5670 if optional_label.is_some() {
5671 drop(self.label_ribs.borrow_mut().pop())
5674 self.value_ribs.borrow_mut().pop();
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) {
5684 format!("use of undeclared label `{}`",
5685 token::get_ident(label)).as_slice())
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)))
5692 self.session.span_bug(expr.span,
5693 "label wasn't mapped to a \
5700 visit::walk_expr(self, expr, ());
5705 fn record_candidate_traits_for_expr_if_necessary(&mut self, expr: &Expr) {
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);
5715 ExprMethodCall(ident, _, _) => {
5716 debug!("(recording candidate traits for expr) recording \
5719 let traits = self.search_for_traits_containing_method(ident.node.name);
5720 self.trait_map.insert(expr.id, traits);
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));
5732 fn add_trait_info(found_traits: &mut Vec<DefId>,
5733 trait_def_id: DefId,
5735 debug!("(adding trait info) found trait {}:{} for method '{}'",
5738 token::get_name(name));
5739 found_traits.push(trait_def_id);
5742 let mut found_traits = Vec::new();
5743 let mut search_module = self.current_module.clone();
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();
5750 if trait_item_map.contains_key(&(name, trait_def_id)) {
5751 add_trait_info(&mut found_traits, trait_def_id, name);
5754 None => {} // Nothing to do.
5757 // Look for trait children.
5758 self.populate_module_if_necessary(&search_module);
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) {
5767 let trait_def_id = match def {
5768 DefTrait(trait_def_id) => trait_def_id,
5771 if trait_item_map.contains_key(&(name, trait_def_id)) {
5772 add_trait_info(&mut found_traits, trait_def_id, name);
5777 // Look for imports.
5778 for (_, import) in search_module.import_resolutions.borrow().iter() {
5779 let target = match import.target_for_namespace(TypeNS) {
5781 Some(target) => target,
5783 let did = match target.bindings.def_for_namespace(TypeNS) {
5784 Some(DefTrait(trait_def_id)) => trait_def_id,
5785 Some(..) | None => continue,
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));
5793 match search_module.parent_link.clone() {
5794 NoParentLink | ModuleParentLink(..) => break,
5795 BlockParentLink(parent_module, _) => {
5796 search_module = parent_module.upgrade().unwrap();
5804 fn record_def(&mut self, node_id: NodeId, (def, lp): (Def, LastPrivate)) {
5805 debug!("(recording def) recording {:?} for {:?}, last private {:?}",
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 {
5816 .bug(format!("node_id {:?} resolved first to {:?} and \
5825 fn enforce_default_binding_mode(&mut self,
5827 pat_binding_mode: BindingMode,
5829 match pat_binding_mode {
5830 BindByValue(_) => {}
5832 self.resolve_error(pat.span,
5833 format!("cannot use `ref` binding mode \
5841 // Unused import checking
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.
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, ());
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 }
5862 ViewItemExternCrate(..) => {} // ignore
5863 ViewItemUse(ref p) => {
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);
5871 ViewPathGlob(_, id) => {
5872 if !self.used_imports.contains(&(id, TypeNS)) &&
5873 !self.used_imports.contains(&(id, ValueNS)) {
5875 .add_lint(lint::builtin::UNUSED_IMPORTS,
5878 "unused import".to_string());
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
5893 fn finalize_import(&mut self, id: NodeId, span: Span) {
5894 debug!("finalizing import uses for {}",
5895 self.session.codemap().span_to_snippet(span));
5897 if !self.used_imports.contains(&(id, TypeNS)) &&
5898 !self.used_imports.contains(&(id, ValueNS)) {
5899 self.session.add_lint(lint::builtin::UNUSED_IMPORTS,
5902 "unused import".to_string());
5905 let (v_priv, t_priv) = match self.last_private.find(&id) {
5913 fail!("we should only have LastImport for `use` directives")
5918 let mut v_used = if self.used_imports.contains(&(id, ValueNS)) {
5923 let t_used = if self.used_imports.contains(&(id, TypeNS)) {
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,
5937 self.last_private.insert(id, LastImport{value_priv: v_priv,
5940 type_used: t_used});
5946 // Diagnostics are not particularly efficient, because they're rarely
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();
5954 fn collect_mod(idents: &mut Vec<ast::Ident>, module: &Module) {
5955 match module.parent_link {
5957 ModuleParentLink(ref module, name) => {
5959 collect_mod(idents, &*module.upgrade().unwrap());
5961 BlockParentLink(ref module, _) => {
5962 // danger, shouldn't be ident?
5963 idents.push(special_idents::opaque);
5964 collect_mod(idents, &*module.upgrade().unwrap());
5968 collect_mod(&mut idents, module);
5970 if idents.len() == 0 {
5971 return "???".to_string();
5973 self.idents_to_string(idents.move_iter().rev()
5974 .collect::<Vec<ast::Ident>>()
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_));
5982 debug!("Children:");
5983 self.populate_module_if_necessary(&module_);
5984 for (&name, _) in module_.children.borrow().iter() {
5985 debug!("* {}", token::get_name(name));
5988 debug!("Import resolutions:");
5989 let import_resolutions = module_.import_resolutions.borrow();
5990 for (&name, import_resolution) in import_resolutions.iter() {
5992 match import_resolution.target_for_namespace(ValueNS) {
5993 None => { value_repr = "".to_string(); }
5995 value_repr = " value:?".to_string();
6001 match import_resolution.target_for_namespace(TypeNS) {
6002 None => { type_repr = "".to_string(); }
6004 type_repr = " type:?".to_string();
6009 debug!("* {}:{}{}", token::get_name(name), value_repr, type_repr);
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,
6022 /// Entry point to crate resolution.
6023 pub fn resolve_crate(session: &Session,
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;
6033 exp_map2: export_map2,
6034 trait_map: trait_map,
6035 external_exports: external_exports,
6036 last_private_map: last_private,