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;
14 use metadata::csearch;
15 use metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
17 use middle::lang_items::LanguageItems;
18 use middle::lint::{UnnecessaryQualification, UnusedImports};
19 use middle::pat_util::pat_bindings;
20 use middle::subst::{ParamSpace, FnSpace, TypeSpace};
21 use util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
25 use syntax::ast_util::{local_def};
26 use syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
27 use syntax::ext::mtwt;
28 use syntax::parse::token::special_idents;
29 use syntax::parse::token;
30 use syntax::print::pprust::path_to_str;
31 use syntax::codemap::{Span, DUMMY_SP, Pos};
32 use syntax::owned_slice::OwnedSlice;
34 use syntax::visit::Visitor;
36 use std::collections::{HashMap, HashSet};
37 use std::cell::{Cell, RefCell};
38 use std::gc::{Gc, GC};
39 use std::mem::replace;
40 use std::rc::{Rc, Weak};
44 pub type DefMap = RefCell<NodeMap<Def>>;
48 binding_mode: BindingMode,
51 // Map from the name in a pattern to its binding mode.
52 type BindingMap = HashMap<Name,binding_info>;
54 // Trait method resolution
55 pub type TraitMap = NodeMap<Vec<DefId> >;
57 // This is the replacement export map. It maps a module to all of the exports
59 pub type ExportMap2 = RefCell<NodeMap<Vec<Export2> >>;
62 pub name: String, // The name of the target.
63 pub def_id: DefId, // The definition of the target.
66 // This set contains all exported definitions from external crates. The set does
67 // not contain any entries from local crates.
68 pub type ExternalExports = DefIdSet;
71 pub type LastPrivateMap = NodeMap<LastPrivate>;
73 pub enum LastPrivate {
75 // `use` directives (imports) can refer to two separate definitions in the
76 // type and value namespaces. We record here the last private node for each
77 // and whether the import is in fact used for each.
78 // If the Option<PrivateDep> fields are None, it means there is no definition
80 LastImport{pub value_priv: Option<PrivateDep>,
81 pub value_used: ImportUse,
82 pub type_priv: Option<PrivateDep>,
83 pub type_used: ImportUse},
91 // How an import is used.
92 #[deriving(PartialEq)]
94 Unused, // The import is not used.
95 Used, // The import is used.
99 fn or(self, other: LastPrivate) -> LastPrivate {
100 match (self, other) {
101 (me, LastMod(AllPublic)) => me,
107 #[deriving(PartialEq)]
108 enum PatternBindingMode {
110 LocalIrrefutableMode,
111 ArgumentIrrefutableMode,
114 #[deriving(PartialEq, Eq, Hash)]
120 #[deriving(PartialEq)]
121 enum NamespaceError {
128 /// A NamespaceResult represents the result of resolving an import in
129 /// a particular namespace. The result is either definitely-resolved,
130 /// definitely- unresolved, or unknown.
132 enum NamespaceResult {
133 /// Means that resolve hasn't gathered enough information yet to determine
134 /// whether the name is bound in this namespace. (That is, it hasn't
135 /// resolved all `use` directives yet.)
137 /// Means that resolve has determined that the name is definitely
138 /// not bound in the namespace.
140 /// Means that resolve has determined that the name is bound in the Module
141 /// argument, and specified by the NameBindings argument.
142 BoundResult(Rc<Module>, Rc<NameBindings>)
145 impl NamespaceResult {
146 fn is_unknown(&self) -> bool {
148 UnknownResult => true,
152 fn is_unbound(&self) -> bool {
154 UnboundResult => true,
160 enum NameDefinition {
161 NoNameDefinition, //< The name was unbound.
162 ChildNameDefinition(Def, LastPrivate), //< The name identifies an immediate child.
163 ImportNameDefinition(Def, LastPrivate) //< The name identifies an import.
166 impl<'a> Visitor<()> for Resolver<'a> {
167 fn visit_item(&mut self, item: &Item, _: ()) {
168 self.resolve_item(item);
170 fn visit_arm(&mut self, arm: &Arm, _: ()) {
171 self.resolve_arm(arm);
173 fn visit_block(&mut self, block: &Block, _: ()) {
174 self.resolve_block(block);
176 fn visit_expr(&mut self, expr: &Expr, _: ()) {
177 self.resolve_expr(expr);
179 fn visit_local(&mut self, local: &Local, _: ()) {
180 self.resolve_local(local);
182 fn visit_ty(&mut self, ty: &Ty, _: ()) {
183 self.resolve_type(ty);
187 /// Contains data for specific types of import directives.
188 enum ImportDirectiveSubclass {
189 SingleImport(Ident /* target */, Ident /* source */),
193 /// The context that we thread through while building the reduced graph.
195 enum ReducedGraphParent {
196 ModuleReducedGraphParent(Rc<Module>)
199 impl ReducedGraphParent {
200 fn module(&self) -> Rc<Module> {
202 ModuleReducedGraphParent(ref m) => {
209 type ErrorMessage = Option<(Span, String)>;
211 enum ResolveResult<T> {
212 Failed(ErrorMessage), // Failed to resolve the name, optional helpful error message.
213 Indeterminate, // Couldn't determine due to unresolved globs.
214 Success(T) // Successfully resolved the import.
217 impl<T> ResolveResult<T> {
218 fn indeterminate(&self) -> bool {
219 match *self { Indeterminate => true, _ => false }
223 enum FallbackSuggestion {
228 StaticMethod(String),
229 StaticTraitMethod(String),
232 enum TypeParameters<'a> {
238 // Identifies the things that these parameters
239 // were declared on (type, fn, etc)
242 // ID of the enclosing item.
245 // The kind of the rib used for type parameters.
249 // The rib kind controls the translation of argument or local definitions
250 // (`def_arg` or `def_local`) to upvars (`def_upvar`).
253 // No translation needs to be applied.
256 // We passed through a function scope at the given node ID. Translate
257 // upvars as appropriate.
258 FunctionRibKind(NodeId /* func id */, NodeId /* body id */),
260 // We passed through an impl or trait and are now in one of its
261 // methods. Allow references to ty params that impl or trait
262 // binds. Disallow any other upvars (including other ty params that are
264 // parent; method itself
265 MethodRibKind(NodeId, MethodSort),
267 // We passed through an item scope. Disallow upvars.
270 // We're in a constant item. Can't refer to dynamic stuff.
274 // Methods can be required or provided. Required methods only occur in traits.
280 enum UseLexicalScopeFlag {
285 enum ModulePrefixResult {
287 PrefixFound(Rc<Module>, uint)
290 #[deriving(PartialEq)]
291 enum NameSearchType {
292 /// We're doing a name search in order to resolve a `use` directive.
295 /// We're doing a name search in order to resolve a path type, a path
296 /// expression, or a path pattern.
300 enum BareIdentifierPatternResolution {
301 FoundStructOrEnumVariant(Def, LastPrivate),
302 FoundConst(Def, LastPrivate),
303 BareIdentifierPatternUnresolved
306 // Specifies how duplicates should be handled when adding a child item if
307 // another item exists with the same name in some namespace.
308 #[deriving(PartialEq)]
309 enum DuplicateCheckingMode {
310 ForbidDuplicateModules,
311 ForbidDuplicateTypes,
312 ForbidDuplicateValues,
313 ForbidDuplicateTypesAndValues,
319 bindings: RefCell<HashMap<Name, DefLike>>,
324 fn new(kind: RibKind) -> Rib {
326 bindings: RefCell::new(HashMap::new()),
332 /// One import directive.
333 struct ImportDirective {
334 module_path: Vec<Ident>,
335 subclass: ImportDirectiveSubclass,
338 is_public: bool, // see note in ImportResolution about how to use this
341 impl ImportDirective {
342 fn new(module_path: Vec<Ident> ,
343 subclass: ImportDirectiveSubclass,
349 module_path: module_path,
353 is_public: is_public,
358 /// The item that an import resolves to.
361 target_module: Rc<Module>,
362 bindings: Rc<NameBindings>,
366 fn new(target_module: Rc<Module>, bindings: Rc<NameBindings>) -> Target {
368 target_module: target_module,
374 /// An ImportResolution represents a particular `use` directive.
375 struct ImportResolution {
376 /// Whether this resolution came from a `use` or a `pub use`. Note that this
377 /// should *not* be used whenever resolution is being performed, this is
378 /// only looked at for glob imports statements currently. Privacy testing
379 /// occurs during a later phase of compilation.
382 // The number of outstanding references to this name. When this reaches
383 // zero, outside modules can count on the targets being correct. Before
384 // then, all bets are off; future imports could override this name.
385 outstanding_references: uint,
387 /// The value that this `use` directive names, if there is one.
388 value_target: Option<Target>,
389 /// The source node of the `use` directive leading to the value target
393 /// The type that this `use` directive names, if there is one.
394 type_target: Option<Target>,
395 /// The source node of the `use` directive leading to the type target
400 impl ImportResolution {
401 fn new(id: NodeId, is_public: bool) -> ImportResolution {
405 outstanding_references: 0,
408 is_public: is_public,
412 fn target_for_namespace(&self, namespace: Namespace)
415 TypeNS => self.type_target.clone(),
416 ValueNS => self.value_target.clone(),
420 fn id(&self, namespace: Namespace) -> NodeId {
422 TypeNS => self.type_id,
423 ValueNS => self.value_id,
428 /// The link from a module up to its nearest parent node.
432 ModuleParentLink(Weak<Module>, Ident),
433 BlockParentLink(Weak<Module>, NodeId)
436 /// The type of module this is.
437 #[deriving(PartialEq)]
446 /// One node in the tree of modules.
448 parent_link: ParentLink,
449 def_id: Cell<Option<DefId>>,
450 kind: Cell<ModuleKind>,
453 children: RefCell<HashMap<Name, Rc<NameBindings>>>,
454 imports: RefCell<Vec<ImportDirective>>,
456 // The external module children of this node that were declared with
458 external_module_children: RefCell<HashMap<Name, Rc<Module>>>,
460 // The anonymous children of this node. Anonymous children are pseudo-
461 // modules that are implicitly created around items contained within
464 // For example, if we have this:
472 // There will be an anonymous module created around `g` with the ID of the
473 // entry block for `f`.
474 anonymous_children: RefCell<NodeMap<Rc<Module>>>,
476 // The status of resolving each import in this module.
477 import_resolutions: RefCell<HashMap<Name, ImportResolution>>,
479 // The number of unresolved globs that this module exports.
480 glob_count: Cell<uint>,
482 // The index of the import we're resolving.
483 resolved_import_count: Cell<uint>,
485 // Whether this module is populated. If not populated, any attempt to
486 // access the children must be preceded with a
487 // `populate_module_if_necessary` call.
488 populated: Cell<bool>,
492 fn new(parent_link: ParentLink,
493 def_id: Option<DefId>,
499 parent_link: parent_link,
500 def_id: Cell::new(def_id),
501 kind: Cell::new(kind),
502 is_public: is_public,
503 children: RefCell::new(HashMap::new()),
504 imports: RefCell::new(Vec::new()),
505 external_module_children: RefCell::new(HashMap::new()),
506 anonymous_children: RefCell::new(NodeMap::new()),
507 import_resolutions: RefCell::new(HashMap::new()),
508 glob_count: Cell::new(0),
509 resolved_import_count: Cell::new(0),
510 populated: Cell::new(!external),
514 fn all_imports_resolved(&self) -> bool {
515 self.imports.borrow().len() == self.resolved_import_count.get()
519 // Records a possibly-private type definition.
522 is_public: bool, // see note in ImportResolution about how to use this
523 module_def: Option<Rc<Module>>,
524 type_def: Option<Def>,
525 type_span: Option<Span>
528 // Records a possibly-private value definition.
531 is_public: bool, // see note in ImportResolution about how to use this
533 value_span: Option<Span>,
536 // Records the definitions (at most one for each namespace) that a name is
538 struct NameBindings {
539 type_def: RefCell<Option<TypeNsDef>>, //< Meaning in type namespace.
540 value_def: RefCell<Option<ValueNsDef>>, //< Meaning in value namespace.
543 /// Ways in which a trait can be referenced
544 enum TraitReferenceType {
545 TraitImplementation, // impl SomeTrait for T { ... }
546 TraitDerivation, // trait T : SomeTrait { ... }
547 TraitBoundingTypeParameter, // fn f<T:SomeTrait>() { ... }
551 fn new() -> NameBindings {
553 type_def: RefCell::new(None),
554 value_def: RefCell::new(None),
558 /// Creates a new module in this set of name bindings.
559 fn define_module(&self,
560 parent_link: ParentLink,
561 def_id: Option<DefId>,
566 // Merges the module with the existing type def or creates a new one.
567 let module_ = Rc::new(Module::new(parent_link, def_id, kind, external,
569 let type_def = self.type_def.borrow().clone();
572 *self.type_def.borrow_mut() = Some(TypeNsDef {
573 is_public: is_public,
574 module_def: Some(module_),
580 *self.type_def.borrow_mut() = Some(TypeNsDef {
581 is_public: is_public,
582 module_def: Some(module_),
584 type_def: type_def.type_def
590 /// Sets the kind of the module, creating a new one if necessary.
591 fn set_module_kind(&self,
592 parent_link: ParentLink,
593 def_id: Option<DefId>,
598 let type_def = self.type_def.borrow().clone();
601 let module = Module::new(parent_link, def_id, kind,
602 external, is_public);
603 *self.type_def.borrow_mut() = Some(TypeNsDef {
604 is_public: is_public,
605 module_def: Some(Rc::new(module)),
611 match type_def.module_def {
613 let module = Module::new(parent_link,
618 *self.type_def.borrow_mut() = Some(TypeNsDef {
619 is_public: is_public,
620 module_def: Some(Rc::new(module)),
621 type_def: type_def.type_def,
625 Some(module_def) => module_def.kind.set(kind),
631 /// Records a type definition.
632 fn define_type(&self, def: Def, sp: Span, is_public: bool) {
633 // Merges the type with the existing type def or creates a new one.
634 let type_def = self.type_def.borrow().clone();
637 *self.type_def.borrow_mut() = Some(TypeNsDef {
641 is_public: is_public,
645 *self.type_def.borrow_mut() = Some(TypeNsDef {
648 module_def: type_def.module_def,
649 is_public: is_public,
655 /// Records a value definition.
656 fn define_value(&self, def: Def, sp: Span, is_public: bool) {
657 *self.value_def.borrow_mut() = Some(ValueNsDef {
659 value_span: Some(sp),
660 is_public: is_public,
664 /// Returns the module node if applicable.
665 fn get_module_if_available(&self) -> Option<Rc<Module>> {
666 match *self.type_def.borrow() {
667 Some(ref type_def) => type_def.module_def.clone(),
673 * Returns the module node. Fails if this node does not have a module
676 fn get_module(&self) -> Rc<Module> {
677 match self.get_module_if_available() {
679 fail!("get_module called on a node with no module \
682 Some(module_def) => module_def
686 fn defined_in_namespace(&self, namespace: Namespace) -> bool {
688 TypeNS => return self.type_def.borrow().is_some(),
689 ValueNS => return self.value_def.borrow().is_some()
693 fn defined_in_public_namespace(&self, namespace: Namespace) -> bool {
695 TypeNS => match *self.type_def.borrow() {
696 Some(ref def) => def.is_public, None => false
698 ValueNS => match *self.value_def.borrow() {
699 Some(ref def) => def.is_public, None => false
704 fn def_for_namespace(&self, namespace: Namespace) -> Option<Def> {
707 match *self.type_def.borrow() {
709 Some(ref type_def) => {
710 match type_def.type_def {
711 Some(type_def) => Some(type_def),
713 match type_def.module_def {
714 Some(ref module) => {
715 match module.def_id.get() {
716 Some(did) => Some(DefMod(did)),
728 match *self.value_def.borrow() {
730 Some(value_def) => Some(value_def.def)
736 fn span_for_namespace(&self, namespace: Namespace) -> Option<Span> {
737 if self.defined_in_namespace(namespace) {
740 match *self.type_def.borrow() {
742 Some(ref type_def) => type_def.type_span
746 match *self.value_def.borrow() {
748 Some(ref value_def) => value_def.value_span
758 /// Interns the names of the primitive types.
759 struct PrimitiveTypeTable {
760 primitive_types: HashMap<Name, PrimTy>,
763 impl PrimitiveTypeTable {
764 fn new() -> PrimitiveTypeTable {
765 let mut table = PrimitiveTypeTable {
766 primitive_types: HashMap::new()
769 table.intern("bool", TyBool);
770 table.intern("char", TyChar);
771 table.intern("f32", TyFloat(TyF32));
772 table.intern("f64", TyFloat(TyF64));
773 table.intern("f128", TyFloat(TyF128));
774 table.intern("int", TyInt(TyI));
775 table.intern("i8", TyInt(TyI8));
776 table.intern("i16", TyInt(TyI16));
777 table.intern("i32", TyInt(TyI32));
778 table.intern("i64", TyInt(TyI64));
779 table.intern("str", TyStr);
780 table.intern("uint", TyUint(TyU));
781 table.intern("u8", TyUint(TyU8));
782 table.intern("u16", TyUint(TyU16));
783 table.intern("u32", TyUint(TyU32));
784 table.intern("u64", TyUint(TyU64));
789 fn intern(&mut self, string: &str, primitive_type: PrimTy) {
790 self.primitive_types.insert(token::intern(string), primitive_type);
795 fn namespace_error_to_str(ns: NamespaceError) -> &'static str {
798 ModuleError => "module",
800 ValueError => "value",
804 /// The main resolver class.
805 struct Resolver<'a> {
806 session: &'a Session,
808 graph_root: NameBindings,
810 method_map: RefCell<FnvHashMap<(Name, DefId), ast::ExplicitSelf_>>,
811 structs: FnvHashMap<DefId, Vec<Name>>,
813 // The number of imports that are currently unresolved.
814 unresolved_imports: uint,
816 // The module that represents the current item scope.
817 current_module: Rc<Module>,
819 // The current set of local scopes, for values.
820 // FIXME #4948: Reuse ribs to avoid allocation.
821 value_ribs: RefCell<Vec<Rib>>,
823 // The current set of local scopes, for types.
824 type_ribs: RefCell<Vec<Rib>>,
826 // The current set of local scopes, for labels.
827 label_ribs: RefCell<Vec<Rib>>,
829 // The trait that the current context can refer to.
830 current_trait_ref: Option<(DefId, TraitRef)>,
832 // The current self type if inside an impl (used for better errors).
833 current_self_type: Option<Ty>,
835 // The ident for the keyword "self".
837 // The ident for the non-keyword "Self".
838 type_self_ident: Ident,
840 // The idents for the primitive types.
841 primitive_type_table: PrimitiveTypeTable,
844 export_map2: ExportMap2,
846 external_exports: ExternalExports,
847 last_private: LastPrivateMap,
849 // Whether or not to print error messages. Can be set to true
850 // when getting additional info for error message suggestions,
851 // so as to avoid printing duplicate errors
854 used_imports: HashSet<(NodeId, Namespace)>,
857 struct BuildReducedGraphVisitor<'a, 'b> {
858 resolver: &'a mut Resolver<'b>,
861 impl<'a, 'b> Visitor<ReducedGraphParent> for BuildReducedGraphVisitor<'a, 'b> {
863 fn visit_item(&mut self, item: &Item, context: ReducedGraphParent) {
864 let p = self.resolver.build_reduced_graph_for_item(item, context);
865 visit::walk_item(self, item, p);
868 fn visit_foreign_item(&mut self, foreign_item: &ForeignItem,
869 context: ReducedGraphParent) {
870 self.resolver.build_reduced_graph_for_foreign_item(foreign_item,
873 let mut v = BuildReducedGraphVisitor{ resolver: r };
874 visit::walk_foreign_item(&mut v, foreign_item, context.clone());
878 fn visit_view_item(&mut self, view_item: &ViewItem, context: ReducedGraphParent) {
879 self.resolver.build_reduced_graph_for_view_item(view_item, context);
882 fn visit_block(&mut self, block: &Block, context: ReducedGraphParent) {
883 let np = self.resolver.build_reduced_graph_for_block(block, context);
884 visit::walk_block(self, block, np);
889 struct UnusedImportCheckVisitor<'a, 'b> { resolver: &'a mut Resolver<'b> }
891 impl<'a, 'b> Visitor<()> for UnusedImportCheckVisitor<'a, 'b> {
892 fn visit_view_item(&mut self, vi: &ViewItem, _: ()) {
893 self.resolver.check_for_item_unused_imports(vi);
894 visit::walk_view_item(self, vi, ());
898 impl<'a> Resolver<'a> {
899 fn new(session: &'a Session, crate_span: Span) -> Resolver<'a> {
900 let graph_root = NameBindings::new();
902 graph_root.define_module(NoParentLink,
903 Some(DefId { krate: 0, node: 0 }),
909 let current_module = graph_root.get_module();
914 // The outermost module has def ID 0; this is not reflected in the
917 graph_root: graph_root,
919 method_map: RefCell::new(FnvHashMap::new()),
920 structs: FnvHashMap::new(),
922 unresolved_imports: 0,
924 current_module: current_module,
925 value_ribs: RefCell::new(Vec::new()),
926 type_ribs: RefCell::new(Vec::new()),
927 label_ribs: RefCell::new(Vec::new()),
929 current_trait_ref: None,
930 current_self_type: None,
932 self_ident: special_idents::self_,
933 type_self_ident: special_idents::type_self,
935 primitive_type_table: PrimitiveTypeTable::new(),
937 def_map: RefCell::new(NodeMap::new()),
938 export_map2: RefCell::new(NodeMap::new()),
939 trait_map: NodeMap::new(),
940 used_imports: HashSet::new(),
941 external_exports: DefIdSet::new(),
942 last_private: NodeMap::new(),
947 /// The main name resolution procedure.
948 fn resolve(&mut self, krate: &ast::Crate) {
949 self.build_reduced_graph(krate);
950 self.session.abort_if_errors();
952 self.resolve_imports();
953 self.session.abort_if_errors();
955 self.record_exports();
956 self.session.abort_if_errors();
958 self.resolve_crate(krate);
959 self.session.abort_if_errors();
961 self.check_for_unused_imports(krate);
965 // Reduced graph building
967 // Here we build the "reduced graph": the graph of the module tree without
968 // any imports resolved.
971 /// Constructs the reduced graph for the entire crate.
972 fn build_reduced_graph(&mut self, krate: &ast::Crate) {
974 ModuleReducedGraphParent(self.graph_root.get_module());
976 let mut visitor = BuildReducedGraphVisitor { resolver: self, };
977 visit::walk_crate(&mut visitor, krate, initial_parent);
981 * Adds a new child item to the module definition of the parent node and
982 * returns its corresponding name bindings as well as the current parent.
983 * Or, if we're inside a block, creates (or reuses) an anonymous module
984 * corresponding to the innermost block ID and returns the name bindings
985 * as well as the newly-created parent.
987 * If this node does not have a module definition and we are not inside
992 reduced_graph_parent: ReducedGraphParent,
993 duplicate_checking_mode: DuplicateCheckingMode,
994 // For printing errors
996 -> Rc<NameBindings> {
997 // If this is the immediate descendant of a module, then we add the
998 // child name directly. Otherwise, we create or reuse an anonymous
999 // module and add the child to that.
1001 let module_ = reduced_graph_parent.module();
1003 // Add or reuse the child.
1004 let child = module_.children.borrow().find_copy(&name.name);
1007 let child = Rc::new(NameBindings::new());
1008 module_.children.borrow_mut().insert(name.name, child.clone());
1012 // Enforce the duplicate checking mode:
1014 // * If we're requesting duplicate module checking, check that
1015 // there isn't a module in the module with the same name.
1017 // * If we're requesting duplicate type checking, check that
1018 // there isn't a type in the module with the same name.
1020 // * If we're requesting duplicate value checking, check that
1021 // there isn't a value in the module with the same name.
1023 // * If we're requesting duplicate type checking and duplicate
1024 // value checking, check that there isn't a duplicate type
1025 // and a duplicate value with the same name.
1027 // * If no duplicate checking was requested at all, do
1030 let mut duplicate_type = NoError;
1031 let ns = match duplicate_checking_mode {
1032 ForbidDuplicateModules => {
1033 if child.get_module_if_available().is_some() {
1034 duplicate_type = ModuleError;
1038 ForbidDuplicateTypes => {
1039 match child.def_for_namespace(TypeNS) {
1040 Some(DefMod(_)) | None => {}
1041 Some(_) => duplicate_type = TypeError
1045 ForbidDuplicateValues => {
1046 if child.defined_in_namespace(ValueNS) {
1047 duplicate_type = ValueError;
1051 ForbidDuplicateTypesAndValues => {
1053 match child.def_for_namespace(TypeNS) {
1054 Some(DefMod(_)) | None => {}
1057 duplicate_type = TypeError;
1060 if child.defined_in_namespace(ValueNS) {
1061 duplicate_type = ValueError;
1066 OverwriteDuplicates => None
1068 if duplicate_type != NoError {
1069 // Return an error here by looking up the namespace that
1070 // had the duplicate.
1071 let ns = ns.unwrap();
1072 self.resolve_error(sp,
1073 format!("duplicate definition of {} `{}`",
1074 namespace_error_to_str(duplicate_type),
1075 token::get_ident(name)).as_slice());
1077 let r = child.span_for_namespace(ns);
1078 for sp in r.iter() {
1079 self.session.span_note(*sp,
1080 format!("first definition of {} `{}` here",
1081 namespace_error_to_str(duplicate_type),
1082 token::get_ident(name)).as_slice());
1091 fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
1092 // If the block has view items, we need an anonymous module.
1093 if block.view_items.len() > 0 {
1097 // Check each statement.
1098 for statement in block.stmts.iter() {
1099 match statement.node {
1100 StmtDecl(declaration, _) => {
1101 match declaration.node {
1116 // If we found neither view items nor items, we don't need to create
1117 // an anonymous module.
1122 fn get_parent_link(&mut self, parent: ReducedGraphParent, name: Ident)
1125 ModuleReducedGraphParent(module_) => {
1126 return ModuleParentLink(module_.downgrade(), name);
1131 /// Constructs the reduced graph for one item.
1132 fn build_reduced_graph_for_item(&mut self,
1134 parent: ReducedGraphParent)
1135 -> ReducedGraphParent
1137 let ident = item.ident;
1139 let is_public = item.vis == ast::Public;
1144 self.add_child(ident, parent.clone(), ForbidDuplicateModules, sp);
1146 let parent_link = self.get_parent_link(parent, ident);
1147 let def_id = DefId { krate: 0, node: item.id };
1148 name_bindings.define_module(parent_link,
1152 item.vis == ast::Public,
1155 ModuleReducedGraphParent(name_bindings.get_module())
1158 ItemForeignMod(..) => parent,
1160 // These items live in the value namespace.
1161 ItemStatic(_, m, _) => {
1163 self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1164 let mutbl = m == ast::MutMutable;
1166 name_bindings.define_value
1167 (DefStatic(local_def(item.id), mutbl), sp, is_public);
1170 ItemFn(_, fn_style, _, _, _) => {
1172 self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1174 let def = DefFn(local_def(item.id), fn_style);
1175 name_bindings.define_value(def, sp, is_public);
1179 // These items live in the type namespace.
1182 self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1184 name_bindings.define_type
1185 (DefTy(local_def(item.id)), sp, is_public);
1189 ItemEnum(ref enum_definition, _) => {
1191 self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1193 name_bindings.define_type
1194 (DefTy(local_def(item.id)), sp, is_public);
1196 for variant in (*enum_definition).variants.iter() {
1197 self.build_reduced_graph_for_variant(
1206 // These items live in both the type and value namespaces.
1207 ItemStruct(struct_def, _) => {
1208 // Adding to both Type and Value namespaces or just Type?
1209 let (forbid, ctor_id) = match struct_def.ctor_id {
1210 Some(ctor_id) => (ForbidDuplicateTypesAndValues, Some(ctor_id)),
1211 None => (ForbidDuplicateTypes, None)
1214 let name_bindings = self.add_child(ident, parent.clone(), forbid, sp);
1216 // Define a name in the type namespace.
1217 name_bindings.define_type(DefTy(local_def(item.id)), sp, is_public);
1219 // If this is a newtype or unit-like struct, define a name
1220 // in the value namespace as well
1221 ctor_id.while_some(|cid| {
1222 name_bindings.define_value(DefStruct(local_def(cid)), sp,
1227 // Record the def ID and fields of this struct.
1228 let named_fields = struct_def.fields.iter().filter_map(|f| {
1230 NamedField(ident, _) => Some(ident.name),
1231 UnnamedField(_) => None
1234 self.structs.insert(local_def(item.id), named_fields);
1239 ItemImpl(_, None, ty, ref methods) => {
1240 // If this implements an anonymous trait, then add all the
1241 // methods within to a new module, if the type was defined
1242 // within this module.
1244 // FIXME (#3785): This is quite unsatisfactory. Perhaps we
1245 // should modify anonymous traits to only be implementable in
1246 // the same module that declared the type.
1248 // Create the module and add all methods.
1250 TyPath(ref path, _, _) if path.segments.len() == 1 => {
1251 let name = path_to_ident(path);
1253 let parent_opt = parent.module().children.borrow()
1254 .find_copy(&name.name);
1255 let new_parent = match parent_opt {
1256 // It already exists
1257 Some(ref child) if child.get_module_if_available()
1259 child.get_module().kind.get() ==
1261 ModuleReducedGraphParent(child.get_module())
1263 // Create the module
1266 self.add_child(name,
1268 ForbidDuplicateModules,
1272 self.get_parent_link(parent.clone(), ident);
1273 let def_id = local_def(item.id);
1276 !name_bindings.defined_in_namespace(ns) ||
1277 name_bindings.defined_in_public_namespace(ns);
1279 name_bindings.define_module(parent_link,
1286 ModuleReducedGraphParent(
1287 name_bindings.get_module())
1291 // For each method...
1292 for method in methods.iter() {
1293 // Add the method to the module.
1294 let ident = method.ident;
1295 let method_name_bindings =
1296 self.add_child(ident,
1298 ForbidDuplicateValues,
1300 let def = match method.explicit_self.node {
1302 // Static methods become
1303 // `def_static_method`s.
1304 DefStaticMethod(local_def(method.id),
1310 // Non-static methods become
1312 DefMethod(local_def(method.id), None)
1316 let is_public = method.vis == ast::Public;
1317 method_name_bindings.define_value(def,
1328 ItemImpl(_, Some(_), _, _) => parent,
1330 ItemTrait(_, _, _, ref methods) => {
1332 self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1334 // Add all the methods within to a new module.
1335 let parent_link = self.get_parent_link(parent.clone(), ident);
1336 name_bindings.define_module(parent_link,
1337 Some(local_def(item.id)),
1340 item.vis == ast::Public,
1342 let module_parent = ModuleReducedGraphParent(name_bindings.
1345 let def_id = local_def(item.id);
1347 // Add the names of all the methods to the trait info.
1348 for method in methods.iter() {
1349 let ty_m = trait_method_to_ty_method(method);
1351 let ident = ty_m.ident;
1353 // Add it as a name in the trait module.
1354 let def = match ty_m.explicit_self.node {
1356 // Static methods become `def_static_method`s.
1357 DefStaticMethod(local_def(ty_m.id),
1358 FromTrait(local_def(item.id)),
1362 // Non-static methods become `def_method`s.
1363 DefMethod(local_def(ty_m.id),
1364 Some(local_def(item.id)))
1368 let method_name_bindings =
1369 self.add_child(ident,
1370 module_parent.clone(),
1371 ForbidDuplicateValues,
1373 method_name_bindings.define_value(def, ty_m.span, true);
1375 self.method_map.borrow_mut().insert((ident.name, def_id),
1376 ty_m.explicit_self.node);
1379 name_bindings.define_type(DefTrait(def_id), sp, is_public);
1382 ItemMac(..) => parent
1386 // Constructs the reduced graph for one variant. Variants exist in the
1387 // type and/or value namespaces.
1388 fn build_reduced_graph_for_variant(&mut self,
1391 parent: ReducedGraphParent,
1393 let ident = variant.node.name;
1395 match variant.node.kind {
1396 TupleVariantKind(_) => {
1397 let child = self.add_child(ident, parent, ForbidDuplicateValues, variant.span);
1398 child.define_value(DefVariant(item_id,
1399 local_def(variant.node.id), false),
1400 variant.span, is_public);
1402 StructVariantKind(_) => {
1403 let child = self.add_child(ident, parent,
1404 ForbidDuplicateTypesAndValues,
1406 child.define_type(DefVariant(item_id,
1407 local_def(variant.node.id), true),
1408 variant.span, is_public);
1410 // Not adding fields for variants as they are not accessed with a self receiver
1411 self.structs.insert(local_def(variant.node.id), Vec::new());
1416 /// Constructs the reduced graph for one 'view item'. View items consist
1417 /// of imports and use directives.
1418 fn build_reduced_graph_for_view_item(&mut self, view_item: &ViewItem,
1419 parent: ReducedGraphParent) {
1420 match view_item.node {
1421 ViewItemUse(ref view_path) => {
1422 // Extract and intern the module part of the path. For
1423 // globs and lists, the path is found directly in the AST;
1424 // for simple paths we have to munge the path a little.
1426 let mut module_path = Vec::new();
1427 match view_path.node {
1428 ViewPathSimple(_, ref full_path, _) => {
1429 let path_len = full_path.segments.len();
1430 assert!(path_len != 0);
1432 for (i, segment) in full_path.segments
1435 if i != path_len - 1 {
1436 module_path.push(segment.identifier)
1441 ViewPathGlob(ref module_ident_path, _) |
1442 ViewPathList(ref module_ident_path, _, _) => {
1443 for segment in module_ident_path.segments.iter() {
1444 module_path.push(segment.identifier)
1449 // Build up the import directives.
1450 let module_ = parent.module();
1451 let is_public = view_item.vis == ast::Public;
1452 match view_path.node {
1453 ViewPathSimple(binding, ref full_path, id) => {
1455 full_path.segments.last().unwrap().identifier;
1456 let subclass = SingleImport(binding,
1458 self.build_import_directive(&*module_,
1465 ViewPathList(_, ref source_idents, _) => {
1466 for source_ident in source_idents.iter() {
1467 let name = source_ident.node.name;
1468 self.build_import_directive(
1470 module_path.clone(),
1471 SingleImport(name, name),
1473 source_ident.node.id,
1477 ViewPathGlob(_, id) => {
1478 self.build_import_directive(&*module_,
1488 ViewItemExternCrate(name, _, node_id) => {
1489 // n.b. we don't need to look at the path option here, because cstore already did
1490 for &crate_id in self.session.cstore
1491 .find_extern_mod_stmt_cnum(node_id).iter() {
1492 let def_id = DefId { krate: crate_id, node: 0 };
1493 self.external_exports.insert(def_id);
1495 ModuleParentLink(parent.module().downgrade(), name);
1496 let external_module = Rc::new(Module::new(parent_link,
1501 debug!("(build reduced graph for item) found extern `{}`",
1502 self.module_to_str(&*external_module));
1503 parent.module().external_module_children.borrow_mut()
1504 .insert(name.name, external_module.clone());
1505 self.build_reduced_graph_for_external_crate(external_module);
1511 /// Constructs the reduced graph for one foreign item.
1512 fn build_reduced_graph_for_foreign_item(&mut self,
1513 foreign_item: &ForeignItem,
1514 parent: ReducedGraphParent,
1515 f: |&mut Resolver|) {
1516 let name = foreign_item.ident;
1517 let is_public = foreign_item.vis == ast::Public;
1519 self.add_child(name, parent, ForbidDuplicateValues,
1522 match foreign_item.node {
1523 ForeignItemFn(_, ref generics) => {
1524 let def = DefFn(local_def(foreign_item.id), UnsafeFn);
1525 name_bindings.define_value(def, foreign_item.span, is_public);
1527 self.with_type_parameter_rib(
1528 HasTypeParameters(generics,
1534 ForeignItemStatic(_, m) => {
1535 let def = DefStatic(local_def(foreign_item.id), m);
1536 name_bindings.define_value(def, foreign_item.span, is_public);
1543 fn build_reduced_graph_for_block(&mut self,
1545 parent: ReducedGraphParent)
1546 -> ReducedGraphParent
1548 if self.block_needs_anonymous_module(block) {
1549 let block_id = block.id;
1551 debug!("(building reduced graph for block) creating a new \
1552 anonymous module for block {}",
1555 let parent_module = parent.module();
1556 let new_module = Rc::new(Module::new(
1557 BlockParentLink(parent_module.downgrade(), block_id),
1559 AnonymousModuleKind,
1562 parent_module.anonymous_children.borrow_mut()
1563 .insert(block_id, new_module.clone());
1564 ModuleReducedGraphParent(new_module)
1570 fn handle_external_def(&mut self,
1573 child_name_bindings: &NameBindings,
1576 new_parent: ReducedGraphParent) {
1577 debug!("(building reduced graph for \
1578 external crate) building external def, priv {:?}",
1580 let is_public = vis == ast::Public;
1581 let is_exported = is_public && match new_parent {
1582 ModuleReducedGraphParent(ref module) => {
1583 match module.def_id.get() {
1585 Some(did) => self.external_exports.contains(&did)
1590 self.external_exports.insert(def.def_id());
1593 DefMod(def_id) | DefForeignMod(def_id) | DefStruct(def_id) |
1595 let type_def = child_name_bindings.type_def.borrow().clone();
1597 Some(TypeNsDef { module_def: Some(module_def), .. }) => {
1598 debug!("(building reduced graph for external crate) \
1599 already created module");
1600 module_def.def_id.set(Some(def_id));
1603 debug!("(building reduced graph for \
1604 external crate) building module \
1606 let parent_link = self.get_parent_link(new_parent.clone(), ident);
1608 child_name_bindings.define_module(parent_link,
1621 DefMod(_) | DefForeignMod(_) => {}
1622 DefVariant(enum_did, variant_id, is_struct) => {
1623 debug!("(building reduced graph for external crate) building \
1626 // If this variant is public, then it was publicly reexported,
1627 // otherwise we need to inherit the visibility of the enum
1629 let is_exported = is_public ||
1630 self.external_exports.contains(&enum_did);
1632 child_name_bindings.define_type(def, DUMMY_SP, is_exported);
1633 // Not adding fields for variants as they are not accessed with a self receiver
1634 self.structs.insert(variant_id, Vec::new());
1636 child_name_bindings.define_value(def, DUMMY_SP, is_exported);
1639 DefFn(..) | DefStaticMethod(..) | DefStatic(..) => {
1640 debug!("(building reduced graph for external \
1641 crate) building value (fn/static) {}", final_ident);
1642 child_name_bindings.define_value(def, DUMMY_SP, is_public);
1644 DefTrait(def_id) => {
1645 debug!("(building reduced graph for external \
1646 crate) building type {}", final_ident);
1648 // If this is a trait, add all the method names
1649 // to the trait info.
1651 let method_def_ids =
1652 csearch::get_trait_method_def_ids(&self.session.cstore, def_id);
1653 for &method_def_id in method_def_ids.iter() {
1654 let (method_name, explicit_self) =
1655 csearch::get_method_name_and_explicit_self(&self.session.cstore,
1658 debug!("(building reduced graph for \
1659 external crate) ... adding \
1661 token::get_ident(method_name));
1663 self.method_map.borrow_mut().insert((method_name.name, def_id), explicit_self);
1666 self.external_exports.insert(method_def_id);
1670 child_name_bindings.define_type(def, DUMMY_SP, is_public);
1672 // Define a module if necessary.
1673 let parent_link = self.get_parent_link(new_parent, ident);
1674 child_name_bindings.set_module_kind(parent_link,
1682 debug!("(building reduced graph for external \
1683 crate) building type {}", final_ident);
1685 child_name_bindings.define_type(def, DUMMY_SP, is_public);
1687 DefStruct(def_id) => {
1688 debug!("(building reduced graph for external \
1689 crate) building type and value for {}",
1691 child_name_bindings.define_type(def, DUMMY_SP, is_public);
1692 let fields = csearch::get_struct_fields(&self.session.cstore, def_id).iter().map(|f| {
1694 }).collect::<Vec<_>>();
1696 if fields.len() == 0 {
1697 child_name_bindings.define_value(def, DUMMY_SP, is_public);
1700 // Record the def ID and fields of this struct.
1701 self.structs.insert(def_id, fields);
1704 debug!("(building reduced graph for external crate) \
1705 ignoring {:?}", def);
1706 // Ignored; handled elsewhere.
1708 DefArg(..) | DefLocal(..) | DefPrimTy(..) |
1709 DefTyParam(..) | DefBinding(..) |
1710 DefUse(..) | DefUpvar(..) | DefRegion(..) |
1711 DefTyParamBinder(..) | DefLabel(..) | DefSelfTy(..) => {
1712 fail!("didn't expect `{:?}`", def);
1717 /// Builds the reduced graph for a single item in an external crate.
1718 fn build_reduced_graph_for_external_crate_def(&mut self,
1722 visibility: Visibility) {
1725 // Add the new child item, if necessary.
1727 DefForeignMod(def_id) => {
1728 // Foreign modules have no names. Recur and populate
1730 csearch::each_child_of_item(&self.session.cstore,
1735 self.build_reduced_graph_for_external_crate_def(
1743 let child_name_bindings =
1744 self.add_child(ident,
1745 ModuleReducedGraphParent(root.clone()),
1746 OverwriteDuplicates,
1749 self.handle_external_def(def,
1751 &*child_name_bindings,
1752 token::get_ident(ident).get(),
1754 ModuleReducedGraphParent(root));
1759 // We only process static methods of impls here.
1760 match csearch::get_type_name_if_impl(&self.session.cstore, def) {
1762 Some(final_ident) => {
1763 let static_methods_opt =
1764 csearch::get_static_methods_if_impl(&self.session.cstore, def);
1765 match static_methods_opt {
1766 Some(ref static_methods) if
1767 static_methods.len() >= 1 => {
1768 debug!("(building reduced graph for \
1769 external crate) processing \
1770 static methods for type name {}",
1771 token::get_ident(final_ident));
1773 let child_name_bindings =
1776 ModuleReducedGraphParent(root.clone()),
1777 OverwriteDuplicates,
1780 // Process the static methods. First,
1781 // create the module.
1783 let type_def = child_name_bindings.type_def.borrow().clone();
1786 module_def: Some(module_def),
1789 // We already have a module. This
1791 type_module = module_def;
1793 // Mark it as an impl module if
1795 type_module.kind.set(ImplModuleKind);
1799 self.get_parent_link(ModuleReducedGraphParent(root),
1801 child_name_bindings.define_module(
1809 child_name_bindings.
1814 // Add each static method to the module.
1816 ModuleReducedGraphParent(type_module);
1817 for static_method_info in
1818 static_methods.iter() {
1819 let ident = static_method_info.ident;
1820 debug!("(building reduced graph for \
1821 external crate) creating \
1822 static method '{}'",
1823 token::get_ident(ident));
1825 let method_name_bindings =
1826 self.add_child(ident,
1828 OverwriteDuplicates,
1831 static_method_info.def_id,
1832 static_method_info.fn_style);
1834 method_name_bindings.define_value(
1836 visibility == ast::Public);
1840 // Otherwise, do nothing.
1841 Some(_) | None => {}
1847 debug!("(building reduced graph for external crate) \
1853 /// Builds the reduced graph rooted at the given external module.
1854 fn populate_external_module(&mut self, module: Rc<Module>) {
1855 debug!("(populating external module) attempting to populate {}",
1856 self.module_to_str(&*module));
1858 let def_id = match module.def_id.get() {
1860 debug!("(populating external module) ... no def ID!");
1863 Some(def_id) => def_id,
1866 csearch::each_child_of_item(&self.session.cstore,
1868 |def_like, child_ident, visibility| {
1869 debug!("(populating external module) ... found ident: {}",
1870 token::get_ident(child_ident));
1871 self.build_reduced_graph_for_external_crate_def(module.clone(),
1876 module.populated.set(true)
1879 /// Ensures that the reduced graph rooted at the given external module
1880 /// is built, building it if it is not.
1881 fn populate_module_if_necessary(&mut self, module: &Rc<Module>) {
1882 if !module.populated.get() {
1883 self.populate_external_module(module.clone())
1885 assert!(module.populated.get())
1888 /// Builds the reduced graph rooted at the 'use' directive for an external
1890 fn build_reduced_graph_for_external_crate(&mut self, root: Rc<Module>) {
1891 csearch::each_top_level_item_of_crate(&self.session.cstore,
1896 |def_like, ident, visibility| {
1897 self.build_reduced_graph_for_external_crate_def(root.clone(),
1904 /// Creates and adds an import directive to the given module.
1905 fn build_import_directive(&mut self,
1907 module_path: Vec<Ident> ,
1908 subclass: ImportDirectiveSubclass,
1912 module_.imports.borrow_mut().push(ImportDirective::new(module_path,
1916 self.unresolved_imports += 1;
1917 // Bump the reference count on the name. Or, if this is a glob, set
1918 // the appropriate flag.
1921 SingleImport(target, _) => {
1922 debug!("(building import directive) building import \
1924 self.idents_to_str(module_.imports.borrow().last().unwrap()
1925 .module_path.as_slice()),
1926 token::get_ident(target));
1928 let mut import_resolutions = module_.import_resolutions
1930 match import_resolutions.find_mut(&target.name) {
1931 Some(resolution) => {
1932 debug!("(building import directive) bumping \
1934 resolution.outstanding_references += 1;
1936 // the source of this name is different now
1937 resolution.type_id = id;
1938 resolution.value_id = id;
1939 resolution.is_public = is_public;
1944 debug!("(building import directive) creating new");
1945 let mut resolution = ImportResolution::new(id, is_public);
1946 resolution.outstanding_references = 1;
1947 import_resolutions.insert(target.name, resolution);
1950 // Set the glob flag. This tells us that we don't know the
1951 // module's exports ahead of time.
1953 module_.glob_count.set(module_.glob_count.get() + 1);
1958 // Import resolution
1960 // This is a fixed-point algorithm. We resolve imports until our efforts
1961 // are stymied by an unresolved import; then we bail out of the current
1962 // module and continue. We terminate successfully once no more imports
1963 // remain or unsuccessfully when no forward progress in resolving imports
1966 /// Resolves all imports for the crate. This method performs the fixed-
1967 /// point iteration.
1968 fn resolve_imports(&mut self) {
1970 let mut prev_unresolved_imports = 0;
1972 debug!("(resolving imports) iteration {}, {} imports left",
1973 i, self.unresolved_imports);
1975 let module_root = self.graph_root.get_module();
1976 self.resolve_imports_for_module_subtree(module_root.clone());
1978 if self.unresolved_imports == 0 {
1979 debug!("(resolving imports) success");
1983 if self.unresolved_imports == prev_unresolved_imports {
1984 self.report_unresolved_imports(module_root);
1989 prev_unresolved_imports = self.unresolved_imports;
1993 /// Attempts to resolve imports for the given module and all of its
1995 fn resolve_imports_for_module_subtree(&mut self, module_: Rc<Module>) {
1996 debug!("(resolving imports for module subtree) resolving {}",
1997 self.module_to_str(&*module_));
1998 let orig_module = replace(&mut self.current_module, module_.clone());
1999 self.resolve_imports_for_module(module_.clone());
2000 self.current_module = orig_module;
2002 self.populate_module_if_necessary(&module_);
2003 for (_, child_node) in module_.children.borrow().iter() {
2004 match child_node.get_module_if_available() {
2008 Some(child_module) => {
2009 self.resolve_imports_for_module_subtree(child_module);
2014 for (_, child_module) in module_.anonymous_children.borrow().iter() {
2015 self.resolve_imports_for_module_subtree(child_module.clone());
2019 /// Attempts to resolve imports for the given module only.
2020 fn resolve_imports_for_module(&mut self, module: Rc<Module>) {
2021 if module.all_imports_resolved() {
2022 debug!("(resolving imports for module) all imports resolved for \
2024 self.module_to_str(&*module));
2028 let imports = module.imports.borrow();
2029 let import_count = imports.len();
2030 while module.resolved_import_count.get() < import_count {
2031 let import_index = module.resolved_import_count.get();
2032 let import_directive = imports.get(import_index);
2033 match self.resolve_import_for_module(module.clone(),
2036 let (span, help) = match err {
2037 Some((span, msg)) => (span, format!(". {}", msg)),
2038 None => (import_directive.span, String::new())
2040 let msg = format!("unresolved import `{}`{}",
2041 self.import_path_to_str(
2042 import_directive.module_path
2044 import_directive.subclass),
2046 self.resolve_error(span, msg.as_slice());
2048 Indeterminate => break, // Bail out. We'll come around next time.
2049 Success(()) => () // Good. Continue.
2052 module.resolved_import_count
2053 .set(module.resolved_import_count.get() + 1);
2057 fn idents_to_str(&self, idents: &[Ident]) -> String {
2058 let mut first = true;
2059 let mut result = String::new();
2060 for ident in idents.iter() {
2064 result.push_str("::")
2066 result.push_str(token::get_ident(*ident).get());
2071 fn path_idents_to_str(&self, path: &Path) -> String {
2072 let identifiers: Vec<ast::Ident> = path.segments
2074 .map(|seg| seg.identifier)
2076 self.idents_to_str(identifiers.as_slice())
2079 fn import_directive_subclass_to_str(&mut self,
2080 subclass: ImportDirectiveSubclass)
2083 SingleImport(_, source) => {
2084 token::get_ident(source).get().to_string()
2086 GlobImport => "*".to_string()
2090 fn import_path_to_str(&mut self,
2092 subclass: ImportDirectiveSubclass)
2094 if idents.is_empty() {
2095 self.import_directive_subclass_to_str(subclass)
2098 self.idents_to_str(idents),
2099 self.import_directive_subclass_to_str(
2100 subclass))).to_string()
2104 /// Attempts to resolve the given import. The return value indicates
2105 /// failure if we're certain the name does not exist, indeterminate if we
2106 /// don't know whether the name exists at the moment due to other
2107 /// currently-unresolved imports, or success if we know the name exists.
2108 /// If successful, the resolved bindings are written into the module.
2109 fn resolve_import_for_module(&mut self,
2110 module_: Rc<Module>,
2111 import_directive: &ImportDirective)
2112 -> ResolveResult<()> {
2113 let mut resolution_result = Failed(None);
2114 let module_path = &import_directive.module_path;
2116 debug!("(resolving import for module) resolving import `{}::...` in \
2118 self.idents_to_str(module_path.as_slice()),
2119 self.module_to_str(&*module_));
2121 // First, resolve the module path for the directive, if necessary.
2122 let container = if module_path.len() == 0 {
2123 // Use the crate root.
2124 Some((self.graph_root.get_module(), LastMod(AllPublic)))
2126 match self.resolve_module_path(module_.clone(),
2127 module_path.as_slice(),
2128 DontUseLexicalScope,
2129 import_directive.span,
2132 resolution_result = Failed(err);
2136 resolution_result = Indeterminate;
2139 Success(container) => Some(container),
2145 Some((containing_module, lp)) => {
2146 // We found the module that the target is contained
2147 // within. Attempt to resolve the import within it.
2149 match import_directive.subclass {
2150 SingleImport(target, source) => {
2152 self.resolve_single_import(&*module_,
2161 self.resolve_glob_import(&*module_,
2163 import_directive.id,
2164 import_directive.is_public,
2171 // Decrement the count of unresolved imports.
2172 match resolution_result {
2174 assert!(self.unresolved_imports >= 1);
2175 self.unresolved_imports -= 1;
2178 // Nothing to do here; just return the error.
2182 // Decrement the count of unresolved globs if necessary. But only if
2183 // the resolution result is indeterminate -- otherwise we'll stop
2184 // processing imports here. (See the loop in
2185 // resolve_imports_for_module.)
2187 if !resolution_result.indeterminate() {
2188 match import_directive.subclass {
2190 assert!(module_.glob_count.get() >= 1);
2191 module_.glob_count.set(module_.glob_count.get() - 1);
2193 SingleImport(..) => {
2199 return resolution_result;
2202 fn create_name_bindings_from_module(module: Rc<Module>) -> NameBindings {
2204 type_def: RefCell::new(Some(TypeNsDef {
2206 module_def: Some(module),
2210 value_def: RefCell::new(None),
2214 fn resolve_single_import(&mut self,
2216 containing_module: Rc<Module>,
2219 directive: &ImportDirective,
2221 -> ResolveResult<()> {
2222 debug!("(resolving single import) resolving `{}` = `{}::{}` from \
2223 `{}` id {}, last private {:?}",
2224 token::get_ident(target),
2225 self.module_to_str(&*containing_module),
2226 token::get_ident(source),
2227 self.module_to_str(module_),
2233 LastImport {..} => {
2235 .span_bug(directive.span,
2236 "not expecting Import here, must be LastMod")
2240 // We need to resolve both namespaces for this to succeed.
2243 let mut value_result = UnknownResult;
2244 let mut type_result = UnknownResult;
2246 // Search for direct children of the containing module.
2247 self.populate_module_if_necessary(&containing_module);
2249 match containing_module.children.borrow().find(&source.name) {
2253 Some(ref child_name_bindings) => {
2254 if child_name_bindings.defined_in_namespace(ValueNS) {
2255 debug!("(resolving single import) found value binding");
2256 value_result = BoundResult(containing_module.clone(),
2257 (*child_name_bindings).clone());
2259 if child_name_bindings.defined_in_namespace(TypeNS) {
2260 debug!("(resolving single import) found type binding");
2261 type_result = BoundResult(containing_module.clone(),
2262 (*child_name_bindings).clone());
2267 // Unless we managed to find a result in both namespaces (unlikely),
2268 // search imports as well.
2269 let mut value_used_reexport = false;
2270 let mut type_used_reexport = false;
2271 match (value_result.clone(), type_result.clone()) {
2272 (BoundResult(..), BoundResult(..)) => {} // Continue.
2274 // If there is an unresolved glob at this point in the
2275 // containing module, bail out. We don't know enough to be
2276 // able to resolve this import.
2278 if containing_module.glob_count.get() > 0 {
2279 debug!("(resolving single import) unresolved glob; \
2281 return Indeterminate;
2284 // Now search the exported imports within the containing module.
2285 match containing_module.import_resolutions.borrow().find(&source.name) {
2287 debug!("(resolving single import) no import");
2288 // The containing module definitely doesn't have an
2289 // exported import with the name in question. We can
2290 // therefore accurately report that the names are
2293 if value_result.is_unknown() {
2294 value_result = UnboundResult;
2296 if type_result.is_unknown() {
2297 type_result = UnboundResult;
2300 Some(import_resolution)
2301 if import_resolution.outstanding_references == 0 => {
2303 fn get_binding(this: &mut Resolver,
2304 import_resolution: &ImportResolution,
2305 namespace: Namespace)
2306 -> NamespaceResult {
2308 // Import resolutions must be declared with "pub"
2309 // in order to be exported.
2310 if !import_resolution.is_public {
2311 return UnboundResult;
2314 match import_resolution.
2315 target_for_namespace(namespace) {
2317 return UnboundResult;
2319 Some(Target {target_module, bindings}) => {
2320 debug!("(resolving single import) found \
2321 import in ns {:?}", namespace);
2322 let id = import_resolution.id(namespace);
2323 this.used_imports.insert((id, namespace));
2324 return BoundResult(target_module, bindings);
2329 // The name is an import which has been fully
2330 // resolved. We can, therefore, just follow it.
2331 if value_result.is_unknown() {
2332 value_result = get_binding(self, import_resolution,
2334 value_used_reexport = import_resolution.is_public;
2336 if type_result.is_unknown() {
2337 type_result = get_binding(self, import_resolution,
2339 type_used_reexport = import_resolution.is_public;
2344 // The import is unresolved. Bail out.
2345 debug!("(resolving single import) unresolved import; \
2347 return Indeterminate;
2353 // If we didn't find a result in the type namespace, search the
2354 // external modules.
2355 let mut value_used_public = false;
2356 let mut type_used_public = false;
2358 BoundResult(..) => {}
2360 match containing_module.external_module_children.borrow_mut()
2361 .find_copy(&source.name) {
2362 None => {} // Continue.
2364 debug!("(resolving single import) found external \
2367 Rc::new(Resolver::create_name_bindings_from_module(
2369 type_result = BoundResult(containing_module.clone(),
2371 type_used_public = true;
2377 // We've successfully resolved the import. Write the results in.
2378 let mut import_resolutions = module_.import_resolutions.borrow_mut();
2379 let import_resolution = import_resolutions.get_mut(&target.name);
2381 match value_result {
2382 BoundResult(ref target_module, ref name_bindings) => {
2383 debug!("(resolving single import) found value target");
2384 import_resolution.value_target = Some(Target::new(target_module.clone(),
2385 name_bindings.clone()));
2386 import_resolution.value_id = directive.id;
2387 import_resolution.is_public = directive.is_public;
2388 value_used_public = name_bindings.defined_in_public_namespace(ValueNS);
2390 UnboundResult => { /* Continue. */ }
2392 fail!("value result should be known at this point");
2396 BoundResult(ref target_module, ref name_bindings) => {
2397 debug!("(resolving single import) found type target: {:?}",
2398 { name_bindings.type_def.borrow().clone().unwrap().type_def });
2399 import_resolution.type_target =
2400 Some(Target::new(target_module.clone(), name_bindings.clone()));
2401 import_resolution.type_id = directive.id;
2402 import_resolution.is_public = directive.is_public;
2403 type_used_public = name_bindings.defined_in_public_namespace(TypeNS);
2405 UnboundResult => { /* Continue. */ }
2407 fail!("type result should be known at this point");
2411 if value_result.is_unbound() && type_result.is_unbound() {
2412 let msg = format!("There is no `{}` in `{}`",
2413 token::get_ident(source),
2414 self.module_to_str(&*containing_module));
2415 return Failed(Some((directive.span, msg)));
2417 let value_used_public = value_used_reexport || value_used_public;
2418 let type_used_public = type_used_reexport || type_used_public;
2420 assert!(import_resolution.outstanding_references >= 1);
2421 import_resolution.outstanding_references -= 1;
2423 // record what this import resolves to for later uses in documentation,
2424 // this may resolve to either a value or a type, but for documentation
2425 // purposes it's good enough to just favor one over the other.
2426 let value_private = match import_resolution.value_target {
2427 Some(ref target) => {
2428 let def = target.bindings.def_for_namespace(ValueNS).unwrap();
2429 self.def_map.borrow_mut().insert(directive.id, def);
2430 let did = def.def_id();
2431 if value_used_public {Some(lp)} else {Some(DependsOn(did))}
2433 // AllPublic here and below is a dummy value, it should never be used because
2434 // _exists is false.
2437 let type_private = match import_resolution.type_target {
2438 Some(ref target) => {
2439 let def = target.bindings.def_for_namespace(TypeNS).unwrap();
2440 self.def_map.borrow_mut().insert(directive.id, def);
2441 let did = def.def_id();
2442 if type_used_public {Some(lp)} else {Some(DependsOn(did))}
2447 self.last_private.insert(directive.id, LastImport{value_priv: value_private,
2449 type_priv: type_private,
2452 debug!("(resolving single import) successfully resolved import");
2456 // Resolves a glob import. Note that this function cannot fail; it either
2457 // succeeds or bails out (as importing * from an empty module or a module
2458 // that exports nothing is valid).
2459 fn resolve_glob_import(&mut self,
2461 containing_module: Rc<Module>,
2465 -> ResolveResult<()> {
2466 // This function works in a highly imperative manner; it eagerly adds
2467 // everything it can to the list of import resolutions of the module
2469 debug!("(resolving glob import) resolving glob import {}", id);
2471 // We must bail out if the node has unresolved imports of any kind
2472 // (including globs).
2473 if !(*containing_module).all_imports_resolved() {
2474 debug!("(resolving glob import) target module has unresolved \
2475 imports; bailing out");
2476 return Indeterminate;
2479 assert_eq!(containing_module.glob_count.get(), 0);
2481 // Add all resolved imports from the containing module.
2482 let import_resolutions = containing_module.import_resolutions
2484 for (ident, target_import_resolution) in import_resolutions.iter() {
2485 debug!("(resolving glob import) writing module resolution \
2487 target_import_resolution.type_target.is_none(),
2488 self.module_to_str(module_));
2490 if !target_import_resolution.is_public {
2491 debug!("(resolving glob import) nevermind, just kidding");
2495 // Here we merge two import resolutions.
2496 let mut import_resolutions = module_.import_resolutions.borrow_mut();
2497 match import_resolutions.find_mut(ident) {
2498 Some(dest_import_resolution) => {
2499 // Merge the two import resolutions at a finer-grained
2502 match target_import_resolution.value_target {
2506 Some(ref value_target) => {
2507 dest_import_resolution.value_target =
2508 Some(value_target.clone());
2511 match target_import_resolution.type_target {
2515 Some(ref type_target) => {
2516 dest_import_resolution.type_target =
2517 Some(type_target.clone());
2520 dest_import_resolution.is_public = is_public;
2526 // Simple: just copy the old import resolution.
2527 let mut new_import_resolution = ImportResolution::new(id, is_public);
2528 new_import_resolution.value_target =
2529 target_import_resolution.value_target.clone();
2530 new_import_resolution.type_target =
2531 target_import_resolution.type_target.clone();
2533 import_resolutions.insert(*ident, new_import_resolution);
2536 // Add all children from the containing module.
2537 self.populate_module_if_necessary(&containing_module);
2539 for (&name, name_bindings) in containing_module.children
2541 self.merge_import_resolution(module_, containing_module.clone(),
2543 name, name_bindings.clone());
2546 // Add external module children from the containing module.
2547 for (&name, module) in containing_module.external_module_children
2550 Rc::new(Resolver::create_name_bindings_from_module(module.clone()));
2551 self.merge_import_resolution(module_, containing_module.clone(),
2553 name, name_bindings);
2556 // Record the destination of this import
2557 match containing_module.def_id.get() {
2559 self.def_map.borrow_mut().insert(id, DefMod(did));
2560 self.last_private.insert(id, lp);
2565 debug!("(resolving glob import) successfully resolved import");
2569 fn merge_import_resolution(&mut self,
2571 containing_module: Rc<Module>,
2575 name_bindings: Rc<NameBindings>) {
2576 let mut import_resolutions = module_.import_resolutions.borrow_mut();
2577 let dest_import_resolution = import_resolutions.find_or_insert_with(name, |_| {
2578 // Create a new import resolution from this child.
2579 ImportResolution::new(id, is_public)
2582 debug!("(resolving glob import) writing resolution `{}` in `{}` \
2584 token::get_name(name).get().to_str(),
2585 self.module_to_str(&*containing_module),
2586 self.module_to_str(module_));
2588 // Merge the child item into the import resolution.
2589 if name_bindings.defined_in_public_namespace(ValueNS) {
2590 debug!("(resolving glob import) ... for value target");
2591 dest_import_resolution.value_target =
2592 Some(Target::new(containing_module.clone(), name_bindings.clone()));
2593 dest_import_resolution.value_id = id;
2595 if name_bindings.defined_in_public_namespace(TypeNS) {
2596 debug!("(resolving glob import) ... for type target");
2597 dest_import_resolution.type_target =
2598 Some(Target::new(containing_module, name_bindings.clone()));
2599 dest_import_resolution.type_id = id;
2601 dest_import_resolution.is_public = is_public;
2604 /// Resolves the given module path from the given root `module_`.
2605 fn resolve_module_path_from_root(&mut self,
2606 module_: Rc<Module>,
2607 module_path: &[Ident],
2610 name_search_type: NameSearchType,
2612 -> ResolveResult<(Rc<Module>, LastPrivate)> {
2613 fn search_parent_externals(needle: Name, module: &Rc<Module>)
2614 -> Option<Rc<Module>> {
2615 module.external_module_children.borrow()
2617 .map(|_| module.clone())
2619 match module.parent_link.clone() {
2620 ModuleParentLink(parent, _) => {
2621 search_parent_externals(needle,
2622 &parent.upgrade().unwrap())
2629 let mut search_module = module_;
2630 let mut index = index;
2631 let module_path_len = module_path.len();
2632 let mut closest_private = lp;
2634 // Resolve the module part of the path. This does not involve looking
2635 // upward though scope chains; we simply resolve names directly in
2636 // modules as we go.
2637 while index < module_path_len {
2638 let name = module_path[index];
2639 match self.resolve_name_in_module(search_module.clone(),
2645 let segment_name = token::get_ident(name);
2646 let module_name = self.module_to_str(&*search_module);
2647 let mut span = span;
2648 let msg = if "???" == module_name.as_slice() {
2649 span.hi = span.lo + Pos::from_uint(segment_name.get().len());
2651 match search_parent_externals(name.name,
2652 &self.current_module) {
2654 let path_str = self.idents_to_str(module_path);
2655 let target_mod_str = self.module_to_str(&*module);
2656 let current_mod_str =
2657 self.module_to_str(&*self.current_module);
2659 let prefix = if target_mod_str == current_mod_str {
2660 "self::".to_string()
2662 format!("{}::", target_mod_str)
2665 format!("Did you mean `{}{}`?", prefix, path_str)
2667 None => format!("Maybe a missing `extern crate {}`?",
2671 format!("Could not find `{}` in `{}`.",
2676 return Failed(Some((span, msg)));
2678 Failed(err) => return Failed(err),
2680 debug!("(resolving module path for import) module \
2681 resolution is indeterminate: {}",
2682 token::get_ident(name));
2683 return Indeterminate;
2685 Success((target, used_proxy)) => {
2686 // Check to see whether there are type bindings, and, if
2687 // so, whether there is a module within.
2688 match *target.bindings.type_def.borrow() {
2689 Some(ref type_def) => {
2690 match type_def.module_def {
2692 let msg = format!("Not a module `{}`",
2693 token::get_ident(name));
2695 return Failed(Some((span, msg)));
2697 Some(ref module_def) => {
2698 // If we're doing the search for an
2699 // import, do not allow traits and impls
2701 match (name_search_type,
2702 module_def.kind.get()) {
2703 (ImportSearch, TraitModuleKind) |
2704 (ImportSearch, ImplModuleKind) => {
2706 "Cannot import from a trait or \
2707 type implementation".to_string();
2708 return Failed(Some((span, msg)));
2711 search_module = module_def.clone();
2713 // Keep track of the closest
2714 // private module used when
2715 // resolving this import chain.
2717 !search_module.is_public {
2718 match search_module.def_id
2722 LastMod(DependsOn(did));
2733 // There are no type bindings at all.
2734 let msg = format!("Not a module `{}`",
2735 token::get_ident(name));
2736 return Failed(Some((span, msg)));
2745 return Success((search_module, closest_private));
2748 /// Attempts to resolve the module part of an import directive or path
2749 /// rooted at the given module.
2751 /// On success, returns the resolved module, and the closest *private*
2752 /// module found to the destination when resolving this path.
2753 fn resolve_module_path(&mut self,
2754 module_: Rc<Module>,
2755 module_path: &[Ident],
2756 use_lexical_scope: UseLexicalScopeFlag,
2758 name_search_type: NameSearchType)
2759 -> ResolveResult<(Rc<Module>, LastPrivate)> {
2760 let module_path_len = module_path.len();
2761 assert!(module_path_len > 0);
2763 debug!("(resolving module path for import) processing `{}` rooted at \
2765 self.idents_to_str(module_path),
2766 self.module_to_str(&*module_));
2768 // Resolve the module prefix, if any.
2769 let module_prefix_result = self.resolve_module_prefix(module_.clone(),
2775 match module_prefix_result {
2777 let mpath = self.idents_to_str(module_path);
2778 let mpath = mpath.as_slice();
2779 match mpath.rfind(':') {
2781 let msg = format!("Could not find `{}` in `{}`",
2782 // idx +- 1 to account for the
2783 // colons on either side
2784 mpath.slice_from(idx + 1),
2785 mpath.slice_to(idx - 1));
2786 return Failed(Some((span, msg)));
2788 None => return Failed(None),
2791 Failed(err) => return Failed(err),
2793 debug!("(resolving module path for import) indeterminate; \
2795 return Indeterminate;
2797 Success(NoPrefixFound) => {
2798 // There was no prefix, so we're considering the first element
2799 // of the path. How we handle this depends on whether we were
2800 // instructed to use lexical scope or not.
2801 match use_lexical_scope {
2802 DontUseLexicalScope => {
2803 // This is a crate-relative path. We will start the
2804 // resolution process at index zero.
2805 search_module = self.graph_root.get_module();
2807 last_private = LastMod(AllPublic);
2809 UseLexicalScope => {
2810 // This is not a crate-relative path. We resolve the
2811 // first component of the path in the current lexical
2812 // scope and then proceed to resolve below that.
2813 match self.resolve_module_in_lexical_scope(
2816 Failed(err) => return Failed(err),
2818 debug!("(resolving module path for import) \
2819 indeterminate; bailing");
2820 return Indeterminate;
2822 Success(containing_module) => {
2823 search_module = containing_module;
2825 last_private = LastMod(AllPublic);
2831 Success(PrefixFound(ref containing_module, index)) => {
2832 search_module = containing_module.clone();
2833 start_index = index;
2834 last_private = LastMod(DependsOn(containing_module.def_id
2840 self.resolve_module_path_from_root(search_module,
2848 /// Invariant: This must only be called during main resolution, not during
2849 /// import resolution.
2850 fn resolve_item_in_lexical_scope(&mut self,
2851 module_: Rc<Module>,
2853 namespace: Namespace)
2854 -> ResolveResult<(Target, bool)> {
2855 debug!("(resolving item in lexical scope) resolving `{}` in \
2856 namespace {:?} in `{}`",
2857 token::get_ident(name),
2859 self.module_to_str(&*module_));
2861 // The current module node is handled specially. First, check for
2862 // its immediate children.
2863 self.populate_module_if_necessary(&module_);
2865 match module_.children.borrow().find(&name.name) {
2867 if name_bindings.defined_in_namespace(namespace) => {
2868 debug!("top name bindings succeeded");
2869 return Success((Target::new(module_.clone(), name_bindings.clone()),
2872 Some(_) | None => { /* Not found; continue. */ }
2875 // Now check for its import directives. We don't have to have resolved
2876 // all its imports in the usual way; this is because chains of
2877 // adjacent import statements are processed as though they mutated the
2879 match module_.import_resolutions.borrow().find(&name.name) {
2881 // Not found; continue.
2883 Some(import_resolution) => {
2884 match (*import_resolution).target_for_namespace(namespace) {
2886 // Not found; continue.
2887 debug!("(resolving item in lexical scope) found \
2888 import resolution, but not in namespace {:?}",
2892 debug!("(resolving item in lexical scope) using \
2893 import resolution");
2894 self.used_imports.insert((import_resolution.id(namespace), namespace));
2895 return Success((target, false));
2901 // Search for external modules.
2902 if namespace == TypeNS {
2903 match module_.external_module_children.borrow().find_copy(&name.name) {
2907 Rc::new(Resolver::create_name_bindings_from_module(module));
2908 debug!("lower name bindings succeeded");
2909 return Success((Target::new(module_, name_bindings), false));
2914 // Finally, proceed up the scope chain looking for parent modules.
2915 let mut search_module = module_;
2917 // Go to the next parent.
2918 match search_module.parent_link.clone() {
2920 // No more parents. This module was unresolved.
2921 debug!("(resolving item in lexical scope) unresolved \
2923 return Failed(None);
2925 ModuleParentLink(parent_module_node, _) => {
2926 match search_module.kind.get() {
2927 NormalModuleKind => {
2928 // We stop the search here.
2929 debug!("(resolving item in lexical \
2930 scope) unresolved module: not \
2931 searching through module \
2933 return Failed(None);
2938 AnonymousModuleKind => {
2939 search_module = parent_module_node.upgrade().unwrap();
2943 BlockParentLink(ref parent_module_node, _) => {
2944 search_module = parent_module_node.upgrade().unwrap();
2948 // Resolve the name in the parent module.
2949 match self.resolve_name_in_module(search_module.clone(),
2954 Failed(Some((span, msg))) =>
2955 self.resolve_error(span, format!("failed to resolve. {}",
2957 Failed(None) => (), // Continue up the search chain.
2959 // We couldn't see through the higher scope because of an
2960 // unresolved import higher up. Bail.
2962 debug!("(resolving item in lexical scope) indeterminate \
2963 higher scope; bailing");
2964 return Indeterminate;
2966 Success((target, used_reexport)) => {
2967 // We found the module.
2968 debug!("(resolving item in lexical scope) found name \
2970 return Success((target, used_reexport));
2976 /// Resolves a module name in the current lexical scope.
2977 fn resolve_module_in_lexical_scope(&mut self,
2978 module_: Rc<Module>,
2980 -> ResolveResult<Rc<Module>> {
2981 // If this module is an anonymous module, resolve the item in the
2982 // lexical scope. Otherwise, resolve the item from the crate root.
2983 let resolve_result = self.resolve_item_in_lexical_scope(
2984 module_, name, TypeNS);
2985 match resolve_result {
2986 Success((target, _)) => {
2987 let bindings = &*target.bindings;
2988 match *bindings.type_def.borrow() {
2989 Some(ref type_def) => {
2990 match type_def.module_def {
2992 debug!("!!! (resolving module in lexical \
2993 scope) module wasn't actually a \
2995 return Failed(None);
2997 Some(ref module_def) => {
2998 return Success(module_def.clone());
3003 debug!("!!! (resolving module in lexical scope) module
3004 wasn't actually a module!");
3005 return Failed(None);
3010 debug!("(resolving module in lexical scope) indeterminate; \
3012 return Indeterminate;
3015 debug!("(resolving module in lexical scope) failed to resolve");
3021 /// Returns the nearest normal module parent of the given module.
3022 fn get_nearest_normal_module_parent(&mut self, module_: Rc<Module>)
3023 -> Option<Rc<Module>> {
3024 let mut module_ = module_;
3026 match module_.parent_link.clone() {
3027 NoParentLink => return None,
3028 ModuleParentLink(new_module, _) |
3029 BlockParentLink(new_module, _) => {
3030 let new_module = new_module.upgrade().unwrap();
3031 match new_module.kind.get() {
3032 NormalModuleKind => return Some(new_module),
3036 AnonymousModuleKind => module_ = new_module,
3043 /// Returns the nearest normal module parent of the given module, or the
3044 /// module itself if it is a normal module.
3045 fn get_nearest_normal_module_parent_or_self(&mut self, module_: Rc<Module>)
3047 match module_.kind.get() {
3048 NormalModuleKind => return module_,
3052 AnonymousModuleKind => {
3053 match self.get_nearest_normal_module_parent(module_.clone()) {
3055 Some(new_module) => new_module
3061 /// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
3062 /// (b) some chain of `super::`.
3063 /// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
3064 fn resolve_module_prefix(&mut self,
3065 module_: Rc<Module>,
3066 module_path: &[Ident])
3067 -> ResolveResult<ModulePrefixResult> {
3068 // Start at the current module if we see `self` or `super`, or at the
3069 // top of the crate otherwise.
3070 let mut containing_module;
3072 let first_module_path_string = token::get_ident(module_path[0]);
3073 if "self" == first_module_path_string.get() {
3075 self.get_nearest_normal_module_parent_or_self(module_);
3077 } else if "super" == first_module_path_string.get() {
3079 self.get_nearest_normal_module_parent_or_self(module_);
3080 i = 0; // We'll handle `super` below.
3082 return Success(NoPrefixFound);
3085 // Now loop through all the `super`s we find.
3086 while i < module_path.len() {
3087 let string = token::get_ident(module_path[i]);
3088 if "super" != string.get() {
3091 debug!("(resolving module prefix) resolving `super` at {}",
3092 self.module_to_str(&*containing_module));
3093 match self.get_nearest_normal_module_parent(containing_module) {
3094 None => return Failed(None),
3095 Some(new_module) => {
3096 containing_module = new_module;
3102 debug!("(resolving module prefix) finished resolving prefix at {}",
3103 self.module_to_str(&*containing_module));
3105 return Success(PrefixFound(containing_module, i));
3108 /// Attempts to resolve the supplied name in the given module for the
3109 /// given namespace. If successful, returns the target corresponding to
3112 /// The boolean returned on success is an indicator of whether this lookup
3113 /// passed through a public re-export proxy.
3114 fn resolve_name_in_module(&mut self,
3115 module_: Rc<Module>,
3117 namespace: Namespace,
3118 name_search_type: NameSearchType,
3119 allow_private_imports: bool)
3120 -> ResolveResult<(Target, bool)> {
3121 debug!("(resolving name in module) resolving `{}` in `{}`",
3122 token::get_name(name).get(),
3123 self.module_to_str(&*module_));
3125 // First, check the direct children of the module.
3126 self.populate_module_if_necessary(&module_);
3128 match module_.children.borrow().find(&name) {
3130 if name_bindings.defined_in_namespace(namespace) => {
3131 debug!("(resolving name in module) found node as child");
3132 return Success((Target::new(module_.clone(), name_bindings.clone()),
3140 // Next, check the module's imports if necessary.
3142 // If this is a search of all imports, we should be done with glob
3143 // resolution at this point.
3144 if name_search_type == PathSearch {
3145 assert_eq!(module_.glob_count.get(), 0);
3148 // Check the list of resolved imports.
3149 match module_.import_resolutions.borrow().find(&name) {
3150 Some(import_resolution) if allow_private_imports ||
3151 import_resolution.is_public => {
3153 if import_resolution.is_public &&
3154 import_resolution.outstanding_references != 0 {
3155 debug!("(resolving name in module) import \
3156 unresolved; bailing out");
3157 return Indeterminate;
3159 match import_resolution.target_for_namespace(namespace) {
3161 debug!("(resolving name in module) name found, \
3162 but not in namespace {:?}",
3166 debug!("(resolving name in module) resolved to \
3168 self.used_imports.insert((import_resolution.id(namespace), namespace));
3169 return Success((target, true));
3173 Some(..) | None => {} // Continue.
3176 // Finally, search through external children.
3177 if namespace == TypeNS {
3178 match module_.external_module_children.borrow().find_copy(&name) {
3182 Rc::new(Resolver::create_name_bindings_from_module(module));
3183 return Success((Target::new(module_, name_bindings), false));
3188 // We're out of luck.
3189 debug!("(resolving name in module) failed to resolve `{}`",
3190 token::get_name(name).get());
3191 return Failed(None);
3194 fn report_unresolved_imports(&mut self, module_: Rc<Module>) {
3195 let index = module_.resolved_import_count.get();
3196 let imports = module_.imports.borrow();
3197 let import_count = imports.len();
3198 if index != import_count {
3199 let sn = self.session
3201 .span_to_snippet(imports.get(index).span)
3203 if sn.as_slice().contains("::") {
3204 self.resolve_error(imports.get(index).span,
3205 "unresolved import");
3207 let err = format!("unresolved import (maybe you meant `{}::*`?)",
3208 sn.as_slice().slice(0, sn.len()));
3209 self.resolve_error(imports.get(index).span, err.as_slice());
3213 // Descend into children and anonymous children.
3214 self.populate_module_if_necessary(&module_);
3216 for (_, child_node) in module_.children.borrow().iter() {
3217 match child_node.get_module_if_available() {
3221 Some(child_module) => {
3222 self.report_unresolved_imports(child_module);
3227 for (_, module_) in module_.anonymous_children.borrow().iter() {
3228 self.report_unresolved_imports(module_.clone());
3234 // This pass simply determines what all "export" keywords refer to and
3235 // writes the results into the export map.
3237 // FIXME #4953 This pass will be removed once exports change to per-item.
3238 // Then this operation can simply be performed as part of item (or import)
3241 fn record_exports(&mut self) {
3242 let root_module = self.graph_root.get_module();
3243 self.record_exports_for_module_subtree(root_module);
3246 fn record_exports_for_module_subtree(&mut self,
3247 module_: Rc<Module>) {
3248 // If this isn't a local krate, then bail out. We don't need to record
3249 // exports for nonlocal crates.
3251 match module_.def_id.get() {
3252 Some(def_id) if def_id.krate == LOCAL_CRATE => {
3254 debug!("(recording exports for module subtree) recording \
3255 exports for local module `{}`",
3256 self.module_to_str(&*module_));
3259 // Record exports for the root module.
3260 debug!("(recording exports for module subtree) recording \
3261 exports for root module `{}`",
3262 self.module_to_str(&*module_));
3266 debug!("(recording exports for module subtree) not recording \
3268 self.module_to_str(&*module_));
3273 self.record_exports_for_module(&*module_);
3274 self.populate_module_if_necessary(&module_);
3276 for (_, child_name_bindings) in module_.children.borrow().iter() {
3277 match child_name_bindings.get_module_if_available() {
3281 Some(child_module) => {
3282 self.record_exports_for_module_subtree(child_module);
3287 for (_, child_module) in module_.anonymous_children.borrow().iter() {
3288 self.record_exports_for_module_subtree(child_module.clone());
3292 fn record_exports_for_module(&mut self, module_: &Module) {
3293 let mut exports2 = Vec::new();
3295 self.add_exports_for_module(&mut exports2, module_);
3296 match module_.def_id.get() {
3298 self.export_map2.borrow_mut().insert(def_id.node, exports2);
3299 debug!("(computing exports) writing exports for {} (some)",
3306 fn add_exports_of_namebindings(&mut self,
3307 exports2: &mut Vec<Export2> ,
3309 namebindings: &NameBindings,
3311 match namebindings.def_for_namespace(ns) {
3313 let name = token::get_name(name);
3314 debug!("(computing exports) YES: export '{}' => {:?}",
3316 exports2.push(Export2 {
3317 name: name.get().to_string(),
3322 debug!("(computing exports) NO: {:?}", d_opt);
3327 fn add_exports_for_module(&mut self,
3328 exports2: &mut Vec<Export2> ,
3330 for (name, importresolution) in module_.import_resolutions.borrow().iter() {
3331 if !importresolution.is_public {
3334 let xs = [TypeNS, ValueNS];
3335 for &ns in xs.iter() {
3336 match importresolution.target_for_namespace(ns) {
3338 debug!("(computing exports) maybe export '{}'",
3339 token::get_name(*name));
3340 self.add_exports_of_namebindings(exports2,
3353 // We maintain a list of value ribs and type ribs.
3355 // Simultaneously, we keep track of the current position in the module
3356 // graph in the `current_module` pointer. When we go to resolve a name in
3357 // the value or type namespaces, we first look through all the ribs and
3358 // then query the module graph. When we resolve a name in the module
3359 // namespace, we can skip all the ribs (since nested modules are not
3360 // allowed within blocks in Rust) and jump straight to the current module
3363 // Named implementations are handled separately. When we find a method
3364 // call, we consult the module node to find all of the implementations in
3365 // scope. This information is lazily cached in the module node. We then
3366 // generate a fake "implementation scope" containing all the
3367 // implementations thus found, for compatibility with old resolve pass.
3369 fn with_scope(&mut self, name: Option<Ident>, f: |&mut Resolver|) {
3370 let orig_module = self.current_module.clone();
3372 // Move down in the graph.
3378 self.populate_module_if_necessary(&orig_module);
3380 match orig_module.children.borrow().find(&name.name) {
3382 debug!("!!! (with scope) didn't find `{}` in `{}`",
3383 token::get_ident(name),
3384 self.module_to_str(&*orig_module));
3386 Some(name_bindings) => {
3387 match (*name_bindings).get_module_if_available() {
3389 debug!("!!! (with scope) didn't find module \
3391 token::get_ident(name),
3392 self.module_to_str(&*orig_module));
3395 self.current_module = module_;
3405 self.current_module = orig_module;
3408 /// Wraps the given definition in the appropriate number of `def_upvar`
3415 -> Option<DefLike> {
3420 DlDef(d @ DefLocal(..)) | DlDef(d @ DefUpvar(..)) |
3421 DlDef(d @ DefArg(..)) | DlDef(d @ DefBinding(..)) => {
3423 is_ty_param = false;
3425 DlDef(d @ DefTyParam(..)) |
3426 DlDef(d @ DefSelfTy(..)) => {
3431 return Some(def_like);
3435 let mut rib_index = rib_index + 1;
3436 while rib_index < ribs.len() {
3437 match ribs[rib_index].kind {
3439 // Nothing to do. Continue.
3441 FunctionRibKind(function_id, body_id) => {
3443 def = DefUpvar(def.def_id().node,
3449 MethodRibKind(item_id, _) => {
3450 // If the def is a ty param, and came from the parent
3453 DefTyParam(_, did, _) if {
3454 self.def_map.borrow().find(&did.node).map(|x| *x)
3455 == Some(DefTyParamBinder(item_id))
3468 // This was an attempt to access an upvar inside a
3469 // named function item. This is not allowed, so we
3474 "can't capture dynamic environment in a fn item; \
3475 use the || { ... } closure form instead");
3477 // This was an attempt to use a type parameter outside
3480 self.resolve_error(span,
3481 "can't use type parameters from \
3482 outer function; try using a local \
3483 type parameter instead");
3492 // This was an attempt to access an upvar inside a
3493 // named function item. This is not allowed, so we
3498 "can't capture dynamic environment in a fn item; \
3499 use the || { ... } closure form instead");
3501 // This was an attempt to use a type parameter outside
3504 self.resolve_error(span,
3505 "can't use type parameters from \
3506 outer function; try using a local \
3507 type parameter instead");
3512 ConstantItemRibKind => {
3515 self.resolve_error(span,
3516 "cannot use an outer type \
3517 parameter in this context");
3519 // Still doesn't deal with upvars
3520 self.resolve_error(span,
3521 "attempt to use a non-constant \
3522 value in a constant");
3531 return Some(DlDef(def));
3534 fn search_ribs(&self,
3538 -> Option<DefLike> {
3539 // FIXME #4950: This should not use a while loop.
3540 // FIXME #4950: Try caching?
3542 let mut i = ribs.len();
3545 let binding_opt = ribs[i].bindings.borrow().find_copy(&name);
3548 return self.upvarify(ribs, i, def_like, span);
3559 fn resolve_crate(&mut self, krate: &ast::Crate) {
3560 debug!("(resolving crate) starting");
3562 visit::walk_crate(self, krate, ());
3565 fn resolve_item(&mut self, item: &Item) {
3566 debug!("(resolving item) resolving {}",
3567 token::get_ident(item.ident));
3571 // enum item: resolve all the variants' discrs,
3572 // then resolve the ty params
3573 ItemEnum(ref enum_def, ref generics) => {
3574 for variant in (*enum_def).variants.iter() {
3575 for dis_expr in variant.node.disr_expr.iter() {
3576 // resolve the discriminator expr
3578 self.with_constant_rib(|this| {
3579 this.resolve_expr(&**dis_expr);
3584 // n.b. the discr expr gets visited twice.
3585 // but maybe it's okay since the first time will signal an
3586 // error if there is one? -- tjc
3587 self.with_type_parameter_rib(HasTypeParameters(generics,
3592 visit::walk_item(this, item, ());
3596 ItemTy(_, ref generics) => {
3597 self.with_type_parameter_rib(HasTypeParameters(generics,
3602 visit::walk_item(this, item, ());
3606 ItemImpl(ref generics,
3607 ref implemented_traits,
3610 self.resolve_implementation(item.id,
3614 methods.as_slice());
3617 ItemTrait(ref generics, _, ref traits, ref methods) => {
3618 // Create a new rib for the self type.
3619 let self_type_rib = Rib::new(ItemRibKind);
3621 // plain insert (no renaming)
3622 let name = self.type_self_ident.name;
3623 self_type_rib.bindings.borrow_mut()
3624 .insert(name, DlDef(DefSelfTy(item.id)));
3625 self.type_ribs.borrow_mut().push(self_type_rib);
3627 // Create a new rib for the trait-wide type parameters.
3628 self.with_type_parameter_rib(HasTypeParameters(generics,
3633 this.resolve_type_parameters(&generics.ty_params);
3635 // Resolve derived traits.
3636 for trt in traits.iter() {
3637 this.resolve_trait_reference(item.id, trt, TraitDerivation);
3640 for method in (*methods).iter() {
3641 // Create a new rib for the method-specific type
3644 // FIXME #4951: Do we need a node ID here?
3647 ast::Required(ref ty_m) => {
3648 this.with_type_parameter_rib
3649 (HasTypeParameters(&ty_m.generics,
3652 MethodRibKind(item.id, Required)),
3655 // Resolve the method-specific type
3657 this.resolve_type_parameters(
3658 &ty_m.generics.ty_params);
3660 for argument in ty_m.decl.inputs.iter() {
3661 this.resolve_type(&*argument.ty);
3664 this.resolve_type(&*ty_m.decl.output);
3667 ast::Provided(ref m) => {
3668 this.resolve_method(MethodRibKind(item.id,
3676 self.type_ribs.borrow_mut().pop();
3679 ItemStruct(ref struct_def, ref generics) => {
3680 self.resolve_struct(item.id,
3682 struct_def.super_struct,
3683 struct_def.fields.as_slice());
3686 ItemMod(ref module_) => {
3687 self.with_scope(Some(item.ident), |this| {
3688 this.resolve_module(module_, item.span, item.ident,
3693 ItemForeignMod(ref foreign_module) => {
3694 self.with_scope(Some(item.ident), |this| {
3695 for foreign_item in foreign_module.items.iter() {
3696 match foreign_item.node {
3697 ForeignItemFn(_, ref generics) => {
3698 this.with_type_parameter_rib(
3700 generics, FnSpace, foreign_item.id,
3702 |this| visit::walk_foreign_item(this,
3706 ForeignItemStatic(..) => {
3707 visit::walk_foreign_item(this,
3716 ItemFn(fn_decl, _, _, ref generics, block) => {
3717 self.resolve_function(ItemRibKind,
3728 self.with_constant_rib(|this| {
3729 visit::walk_item(this, item, ());
3734 // do nothing, these are just around to be encoded
3739 fn with_type_parameter_rib(&mut self,
3740 type_parameters: TypeParameters,
3741 f: |&mut Resolver|) {
3742 match type_parameters {
3743 HasTypeParameters(generics, space, node_id,
3746 let function_type_rib = Rib::new(rib_kind);
3748 for (index, type_parameter) in generics.ty_params.iter().enumerate() {
3749 let ident = type_parameter.ident;
3750 debug!("with_type_parameter_rib: {} {}", node_id,
3752 let def_like = DlDef(DefTyParam(space,
3753 local_def(type_parameter.id),
3755 // Associate this type parameter with
3756 // the item that bound it
3757 self.record_def(type_parameter.id,
3758 (DefTyParamBinder(node_id), LastMod(AllPublic)));
3759 // plain insert (no renaming)
3760 function_type_rib.bindings.borrow_mut()
3761 .insert(ident.name, def_like);
3763 self.type_ribs.borrow_mut().push(function_type_rib);
3766 NoTypeParameters => {
3773 match type_parameters {
3774 HasTypeParameters(..) => { self.type_ribs.borrow_mut().pop(); }
3775 NoTypeParameters => { }
3779 fn with_label_rib(&mut self, f: |&mut Resolver|) {
3780 self.label_ribs.borrow_mut().push(Rib::new(NormalRibKind));
3782 self.label_ribs.borrow_mut().pop();
3785 fn with_constant_rib(&mut self, f: |&mut Resolver|) {
3786 self.value_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind));
3787 self.type_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind));
3789 self.type_ribs.borrow_mut().pop();
3790 self.value_ribs.borrow_mut().pop();
3793 fn resolve_function(&mut self,
3795 optional_declaration: Option<P<FnDecl>>,
3796 type_parameters: TypeParameters,
3798 // Create a value rib for the function.
3799 let function_value_rib = Rib::new(rib_kind);
3800 self.value_ribs.borrow_mut().push(function_value_rib);
3802 // Create a label rib for the function.
3803 let function_label_rib = Rib::new(rib_kind);
3804 self.label_ribs.borrow_mut().push(function_label_rib);
3806 // If this function has type parameters, add them now.
3807 self.with_type_parameter_rib(type_parameters, |this| {
3808 // Resolve the type parameters.
3809 match type_parameters {
3810 NoTypeParameters => {
3813 HasTypeParameters(ref generics, _, _, _) => {
3814 this.resolve_type_parameters(&generics.ty_params);
3818 // Add each argument to the rib.
3819 match optional_declaration {
3823 Some(declaration) => {
3824 for argument in declaration.inputs.iter() {
3825 let mut bindings_list = HashMap::new();
3826 this.resolve_pattern(&*argument.pat,
3827 ArgumentIrrefutableMode,
3828 &mut bindings_list);
3830 this.resolve_type(&*argument.ty);
3832 debug!("(resolving function) recorded argument");
3835 this.resolve_type(&*declaration.output);
3839 // Resolve the function body.
3840 this.resolve_block(&*block);
3842 debug!("(resolving function) leaving function");
3845 self.label_ribs.borrow_mut().pop();
3846 self.value_ribs.borrow_mut().pop();
3849 fn resolve_type_parameters(&mut self,
3850 type_parameters: &OwnedSlice<TyParam>) {
3851 for type_parameter in type_parameters.iter() {
3852 for bound in type_parameter.bounds.iter() {
3853 self.resolve_type_parameter_bound(type_parameter.id, bound);
3855 match type_parameter.default {
3856 Some(ref ty) => self.resolve_type(&**ty),
3862 fn resolve_type_parameter_bound(&mut self,
3864 type_parameter_bound: &TyParamBound) {
3865 match *type_parameter_bound {
3866 TraitTyParamBound(ref tref) => {
3867 self.resolve_trait_reference(id, tref, TraitBoundingTypeParameter)
3869 UnboxedFnTyParamBound(ref unboxed_function) => {
3870 for argument in unboxed_function.decl.inputs.iter() {
3871 self.resolve_type(&*argument.ty);
3874 self.resolve_type(&*unboxed_function.decl.output);
3876 StaticRegionTyParamBound | OtherRegionTyParamBound(_) => {}
3880 fn resolve_trait_reference(&mut self,
3882 trait_reference: &TraitRef,
3883 reference_type: TraitReferenceType) {
3884 match self.resolve_path(id, &trait_reference.path, TypeNS, true) {
3886 let path_str = self.path_idents_to_str(&trait_reference.path);
3887 let usage_str = match reference_type {
3888 TraitBoundingTypeParameter => "bound type parameter with",
3889 TraitImplementation => "implement",
3890 TraitDerivation => "derive"
3893 let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str);
3894 self.resolve_error(trait_reference.path.span, msg.as_slice());
3897 debug!("(resolving trait) found trait def: {:?}", def);
3898 self.record_def(trait_reference.ref_id, def);
3903 fn resolve_struct(&mut self,
3905 generics: &Generics,
3906 super_struct: Option<P<Ty>>,
3907 fields: &[StructField]) {
3908 // If applicable, create a rib for the type parameters.
3909 self.with_type_parameter_rib(HasTypeParameters(generics,
3914 // Resolve the type parameters.
3915 this.resolve_type_parameters(&generics.ty_params);
3917 // Resolve the super struct.
3918 match super_struct {
3919 Some(t) => match t.node {
3920 TyPath(ref path, None, path_id) => {
3921 match this.resolve_path(id, path, TypeNS, true) {
3922 Some((DefTy(def_id), lp)) if this.structs.contains_key(&def_id) => {
3923 let def = DefStruct(def_id);
3924 debug!("(resolving struct) resolved `{}` to type {:?}",
3925 token::get_ident(path.segments
3929 debug!("(resolving struct) writing resolution for `{}` (id {})",
3930 this.path_idents_to_str(path),
3932 this.record_def(path_id, (def, lp));
3934 Some((DefStruct(_), _)) => {
3935 this.session.span_err(t.span,
3936 "super-struct is defined \
3937 in a different crate")
3939 Some(_) => this.session.span_err(t.span,
3940 "super-struct is not a struct type"),
3941 None => this.session.span_err(t.span,
3942 "super-struct could not be resolved"),
3945 _ => this.session.span_bug(t.span, "path not mapped to a TyPath")
3951 for field in fields.iter() {
3952 this.resolve_type(&*field.node.ty);
3957 // Does this really need to take a RibKind or is it always going
3958 // to be NormalRibKind?
3959 fn resolve_method(&mut self,
3962 let method_generics = &method.generics;
3963 let type_parameters = HasTypeParameters(method_generics,
3968 self.resolve_function(rib_kind, Some(method.decl), type_parameters, method.body);
3971 fn with_current_self_type<T>(&mut self, self_type: &Ty, f: |&mut Resolver| -> T) -> T {
3972 // Handle nested impls (inside fn bodies)
3973 let previous_value = replace(&mut self.current_self_type, Some(self_type.clone()));
3974 let result = f(self);
3975 self.current_self_type = previous_value;
3979 fn with_optional_trait_ref<T>(&mut self, id: NodeId,
3980 opt_trait_ref: &Option<TraitRef>,
3981 f: |&mut Resolver| -> T) -> T {
3982 let new_val = match *opt_trait_ref {
3983 Some(ref trait_ref) => {
3984 self.resolve_trait_reference(id, trait_ref, TraitImplementation);
3986 match self.def_map.borrow().find(&trait_ref.ref_id) {
3988 let did = def.def_id();
3989 Some((did, trait_ref.clone()))
3996 let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
3997 let result = f(self);
3998 self.current_trait_ref = original_trait_ref;
4002 fn resolve_implementation(&mut self,
4004 generics: &Generics,
4005 opt_trait_reference: &Option<TraitRef>,
4007 methods: &[Gc<Method>]) {
4008 // If applicable, create a rib for the type parameters.
4009 self.with_type_parameter_rib(HasTypeParameters(generics,
4014 // Resolve the type parameters.
4015 this.resolve_type_parameters(&generics.ty_params);
4017 // Resolve the trait reference, if necessary.
4018 this.with_optional_trait_ref(id, opt_trait_reference, |this| {
4019 // Resolve the self type.
4020 this.resolve_type(self_type);
4022 this.with_current_self_type(self_type, |this| {
4023 for method in methods.iter() {
4024 // We also need a new scope for the method-specific type parameters.
4025 this.resolve_method(MethodRibKind(id, Provided(method.id)),
4033 fn resolve_module(&mut self, module: &Mod, _span: Span,
4034 _name: Ident, id: NodeId) {
4035 // Write the implementations in scope into the module metadata.
4036 debug!("(resolving module) resolving module ID {}", id);
4037 visit::walk_mod(self, module, ());
4040 fn resolve_local(&mut self, local: &Local) {
4041 // Resolve the type.
4042 self.resolve_type(&*local.ty);
4044 // Resolve the initializer, if necessary.
4049 Some(ref initializer) => {
4050 self.resolve_expr(&**initializer);
4054 // Resolve the pattern.
4055 let mut bindings_list = HashMap::new();
4056 self.resolve_pattern(&*local.pat,
4057 LocalIrrefutableMode,
4058 &mut bindings_list);
4061 // build a map from pattern identifiers to binding-info's.
4062 // this is done hygienically. This could arise for a macro
4063 // that expands into an or-pattern where one 'x' was from the
4064 // user and one 'x' came from the macro.
4065 fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap {
4066 let mut result = HashMap::new();
4067 pat_bindings(&self.def_map, pat, |binding_mode, _id, sp, path| {
4068 let name = mtwt::resolve(path_to_ident(path));
4070 binding_info {span: sp,
4071 binding_mode: binding_mode});
4076 // check that all of the arms in an or-pattern have exactly the
4077 // same set of bindings, with the same binding modes for each.
4078 fn check_consistent_bindings(&mut self, arm: &Arm) {
4079 if arm.pats.len() == 0 {
4082 let map_0 = self.binding_mode_map(&**arm.pats.get(0));
4083 for (i, p) in arm.pats.iter().enumerate() {
4084 let map_i = self.binding_mode_map(&**p);
4086 for (&key, &binding_0) in map_0.iter() {
4087 match map_i.find(&key) {
4091 format!("variable `{}` from pattern #1 is \
4092 not bound in pattern #{}",
4093 token::get_name(key),
4096 Some(binding_i) => {
4097 if binding_0.binding_mode != binding_i.binding_mode {
4100 format!("variable `{}` is bound with different \
4101 mode in pattern #{} than in pattern #1",
4102 token::get_name(key),
4109 for (&key, &binding) in map_i.iter() {
4110 if !map_0.contains_key(&key) {
4113 format!("variable `{}` from pattern {}{} is \
4114 not bound in pattern {}1",
4115 token::get_name(key),
4116 "#", i + 1, "#").as_slice());
4122 fn resolve_arm(&mut self, arm: &Arm) {
4123 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4125 let mut bindings_list = HashMap::new();
4126 for pattern in arm.pats.iter() {
4127 self.resolve_pattern(&**pattern, RefutableMode, &mut bindings_list);
4130 // This has to happen *after* we determine which
4131 // pat_idents are variants
4132 self.check_consistent_bindings(arm);
4134 visit::walk_expr_opt(self, arm.guard, ());
4135 self.resolve_expr(&*arm.body);
4137 self.value_ribs.borrow_mut().pop();
4140 fn resolve_block(&mut self, block: &Block) {
4141 debug!("(resolving block) entering block");
4142 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4144 // Move down in the graph, if there's an anonymous module rooted here.
4145 let orig_module = self.current_module.clone();
4146 match orig_module.anonymous_children.borrow().find(&block.id) {
4147 None => { /* Nothing to do. */ }
4148 Some(anonymous_module) => {
4149 debug!("(resolving block) found anonymous module, moving \
4151 self.current_module = anonymous_module.clone();
4155 // Descend into the block.
4156 visit::walk_block(self, block, ());
4159 self.current_module = orig_module;
4161 self.value_ribs.borrow_mut().pop();
4162 debug!("(resolving block) leaving block");
4165 fn resolve_type(&mut self, ty: &Ty) {
4167 // Like path expressions, the interpretation of path types depends
4168 // on whether the path has multiple elements in it or not.
4170 TyPath(ref path, ref bounds, path_id) => {
4171 // This is a path in the type namespace. Walk through scopes
4173 let mut result_def = None;
4175 // First, check to see whether the name is a primitive type.
4176 if path.segments.len() == 1 {
4177 let id = path.segments.last().unwrap().identifier;
4179 match self.primitive_type_table
4183 Some(&primitive_type) => {
4185 Some((DefPrimTy(primitive_type), LastMod(AllPublic)));
4189 .any(|s| !s.lifetimes.is_empty()) {
4190 self.session.span_err(path.span,
4191 "lifetime parameters \
4192 are not allowed on \
4194 } else if path.segments
4196 .any(|s| s.types.len() > 0) {
4197 self.session.span_err(path.span,
4198 "type parameters are \
4199 not allowed on this \
4211 match self.resolve_path(ty.id, path, TypeNS, true) {
4213 debug!("(resolving type) resolved `{}` to \
4215 token::get_ident(path.segments
4219 result_def = Some(def);
4226 Some(_) => {} // Continue.
4231 // Write the result into the def map.
4232 debug!("(resolving type) writing resolution for `{}` \
4234 self.path_idents_to_str(path),
4236 self.record_def(path_id, def);
4239 let msg = format!("use of undeclared type name `{}`",
4240 self.path_idents_to_str(path));
4241 self.resolve_error(ty.span, msg.as_slice());
4245 bounds.as_ref().map(|bound_vec| {
4246 for bound in bound_vec.iter() {
4247 self.resolve_type_parameter_bound(ty.id, bound);
4252 TyClosure(c, _) | TyProc(c) => {
4253 c.bounds.as_ref().map(|bounds| {
4254 for bound in bounds.iter() {
4255 self.resolve_type_parameter_bound(ty.id, bound);
4258 visit::walk_ty(self, ty, ());
4262 // Just resolve embedded types.
4263 visit::walk_ty(self, ty, ());
4268 fn resolve_pattern(&mut self,
4270 mode: PatternBindingMode,
4271 // Maps idents to the node ID for the (outermost)
4272 // pattern that binds them
4273 bindings_list: &mut HashMap<Name,NodeId>) {
4274 let pat_id = pattern.id;
4275 walk_pat(pattern, |pattern| {
4276 match pattern.node {
4277 PatIdent(binding_mode, ref path, _)
4278 if !path.global && path.segments.len() == 1 => {
4280 // The meaning of pat_ident with no type parameters
4281 // depends on whether an enum variant or unit-like struct
4282 // with that name is in scope. The probing lookup has to
4283 // be careful not to emit spurious errors. Only matching
4284 // patterns (match) can match nullary variants or
4285 // unit-like structs. For binding patterns (let), matching
4286 // such a value is simply disallowed (since it's rarely
4289 let ident = path.segments.get(0).identifier;
4290 let renamed = mtwt::resolve(ident);
4292 match self.resolve_bare_identifier_pattern(ident) {
4293 FoundStructOrEnumVariant(def, lp)
4294 if mode == RefutableMode => {
4295 debug!("(resolving pattern) resolving `{}` to \
4296 struct or enum variant",
4297 token::get_name(renamed));
4299 self.enforce_default_binding_mode(
4303 self.record_def(pattern.id, (def, lp));
4305 FoundStructOrEnumVariant(..) => {
4308 format!("declaration of `{}` shadows an enum \
4309 variant or unit-like struct in \
4311 token::get_name(renamed)).as_slice());
4313 FoundConst(def, lp) if mode == RefutableMode => {
4314 debug!("(resolving pattern) resolving `{}` to \
4316 token::get_name(renamed));
4318 self.enforce_default_binding_mode(
4322 self.record_def(pattern.id, (def, lp));
4325 self.resolve_error(pattern.span,
4326 "only irrefutable patterns \
4329 BareIdentifierPatternUnresolved => {
4330 debug!("(resolving pattern) binding `{}`",
4331 token::get_name(renamed));
4333 let def = match mode {
4335 // For pattern arms, we must use
4336 // `def_binding` definitions.
4338 DefBinding(pattern.id, binding_mode)
4340 LocalIrrefutableMode => {
4341 // But for locals, we use `def_local`.
4342 DefLocal(pattern.id, binding_mode)
4344 ArgumentIrrefutableMode => {
4345 // And for function arguments, `def_arg`.
4346 DefArg(pattern.id, binding_mode)
4350 // Record the definition so that later passes
4351 // will be able to distinguish variants from
4352 // locals in patterns.
4354 self.record_def(pattern.id, (def, LastMod(AllPublic)));
4356 // Add the binding to the local ribs, if it
4357 // doesn't already exist in the bindings list. (We
4358 // must not add it if it's in the bindings list
4359 // because that breaks the assumptions later
4360 // passes make about or-patterns.)
4362 if !bindings_list.contains_key(&renamed) {
4363 let this = &mut *self;
4364 let value_ribs = this.value_ribs.borrow();
4365 let length = value_ribs.len();
4366 let last_rib = value_ribs.get(
4368 last_rib.bindings.borrow_mut()
4369 .insert(renamed, DlDef(def));
4370 bindings_list.insert(renamed, pat_id);
4371 } else if bindings_list.find(&renamed) ==
4373 // Then this is a duplicate variable in the
4374 // same disjunction, which is an error.
4375 self.resolve_error(pattern.span,
4376 format!("identifier `{}` is bound \
4377 more than once in the same \
4379 path_to_str(path)).as_slice());
4381 // Else, not bound in the same pattern: do
4386 // Check the types in the path pattern.
4387 for ty in path.segments
4389 .flat_map(|seg| seg.types.iter()) {
4390 self.resolve_type(&**ty);
4394 PatIdent(binding_mode, ref path, _) => {
4395 // This must be an enum variant, struct, or constant.
4396 match self.resolve_path(pat_id, path, ValueNS, false) {
4397 Some(def @ (DefVariant(..), _)) |
4398 Some(def @ (DefStruct(..), _)) => {
4399 self.record_def(pattern.id, def);
4401 Some(def @ (DefStatic(..), _)) => {
4402 self.enforce_default_binding_mode(
4406 self.record_def(pattern.id, def);
4411 format!("`{}` is not an enum variant or constant",
4416 .identifier)).as_slice())
4419 self.resolve_error(path.span,
4420 "unresolved enum variant");
4424 // Check the types in the path pattern.
4425 for ty in path.segments
4427 .flat_map(|s| s.types.iter()) {
4428 self.resolve_type(&**ty);
4432 PatEnum(ref path, _) => {
4433 // This must be an enum variant, struct or const.
4434 match self.resolve_path(pat_id, path, ValueNS, false) {
4435 Some(def @ (DefFn(..), _)) |
4436 Some(def @ (DefVariant(..), _)) |
4437 Some(def @ (DefStruct(..), _)) |
4438 Some(def @ (DefStatic(..), _)) => {
4439 self.record_def(pattern.id, def);
4442 self.resolve_error(path.span,
4443 format!("`{}` is not an enum variant, struct or const",
4448 .identifier)).as_slice());
4451 self.resolve_error(path.span,
4452 format!("unresolved enum variant, struct or const `{}`",
4457 .identifier)).as_slice());
4461 // Check the types in the path pattern.
4462 for ty in path.segments
4464 .flat_map(|s| s.types.iter()) {
4465 self.resolve_type(&**ty);
4469 PatLit(ref expr) => {
4470 self.resolve_expr(&**expr);
4473 PatRange(ref first_expr, ref last_expr) => {
4474 self.resolve_expr(&**first_expr);
4475 self.resolve_expr(&**last_expr);
4478 PatStruct(ref path, _, _) => {
4479 match self.resolve_path(pat_id, path, TypeNS, false) {
4480 Some((DefTy(class_id), lp))
4481 if self.structs.contains_key(&class_id) => {
4482 let class_def = DefStruct(class_id);
4483 self.record_def(pattern.id, (class_def, lp));
4485 Some(definition @ (DefStruct(class_id), _)) => {
4486 assert!(self.structs.contains_key(&class_id));
4487 self.record_def(pattern.id, definition);
4489 Some(definition @ (DefVariant(_, variant_id, _), _))
4490 if self.structs.contains_key(&variant_id) => {
4491 self.record_def(pattern.id, definition);
4494 debug!("(resolving pattern) didn't find struct \
4495 def: {:?}", result);
4496 let msg = format!("`{}` does not name a structure",
4497 self.path_idents_to_str(path));
4498 self.resolve_error(path.span, msg.as_slice());
4511 fn resolve_bare_identifier_pattern(&mut self, name: Ident)
4512 -> BareIdentifierPatternResolution {
4513 let module = self.current_module.clone();
4514 match self.resolve_item_in_lexical_scope(module,
4517 Success((target, _)) => {
4518 debug!("(resolve bare identifier pattern) succeeded in \
4519 finding {} at {:?}",
4520 token::get_ident(name),
4521 target.bindings.value_def.borrow());
4522 match *target.bindings.value_def.borrow() {
4524 fail!("resolved name in the value namespace to a \
4525 set of name bindings with no def?!");
4528 // For the two success cases, this lookup can be
4529 // considered as not having a private component because
4530 // the lookup happened only within the current module.
4532 def @ DefVariant(..) | def @ DefStruct(..) => {
4533 return FoundStructOrEnumVariant(def, LastMod(AllPublic));
4535 def @ DefStatic(_, false) => {
4536 return FoundConst(def, LastMod(AllPublic));
4539 return BareIdentifierPatternUnresolved;
4547 fail!("unexpected indeterminate result");
4551 Some((span, msg)) => {
4552 self.resolve_error(span, format!("failed to resolve: {}",
4558 debug!("(resolve bare identifier pattern) failed to find {}",
4559 token::get_ident(name));
4560 return BareIdentifierPatternUnresolved;
4565 /// If `check_ribs` is true, checks the local definitions first; i.e.
4566 /// doesn't skip straight to the containing module.
4567 fn resolve_path(&mut self,
4570 namespace: Namespace,
4571 check_ribs: bool) -> Option<(Def, LastPrivate)> {
4572 // First, resolve the types.
4573 for ty in path.segments.iter().flat_map(|s| s.types.iter()) {
4574 self.resolve_type(&**ty);
4578 return self.resolve_crate_relative_path(path, namespace);
4581 let unqualified_def =
4582 self.resolve_identifier(path.segments
4589 if path.segments.len() > 1 {
4590 let def = self.resolve_module_relative_path(path, namespace);
4591 match (def, unqualified_def) {
4592 (Some((d, _)), Some((ud, _))) if d == ud => {
4594 .add_lint(UnnecessaryQualification,
4597 "unnecessary qualification".to_string());
4605 return unqualified_def;
4608 // resolve a single identifier (used as a varref)
4609 fn resolve_identifier(&mut self,
4611 namespace: Namespace,
4614 -> Option<(Def, LastPrivate)> {
4616 match self.resolve_identifier_in_local_ribs(identifier,
4620 return Some((def, LastMod(AllPublic)));
4628 return self.resolve_item_by_identifier_in_lexical_scope(identifier,
4632 // FIXME #4952: Merge me with resolve_name_in_module?
4633 fn resolve_definition_of_name_in_module(&mut self,
4634 containing_module: Rc<Module>,
4636 namespace: Namespace)
4638 // First, search children.
4639 self.populate_module_if_necessary(&containing_module);
4641 match containing_module.children.borrow().find(&name) {
4642 Some(child_name_bindings) => {
4643 match child_name_bindings.def_for_namespace(namespace) {
4645 // Found it. Stop the search here.
4646 let p = child_name_bindings.defined_in_public_namespace(
4648 let lp = if p {LastMod(AllPublic)} else {
4649 LastMod(DependsOn(def.def_id()))
4651 return ChildNameDefinition(def, lp);
4659 // Next, search import resolutions.
4660 match containing_module.import_resolutions.borrow().find(&name) {
4661 Some(import_resolution) if import_resolution.is_public => {
4662 match (*import_resolution).target_for_namespace(namespace) {
4664 match target.bindings.def_for_namespace(namespace) {
4667 let id = import_resolution.id(namespace);
4668 self.used_imports.insert((id, namespace));
4669 return ImportNameDefinition(def, LastMod(AllPublic));
4672 // This can happen with external impls, due to
4673 // the imperfect way we read the metadata.
4680 Some(..) | None => {} // Continue.
4683 // Finally, search through external children.
4684 if namespace == TypeNS {
4685 match containing_module.external_module_children.borrow()
4689 match module.def_id.get() {
4690 None => {} // Continue.
4692 let lp = if module.is_public {LastMod(AllPublic)} else {
4693 LastMod(DependsOn(def_id))
4695 return ChildNameDefinition(DefMod(def_id), lp);
4702 return NoNameDefinition;
4705 // resolve a "module-relative" path, e.g. a::b::c
4706 fn resolve_module_relative_path(&mut self,
4708 namespace: Namespace)
4709 -> Option<(Def, LastPrivate)> {
4710 let module_path_idents = path.segments.init().iter()
4711 .map(|ps| ps.identifier)
4712 .collect::<Vec<_>>();
4714 let containing_module;
4716 let module = self.current_module.clone();
4717 match self.resolve_module_path(module,
4718 module_path_idents.as_slice(),
4723 let (span, msg) = match err {
4724 Some((span, msg)) => (span, msg),
4726 let msg = format!("Use of undeclared module `{}`",
4728 module_path_idents.as_slice()));
4733 self.resolve_error(span, format!("failed to resolve. {}",
4737 Indeterminate => fail!("indeterminate unexpected"),
4738 Success((resulting_module, resulting_last_private)) => {
4739 containing_module = resulting_module;
4740 last_private = resulting_last_private;
4744 let ident = path.segments.last().unwrap().identifier;
4745 let def = match self.resolve_definition_of_name_in_module(containing_module.clone(),
4748 NoNameDefinition => {
4749 // We failed to resolve the name. Report an error.
4752 ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
4753 (def, last_private.or(lp))
4756 match containing_module.kind.get() {
4757 TraitModuleKind | ImplModuleKind => {
4758 match containing_module.def_id.get() {
4760 match self.method_map.borrow().find(&(ident.name, def_id)) {
4761 Some(x) if *x == SelfStatic => (),
4764 debug!("containing module was a trait or impl \
4765 and name was a method -> not resolved");
4778 /// Invariant: This must be called only during main resolution, not during
4779 /// import resolution.
4780 fn resolve_crate_relative_path(&mut self,
4782 namespace: Namespace)
4783 -> Option<(Def, LastPrivate)> {
4784 let module_path_idents = path.segments.init().iter()
4785 .map(|ps| ps.identifier)
4786 .collect::<Vec<_>>();
4788 let root_module = self.graph_root.get_module();
4790 let containing_module;
4792 match self.resolve_module_path_from_root(root_module,
4793 module_path_idents.as_slice(),
4797 LastMod(AllPublic)) {
4799 let (span, msg) = match err {
4800 Some((span, msg)) => (span, msg),
4802 let msg = format!("Use of undeclared module `::{}`",
4804 module_path_idents.as_slice()));
4809 self.resolve_error(span, format!("failed to resolve. {}",
4815 fail!("indeterminate unexpected");
4818 Success((resulting_module, resulting_last_private)) => {
4819 containing_module = resulting_module;
4820 last_private = resulting_last_private;
4824 let name = path.segments.last().unwrap().identifier.name;
4825 match self.resolve_definition_of_name_in_module(containing_module,
4828 NoNameDefinition => {
4829 // We failed to resolve the name. Report an error.
4832 ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
4833 return Some((def, last_private.or(lp)));
4838 fn resolve_identifier_in_local_ribs(&mut self,
4840 namespace: Namespace,
4843 // Check the local set of ribs.
4844 let search_result = match namespace {
4846 let renamed = mtwt::resolve(ident);
4847 self.search_ribs(self.value_ribs.borrow().as_slice(),
4851 let name = ident.name;
4852 self.search_ribs(self.type_ribs.borrow().as_slice(), name, span)
4856 match search_result {
4857 Some(DlDef(def)) => {
4858 debug!("(resolving path in local ribs) resolved `{}` to \
4860 token::get_ident(ident),
4864 Some(DlField) | Some(DlImpl(_)) | None => {
4870 fn resolve_item_by_identifier_in_lexical_scope(&mut self,
4872 namespace: Namespace)
4873 -> Option<(Def, LastPrivate)> {
4875 let module = self.current_module.clone();
4876 match self.resolve_item_in_lexical_scope(module,
4879 Success((target, _)) => {
4880 match (*target.bindings).def_for_namespace(namespace) {
4882 // This can happen if we were looking for a type and
4883 // found a module instead. Modules don't have defs.
4884 debug!("(resolving item path by identifier in lexical \
4885 scope) failed to resolve {} after success...",
4886 token::get_ident(ident));
4890 debug!("(resolving item path in lexical scope) \
4891 resolved `{}` to item",
4892 token::get_ident(ident));
4893 // This lookup is "all public" because it only searched
4894 // for one identifier in the current module (couldn't
4895 // have passed through reexports or anything like that.
4896 return Some((def, LastMod(AllPublic)));
4901 fail!("unexpected indeterminate result");
4905 Some((span, msg)) =>
4906 self.resolve_error(span, format!("failed to resolve. {}", msg)),
4910 debug!("(resolving item path by identifier in lexical scope) \
4911 failed to resolve {}", token::get_ident(ident));
4917 fn with_no_errors<T>(&mut self, f: |&mut Resolver| -> T) -> T {
4918 self.emit_errors = false;
4920 self.emit_errors = true;
4924 fn resolve_error<T: Str>(&self, span: Span, s: T) {
4925 if self.emit_errors {
4926 self.session.span_err(span, s.as_slice());
4930 fn find_fallback_in_self_type(&mut self, name: Name) -> FallbackSuggestion {
4931 #[deriving(PartialEq)]
4932 enum FallbackChecks {
4937 fn extract_path_and_node_id(t: &Ty, allow: FallbackChecks)
4938 -> Option<(Path, NodeId, FallbackChecks)> {
4940 TyPath(ref path, _, node_id) => Some((path.clone(), node_id, allow)),
4941 TyPtr(mut_ty) => extract_path_and_node_id(&*mut_ty.ty, OnlyTraitAndStatics),
4942 TyRptr(_, mut_ty) => extract_path_and_node_id(&*mut_ty.ty, allow),
4943 // This doesn't handle the remaining `Ty` variants as they are not
4944 // that commonly the self_type, it might be interesting to provide
4945 // support for those in future.
4950 fn get_module(this: &mut Resolver, span: Span, ident_path: &[ast::Ident])
4951 -> Option<Rc<Module>> {
4952 let root = this.current_module.clone();
4953 let last_name = ident_path.last().unwrap().name;
4955 if ident_path.len() == 1 {
4956 match this.primitive_type_table.primitive_types.find(&last_name) {
4959 match this.current_module.children.borrow().find(&last_name) {
4960 Some(child) => child.get_module_if_available(),
4966 match this.resolve_module_path(root,
4967 ident_path.as_slice(),
4971 Success((module, _)) => Some(module),
4977 let (path, node_id, allowed) = match self.current_self_type {
4978 Some(ref ty) => match extract_path_and_node_id(ty, Everything) {
4980 None => return NoSuggestion,
4982 None => return NoSuggestion,
4985 if allowed == Everything {
4986 // Look for a field with the same name in the current self_type.
4987 match self.def_map.borrow().find(&node_id) {
4989 | Some(&DefStruct(did))
4990 | Some(&DefVariant(_, did, _)) => match self.structs.find(&did) {
4993 if fields.iter().any(|&field_name| name == field_name) {
4998 _ => {} // Self type didn't resolve properly
5002 let ident_path = path.segments.iter().map(|seg| seg.identifier).collect::<Vec<_>>();
5004 // Look for a method in the current self type's impl module.
5005 match get_module(self, path.span, ident_path.as_slice()) {
5006 Some(module) => match module.children.borrow().find(&name) {
5008 let p_str = self.path_idents_to_str(&path);
5009 match binding.def_for_namespace(ValueNS) {
5010 Some(DefStaticMethod(_, provenance, _)) => {
5012 FromImpl(_) => return StaticMethod(p_str),
5013 FromTrait(_) => unreachable!()
5016 Some(DefMethod(_, None)) if allowed == Everything => return Method,
5017 Some(DefMethod(_, Some(_))) => return TraitMethod,
5026 // Look for a method in the current trait.
5027 let method_map = self.method_map.borrow();
5028 match self.current_trait_ref {
5029 Some((did, ref trait_ref)) => {
5030 let path_str = self.path_idents_to_str(&trait_ref.path);
5032 match method_map.find(&(name, did)) {
5033 Some(&SelfStatic) => return StaticTraitMethod(path_str),
5034 Some(_) => return TraitMethod,
5044 fn find_best_match_for_name(&mut self, name: &str, max_distance: uint)
5046 let this = &mut *self;
5048 let mut maybes: Vec<token::InternedString> = Vec::new();
5049 let mut values: Vec<uint> = Vec::new();
5051 let mut j = this.value_ribs.borrow().len();
5054 let value_ribs = this.value_ribs.borrow();
5055 let bindings = value_ribs.get(j).bindings.borrow();
5056 for (&k, _) in bindings.iter() {
5057 maybes.push(token::get_name(k));
5058 values.push(uint::MAX);
5062 let mut smallest = 0;
5063 for (i, other) in maybes.iter().enumerate() {
5064 *values.get_mut(i) = name.lev_distance(other.get());
5066 if *values.get(i) <= *values.get(smallest) {
5071 if values.len() > 0 &&
5072 *values.get(smallest) != uint::MAX &&
5073 *values.get(smallest) < name.len() + 2 &&
5074 *values.get(smallest) <= max_distance &&
5075 name != maybes.get(smallest).get() {
5077 Some(maybes.get(smallest).get().to_string())
5084 fn resolve_expr(&mut self, expr: &Expr) {
5085 // First, record candidate traits for this expression if it could
5086 // result in the invocation of a method call.
5088 self.record_candidate_traits_for_expr_if_necessary(expr);
5090 // Next, resolve the node.
5092 // The interpretation of paths depends on whether the path has
5093 // multiple elements in it or not.
5095 ExprPath(ref path) => {
5096 // This is a local path in the value namespace. Walk through
5097 // scopes looking for it.
5099 match self.resolve_path(expr.id, path, ValueNS, true) {
5101 // Write the result into the def map.
5102 debug!("(resolving expr) resolved `{}`",
5103 self.path_idents_to_str(path));
5105 // First-class methods are not supported yet; error
5108 (DefMethod(..), _) => {
5109 self.resolve_error(expr.span,
5110 "first-class methods \
5111 are not supported");
5112 self.session.span_note(expr.span,
5120 self.record_def(expr.id, def);
5123 let wrong_name = self.path_idents_to_str(path);
5124 // Be helpful if the name refers to a struct
5125 // (The pattern matching def_tys where the id is in self.structs
5126 // matches on regular structs while excluding tuple- and enum-like
5127 // structs, which wouldn't result in this error.)
5128 match self.with_no_errors(|this|
5129 this.resolve_path(expr.id, path, TypeNS, false)) {
5130 Some((DefTy(struct_id), _))
5131 if self.structs.contains_key(&struct_id) => {
5132 self.resolve_error(expr.span,
5133 format!("`{}` is a structure name, but \
5135 uses it like a function name",
5136 wrong_name).as_slice());
5138 self.session.span_note(expr.span,
5139 format!("Did you mean to write: \
5140 `{} {{ /* fields */ }}`?",
5141 wrong_name).as_slice());
5145 let mut method_scope = false;
5146 self.value_ribs.borrow().iter().rev().advance(|rib| {
5147 let res = match *rib {
5148 Rib { bindings: _, kind: MethodRibKind(_, _) } => true,
5149 Rib { bindings: _, kind: ItemRibKind } => false,
5150 _ => return true, // Keep advancing
5154 false // Stop advancing
5157 if method_scope && token::get_name(self.self_ident.name).get()
5158 == wrong_name.as_slice() {
5161 "`self` is not available \
5162 in a static method. Maybe a \
5163 `self` argument is missing?");
5165 let name = path_to_ident(path).name;
5166 let mut msg = match self.find_fallback_in_self_type(name) {
5168 // limit search to 5 to reduce the number
5169 // of stupid suggestions
5170 self.find_best_match_for_name(wrong_name.as_slice(), 5)
5171 .map_or("".to_string(),
5172 |x| format!("`{}`", x))
5175 format!("`self.{}`", wrong_name),
5178 format!("to call `self.{}`", wrong_name),
5179 StaticTraitMethod(path_str)
5180 | StaticMethod(path_str) =>
5181 format!("to call `{}::{}`", path_str, wrong_name)
5185 msg = format!(" Did you mean {}?", msg)
5190 format!("unresolved name `{}`.{}",
5199 visit::walk_expr(self, expr, ());
5202 ExprFnBlock(fn_decl, block) |
5203 ExprProc(fn_decl, block) => {
5204 self.resolve_function(FunctionRibKind(expr.id, block.id),
5205 Some(fn_decl), NoTypeParameters,
5209 ExprStruct(ref path, _, _) => {
5210 // Resolve the path to the structure it goes to.
5211 match self.resolve_path(expr.id, path, TypeNS, false) {
5212 Some((DefTy(class_id), lp)) | Some((DefStruct(class_id), lp))
5213 if self.structs.contains_key(&class_id) => {
5214 let class_def = DefStruct(class_id);
5215 self.record_def(expr.id, (class_def, lp));
5217 Some(definition @ (DefVariant(_, class_id, _), _))
5218 if self.structs.contains_key(&class_id) => {
5219 self.record_def(expr.id, definition);
5222 debug!("(resolving expression) didn't find struct \
5223 def: {:?}", result);
5224 let msg = format!("`{}` does not name a structure",
5225 self.path_idents_to_str(path));
5226 self.resolve_error(path.span, msg.as_slice());
5230 visit::walk_expr(self, expr, ());
5233 ExprLoop(_, Some(label)) => {
5234 self.with_label_rib(|this| {
5235 let def_like = DlDef(DefLabel(expr.id));
5238 let label_ribs = this.label_ribs.borrow();
5239 let length = label_ribs.len();
5240 let rib = label_ribs.get(length - 1);
5241 let renamed = mtwt::resolve(label);
5242 rib.bindings.borrow_mut().insert(renamed, def_like);
5245 visit::walk_expr(this, expr, ());
5249 ExprForLoop(..) => fail!("non-desugared expr_for_loop"),
5251 ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
5252 let renamed = mtwt::resolve(label);
5253 match self.search_ribs(self.label_ribs.borrow().as_slice(),
5254 renamed, expr.span) {
5258 format!("use of undeclared label `{}`",
5259 token::get_ident(label)).as_slice())
5261 Some(DlDef(def @ DefLabel(_))) => {
5262 // Since this def is a label, it is never read.
5263 self.record_def(expr.id, (def, LastMod(AllPublic)))
5266 self.session.span_bug(expr.span,
5267 "label wasn't mapped to a \
5274 visit::walk_expr(self, expr, ());
5279 fn record_candidate_traits_for_expr_if_necessary(&mut self, expr: &Expr) {
5281 ExprField(_, ident, _) => {
5282 // FIXME(#6890): Even though you can't treat a method like a
5283 // field, we need to add any trait methods we find that match
5284 // the field name so that we can do some nice error reporting
5285 // later on in typeck.
5286 let traits = self.search_for_traits_containing_method(ident.node.name);
5287 self.trait_map.insert(expr.id, traits);
5289 ExprMethodCall(ident, _, _) => {
5290 debug!("(recording candidate traits for expr) recording \
5293 let traits = self.search_for_traits_containing_method(ident.node.name);
5294 self.trait_map.insert(expr.id, traits);
5302 fn search_for_traits_containing_method(&mut self, name: Name) -> Vec<DefId> {
5303 debug!("(searching for traits containing method) looking for '{}'",
5304 token::get_name(name));
5306 fn add_trait_info(found_traits: &mut Vec<DefId>,
5307 trait_def_id: DefId,
5309 debug!("(adding trait info) found trait {}:{} for method '{}'",
5312 token::get_name(name));
5313 found_traits.push(trait_def_id);
5316 let mut found_traits = Vec::new();
5317 let mut search_module = self.current_module.clone();
5319 // Look for the current trait.
5320 match self.current_trait_ref {
5321 Some((trait_def_id, _)) => {
5322 let method_map = self.method_map.borrow();
5324 if method_map.contains_key(&(name, trait_def_id)) {
5325 add_trait_info(&mut found_traits, trait_def_id, name);
5328 None => {} // Nothing to do.
5331 // Look for trait children.
5332 self.populate_module_if_necessary(&search_module);
5335 let method_map = self.method_map.borrow();
5336 for (_, child_names) in search_module.children.borrow().iter() {
5337 let def = match child_names.def_for_namespace(TypeNS) {
5341 let trait_def_id = match def {
5342 DefTrait(trait_def_id) => trait_def_id,
5345 if method_map.contains_key(&(name, trait_def_id)) {
5346 add_trait_info(&mut found_traits, trait_def_id, name);
5351 // Look for imports.
5352 for (_, import) in search_module.import_resolutions.borrow().iter() {
5353 let target = match import.target_for_namespace(TypeNS) {
5355 Some(target) => target,
5357 let did = match target.bindings.def_for_namespace(TypeNS) {
5358 Some(DefTrait(trait_def_id)) => trait_def_id,
5359 Some(..) | None => continue,
5361 if self.method_map.borrow().contains_key(&(name, did)) {
5362 add_trait_info(&mut found_traits, did, name);
5363 self.used_imports.insert((import.type_id, TypeNS));
5367 match search_module.parent_link.clone() {
5368 NoParentLink | ModuleParentLink(..) => break,
5369 BlockParentLink(parent_module, _) => {
5370 search_module = parent_module.upgrade().unwrap();
5378 fn record_def(&mut self, node_id: NodeId, (def, lp): (Def, LastPrivate)) {
5379 debug!("(recording def) recording {:?} for {:?}, last private {:?}",
5381 assert!(match lp {LastImport{..} => false, _ => true},
5382 "Import should only be used for `use` directives");
5383 self.last_private.insert(node_id, lp);
5384 self.def_map.borrow_mut().insert_or_update_with(node_id, def, |_, old_value| {
5385 // Resolve appears to "resolve" the same ID multiple
5386 // times, so here is a sanity check it at least comes to
5387 // the same conclusion! - nmatsakis
5388 if def != *old_value {
5390 .bug(format!("node_id {:?} resolved first to {:?} and \
5399 fn enforce_default_binding_mode(&mut self,
5401 pat_binding_mode: BindingMode,
5403 match pat_binding_mode {
5404 BindByValue(_) => {}
5406 self.resolve_error(pat.span,
5407 format!("cannot use `ref` binding mode \
5415 // Unused import checking
5417 // Although this is mostly a lint pass, it lives in here because it depends on
5418 // resolve data structures and because it finalises the privacy information for
5419 // `use` directives.
5422 fn check_for_unused_imports(&mut self, krate: &ast::Crate) {
5423 let mut visitor = UnusedImportCheckVisitor{ resolver: self };
5424 visit::walk_crate(&mut visitor, krate, ());
5427 fn check_for_item_unused_imports(&mut self, vi: &ViewItem) {
5428 // Ignore is_public import statements because there's no way to be sure
5429 // whether they're used or not. Also ignore imports with a dummy span
5430 // because this means that they were generated in some fashion by the
5431 // compiler and we don't need to consider them.
5432 if vi.vis == Public { return }
5433 if vi.span == DUMMY_SP { return }
5436 ViewItemExternCrate(..) => {} // ignore
5437 ViewItemUse(ref p) => {
5439 ViewPathSimple(_, _, id) => self.finalize_import(id, p.span),
5440 ViewPathList(_, ref list, _) => {
5441 for i in list.iter() {
5442 self.finalize_import(i.node.id, i.span);
5445 ViewPathGlob(_, id) => {
5446 if !self.used_imports.contains(&(id, TypeNS)) &&
5447 !self.used_imports.contains(&(id, ValueNS)) {
5449 .add_lint(UnusedImports,
5452 "unused import".to_string());
5460 // We have information about whether `use` (import) directives are actually used now.
5461 // If an import is not used at all, we signal a lint error. If an import is only used
5462 // for a single namespace, we remove the other namespace from the recorded privacy
5463 // information. That means in privacy.rs, we will only check imports and namespaces
5464 // which are used. In particular, this means that if an import could name either a
5465 // public or private item, we will check the correct thing, dependent on how the import
5467 fn finalize_import(&mut self, id: NodeId, span: Span) {
5468 debug!("finalizing import uses for {}",
5469 self.session.codemap().span_to_snippet(span));
5471 if !self.used_imports.contains(&(id, TypeNS)) &&
5472 !self.used_imports.contains(&(id, ValueNS)) {
5473 self.session.add_lint(UnusedImports,
5476 "unused import".to_string());
5479 let (v_priv, t_priv) = match self.last_private.find(&id) {
5487 fail!("we should only have LastImport for `use` directives")
5492 let mut v_used = if self.used_imports.contains(&(id, ValueNS)) {
5497 let t_used = if self.used_imports.contains(&(id, TypeNS)) {
5503 match (v_priv, t_priv) {
5504 // Since some items may be both in the value _and_ type namespaces (e.g., structs)
5505 // we might have two LastPrivates pointing at the same thing. There is no point
5506 // checking both, so lets not check the value one.
5507 (Some(DependsOn(def_v)), Some(DependsOn(def_t))) if def_v == def_t => v_used = Unused,
5511 self.last_private.insert(id, LastImport{value_priv: v_priv,
5514 type_used: t_used});
5520 // Diagnostics are not particularly efficient, because they're rarely
5524 /// A somewhat inefficient routine to obtain the name of a module.
5525 fn module_to_str(&self, module: &Module) -> String {
5526 let mut idents = Vec::new();
5528 fn collect_mod(idents: &mut Vec<ast::Ident>, module: &Module) {
5529 match module.parent_link {
5531 ModuleParentLink(ref module, name) => {
5533 collect_mod(idents, &*module.upgrade().unwrap());
5535 BlockParentLink(ref module, _) => {
5536 idents.push(special_idents::opaque);
5537 collect_mod(idents, &*module.upgrade().unwrap());
5541 collect_mod(&mut idents, module);
5543 if idents.len() == 0 {
5544 return "???".to_string();
5546 self.idents_to_str(idents.move_iter().rev()
5547 .collect::<Vec<ast::Ident>>()
5551 #[allow(dead_code)] // useful for debugging
5552 fn dump_module(&mut self, module_: Rc<Module>) {
5553 debug!("Dump of module `{}`:", self.module_to_str(&*module_));
5555 debug!("Children:");
5556 self.populate_module_if_necessary(&module_);
5557 for (&name, _) in module_.children.borrow().iter() {
5558 debug!("* {}", token::get_name(name));
5561 debug!("Import resolutions:");
5562 let import_resolutions = module_.import_resolutions.borrow();
5563 for (&name, import_resolution) in import_resolutions.iter() {
5565 match import_resolution.target_for_namespace(ValueNS) {
5566 None => { value_repr = "".to_string(); }
5568 value_repr = " value:?".to_string();
5574 match import_resolution.target_for_namespace(TypeNS) {
5575 None => { type_repr = "".to_string(); }
5577 type_repr = " type:?".to_string();
5582 debug!("* {}:{}{}", token::get_name(name), value_repr, type_repr);
5587 pub struct CrateMap {
5588 pub def_map: DefMap,
5589 pub exp_map2: ExportMap2,
5590 pub trait_map: TraitMap,
5591 pub external_exports: ExternalExports,
5592 pub last_private_map: LastPrivateMap,
5595 /// Entry point to crate resolution.
5596 pub fn resolve_crate(session: &Session,
5600 let mut resolver = Resolver::new(session, krate.span);
5601 resolver.resolve(krate);
5602 let Resolver { def_map, export_map2, trait_map, last_private,
5603 external_exports, .. } = resolver;
5606 exp_map2: export_map2,
5607 trait_map: trait_map,
5608 external_exports: external_exports,
5609 last_private_map: last_private,