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 enum ResolveResult<T> {
210 Failed, // Failed to resolve the name.
211 Indeterminate, // Couldn't determine due to unresolved globs.
212 Success(T) // Successfully resolved the import.
215 impl<T> ResolveResult<T> {
216 fn indeterminate(&self) -> bool {
217 match *self { Indeterminate => true, _ => false }
221 enum FallbackSuggestion {
226 StaticMethod(String),
227 StaticTraitMethod(String),
230 enum TypeParameters<'a> {
236 // Identifies the things that these parameters
237 // were declared on (type, fn, etc)
240 // ID of the enclosing item.
243 // The kind of the rib used for type parameters.
247 // The rib kind controls the translation of argument or local definitions
248 // (`def_arg` or `def_local`) to upvars (`def_upvar`).
251 // No translation needs to be applied.
254 // We passed through a function scope at the given node ID. Translate
255 // upvars as appropriate.
256 FunctionRibKind(NodeId /* func id */, NodeId /* body id */),
258 // We passed through an impl or trait and are now in one of its
259 // methods. Allow references to ty params that impl or trait
260 // binds. Disallow any other upvars (including other ty params that are
262 // parent; method itself
263 MethodRibKind(NodeId, MethodSort),
265 // We passed through an item scope. Disallow upvars.
268 // We're in a constant item. Can't refer to dynamic stuff.
272 // Methods can be required or provided. Required methods only occur in traits.
278 enum UseLexicalScopeFlag {
283 enum ModulePrefixResult {
285 PrefixFound(Rc<Module>, uint)
288 #[deriving(PartialEq)]
289 enum NameSearchType {
290 /// We're doing a name search in order to resolve a `use` directive.
293 /// We're doing a name search in order to resolve a path type, a path
294 /// expression, or a path pattern.
298 enum BareIdentifierPatternResolution {
299 FoundStructOrEnumVariant(Def, LastPrivate),
300 FoundConst(Def, LastPrivate),
301 BareIdentifierPatternUnresolved
304 // Specifies how duplicates should be handled when adding a child item if
305 // another item exists with the same name in some namespace.
306 #[deriving(PartialEq)]
307 enum DuplicateCheckingMode {
308 ForbidDuplicateModules,
309 ForbidDuplicateTypes,
310 ForbidDuplicateValues,
311 ForbidDuplicateTypesAndValues,
317 bindings: RefCell<HashMap<Name, DefLike>>,
322 fn new(kind: RibKind) -> Rib {
324 bindings: RefCell::new(HashMap::new()),
330 /// One import directive.
331 struct ImportDirective {
332 module_path: Vec<Ident>,
333 subclass: ImportDirectiveSubclass,
336 is_public: bool, // see note in ImportResolution about how to use this
339 impl ImportDirective {
340 fn new(module_path: Vec<Ident> ,
341 subclass: ImportDirectiveSubclass,
347 module_path: module_path,
351 is_public: is_public,
356 /// The item that an import resolves to.
359 target_module: Rc<Module>,
360 bindings: Rc<NameBindings>,
364 fn new(target_module: Rc<Module>, bindings: Rc<NameBindings>) -> Target {
366 target_module: target_module,
372 /// An ImportResolution represents a particular `use` directive.
373 struct ImportResolution {
374 /// Whether this resolution came from a `use` or a `pub use`. Note that this
375 /// should *not* be used whenever resolution is being performed, this is
376 /// only looked at for glob imports statements currently. Privacy testing
377 /// occurs during a later phase of compilation.
380 // The number of outstanding references to this name. When this reaches
381 // zero, outside modules can count on the targets being correct. Before
382 // then, all bets are off; future imports could override this name.
383 outstanding_references: uint,
385 /// The value that this `use` directive names, if there is one.
386 value_target: Option<Target>,
387 /// The source node of the `use` directive leading to the value target
391 /// The type that this `use` directive names, if there is one.
392 type_target: Option<Target>,
393 /// The source node of the `use` directive leading to the type target
398 impl ImportResolution {
399 fn new(id: NodeId, is_public: bool) -> ImportResolution {
403 outstanding_references: 0,
406 is_public: is_public,
410 fn target_for_namespace(&self, namespace: Namespace)
413 TypeNS => self.type_target.clone(),
414 ValueNS => self.value_target.clone(),
418 fn id(&self, namespace: Namespace) -> NodeId {
420 TypeNS => self.type_id,
421 ValueNS => self.value_id,
426 /// The link from a module up to its nearest parent node.
430 ModuleParentLink(Weak<Module>, Ident),
431 BlockParentLink(Weak<Module>, NodeId)
434 /// The type of module this is.
435 #[deriving(PartialEq)]
444 /// One node in the tree of modules.
446 parent_link: ParentLink,
447 def_id: Cell<Option<DefId>>,
448 kind: Cell<ModuleKind>,
451 children: RefCell<HashMap<Name, Rc<NameBindings>>>,
452 imports: RefCell<Vec<ImportDirective>>,
454 // The external module children of this node that were declared with
456 external_module_children: RefCell<HashMap<Name, Rc<Module>>>,
458 // The anonymous children of this node. Anonymous children are pseudo-
459 // modules that are implicitly created around items contained within
462 // For example, if we have this:
470 // There will be an anonymous module created around `g` with the ID of the
471 // entry block for `f`.
472 anonymous_children: RefCell<NodeMap<Rc<Module>>>,
474 // The status of resolving each import in this module.
475 import_resolutions: RefCell<HashMap<Name, ImportResolution>>,
477 // The number of unresolved globs that this module exports.
478 glob_count: Cell<uint>,
480 // The index of the import we're resolving.
481 resolved_import_count: Cell<uint>,
483 // Whether this module is populated. If not populated, any attempt to
484 // access the children must be preceded with a
485 // `populate_module_if_necessary` call.
486 populated: Cell<bool>,
490 fn new(parent_link: ParentLink,
491 def_id: Option<DefId>,
497 parent_link: parent_link,
498 def_id: Cell::new(def_id),
499 kind: Cell::new(kind),
500 is_public: is_public,
501 children: RefCell::new(HashMap::new()),
502 imports: RefCell::new(Vec::new()),
503 external_module_children: RefCell::new(HashMap::new()),
504 anonymous_children: RefCell::new(NodeMap::new()),
505 import_resolutions: RefCell::new(HashMap::new()),
506 glob_count: Cell::new(0),
507 resolved_import_count: Cell::new(0),
508 populated: Cell::new(!external),
512 fn all_imports_resolved(&self) -> bool {
513 self.imports.borrow().len() == self.resolved_import_count.get()
517 // Records a possibly-private type definition.
520 is_public: bool, // see note in ImportResolution about how to use this
521 module_def: Option<Rc<Module>>,
522 type_def: Option<Def>,
523 type_span: Option<Span>
526 // Records a possibly-private value definition.
529 is_public: bool, // see note in ImportResolution about how to use this
531 value_span: Option<Span>,
534 // Records the definitions (at most one for each namespace) that a name is
536 struct NameBindings {
537 type_def: RefCell<Option<TypeNsDef>>, //< Meaning in type namespace.
538 value_def: RefCell<Option<ValueNsDef>>, //< Meaning in value namespace.
541 /// Ways in which a trait can be referenced
542 enum TraitReferenceType {
543 TraitImplementation, // impl SomeTrait for T { ... }
544 TraitDerivation, // trait T : SomeTrait { ... }
545 TraitBoundingTypeParameter, // fn f<T:SomeTrait>() { ... }
549 fn new() -> NameBindings {
551 type_def: RefCell::new(None),
552 value_def: RefCell::new(None),
556 /// Creates a new module in this set of name bindings.
557 fn define_module(&self,
558 parent_link: ParentLink,
559 def_id: Option<DefId>,
564 // Merges the module with the existing type def or creates a new one.
565 let module_ = Rc::new(Module::new(parent_link, def_id, kind, external,
567 let type_def = self.type_def.borrow().clone();
570 *self.type_def.borrow_mut() = Some(TypeNsDef {
571 is_public: is_public,
572 module_def: Some(module_),
578 *self.type_def.borrow_mut() = Some(TypeNsDef {
579 is_public: is_public,
580 module_def: Some(module_),
582 type_def: type_def.type_def
588 /// Sets the kind of the module, creating a new one if necessary.
589 fn set_module_kind(&self,
590 parent_link: ParentLink,
591 def_id: Option<DefId>,
596 let type_def = self.type_def.borrow().clone();
599 let module = Module::new(parent_link, def_id, kind,
600 external, is_public);
601 *self.type_def.borrow_mut() = Some(TypeNsDef {
602 is_public: is_public,
603 module_def: Some(Rc::new(module)),
609 match type_def.module_def {
611 let module = Module::new(parent_link,
616 *self.type_def.borrow_mut() = Some(TypeNsDef {
617 is_public: is_public,
618 module_def: Some(Rc::new(module)),
619 type_def: type_def.type_def,
623 Some(module_def) => module_def.kind.set(kind),
629 /// Records a type definition.
630 fn define_type(&self, def: Def, sp: Span, is_public: bool) {
631 // Merges the type with the existing type def or creates a new one.
632 let type_def = self.type_def.borrow().clone();
635 *self.type_def.borrow_mut() = Some(TypeNsDef {
639 is_public: is_public,
643 *self.type_def.borrow_mut() = Some(TypeNsDef {
646 module_def: type_def.module_def,
647 is_public: is_public,
653 /// Records a value definition.
654 fn define_value(&self, def: Def, sp: Span, is_public: bool) {
655 *self.value_def.borrow_mut() = Some(ValueNsDef {
657 value_span: Some(sp),
658 is_public: is_public,
662 /// Returns the module node if applicable.
663 fn get_module_if_available(&self) -> Option<Rc<Module>> {
664 match *self.type_def.borrow() {
665 Some(ref type_def) => type_def.module_def.clone(),
671 * Returns the module node. Fails if this node does not have a module
674 fn get_module(&self) -> Rc<Module> {
675 match self.get_module_if_available() {
677 fail!("get_module called on a node with no module \
680 Some(module_def) => module_def
684 fn defined_in_namespace(&self, namespace: Namespace) -> bool {
686 TypeNS => return self.type_def.borrow().is_some(),
687 ValueNS => return self.value_def.borrow().is_some()
691 fn defined_in_public_namespace(&self, namespace: Namespace) -> bool {
693 TypeNS => match *self.type_def.borrow() {
694 Some(ref def) => def.is_public, None => false
696 ValueNS => match *self.value_def.borrow() {
697 Some(ref def) => def.is_public, None => false
702 fn def_for_namespace(&self, namespace: Namespace) -> Option<Def> {
705 match *self.type_def.borrow() {
707 Some(ref type_def) => {
708 match type_def.type_def {
709 Some(type_def) => Some(type_def),
711 match type_def.module_def {
712 Some(ref module) => {
713 match module.def_id.get() {
714 Some(did) => Some(DefMod(did)),
726 match *self.value_def.borrow() {
728 Some(value_def) => Some(value_def.def)
734 fn span_for_namespace(&self, namespace: Namespace) -> Option<Span> {
735 if self.defined_in_namespace(namespace) {
738 match *self.type_def.borrow() {
740 Some(ref type_def) => type_def.type_span
744 match *self.value_def.borrow() {
746 Some(ref value_def) => value_def.value_span
756 /// Interns the names of the primitive types.
757 struct PrimitiveTypeTable {
758 primitive_types: HashMap<Name, PrimTy>,
761 impl PrimitiveTypeTable {
762 fn new() -> PrimitiveTypeTable {
763 let mut table = PrimitiveTypeTable {
764 primitive_types: HashMap::new()
767 table.intern("bool", TyBool);
768 table.intern("char", TyChar);
769 table.intern("f32", TyFloat(TyF32));
770 table.intern("f64", TyFloat(TyF64));
771 table.intern("f128", TyFloat(TyF128));
772 table.intern("int", TyInt(TyI));
773 table.intern("i8", TyInt(TyI8));
774 table.intern("i16", TyInt(TyI16));
775 table.intern("i32", TyInt(TyI32));
776 table.intern("i64", TyInt(TyI64));
777 table.intern("str", TyStr);
778 table.intern("uint", TyUint(TyU));
779 table.intern("u8", TyUint(TyU8));
780 table.intern("u16", TyUint(TyU16));
781 table.intern("u32", TyUint(TyU32));
782 table.intern("u64", TyUint(TyU64));
787 fn intern(&mut self, string: &str, primitive_type: PrimTy) {
788 self.primitive_types.insert(token::intern(string), primitive_type);
793 fn namespace_error_to_str(ns: NamespaceError) -> &'static str {
796 ModuleError => "module",
798 ValueError => "value",
802 /// The main resolver class.
803 struct Resolver<'a> {
804 session: &'a Session,
806 graph_root: NameBindings,
808 method_map: RefCell<FnvHashMap<(Name, DefId), ast::ExplicitSelf_>>,
809 structs: FnvHashMap<DefId, Vec<Name>>,
811 // The number of imports that are currently unresolved.
812 unresolved_imports: uint,
814 // The module that represents the current item scope.
815 current_module: Rc<Module>,
817 // The current set of local scopes, for values.
818 // FIXME #4948: Reuse ribs to avoid allocation.
819 value_ribs: RefCell<Vec<Rib>>,
821 // The current set of local scopes, for types.
822 type_ribs: RefCell<Vec<Rib>>,
824 // The current set of local scopes, for labels.
825 label_ribs: RefCell<Vec<Rib>>,
827 // The trait that the current context can refer to.
828 current_trait_ref: Option<(DefId, TraitRef)>,
830 // The current self type if inside an impl (used for better errors).
831 current_self_type: Option<Ty>,
833 // The ident for the keyword "self".
835 // The ident for the non-keyword "Self".
836 type_self_ident: Ident,
838 // The idents for the primitive types.
839 primitive_type_table: PrimitiveTypeTable,
842 export_map2: ExportMap2,
844 external_exports: ExternalExports,
845 last_private: LastPrivateMap,
847 // Whether or not to print error messages. Can be set to true
848 // when getting additional info for error message suggestions,
849 // so as to avoid printing duplicate errors
852 used_imports: HashSet<(NodeId, Namespace)>,
855 struct BuildReducedGraphVisitor<'a, 'b> {
856 resolver: &'a mut Resolver<'b>,
859 impl<'a, 'b> Visitor<ReducedGraphParent> for BuildReducedGraphVisitor<'a, 'b> {
861 fn visit_item(&mut self, item: &Item, context: ReducedGraphParent) {
862 let p = self.resolver.build_reduced_graph_for_item(item, context);
863 visit::walk_item(self, item, p);
866 fn visit_foreign_item(&mut self, foreign_item: &ForeignItem,
867 context: ReducedGraphParent) {
868 self.resolver.build_reduced_graph_for_foreign_item(foreign_item,
871 let mut v = BuildReducedGraphVisitor{ resolver: r };
872 visit::walk_foreign_item(&mut v, foreign_item, context.clone());
876 fn visit_view_item(&mut self, view_item: &ViewItem, context: ReducedGraphParent) {
877 self.resolver.build_reduced_graph_for_view_item(view_item, context);
880 fn visit_block(&mut self, block: &Block, context: ReducedGraphParent) {
881 let np = self.resolver.build_reduced_graph_for_block(block, context);
882 visit::walk_block(self, block, np);
887 struct UnusedImportCheckVisitor<'a, 'b> { resolver: &'a mut Resolver<'b> }
889 impl<'a, 'b> Visitor<()> for UnusedImportCheckVisitor<'a, 'b> {
890 fn visit_view_item(&mut self, vi: &ViewItem, _: ()) {
891 self.resolver.check_for_item_unused_imports(vi);
892 visit::walk_view_item(self, vi, ());
896 impl<'a> Resolver<'a> {
897 fn new(session: &'a Session, crate_span: Span) -> Resolver<'a> {
898 let graph_root = NameBindings::new();
900 graph_root.define_module(NoParentLink,
901 Some(DefId { krate: 0, node: 0 }),
907 let current_module = graph_root.get_module();
912 // The outermost module has def ID 0; this is not reflected in the
915 graph_root: graph_root,
917 method_map: RefCell::new(FnvHashMap::new()),
918 structs: FnvHashMap::new(),
920 unresolved_imports: 0,
922 current_module: current_module,
923 value_ribs: RefCell::new(Vec::new()),
924 type_ribs: RefCell::new(Vec::new()),
925 label_ribs: RefCell::new(Vec::new()),
927 current_trait_ref: None,
928 current_self_type: None,
930 self_ident: special_idents::self_,
931 type_self_ident: special_idents::type_self,
933 primitive_type_table: PrimitiveTypeTable::new(),
935 def_map: RefCell::new(NodeMap::new()),
936 export_map2: RefCell::new(NodeMap::new()),
937 trait_map: NodeMap::new(),
938 used_imports: HashSet::new(),
939 external_exports: DefIdSet::new(),
940 last_private: NodeMap::new(),
945 /// The main name resolution procedure.
946 fn resolve(&mut self, krate: &ast::Crate) {
947 self.build_reduced_graph(krate);
948 self.session.abort_if_errors();
950 self.resolve_imports();
951 self.session.abort_if_errors();
953 self.record_exports();
954 self.session.abort_if_errors();
956 self.resolve_crate(krate);
957 self.session.abort_if_errors();
959 self.check_for_unused_imports(krate);
963 // Reduced graph building
965 // Here we build the "reduced graph": the graph of the module tree without
966 // any imports resolved.
969 /// Constructs the reduced graph for the entire crate.
970 fn build_reduced_graph(&mut self, krate: &ast::Crate) {
972 ModuleReducedGraphParent(self.graph_root.get_module());
974 let mut visitor = BuildReducedGraphVisitor { resolver: self, };
975 visit::walk_crate(&mut visitor, krate, initial_parent);
979 * Adds a new child item to the module definition of the parent node and
980 * returns its corresponding name bindings as well as the current parent.
981 * Or, if we're inside a block, creates (or reuses) an anonymous module
982 * corresponding to the innermost block ID and returns the name bindings
983 * as well as the newly-created parent.
985 * If this node does not have a module definition and we are not inside
990 reduced_graph_parent: ReducedGraphParent,
991 duplicate_checking_mode: DuplicateCheckingMode,
992 // For printing errors
994 -> Rc<NameBindings> {
995 // If this is the immediate descendant of a module, then we add the
996 // child name directly. Otherwise, we create or reuse an anonymous
997 // module and add the child to that.
999 let module_ = reduced_graph_parent.module();
1001 // Add or reuse the child.
1002 let child = module_.children.borrow().find_copy(&name.name);
1005 let child = Rc::new(NameBindings::new());
1006 module_.children.borrow_mut().insert(name.name, child.clone());
1010 // Enforce the duplicate checking mode:
1012 // * If we're requesting duplicate module checking, check that
1013 // there isn't a module in the module with the same name.
1015 // * If we're requesting duplicate type checking, check that
1016 // there isn't a type in the module with the same name.
1018 // * If we're requesting duplicate value checking, check that
1019 // there isn't a value in the module with the same name.
1021 // * If we're requesting duplicate type checking and duplicate
1022 // value checking, check that there isn't a duplicate type
1023 // and a duplicate value with the same name.
1025 // * If no duplicate checking was requested at all, do
1028 let mut duplicate_type = NoError;
1029 let ns = match duplicate_checking_mode {
1030 ForbidDuplicateModules => {
1031 if child.get_module_if_available().is_some() {
1032 duplicate_type = ModuleError;
1036 ForbidDuplicateTypes => {
1037 match child.def_for_namespace(TypeNS) {
1038 Some(DefMod(_)) | None => {}
1039 Some(_) => duplicate_type = TypeError
1043 ForbidDuplicateValues => {
1044 if child.defined_in_namespace(ValueNS) {
1045 duplicate_type = ValueError;
1049 ForbidDuplicateTypesAndValues => {
1051 match child.def_for_namespace(TypeNS) {
1052 Some(DefMod(_)) | None => {}
1055 duplicate_type = TypeError;
1058 if child.defined_in_namespace(ValueNS) {
1059 duplicate_type = ValueError;
1064 OverwriteDuplicates => None
1066 if duplicate_type != NoError {
1067 // Return an error here by looking up the namespace that
1068 // had the duplicate.
1069 let ns = ns.unwrap();
1070 self.resolve_error(sp,
1071 format!("duplicate definition of {} `{}`",
1072 namespace_error_to_str(duplicate_type),
1073 token::get_ident(name)).as_slice());
1075 let r = child.span_for_namespace(ns);
1076 for sp in r.iter() {
1077 self.session.span_note(*sp,
1078 format!("first definition of {} `{}` here",
1079 namespace_error_to_str(duplicate_type),
1080 token::get_ident(name)).as_slice());
1089 fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
1090 // If the block has view items, we need an anonymous module.
1091 if block.view_items.len() > 0 {
1095 // Check each statement.
1096 for statement in block.stmts.iter() {
1097 match statement.node {
1098 StmtDecl(declaration, _) => {
1099 match declaration.node {
1114 // If we found neither view items nor items, we don't need to create
1115 // an anonymous module.
1120 fn get_parent_link(&mut self, parent: ReducedGraphParent, name: Ident)
1123 ModuleReducedGraphParent(module_) => {
1124 return ModuleParentLink(module_.downgrade(), name);
1129 /// Constructs the reduced graph for one item.
1130 fn build_reduced_graph_for_item(&mut self,
1132 parent: ReducedGraphParent)
1133 -> ReducedGraphParent
1135 let ident = item.ident;
1137 let is_public = item.vis == ast::Public;
1142 self.add_child(ident, parent.clone(), ForbidDuplicateModules, sp);
1144 let parent_link = self.get_parent_link(parent, ident);
1145 let def_id = DefId { krate: 0, node: item.id };
1146 name_bindings.define_module(parent_link,
1150 item.vis == ast::Public,
1153 ModuleReducedGraphParent(name_bindings.get_module())
1156 ItemForeignMod(..) => parent,
1158 // These items live in the value namespace.
1159 ItemStatic(_, m, _) => {
1161 self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1162 let mutbl = m == ast::MutMutable;
1164 name_bindings.define_value
1165 (DefStatic(local_def(item.id), mutbl), sp, is_public);
1168 ItemFn(_, fn_style, _, _, _) => {
1170 self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
1172 let def = DefFn(local_def(item.id), fn_style);
1173 name_bindings.define_value(def, sp, is_public);
1177 // These items live in the type namespace.
1180 self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1182 name_bindings.define_type
1183 (DefTy(local_def(item.id)), sp, is_public);
1187 ItemEnum(ref enum_definition, _) => {
1189 self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1191 name_bindings.define_type
1192 (DefTy(local_def(item.id)), sp, is_public);
1194 for variant in (*enum_definition).variants.iter() {
1195 self.build_reduced_graph_for_variant(
1204 // These items live in both the type and value namespaces.
1205 ItemStruct(struct_def, _) => {
1206 // Adding to both Type and Value namespaces or just Type?
1207 let (forbid, ctor_id) = match struct_def.ctor_id {
1208 Some(ctor_id) => (ForbidDuplicateTypesAndValues, Some(ctor_id)),
1209 None => (ForbidDuplicateTypes, None)
1212 let name_bindings = self.add_child(ident, parent.clone(), forbid, sp);
1214 // Define a name in the type namespace.
1215 name_bindings.define_type(DefTy(local_def(item.id)), sp, is_public);
1217 // If this is a newtype or unit-like struct, define a name
1218 // in the value namespace as well
1219 ctor_id.while_some(|cid| {
1220 name_bindings.define_value(DefStruct(local_def(cid)), sp,
1225 // Record the def ID and fields of this struct.
1226 let named_fields = struct_def.fields.iter().filter_map(|f| {
1228 NamedField(ident, _) => Some(ident.name),
1229 UnnamedField(_) => None
1232 self.structs.insert(local_def(item.id), named_fields);
1237 ItemImpl(_, None, ty, ref methods) => {
1238 // If this implements an anonymous trait, then add all the
1239 // methods within to a new module, if the type was defined
1240 // within this module.
1242 // FIXME (#3785): This is quite unsatisfactory. Perhaps we
1243 // should modify anonymous traits to only be implementable in
1244 // the same module that declared the type.
1246 // Create the module and add all methods.
1248 TyPath(ref path, _, _) if path.segments.len() == 1 => {
1249 let name = path_to_ident(path);
1251 let parent_opt = parent.module().children.borrow()
1252 .find_copy(&name.name);
1253 let new_parent = match parent_opt {
1254 // It already exists
1255 Some(ref child) if child.get_module_if_available()
1257 child.get_module().kind.get() ==
1259 ModuleReducedGraphParent(child.get_module())
1261 // Create the module
1264 self.add_child(name,
1266 ForbidDuplicateModules,
1270 self.get_parent_link(parent.clone(), ident);
1271 let def_id = local_def(item.id);
1274 !name_bindings.defined_in_namespace(ns) ||
1275 name_bindings.defined_in_public_namespace(ns);
1277 name_bindings.define_module(parent_link,
1284 ModuleReducedGraphParent(
1285 name_bindings.get_module())
1289 // For each method...
1290 for method in methods.iter() {
1291 // Add the method to the module.
1292 let ident = method.ident;
1293 let method_name_bindings =
1294 self.add_child(ident,
1296 ForbidDuplicateValues,
1298 let def = match method.explicit_self.node {
1300 // Static methods become
1301 // `def_static_method`s.
1302 DefStaticMethod(local_def(method.id),
1308 // Non-static methods become
1310 DefMethod(local_def(method.id), None)
1314 let is_public = method.vis == ast::Public;
1315 method_name_bindings.define_value(def,
1326 ItemImpl(_, Some(_), _, _) => parent,
1328 ItemTrait(_, _, _, ref methods) => {
1330 self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1332 // Add all the methods within to a new module.
1333 let parent_link = self.get_parent_link(parent.clone(), ident);
1334 name_bindings.define_module(parent_link,
1335 Some(local_def(item.id)),
1338 item.vis == ast::Public,
1340 let module_parent = ModuleReducedGraphParent(name_bindings.
1343 let def_id = local_def(item.id);
1345 // Add the names of all the methods to the trait info.
1346 for method in methods.iter() {
1347 let ty_m = trait_method_to_ty_method(method);
1349 let ident = ty_m.ident;
1351 // Add it as a name in the trait module.
1352 let def = match ty_m.explicit_self.node {
1354 // Static methods become `def_static_method`s.
1355 DefStaticMethod(local_def(ty_m.id),
1356 FromTrait(local_def(item.id)),
1360 // Non-static methods become `def_method`s.
1361 DefMethod(local_def(ty_m.id),
1362 Some(local_def(item.id)))
1366 let method_name_bindings =
1367 self.add_child(ident,
1368 module_parent.clone(),
1369 ForbidDuplicateValues,
1371 method_name_bindings.define_value(def, ty_m.span, true);
1373 self.method_map.borrow_mut().insert((ident.name, def_id),
1374 ty_m.explicit_self.node);
1377 name_bindings.define_type(DefTrait(def_id), sp, is_public);
1380 ItemMac(..) => parent
1384 // Constructs the reduced graph for one variant. Variants exist in the
1385 // type and/or value namespaces.
1386 fn build_reduced_graph_for_variant(&mut self,
1389 parent: ReducedGraphParent,
1391 let ident = variant.node.name;
1393 match variant.node.kind {
1394 TupleVariantKind(_) => {
1395 let child = self.add_child(ident, parent, ForbidDuplicateValues, variant.span);
1396 child.define_value(DefVariant(item_id,
1397 local_def(variant.node.id), false),
1398 variant.span, is_public);
1400 StructVariantKind(_) => {
1401 let child = self.add_child(ident, parent,
1402 ForbidDuplicateTypesAndValues,
1404 child.define_type(DefVariant(item_id,
1405 local_def(variant.node.id), true),
1406 variant.span, is_public);
1408 // Not adding fields for variants as they are not accessed with a self receiver
1409 self.structs.insert(local_def(variant.node.id), Vec::new());
1414 /// Constructs the reduced graph for one 'view item'. View items consist
1415 /// of imports and use directives.
1416 fn build_reduced_graph_for_view_item(&mut self, view_item: &ViewItem,
1417 parent: ReducedGraphParent) {
1418 match view_item.node {
1419 ViewItemUse(ref view_path) => {
1420 // Extract and intern the module part of the path. For
1421 // globs and lists, the path is found directly in the AST;
1422 // for simple paths we have to munge the path a little.
1424 let mut module_path = Vec::new();
1425 match view_path.node {
1426 ViewPathSimple(_, ref full_path, _) => {
1427 let path_len = full_path.segments.len();
1428 assert!(path_len != 0);
1430 for (i, segment) in full_path.segments
1433 if i != path_len - 1 {
1434 module_path.push(segment.identifier)
1439 ViewPathGlob(ref module_ident_path, _) |
1440 ViewPathList(ref module_ident_path, _, _) => {
1441 for segment in module_ident_path.segments.iter() {
1442 module_path.push(segment.identifier)
1447 // Build up the import directives.
1448 let module_ = parent.module();
1449 let is_public = view_item.vis == ast::Public;
1450 match view_path.node {
1451 ViewPathSimple(binding, ref full_path, id) => {
1453 full_path.segments.last().unwrap().identifier;
1454 let subclass = SingleImport(binding,
1456 self.build_import_directive(&*module_,
1463 ViewPathList(_, ref source_idents, _) => {
1464 for source_ident in source_idents.iter() {
1465 let name = source_ident.node.name;
1466 self.build_import_directive(
1468 module_path.clone(),
1469 SingleImport(name, name),
1471 source_ident.node.id,
1475 ViewPathGlob(_, id) => {
1476 self.build_import_directive(&*module_,
1486 ViewItemExternCrate(name, _, node_id) => {
1487 // n.b. we don't need to look at the path option here, because cstore already did
1488 match self.session.cstore.find_extern_mod_stmt_cnum(node_id) {
1490 let def_id = DefId { krate: crate_id, node: 0 };
1491 self.external_exports.insert(def_id);
1492 let parent_link = ModuleParentLink
1493 (parent.module().downgrade(), name);
1494 let external_module = Rc::new(Module::new(parent_link,
1500 parent.module().external_module_children
1501 .borrow_mut().insert(name.name,
1502 external_module.clone());
1504 self.build_reduced_graph_for_external_crate(
1507 None => {} // Ignore.
1513 /// Constructs the reduced graph for one foreign item.
1514 fn build_reduced_graph_for_foreign_item(&mut self,
1515 foreign_item: &ForeignItem,
1516 parent: ReducedGraphParent,
1517 f: |&mut Resolver|) {
1518 let name = foreign_item.ident;
1519 let is_public = foreign_item.vis == ast::Public;
1521 self.add_child(name, parent, ForbidDuplicateValues,
1524 match foreign_item.node {
1525 ForeignItemFn(_, ref generics) => {
1526 let def = DefFn(local_def(foreign_item.id), UnsafeFn);
1527 name_bindings.define_value(def, foreign_item.span, is_public);
1529 self.with_type_parameter_rib(
1530 HasTypeParameters(generics,
1536 ForeignItemStatic(_, m) => {
1537 let def = DefStatic(local_def(foreign_item.id), m);
1538 name_bindings.define_value(def, foreign_item.span, is_public);
1545 fn build_reduced_graph_for_block(&mut self,
1547 parent: ReducedGraphParent)
1548 -> ReducedGraphParent
1550 if self.block_needs_anonymous_module(block) {
1551 let block_id = block.id;
1553 debug!("(building reduced graph for block) creating a new \
1554 anonymous module for block {}",
1557 let parent_module = parent.module();
1558 let new_module = Rc::new(Module::new(
1559 BlockParentLink(parent_module.downgrade(), block_id),
1561 AnonymousModuleKind,
1564 parent_module.anonymous_children.borrow_mut()
1565 .insert(block_id, new_module.clone());
1566 ModuleReducedGraphParent(new_module)
1572 fn handle_external_def(&mut self,
1575 child_name_bindings: &NameBindings,
1578 new_parent: ReducedGraphParent) {
1579 debug!("(building reduced graph for \
1580 external crate) building external def, priv {:?}",
1582 let is_public = vis == ast::Public;
1583 let is_exported = is_public && match new_parent {
1584 ModuleReducedGraphParent(ref module) => {
1585 match module.def_id.get() {
1587 Some(did) => self.external_exports.contains(&did)
1592 self.external_exports.insert(def.def_id());
1595 DefMod(def_id) | DefForeignMod(def_id) | DefStruct(def_id) |
1597 let type_def = child_name_bindings.type_def.borrow().clone();
1599 Some(TypeNsDef { module_def: Some(module_def), .. }) => {
1600 debug!("(building reduced graph for external crate) \
1601 already created module");
1602 module_def.def_id.set(Some(def_id));
1605 debug!("(building reduced graph for \
1606 external crate) building module \
1608 let parent_link = self.get_parent_link(new_parent.clone(), ident);
1610 child_name_bindings.define_module(parent_link,
1623 DefMod(_) | DefForeignMod(_) => {}
1624 DefVariant(enum_did, variant_id, is_struct) => {
1625 debug!("(building reduced graph for external crate) building \
1628 // If this variant is public, then it was publicly reexported,
1629 // otherwise we need to inherit the visibility of the enum
1631 let is_exported = is_public ||
1632 self.external_exports.contains(&enum_did);
1634 child_name_bindings.define_type(def, DUMMY_SP, is_exported);
1635 // Not adding fields for variants as they are not accessed with a self receiver
1636 self.structs.insert(variant_id, Vec::new());
1638 child_name_bindings.define_value(def, DUMMY_SP, is_exported);
1641 DefFn(..) | DefStaticMethod(..) | DefStatic(..) => {
1642 debug!("(building reduced graph for external \
1643 crate) building value (fn/static) {}", final_ident);
1644 child_name_bindings.define_value(def, DUMMY_SP, is_public);
1646 DefTrait(def_id) => {
1647 debug!("(building reduced graph for external \
1648 crate) building type {}", final_ident);
1650 // If this is a trait, add all the method names
1651 // to the trait info.
1653 let method_def_ids =
1654 csearch::get_trait_method_def_ids(&self.session.cstore, def_id);
1655 for &method_def_id in method_def_ids.iter() {
1656 let (method_name, explicit_self) =
1657 csearch::get_method_name_and_explicit_self(&self.session.cstore,
1660 debug!("(building reduced graph for \
1661 external crate) ... adding \
1663 token::get_ident(method_name));
1665 self.method_map.borrow_mut().insert((method_name.name, def_id), explicit_self);
1668 self.external_exports.insert(method_def_id);
1672 child_name_bindings.define_type(def, DUMMY_SP, is_public);
1674 // Define a module if necessary.
1675 let parent_link = self.get_parent_link(new_parent, ident);
1676 child_name_bindings.set_module_kind(parent_link,
1684 debug!("(building reduced graph for external \
1685 crate) building type {}", final_ident);
1687 child_name_bindings.define_type(def, DUMMY_SP, is_public);
1689 DefStruct(def_id) => {
1690 debug!("(building reduced graph for external \
1691 crate) building type and value for {}",
1693 child_name_bindings.define_type(def, DUMMY_SP, is_public);
1694 let fields = csearch::get_struct_fields(&self.session.cstore, def_id).iter().map(|f| {
1696 }).collect::<Vec<_>>();
1698 if fields.len() == 0 {
1699 child_name_bindings.define_value(def, DUMMY_SP, is_public);
1702 // Record the def ID and fields of this struct.
1703 self.structs.insert(def_id, fields);
1706 debug!("(building reduced graph for external crate) \
1707 ignoring {:?}", def);
1708 // Ignored; handled elsewhere.
1710 DefArg(..) | DefLocal(..) | DefPrimTy(..) |
1711 DefTyParam(..) | DefBinding(..) |
1712 DefUse(..) | DefUpvar(..) | DefRegion(..) |
1713 DefTyParamBinder(..) | DefLabel(..) | DefSelfTy(..) => {
1714 fail!("didn't expect `{:?}`", def);
1719 /// Builds the reduced graph for a single item in an external crate.
1720 fn build_reduced_graph_for_external_crate_def(&mut self,
1724 visibility: Visibility) {
1727 // Add the new child item, if necessary.
1729 DefForeignMod(def_id) => {
1730 // Foreign modules have no names. Recur and populate
1732 csearch::each_child_of_item(&self.session.cstore,
1737 self.build_reduced_graph_for_external_crate_def(
1745 let child_name_bindings =
1746 self.add_child(ident,
1747 ModuleReducedGraphParent(root.clone()),
1748 OverwriteDuplicates,
1751 self.handle_external_def(def,
1753 &*child_name_bindings,
1754 token::get_ident(ident).get(),
1756 ModuleReducedGraphParent(root));
1761 // We only process static methods of impls here.
1762 match csearch::get_type_name_if_impl(&self.session.cstore, def) {
1764 Some(final_ident) => {
1765 let static_methods_opt =
1766 csearch::get_static_methods_if_impl(&self.session.cstore, def);
1767 match static_methods_opt {
1768 Some(ref static_methods) if
1769 static_methods.len() >= 1 => {
1770 debug!("(building reduced graph for \
1771 external crate) processing \
1772 static methods for type name {}",
1773 token::get_ident(final_ident));
1775 let child_name_bindings =
1778 ModuleReducedGraphParent(root.clone()),
1779 OverwriteDuplicates,
1782 // Process the static methods. First,
1783 // create the module.
1785 let type_def = child_name_bindings.type_def.borrow().clone();
1788 module_def: Some(module_def),
1791 // We already have a module. This
1793 type_module = module_def;
1795 // Mark it as an impl module if
1797 type_module.kind.set(ImplModuleKind);
1801 self.get_parent_link(ModuleReducedGraphParent(root),
1803 child_name_bindings.define_module(
1811 child_name_bindings.
1816 // Add each static method to the module.
1818 ModuleReducedGraphParent(type_module);
1819 for static_method_info in
1820 static_methods.iter() {
1821 let ident = static_method_info.ident;
1822 debug!("(building reduced graph for \
1823 external crate) creating \
1824 static method '{}'",
1825 token::get_ident(ident));
1827 let method_name_bindings =
1828 self.add_child(ident,
1830 OverwriteDuplicates,
1833 static_method_info.def_id,
1834 static_method_info.fn_style);
1836 method_name_bindings.define_value(
1838 visibility == ast::Public);
1842 // Otherwise, do nothing.
1843 Some(_) | None => {}
1849 debug!("(building reduced graph for external crate) \
1855 /// Builds the reduced graph rooted at the given external module.
1856 fn populate_external_module(&mut self, module: Rc<Module>) {
1857 debug!("(populating external module) attempting to populate {}",
1858 self.module_to_str(&*module));
1860 let def_id = match module.def_id.get() {
1862 debug!("(populating external module) ... no def ID!");
1865 Some(def_id) => def_id,
1868 csearch::each_child_of_item(&self.session.cstore,
1870 |def_like, child_ident, visibility| {
1871 debug!("(populating external module) ... found ident: {}",
1872 token::get_ident(child_ident));
1873 self.build_reduced_graph_for_external_crate_def(module.clone(),
1878 module.populated.set(true)
1881 /// Ensures that the reduced graph rooted at the given external module
1882 /// is built, building it if it is not.
1883 fn populate_module_if_necessary(&mut self, module: &Rc<Module>) {
1884 if !module.populated.get() {
1885 self.populate_external_module(module.clone())
1887 assert!(module.populated.get())
1890 /// Builds the reduced graph rooted at the 'use' directive for an external
1892 fn build_reduced_graph_for_external_crate(&mut self, root: Rc<Module>) {
1893 csearch::each_top_level_item_of_crate(&self.session.cstore,
1898 |def_like, ident, visibility| {
1899 self.build_reduced_graph_for_external_crate_def(root.clone(),
1906 /// Creates and adds an import directive to the given module.
1907 fn build_import_directive(&mut self,
1909 module_path: Vec<Ident> ,
1910 subclass: ImportDirectiveSubclass,
1914 module_.imports.borrow_mut().push(ImportDirective::new(module_path,
1918 self.unresolved_imports += 1;
1919 // Bump the reference count on the name. Or, if this is a glob, set
1920 // the appropriate flag.
1923 SingleImport(target, _) => {
1924 debug!("(building import directive) building import \
1926 self.idents_to_str(module_.imports.borrow().last().unwrap()
1927 .module_path.as_slice()),
1928 token::get_ident(target));
1930 let mut import_resolutions = module_.import_resolutions
1932 match import_resolutions.find_mut(&target.name) {
1933 Some(resolution) => {
1934 debug!("(building import directive) bumping \
1936 resolution.outstanding_references += 1;
1938 // the source of this name is different now
1939 resolution.type_id = id;
1940 resolution.value_id = id;
1941 resolution.is_public = is_public;
1946 debug!("(building import directive) creating new");
1947 let mut resolution = ImportResolution::new(id, is_public);
1948 resolution.outstanding_references = 1;
1949 import_resolutions.insert(target.name, resolution);
1952 // Set the glob flag. This tells us that we don't know the
1953 // module's exports ahead of time.
1955 module_.glob_count.set(module_.glob_count.get() + 1);
1960 // Import resolution
1962 // This is a fixed-point algorithm. We resolve imports until our efforts
1963 // are stymied by an unresolved import; then we bail out of the current
1964 // module and continue. We terminate successfully once no more imports
1965 // remain or unsuccessfully when no forward progress in resolving imports
1968 /// Resolves all imports for the crate. This method performs the fixed-
1969 /// point iteration.
1970 fn resolve_imports(&mut self) {
1972 let mut prev_unresolved_imports = 0;
1974 debug!("(resolving imports) iteration {}, {} imports left",
1975 i, self.unresolved_imports);
1977 let module_root = self.graph_root.get_module();
1978 self.resolve_imports_for_module_subtree(module_root.clone());
1980 if self.unresolved_imports == 0 {
1981 debug!("(resolving imports) success");
1985 if self.unresolved_imports == prev_unresolved_imports {
1986 self.report_unresolved_imports(module_root);
1991 prev_unresolved_imports = self.unresolved_imports;
1995 /// Attempts to resolve imports for the given module and all of its
1997 fn resolve_imports_for_module_subtree(&mut self, module_: Rc<Module>) {
1998 debug!("(resolving imports for module subtree) resolving {}",
1999 self.module_to_str(&*module_));
2000 self.resolve_imports_for_module(module_.clone());
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 // We presumably emitted an error. Continue.
2037 let msg = format!("failed to resolve import `{}`",
2038 self.import_path_to_str(
2039 import_directive.module_path
2041 import_directive.subclass));
2042 self.resolve_error(import_directive.span, msg.as_slice());
2045 // Bail out. We'll come around next time.
2053 module.resolved_import_count
2054 .set(module.resolved_import_count.get() + 1);
2058 fn idents_to_str(&self, idents: &[Ident]) -> String {
2059 let mut first = true;
2060 let mut result = String::new();
2061 for ident in idents.iter() {
2065 result.push_str("::")
2067 result.push_str(token::get_ident(*ident).get());
2072 fn path_idents_to_str(&self, path: &Path) -> String {
2073 let identifiers: Vec<ast::Ident> = path.segments
2075 .map(|seg| seg.identifier)
2077 self.idents_to_str(identifiers.as_slice())
2080 fn import_directive_subclass_to_str(&mut self,
2081 subclass: ImportDirectiveSubclass)
2084 SingleImport(_, source) => {
2085 token::get_ident(source).get().to_string()
2087 GlobImport => "*".to_string()
2091 fn import_path_to_str(&mut self,
2093 subclass: ImportDirectiveSubclass)
2095 if idents.is_empty() {
2096 self.import_directive_subclass_to_str(subclass)
2099 self.idents_to_str(idents),
2100 self.import_directive_subclass_to_str(
2101 subclass))).to_string()
2105 /// Attempts to resolve the given import. The return value indicates
2106 /// failure if we're certain the name does not exist, indeterminate if we
2107 /// don't know whether the name exists at the moment due to other
2108 /// currently-unresolved imports, or success if we know the name exists.
2109 /// If successful, the resolved bindings are written into the module.
2110 fn resolve_import_for_module(&mut self,
2111 module_: Rc<Module>,
2112 import_directive: &ImportDirective)
2113 -> ResolveResult<()> {
2114 let mut resolution_result = Failed;
2115 let module_path = &import_directive.module_path;
2117 debug!("(resolving import for module) resolving import `{}::...` in \
2119 self.idents_to_str(module_path.as_slice()),
2120 self.module_to_str(&*module_));
2122 // First, resolve the module path for the directive, if necessary.
2123 let container = if module_path.len() == 0 {
2124 // Use the crate root.
2125 Some((self.graph_root.get_module(), LastMod(AllPublic)))
2127 match self.resolve_module_path(module_.clone(),
2128 module_path.as_slice(),
2129 DontUseLexicalScope,
2130 import_directive.span,
2135 resolution_result = Indeterminate;
2138 Success(container) => Some(container),
2144 Some((containing_module, lp)) => {
2145 // We found the module that the target is contained
2146 // within. Attempt to resolve the import within it.
2148 match import_directive.subclass {
2149 SingleImport(target, source) => {
2151 self.resolve_single_import(&*module_,
2160 self.resolve_glob_import(&*module_,
2162 import_directive.id,
2163 import_directive.is_public,
2170 // Decrement the count of unresolved imports.
2171 match resolution_result {
2173 assert!(self.unresolved_imports >= 1);
2174 self.unresolved_imports -= 1;
2177 // Nothing to do here; just return the error.
2181 // Decrement the count of unresolved globs if necessary. But only if
2182 // the resolution result is indeterminate -- otherwise we'll stop
2183 // processing imports here. (See the loop in
2184 // resolve_imports_for_module.)
2186 if !resolution_result.indeterminate() {
2187 match import_directive.subclass {
2189 assert!(module_.glob_count.get() >= 1);
2190 module_.glob_count.set(module_.glob_count.get() - 1);
2192 SingleImport(..) => {
2198 return resolution_result;
2201 fn create_name_bindings_from_module(module: Rc<Module>) -> NameBindings {
2203 type_def: RefCell::new(Some(TypeNsDef {
2205 module_def: Some(module),
2209 value_def: RefCell::new(None),
2213 fn resolve_single_import(&mut self,
2215 containing_module: Rc<Module>,
2218 directive: &ImportDirective,
2220 -> ResolveResult<()> {
2221 debug!("(resolving single import) resolving `{}` = `{}::{}` from \
2222 `{}` id {}, last private {:?}",
2223 token::get_ident(target),
2224 self.module_to_str(&*containing_module),
2225 token::get_ident(source),
2226 self.module_to_str(module_),
2232 LastImport {..} => {
2234 .span_bug(directive.span,
2235 "not expecting Import here, must be LastMod")
2239 // We need to resolve both namespaces for this to succeed.
2242 let mut value_result = UnknownResult;
2243 let mut type_result = UnknownResult;
2245 // Search for direct children of the containing module.
2246 self.populate_module_if_necessary(&containing_module);
2248 match containing_module.children.borrow().find(&source.name) {
2252 Some(ref child_name_bindings) => {
2253 if child_name_bindings.defined_in_namespace(ValueNS) {
2254 debug!("(resolving single import) found value binding");
2255 value_result = BoundResult(containing_module.clone(),
2256 (*child_name_bindings).clone());
2258 if child_name_bindings.defined_in_namespace(TypeNS) {
2259 debug!("(resolving single import) found type binding");
2260 type_result = BoundResult(containing_module.clone(),
2261 (*child_name_bindings).clone());
2266 // Unless we managed to find a result in both namespaces (unlikely),
2267 // search imports as well.
2268 let mut value_used_reexport = false;
2269 let mut type_used_reexport = false;
2270 match (value_result.clone(), type_result.clone()) {
2271 (BoundResult(..), BoundResult(..)) => {} // Continue.
2273 // If there is an unresolved glob at this point in the
2274 // containing module, bail out. We don't know enough to be
2275 // able to resolve this import.
2277 if containing_module.glob_count.get() > 0 {
2278 debug!("(resolving single import) unresolved glob; \
2280 return Indeterminate;
2283 // Now search the exported imports within the containing module.
2284 match containing_module.import_resolutions.borrow().find(&source.name) {
2286 debug!("(resolving single import) no import");
2287 // The containing module definitely doesn't have an
2288 // exported import with the name in question. We can
2289 // therefore accurately report that the names are
2292 if value_result.is_unknown() {
2293 value_result = UnboundResult;
2295 if type_result.is_unknown() {
2296 type_result = UnboundResult;
2299 Some(import_resolution)
2300 if import_resolution.outstanding_references == 0 => {
2302 fn get_binding(this: &mut Resolver,
2303 import_resolution: &ImportResolution,
2304 namespace: Namespace)
2305 -> NamespaceResult {
2307 // Import resolutions must be declared with "pub"
2308 // in order to be exported.
2309 if !import_resolution.is_public {
2310 return UnboundResult;
2313 match import_resolution.
2314 target_for_namespace(namespace) {
2316 return UnboundResult;
2318 Some(Target {target_module, bindings}) => {
2319 debug!("(resolving single import) found \
2320 import in ns {:?}", namespace);
2321 let id = import_resolution.id(namespace);
2322 this.used_imports.insert((id, namespace));
2323 return BoundResult(target_module, bindings);
2328 // The name is an import which has been fully
2329 // resolved. We can, therefore, just follow it.
2330 if value_result.is_unknown() {
2331 value_result = get_binding(self, import_resolution,
2333 value_used_reexport = import_resolution.is_public;
2335 if type_result.is_unknown() {
2336 type_result = get_binding(self, import_resolution,
2338 type_used_reexport = import_resolution.is_public;
2343 // The import is unresolved. Bail out.
2344 debug!("(resolving single import) unresolved import; \
2346 return Indeterminate;
2352 // If we didn't find a result in the type namespace, search the
2353 // external modules.
2354 let mut value_used_public = false;
2355 let mut type_used_public = false;
2357 BoundResult(..) => {}
2359 match containing_module.external_module_children.borrow_mut()
2360 .find_copy(&source.name) {
2361 None => {} // Continue.
2363 debug!("(resolving single import) found external \
2366 Rc::new(Resolver::create_name_bindings_from_module(
2368 type_result = BoundResult(containing_module.clone(),
2370 type_used_public = true;
2376 // We've successfully resolved the import. Write the results in.
2377 let mut import_resolutions = module_.import_resolutions.borrow_mut();
2378 let import_resolution = import_resolutions.get_mut(&target.name);
2380 match value_result {
2381 BoundResult(ref target_module, ref name_bindings) => {
2382 debug!("(resolving single import) found value target");
2383 import_resolution.value_target = Some(Target::new(target_module.clone(),
2384 name_bindings.clone()));
2385 import_resolution.value_id = directive.id;
2386 import_resolution.is_public = directive.is_public;
2387 value_used_public = name_bindings.defined_in_public_namespace(ValueNS);
2389 UnboundResult => { /* Continue. */ }
2391 fail!("value result should be known at this point");
2395 BoundResult(ref target_module, ref name_bindings) => {
2396 debug!("(resolving single import) found type target: {:?}",
2397 { name_bindings.type_def.borrow().clone().unwrap().type_def });
2398 import_resolution.type_target =
2399 Some(Target::new(target_module.clone(), name_bindings.clone()));
2400 import_resolution.type_id = directive.id;
2401 import_resolution.is_public = directive.is_public;
2402 type_used_public = name_bindings.defined_in_public_namespace(TypeNS);
2404 UnboundResult => { /* Continue. */ }
2406 fail!("type result should be known at this point");
2410 if value_result.is_unbound() && type_result.is_unbound() {
2411 let msg = format!("unresolved import: there is no \
2413 token::get_ident(source),
2414 self.module_to_str(&*containing_module));
2415 self.resolve_error(directive.span, msg.as_slice());
2418 let value_used_public = value_used_reexport || value_used_public;
2419 let type_used_public = type_used_reexport || type_used_public;
2421 assert!(import_resolution.outstanding_references >= 1);
2422 import_resolution.outstanding_references -= 1;
2424 // record what this import resolves to for later uses in documentation,
2425 // this may resolve to either a value or a type, but for documentation
2426 // purposes it's good enough to just favor one over the other.
2427 let value_private = match import_resolution.value_target {
2428 Some(ref target) => {
2429 let def = target.bindings.def_for_namespace(ValueNS).unwrap();
2430 self.def_map.borrow_mut().insert(directive.id, def);
2431 let did = def.def_id();
2432 if value_used_public {Some(lp)} else {Some(DependsOn(did))}
2434 // AllPublic here and below is a dummy value, it should never be used because
2435 // _exists is false.
2438 let type_private = match import_resolution.type_target {
2439 Some(ref target) => {
2440 let def = target.bindings.def_for_namespace(TypeNS).unwrap();
2441 self.def_map.borrow_mut().insert(directive.id, def);
2442 let did = def.def_id();
2443 if type_used_public {Some(lp)} else {Some(DependsOn(did))}
2448 self.last_private.insert(directive.id, LastImport{value_priv: value_private,
2450 type_priv: type_private,
2453 debug!("(resolving single import) successfully resolved import");
2457 // Resolves a glob import. Note that this function cannot fail; it either
2458 // succeeds or bails out (as importing * from an empty module or a module
2459 // that exports nothing is valid).
2460 fn resolve_glob_import(&mut self,
2462 containing_module: Rc<Module>,
2466 -> ResolveResult<()> {
2467 // This function works in a highly imperative manner; it eagerly adds
2468 // everything it can to the list of import resolutions of the module
2470 debug!("(resolving glob import) resolving glob import {}", id);
2472 // We must bail out if the node has unresolved imports of any kind
2473 // (including globs).
2474 if !(*containing_module).all_imports_resolved() {
2475 debug!("(resolving glob import) target module has unresolved \
2476 imports; bailing out");
2477 return Indeterminate;
2480 assert_eq!(containing_module.glob_count.get(), 0);
2482 // Add all resolved imports from the containing module.
2483 let import_resolutions = containing_module.import_resolutions
2485 for (ident, target_import_resolution) in import_resolutions.iter() {
2486 debug!("(resolving glob import) writing module resolution \
2488 target_import_resolution.type_target.is_none(),
2489 self.module_to_str(module_));
2491 if !target_import_resolution.is_public {
2492 debug!("(resolving glob import) nevermind, just kidding");
2496 // Here we merge two import resolutions.
2497 let mut import_resolutions = module_.import_resolutions.borrow_mut();
2498 match import_resolutions.find_mut(ident) {
2499 Some(dest_import_resolution) => {
2500 // Merge the two import resolutions at a finer-grained
2503 match target_import_resolution.value_target {
2507 Some(ref value_target) => {
2508 dest_import_resolution.value_target =
2509 Some(value_target.clone());
2512 match target_import_resolution.type_target {
2516 Some(ref type_target) => {
2517 dest_import_resolution.type_target =
2518 Some(type_target.clone());
2521 dest_import_resolution.is_public = is_public;
2527 // Simple: just copy the old import resolution.
2528 let mut new_import_resolution = ImportResolution::new(id, is_public);
2529 new_import_resolution.value_target =
2530 target_import_resolution.value_target.clone();
2531 new_import_resolution.type_target =
2532 target_import_resolution.type_target.clone();
2534 import_resolutions.insert(*ident, new_import_resolution);
2537 // Add all children from the containing module.
2538 self.populate_module_if_necessary(&containing_module);
2540 for (&name, name_bindings) in containing_module.children
2542 self.merge_import_resolution(module_, containing_module.clone(),
2544 name, name_bindings.clone());
2547 // Add external module children from the containing module.
2548 for (&name, module) in containing_module.external_module_children
2551 Rc::new(Resolver::create_name_bindings_from_module(module.clone()));
2552 self.merge_import_resolution(module_, containing_module.clone(),
2554 name, name_bindings);
2557 // Record the destination of this import
2558 match containing_module.def_id.get() {
2560 self.def_map.borrow_mut().insert(id, DefMod(did));
2561 self.last_private.insert(id, lp);
2566 debug!("(resolving glob import) successfully resolved import");
2570 fn merge_import_resolution(&mut self,
2572 containing_module: Rc<Module>,
2576 name_bindings: Rc<NameBindings>) {
2577 let mut import_resolutions = module_.import_resolutions.borrow_mut();
2578 let dest_import_resolution = import_resolutions.find_or_insert_with(name, |_| {
2579 // Create a new import resolution from this child.
2580 ImportResolution::new(id, is_public)
2583 debug!("(resolving glob import) writing resolution `{}` in `{}` \
2585 token::get_name(name).get().to_str(),
2586 self.module_to_str(&*containing_module),
2587 self.module_to_str(module_));
2589 // Merge the child item into the import resolution.
2590 if name_bindings.defined_in_public_namespace(ValueNS) {
2591 debug!("(resolving glob import) ... for value target");
2592 dest_import_resolution.value_target =
2593 Some(Target::new(containing_module.clone(), name_bindings.clone()));
2594 dest_import_resolution.value_id = id;
2596 if name_bindings.defined_in_public_namespace(TypeNS) {
2597 debug!("(resolving glob import) ... for type target");
2598 dest_import_resolution.type_target =
2599 Some(Target::new(containing_module, name_bindings.clone()));
2600 dest_import_resolution.type_id = id;
2602 dest_import_resolution.is_public = is_public;
2605 /// Resolves the given module path from the given root `module_`.
2606 fn resolve_module_path_from_root(&mut self,
2607 module_: Rc<Module>,
2608 module_path: &[Ident],
2611 name_search_type: NameSearchType,
2613 -> ResolveResult<(Rc<Module>, LastPrivate)> {
2614 let mut search_module = module_;
2615 let mut index = index;
2616 let module_path_len = module_path.len();
2617 let mut closest_private = lp;
2619 // Resolve the module part of the path. This does not involve looking
2620 // upward though scope chains; we simply resolve names directly in
2621 // modules as we go.
2622 while index < module_path_len {
2623 let name = module_path[index];
2624 match self.resolve_name_in_module(search_module.clone(),
2630 let segment_name = token::get_ident(name);
2631 let module_name = self.module_to_str(&*search_module);
2632 if "???" == module_name.as_slice() {
2635 hi: span.lo + Pos::from_uint(segment_name.get().len()),
2636 expn_info: span.expn_info,
2638 self.resolve_error(span,
2639 format!("unresolved import. maybe \
2640 a missing `extern crate \
2642 segment_name).as_slice());
2645 self.resolve_error(span,
2646 format!("unresolved import: could not \
2647 find `{}` in `{}`.",
2649 module_name).as_slice());
2653 debug!("(resolving module path for import) module \
2654 resolution is indeterminate: {}",
2655 token::get_ident(name));
2656 return Indeterminate;
2658 Success((target, used_proxy)) => {
2659 // Check to see whether there are type bindings, and, if
2660 // so, whether there is a module within.
2661 match *target.bindings.type_def.borrow() {
2662 Some(ref type_def) => {
2663 match type_def.module_def {
2668 format!("not a module `{}`",
2669 token::get_ident(name))
2673 Some(ref module_def) => {
2674 // If we're doing the search for an
2675 // import, do not allow traits and impls
2677 match (name_search_type,
2678 module_def.kind.get()) {
2679 (ImportSearch, TraitModuleKind) |
2680 (ImportSearch, ImplModuleKind) => {
2683 "cannot import from a trait \
2684 or type implementation");
2688 search_module = module_def.clone();
2690 // Keep track of the closest
2691 // private module used when
2692 // resolving this import chain.
2694 !search_module.is_public {
2695 match search_module.def_id
2699 LastMod(DependsOn(did));
2710 // There are no type bindings at all.
2713 format!("not a module `{}`",
2714 token::get_ident(name)).as_slice());
2724 return Success((search_module, closest_private));
2727 /// Attempts to resolve the module part of an import directive or path
2728 /// rooted at the given module.
2730 /// On success, returns the resolved module, and the closest *private*
2731 /// module found to the destination when resolving this path.
2732 fn resolve_module_path(&mut self,
2733 module_: Rc<Module>,
2734 module_path: &[Ident],
2735 use_lexical_scope: UseLexicalScopeFlag,
2737 name_search_type: NameSearchType)
2738 -> ResolveResult<(Rc<Module>, LastPrivate)> {
2739 let module_path_len = module_path.len();
2740 assert!(module_path_len > 0);
2742 debug!("(resolving module path for import) processing `{}` rooted at \
2744 self.idents_to_str(module_path),
2745 self.module_to_str(&*module_));
2747 // Resolve the module prefix, if any.
2748 let module_prefix_result = self.resolve_module_prefix(module_.clone(),
2754 match module_prefix_result {
2756 let mpath = self.idents_to_str(module_path);
2757 match mpath.as_slice().rfind(':') {
2761 format!("unresolved import: could not find `{}` \
2763 // idx +- 1 to account for the colons on \
2765 mpath.as_slice().slice_from(idx + 1),
2767 .slice_to(idx - 1)).as_slice());
2774 debug!("(resolving module path for import) indeterminate; \
2776 return Indeterminate;
2778 Success(NoPrefixFound) => {
2779 // There was no prefix, so we're considering the first element
2780 // of the path. How we handle this depends on whether we were
2781 // instructed to use lexical scope or not.
2782 match use_lexical_scope {
2783 DontUseLexicalScope => {
2784 // This is a crate-relative path. We will start the
2785 // resolution process at index zero.
2786 search_module = self.graph_root.get_module();
2788 last_private = LastMod(AllPublic);
2790 UseLexicalScope => {
2791 // This is not a crate-relative path. We resolve the
2792 // first component of the path in the current lexical
2793 // scope and then proceed to resolve below that.
2794 let result = self.resolve_module_in_lexical_scope(
2799 self.resolve_error(span, "unresolved name");
2803 debug!("(resolving module path for import) \
2804 indeterminate; bailing");
2805 return Indeterminate;
2807 Success(containing_module) => {
2808 search_module = containing_module;
2810 last_private = LastMod(AllPublic);
2816 Success(PrefixFound(ref containing_module, index)) => {
2817 search_module = containing_module.clone();
2818 start_index = index;
2819 last_private = LastMod(DependsOn(containing_module.def_id
2825 self.resolve_module_path_from_root(search_module,
2833 /// Invariant: This must only be called during main resolution, not during
2834 /// import resolution.
2835 fn resolve_item_in_lexical_scope(&mut self,
2836 module_: Rc<Module>,
2838 namespace: Namespace)
2839 -> ResolveResult<(Target, bool)> {
2840 debug!("(resolving item in lexical scope) resolving `{}` in \
2841 namespace {:?} in `{}`",
2842 token::get_ident(name),
2844 self.module_to_str(&*module_));
2846 // The current module node is handled specially. First, check for
2847 // its immediate children.
2848 self.populate_module_if_necessary(&module_);
2850 match module_.children.borrow().find(&name.name) {
2852 if name_bindings.defined_in_namespace(namespace) => {
2853 debug!("top name bindings succeeded");
2854 return Success((Target::new(module_.clone(), name_bindings.clone()),
2857 Some(_) | None => { /* Not found; continue. */ }
2860 // Now check for its import directives. We don't have to have resolved
2861 // all its imports in the usual way; this is because chains of
2862 // adjacent import statements are processed as though they mutated the
2864 match module_.import_resolutions.borrow().find(&name.name) {
2866 // Not found; continue.
2868 Some(import_resolution) => {
2869 match (*import_resolution).target_for_namespace(namespace) {
2871 // Not found; continue.
2872 debug!("(resolving item in lexical scope) found \
2873 import resolution, but not in namespace {:?}",
2877 debug!("(resolving item in lexical scope) using \
2878 import resolution");
2879 self.used_imports.insert((import_resolution.id(namespace), namespace));
2880 return Success((target, false));
2886 // Search for external modules.
2887 if namespace == TypeNS {
2888 match module_.external_module_children.borrow().find_copy(&name.name) {
2892 Rc::new(Resolver::create_name_bindings_from_module(module));
2893 debug!("lower name bindings succeeded");
2894 return Success((Target::new(module_, name_bindings), false));
2899 // Finally, proceed up the scope chain looking for parent modules.
2900 let mut search_module = module_;
2902 // Go to the next parent.
2903 match search_module.parent_link.clone() {
2905 // No more parents. This module was unresolved.
2906 debug!("(resolving item in lexical scope) unresolved \
2910 ModuleParentLink(parent_module_node, _) => {
2911 match search_module.kind.get() {
2912 NormalModuleKind => {
2913 // We stop the search here.
2914 debug!("(resolving item in lexical \
2915 scope) unresolved module: not \
2916 searching through module \
2923 AnonymousModuleKind => {
2924 search_module = parent_module_node.upgrade().unwrap();
2928 BlockParentLink(ref parent_module_node, _) => {
2929 search_module = parent_module_node.upgrade().unwrap();
2933 // Resolve the name in the parent module.
2934 match self.resolve_name_in_module(search_module.clone(),
2940 // Continue up the search chain.
2943 // We couldn't see through the higher scope because of an
2944 // unresolved import higher up. Bail.
2946 debug!("(resolving item in lexical scope) indeterminate \
2947 higher scope; bailing");
2948 return Indeterminate;
2950 Success((target, used_reexport)) => {
2951 // We found the module.
2952 debug!("(resolving item in lexical scope) found name \
2954 return Success((target, used_reexport));
2960 /// Resolves a module name in the current lexical scope.
2961 fn resolve_module_in_lexical_scope(&mut self,
2962 module_: Rc<Module>,
2964 -> ResolveResult<Rc<Module>> {
2965 // If this module is an anonymous module, resolve the item in the
2966 // lexical scope. Otherwise, resolve the item from the crate root.
2967 let resolve_result = self.resolve_item_in_lexical_scope(
2968 module_, name, TypeNS);
2969 match resolve_result {
2970 Success((target, _)) => {
2971 let bindings = &*target.bindings;
2972 match *bindings.type_def.borrow() {
2973 Some(ref type_def) => {
2974 match type_def.module_def {
2976 debug!("!!! (resolving module in lexical \
2977 scope) module wasn't actually a \
2981 Some(ref module_def) => {
2982 return Success(module_def.clone());
2987 debug!("!!! (resolving module in lexical scope) module
2988 wasn't actually a module!");
2994 debug!("(resolving module in lexical scope) indeterminate; \
2996 return Indeterminate;
2999 debug!("(resolving module in lexical scope) failed to \
3006 /// Returns the nearest normal module parent of the given module.
3007 fn get_nearest_normal_module_parent(&mut self, module_: Rc<Module>)
3008 -> Option<Rc<Module>> {
3009 let mut module_ = module_;
3011 match module_.parent_link.clone() {
3012 NoParentLink => return None,
3013 ModuleParentLink(new_module, _) |
3014 BlockParentLink(new_module, _) => {
3015 let new_module = new_module.upgrade().unwrap();
3016 match new_module.kind.get() {
3017 NormalModuleKind => return Some(new_module),
3021 AnonymousModuleKind => module_ = new_module,
3028 /// Returns the nearest normal module parent of the given module, or the
3029 /// module itself if it is a normal module.
3030 fn get_nearest_normal_module_parent_or_self(&mut self, module_: Rc<Module>)
3032 match module_.kind.get() {
3033 NormalModuleKind => return module_,
3037 AnonymousModuleKind => {
3038 match self.get_nearest_normal_module_parent(module_.clone()) {
3040 Some(new_module) => new_module
3046 /// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
3047 /// (b) some chain of `super::`.
3048 /// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
3049 fn resolve_module_prefix(&mut self,
3050 module_: Rc<Module>,
3051 module_path: &[Ident])
3052 -> ResolveResult<ModulePrefixResult> {
3053 // Start at the current module if we see `self` or `super`, or at the
3054 // top of the crate otherwise.
3055 let mut containing_module;
3057 let first_module_path_string = token::get_ident(module_path[0]);
3058 if "self" == first_module_path_string.get() {
3060 self.get_nearest_normal_module_parent_or_self(module_);
3062 } else if "super" == first_module_path_string.get() {
3064 self.get_nearest_normal_module_parent_or_self(module_);
3065 i = 0; // We'll handle `super` below.
3067 return Success(NoPrefixFound);
3070 // Now loop through all the `super`s we find.
3071 while i < module_path.len() {
3072 let string = token::get_ident(module_path[i]);
3073 if "super" != string.get() {
3076 debug!("(resolving module prefix) resolving `super` at {}",
3077 self.module_to_str(&*containing_module));
3078 match self.get_nearest_normal_module_parent(containing_module) {
3079 None => return Failed,
3080 Some(new_module) => {
3081 containing_module = new_module;
3087 debug!("(resolving module prefix) finished resolving prefix at {}",
3088 self.module_to_str(&*containing_module));
3090 return Success(PrefixFound(containing_module, i));
3093 /// Attempts to resolve the supplied name in the given module for the
3094 /// given namespace. If successful, returns the target corresponding to
3097 /// The boolean returned on success is an indicator of whether this lookup
3098 /// passed through a public re-export proxy.
3099 fn resolve_name_in_module(&mut self,
3100 module_: Rc<Module>,
3102 namespace: Namespace,
3103 name_search_type: NameSearchType,
3104 allow_private_imports: bool)
3105 -> ResolveResult<(Target, bool)> {
3106 debug!("(resolving name in module) resolving `{}` in `{}`",
3107 token::get_name(name).get(),
3108 self.module_to_str(&*module_));
3110 // First, check the direct children of the module.
3111 self.populate_module_if_necessary(&module_);
3113 match module_.children.borrow().find(&name) {
3115 if name_bindings.defined_in_namespace(namespace) => {
3116 debug!("(resolving name in module) found node as child");
3117 return Success((Target::new(module_.clone(), name_bindings.clone()),
3125 // Next, check the module's imports if necessary.
3127 // If this is a search of all imports, we should be done with glob
3128 // resolution at this point.
3129 if name_search_type == PathSearch {
3130 assert_eq!(module_.glob_count.get(), 0);
3133 // Check the list of resolved imports.
3134 match module_.import_resolutions.borrow().find(&name) {
3135 Some(import_resolution) if allow_private_imports ||
3136 import_resolution.is_public => {
3138 if import_resolution.is_public &&
3139 import_resolution.outstanding_references != 0 {
3140 debug!("(resolving name in module) import \
3141 unresolved; bailing out");
3142 return Indeterminate;
3144 match import_resolution.target_for_namespace(namespace) {
3146 debug!("(resolving name in module) name found, \
3147 but not in namespace {:?}",
3151 debug!("(resolving name in module) resolved to \
3153 self.used_imports.insert((import_resolution.id(namespace), namespace));
3154 return Success((target, true));
3158 Some(..) | None => {} // Continue.
3161 // Finally, search through external children.
3162 if namespace == TypeNS {
3163 match module_.external_module_children.borrow().find_copy(&name) {
3167 Rc::new(Resolver::create_name_bindings_from_module(module));
3168 return Success((Target::new(module_, name_bindings), false));
3173 // We're out of luck.
3174 debug!("(resolving name in module) failed to resolve `{}`",
3175 token::get_name(name).get());
3179 fn report_unresolved_imports(&mut self, module_: Rc<Module>) {
3180 let index = module_.resolved_import_count.get();
3181 let imports = module_.imports.borrow();
3182 let import_count = imports.len();
3183 if index != import_count {
3184 let sn = self.session
3186 .span_to_snippet(imports.get(index).span)
3188 if sn.as_slice().contains("::") {
3189 self.resolve_error(imports.get(index).span,
3190 "unresolved import");
3192 let err = format!("unresolved import (maybe you meant `{}::*`?)",
3193 sn.as_slice().slice(0, sn.len()));
3194 self.resolve_error(imports.get(index).span, err.as_slice());
3198 // Descend into children and anonymous children.
3199 self.populate_module_if_necessary(&module_);
3201 for (_, child_node) in module_.children.borrow().iter() {
3202 match child_node.get_module_if_available() {
3206 Some(child_module) => {
3207 self.report_unresolved_imports(child_module);
3212 for (_, module_) in module_.anonymous_children.borrow().iter() {
3213 self.report_unresolved_imports(module_.clone());
3219 // This pass simply determines what all "export" keywords refer to and
3220 // writes the results into the export map.
3222 // FIXME #4953 This pass will be removed once exports change to per-item.
3223 // Then this operation can simply be performed as part of item (or import)
3226 fn record_exports(&mut self) {
3227 let root_module = self.graph_root.get_module();
3228 self.record_exports_for_module_subtree(root_module);
3231 fn record_exports_for_module_subtree(&mut self,
3232 module_: Rc<Module>) {
3233 // If this isn't a local krate, then bail out. We don't need to record
3234 // exports for nonlocal crates.
3236 match module_.def_id.get() {
3237 Some(def_id) if def_id.krate == LOCAL_CRATE => {
3239 debug!("(recording exports for module subtree) recording \
3240 exports for local module `{}`",
3241 self.module_to_str(&*module_));
3244 // Record exports for the root module.
3245 debug!("(recording exports for module subtree) recording \
3246 exports for root module `{}`",
3247 self.module_to_str(&*module_));
3251 debug!("(recording exports for module subtree) not recording \
3253 self.module_to_str(&*module_));
3258 self.record_exports_for_module(&*module_);
3259 self.populate_module_if_necessary(&module_);
3261 for (_, child_name_bindings) in module_.children.borrow().iter() {
3262 match child_name_bindings.get_module_if_available() {
3266 Some(child_module) => {
3267 self.record_exports_for_module_subtree(child_module);
3272 for (_, child_module) in module_.anonymous_children.borrow().iter() {
3273 self.record_exports_for_module_subtree(child_module.clone());
3277 fn record_exports_for_module(&mut self, module_: &Module) {
3278 let mut exports2 = Vec::new();
3280 self.add_exports_for_module(&mut exports2, module_);
3281 match module_.def_id.get() {
3283 self.export_map2.borrow_mut().insert(def_id.node, exports2);
3284 debug!("(computing exports) writing exports for {} (some)",
3291 fn add_exports_of_namebindings(&mut self,
3292 exports2: &mut Vec<Export2> ,
3294 namebindings: &NameBindings,
3296 match namebindings.def_for_namespace(ns) {
3298 let name = token::get_name(name);
3299 debug!("(computing exports) YES: export '{}' => {:?}",
3301 exports2.push(Export2 {
3302 name: name.get().to_string(),
3307 debug!("(computing exports) NO: {:?}", d_opt);
3312 fn add_exports_for_module(&mut self,
3313 exports2: &mut Vec<Export2> ,
3315 for (name, importresolution) in module_.import_resolutions.borrow().iter() {
3316 if !importresolution.is_public {
3319 let xs = [TypeNS, ValueNS];
3320 for &ns in xs.iter() {
3321 match importresolution.target_for_namespace(ns) {
3323 debug!("(computing exports) maybe export '{}'",
3324 token::get_name(*name));
3325 self.add_exports_of_namebindings(exports2,
3338 // We maintain a list of value ribs and type ribs.
3340 // Simultaneously, we keep track of the current position in the module
3341 // graph in the `current_module` pointer. When we go to resolve a name in
3342 // the value or type namespaces, we first look through all the ribs and
3343 // then query the module graph. When we resolve a name in the module
3344 // namespace, we can skip all the ribs (since nested modules are not
3345 // allowed within blocks in Rust) and jump straight to the current module
3348 // Named implementations are handled separately. When we find a method
3349 // call, we consult the module node to find all of the implementations in
3350 // scope. This information is lazily cached in the module node. We then
3351 // generate a fake "implementation scope" containing all the
3352 // implementations thus found, for compatibility with old resolve pass.
3354 fn with_scope(&mut self, name: Option<Ident>, f: |&mut Resolver|) {
3355 let orig_module = self.current_module.clone();
3357 // Move down in the graph.
3363 self.populate_module_if_necessary(&orig_module);
3365 match orig_module.children.borrow().find(&name.name) {
3367 debug!("!!! (with scope) didn't find `{}` in `{}`",
3368 token::get_ident(name),
3369 self.module_to_str(&*orig_module));
3371 Some(name_bindings) => {
3372 match (*name_bindings).get_module_if_available() {
3374 debug!("!!! (with scope) didn't find module \
3376 token::get_ident(name),
3377 self.module_to_str(&*orig_module));
3380 self.current_module = module_;
3390 self.current_module = orig_module;
3393 /// Wraps the given definition in the appropriate number of `def_upvar`
3400 -> Option<DefLike> {
3405 DlDef(d @ DefLocal(..)) | DlDef(d @ DefUpvar(..)) |
3406 DlDef(d @ DefArg(..)) | DlDef(d @ DefBinding(..)) => {
3408 is_ty_param = false;
3410 DlDef(d @ DefTyParam(..)) |
3411 DlDef(d @ DefSelfTy(..)) => {
3416 return Some(def_like);
3420 let mut rib_index = rib_index + 1;
3421 while rib_index < ribs.len() {
3422 match ribs[rib_index].kind {
3424 // Nothing to do. Continue.
3426 FunctionRibKind(function_id, body_id) => {
3428 def = DefUpvar(def.def_id().node,
3434 MethodRibKind(item_id, _) => {
3435 // If the def is a ty param, and came from the parent
3438 DefTyParam(_, did, _) if {
3439 self.def_map.borrow().find(&did.node).map(|x| *x)
3440 == Some(DefTyParamBinder(item_id))
3453 // This was an attempt to access an upvar inside a
3454 // named function item. This is not allowed, so we
3459 "can't capture dynamic environment in a fn item; \
3460 use the || { ... } closure form instead");
3462 // This was an attempt to use a type parameter outside
3465 self.resolve_error(span,
3466 "can't use type parameters from \
3467 outer function; try using a local \
3468 type parameter instead");
3477 // This was an attempt to access an upvar inside a
3478 // named function item. This is not allowed, so we
3483 "can't capture dynamic environment in a fn item; \
3484 use the || { ... } closure form instead");
3486 // This was an attempt to use a type parameter outside
3489 self.resolve_error(span,
3490 "can't use type parameters from \
3491 outer function; try using a local \
3492 type parameter instead");
3497 ConstantItemRibKind => {
3500 self.resolve_error(span,
3501 "cannot use an outer type \
3502 parameter in this context");
3504 // Still doesn't deal with upvars
3505 self.resolve_error(span,
3506 "attempt to use a non-constant \
3507 value in a constant");
3516 return Some(DlDef(def));
3519 fn search_ribs(&self,
3523 -> Option<DefLike> {
3524 // FIXME #4950: This should not use a while loop.
3525 // FIXME #4950: Try caching?
3527 let mut i = ribs.len();
3530 let binding_opt = ribs[i].bindings.borrow().find_copy(&name);
3533 return self.upvarify(ribs, i, def_like, span);
3544 fn resolve_crate(&mut self, krate: &ast::Crate) {
3545 debug!("(resolving crate) starting");
3547 visit::walk_crate(self, krate, ());
3550 fn resolve_item(&mut self, item: &Item) {
3551 debug!("(resolving item) resolving {}",
3552 token::get_ident(item.ident));
3556 // enum item: resolve all the variants' discrs,
3557 // then resolve the ty params
3558 ItemEnum(ref enum_def, ref generics) => {
3559 for variant in (*enum_def).variants.iter() {
3560 for dis_expr in variant.node.disr_expr.iter() {
3561 // resolve the discriminator expr
3563 self.with_constant_rib(|this| {
3564 this.resolve_expr(&**dis_expr);
3569 // n.b. the discr expr gets visited twice.
3570 // but maybe it's okay since the first time will signal an
3571 // error if there is one? -- tjc
3572 self.with_type_parameter_rib(HasTypeParameters(generics,
3577 visit::walk_item(this, item, ());
3581 ItemTy(_, ref generics) => {
3582 self.with_type_parameter_rib(HasTypeParameters(generics,
3587 visit::walk_item(this, item, ());
3591 ItemImpl(ref generics,
3592 ref implemented_traits,
3595 self.resolve_implementation(item.id,
3599 methods.as_slice());
3602 ItemTrait(ref generics, _, ref traits, ref methods) => {
3603 // Create a new rib for the self type.
3604 let self_type_rib = Rib::new(ItemRibKind);
3606 // plain insert (no renaming)
3607 let name = self.type_self_ident.name;
3608 self_type_rib.bindings.borrow_mut()
3609 .insert(name, DlDef(DefSelfTy(item.id)));
3610 self.type_ribs.borrow_mut().push(self_type_rib);
3612 // Create a new rib for the trait-wide type parameters.
3613 self.with_type_parameter_rib(HasTypeParameters(generics,
3618 this.resolve_type_parameters(&generics.ty_params);
3620 // Resolve derived traits.
3621 for trt in traits.iter() {
3622 this.resolve_trait_reference(item.id, trt, TraitDerivation);
3625 for method in (*methods).iter() {
3626 // Create a new rib for the method-specific type
3629 // FIXME #4951: Do we need a node ID here?
3632 ast::Required(ref ty_m) => {
3633 this.with_type_parameter_rib
3634 (HasTypeParameters(&ty_m.generics,
3637 MethodRibKind(item.id, Required)),
3640 // Resolve the method-specific type
3642 this.resolve_type_parameters(
3643 &ty_m.generics.ty_params);
3645 for argument in ty_m.decl.inputs.iter() {
3646 this.resolve_type(&*argument.ty);
3649 this.resolve_type(&*ty_m.decl.output);
3652 ast::Provided(ref m) => {
3653 this.resolve_method(MethodRibKind(item.id,
3661 self.type_ribs.borrow_mut().pop();
3664 ItemStruct(ref struct_def, ref generics) => {
3665 self.resolve_struct(item.id,
3667 struct_def.super_struct,
3668 struct_def.fields.as_slice());
3671 ItemMod(ref module_) => {
3672 self.with_scope(Some(item.ident), |this| {
3673 this.resolve_module(module_, item.span, item.ident,
3678 ItemForeignMod(ref foreign_module) => {
3679 self.with_scope(Some(item.ident), |this| {
3680 for foreign_item in foreign_module.items.iter() {
3681 match foreign_item.node {
3682 ForeignItemFn(_, ref generics) => {
3683 this.with_type_parameter_rib(
3685 generics, FnSpace, foreign_item.id,
3687 |this| visit::walk_foreign_item(this,
3691 ForeignItemStatic(..) => {
3692 visit::walk_foreign_item(this,
3701 ItemFn(fn_decl, _, _, ref generics, block) => {
3702 self.resolve_function(ItemRibKind,
3713 self.with_constant_rib(|this| {
3714 visit::walk_item(this, item, ());
3719 // do nothing, these are just around to be encoded
3724 fn with_type_parameter_rib(&mut self,
3725 type_parameters: TypeParameters,
3726 f: |&mut Resolver|) {
3727 match type_parameters {
3728 HasTypeParameters(generics, space, node_id,
3731 let function_type_rib = Rib::new(rib_kind);
3733 for (index, type_parameter) in generics.ty_params.iter().enumerate() {
3734 let ident = type_parameter.ident;
3735 debug!("with_type_parameter_rib: {} {}", node_id,
3737 let def_like = DlDef(DefTyParam(space,
3738 local_def(type_parameter.id),
3740 // Associate this type parameter with
3741 // the item that bound it
3742 self.record_def(type_parameter.id,
3743 (DefTyParamBinder(node_id), LastMod(AllPublic)));
3744 // plain insert (no renaming)
3745 function_type_rib.bindings.borrow_mut()
3746 .insert(ident.name, def_like);
3748 self.type_ribs.borrow_mut().push(function_type_rib);
3751 NoTypeParameters => {
3758 match type_parameters {
3759 HasTypeParameters(..) => { self.type_ribs.borrow_mut().pop(); }
3760 NoTypeParameters => { }
3764 fn with_label_rib(&mut self, f: |&mut Resolver|) {
3765 self.label_ribs.borrow_mut().push(Rib::new(NormalRibKind));
3767 self.label_ribs.borrow_mut().pop();
3770 fn with_constant_rib(&mut self, f: |&mut Resolver|) {
3771 self.value_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind));
3772 self.type_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind));
3774 self.type_ribs.borrow_mut().pop();
3775 self.value_ribs.borrow_mut().pop();
3778 fn resolve_function(&mut self,
3780 optional_declaration: Option<P<FnDecl>>,
3781 type_parameters: TypeParameters,
3783 // Create a value rib for the function.
3784 let function_value_rib = Rib::new(rib_kind);
3785 self.value_ribs.borrow_mut().push(function_value_rib);
3787 // Create a label rib for the function.
3788 let function_label_rib = Rib::new(rib_kind);
3789 self.label_ribs.borrow_mut().push(function_label_rib);
3791 // If this function has type parameters, add them now.
3792 self.with_type_parameter_rib(type_parameters, |this| {
3793 // Resolve the type parameters.
3794 match type_parameters {
3795 NoTypeParameters => {
3798 HasTypeParameters(ref generics, _, _, _) => {
3799 this.resolve_type_parameters(&generics.ty_params);
3803 // Add each argument to the rib.
3804 match optional_declaration {
3808 Some(declaration) => {
3809 for argument in declaration.inputs.iter() {
3810 let mut bindings_list = HashMap::new();
3811 this.resolve_pattern(&*argument.pat,
3812 ArgumentIrrefutableMode,
3813 &mut bindings_list);
3815 this.resolve_type(&*argument.ty);
3817 debug!("(resolving function) recorded argument");
3820 this.resolve_type(&*declaration.output);
3824 // Resolve the function body.
3825 this.resolve_block(&*block);
3827 debug!("(resolving function) leaving function");
3830 self.label_ribs.borrow_mut().pop();
3831 self.value_ribs.borrow_mut().pop();
3834 fn resolve_type_parameters(&mut self,
3835 type_parameters: &OwnedSlice<TyParam>) {
3836 for type_parameter in type_parameters.iter() {
3837 for bound in type_parameter.bounds.iter() {
3838 self.resolve_type_parameter_bound(type_parameter.id, bound);
3840 match type_parameter.default {
3841 Some(ref ty) => self.resolve_type(&**ty),
3847 fn resolve_type_parameter_bound(&mut self,
3849 type_parameter_bound: &TyParamBound) {
3850 match *type_parameter_bound {
3851 TraitTyParamBound(ref tref) => {
3852 self.resolve_trait_reference(id, tref, TraitBoundingTypeParameter)
3854 UnboxedFnTyParamBound(ref unboxed_function) => {
3855 for argument in unboxed_function.decl.inputs.iter() {
3856 self.resolve_type(&*argument.ty);
3859 self.resolve_type(&*unboxed_function.decl.output);
3861 StaticRegionTyParamBound | OtherRegionTyParamBound(_) => {}
3865 fn resolve_trait_reference(&mut self,
3867 trait_reference: &TraitRef,
3868 reference_type: TraitReferenceType) {
3869 match self.resolve_path(id, &trait_reference.path, TypeNS, true) {
3871 let path_str = self.path_idents_to_str(&trait_reference.path);
3872 let usage_str = match reference_type {
3873 TraitBoundingTypeParameter => "bound type parameter with",
3874 TraitImplementation => "implement",
3875 TraitDerivation => "derive"
3878 let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str);
3879 self.resolve_error(trait_reference.path.span, msg.as_slice());
3882 debug!("(resolving trait) found trait def: {:?}", def);
3883 self.record_def(trait_reference.ref_id, def);
3888 fn resolve_struct(&mut self,
3890 generics: &Generics,
3891 super_struct: Option<P<Ty>>,
3892 fields: &[StructField]) {
3893 // If applicable, create a rib for the type parameters.
3894 self.with_type_parameter_rib(HasTypeParameters(generics,
3899 // Resolve the type parameters.
3900 this.resolve_type_parameters(&generics.ty_params);
3902 // Resolve the super struct.
3903 match super_struct {
3904 Some(t) => match t.node {
3905 TyPath(ref path, None, path_id) => {
3906 match this.resolve_path(id, path, TypeNS, true) {
3907 Some((DefTy(def_id), lp)) if this.structs.contains_key(&def_id) => {
3908 let def = DefStruct(def_id);
3909 debug!("(resolving struct) resolved `{}` to type {:?}",
3910 token::get_ident(path.segments
3914 debug!("(resolving struct) writing resolution for `{}` (id {})",
3915 this.path_idents_to_str(path),
3917 this.record_def(path_id, (def, lp));
3919 Some((DefStruct(_), _)) => {
3920 this.session.span_err(t.span,
3921 "super-struct is defined \
3922 in a different crate")
3924 Some(_) => this.session.span_err(t.span,
3925 "super-struct is not a struct type"),
3926 None => this.session.span_err(t.span,
3927 "super-struct could not be resolved"),
3930 _ => this.session.span_bug(t.span, "path not mapped to a TyPath")
3936 for field in fields.iter() {
3937 this.resolve_type(&*field.node.ty);
3942 // Does this really need to take a RibKind or is it always going
3943 // to be NormalRibKind?
3944 fn resolve_method(&mut self,
3947 let method_generics = &method.generics;
3948 let type_parameters = HasTypeParameters(method_generics,
3953 self.resolve_function(rib_kind, Some(method.decl), type_parameters, method.body);
3956 fn with_current_self_type<T>(&mut self, self_type: &Ty, f: |&mut Resolver| -> T) -> T {
3957 // Handle nested impls (inside fn bodies)
3958 let previous_value = replace(&mut self.current_self_type, Some(self_type.clone()));
3959 let result = f(self);
3960 self.current_self_type = previous_value;
3964 fn with_optional_trait_ref<T>(&mut self, id: NodeId,
3965 opt_trait_ref: &Option<TraitRef>,
3966 f: |&mut Resolver| -> T) -> T {
3967 let new_val = match *opt_trait_ref {
3968 Some(ref trait_ref) => {
3969 self.resolve_trait_reference(id, trait_ref, TraitImplementation);
3971 match self.def_map.borrow().find(&trait_ref.ref_id) {
3973 let did = def.def_id();
3974 Some((did, trait_ref.clone()))
3981 let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
3982 let result = f(self);
3983 self.current_trait_ref = original_trait_ref;
3987 fn resolve_implementation(&mut self,
3989 generics: &Generics,
3990 opt_trait_reference: &Option<TraitRef>,
3992 methods: &[Gc<Method>]) {
3993 // If applicable, create a rib for the type parameters.
3994 self.with_type_parameter_rib(HasTypeParameters(generics,
3999 // Resolve the type parameters.
4000 this.resolve_type_parameters(&generics.ty_params);
4002 // Resolve the trait reference, if necessary.
4003 this.with_optional_trait_ref(id, opt_trait_reference, |this| {
4004 // Resolve the self type.
4005 this.resolve_type(self_type);
4007 this.with_current_self_type(self_type, |this| {
4008 for method in methods.iter() {
4009 // We also need a new scope for the method-specific type parameters.
4010 this.resolve_method(MethodRibKind(id, Provided(method.id)),
4018 fn resolve_module(&mut self, module: &Mod, _span: Span,
4019 _name: Ident, id: NodeId) {
4020 // Write the implementations in scope into the module metadata.
4021 debug!("(resolving module) resolving module ID {}", id);
4022 visit::walk_mod(self, module, ());
4025 fn resolve_local(&mut self, local: &Local) {
4026 // Resolve the type.
4027 self.resolve_type(&*local.ty);
4029 // Resolve the initializer, if necessary.
4034 Some(ref initializer) => {
4035 self.resolve_expr(&**initializer);
4039 // Resolve the pattern.
4040 let mut bindings_list = HashMap::new();
4041 self.resolve_pattern(&*local.pat,
4042 LocalIrrefutableMode,
4043 &mut bindings_list);
4046 // build a map from pattern identifiers to binding-info's.
4047 // this is done hygienically. This could arise for a macro
4048 // that expands into an or-pattern where one 'x' was from the
4049 // user and one 'x' came from the macro.
4050 fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap {
4051 let mut result = HashMap::new();
4052 pat_bindings(&self.def_map, pat, |binding_mode, _id, sp, path| {
4053 let name = mtwt::resolve(path_to_ident(path));
4055 binding_info {span: sp,
4056 binding_mode: binding_mode});
4061 // check that all of the arms in an or-pattern have exactly the
4062 // same set of bindings, with the same binding modes for each.
4063 fn check_consistent_bindings(&mut self, arm: &Arm) {
4064 if arm.pats.len() == 0 {
4067 let map_0 = self.binding_mode_map(&**arm.pats.get(0));
4068 for (i, p) in arm.pats.iter().enumerate() {
4069 let map_i = self.binding_mode_map(&**p);
4071 for (&key, &binding_0) in map_0.iter() {
4072 match map_i.find(&key) {
4076 format!("variable `{}` from pattern #1 is \
4077 not bound in pattern #{}",
4078 token::get_name(key),
4081 Some(binding_i) => {
4082 if binding_0.binding_mode != binding_i.binding_mode {
4085 format!("variable `{}` is bound with different \
4086 mode in pattern #{} than in pattern #1",
4087 token::get_name(key),
4094 for (&key, &binding) in map_i.iter() {
4095 if !map_0.contains_key(&key) {
4098 format!("variable `{}` from pattern {}{} is \
4099 not bound in pattern {}1",
4100 token::get_name(key),
4101 "#", i + 1, "#").as_slice());
4107 fn resolve_arm(&mut self, arm: &Arm) {
4108 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4110 let mut bindings_list = HashMap::new();
4111 for pattern in arm.pats.iter() {
4112 self.resolve_pattern(&**pattern, RefutableMode, &mut bindings_list);
4115 // This has to happen *after* we determine which
4116 // pat_idents are variants
4117 self.check_consistent_bindings(arm);
4119 visit::walk_expr_opt(self, arm.guard, ());
4120 self.resolve_expr(&*arm.body);
4122 self.value_ribs.borrow_mut().pop();
4125 fn resolve_block(&mut self, block: &Block) {
4126 debug!("(resolving block) entering block");
4127 self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind));
4129 // Move down in the graph, if there's an anonymous module rooted here.
4130 let orig_module = self.current_module.clone();
4131 match orig_module.anonymous_children.borrow().find(&block.id) {
4132 None => { /* Nothing to do. */ }
4133 Some(anonymous_module) => {
4134 debug!("(resolving block) found anonymous module, moving \
4136 self.current_module = anonymous_module.clone();
4140 // Descend into the block.
4141 visit::walk_block(self, block, ());
4144 self.current_module = orig_module;
4146 self.value_ribs.borrow_mut().pop();
4147 debug!("(resolving block) leaving block");
4150 fn resolve_type(&mut self, ty: &Ty) {
4152 // Like path expressions, the interpretation of path types depends
4153 // on whether the path has multiple elements in it or not.
4155 TyPath(ref path, ref bounds, path_id) => {
4156 // This is a path in the type namespace. Walk through scopes
4158 let mut result_def = None;
4160 // First, check to see whether the name is a primitive type.
4161 if path.segments.len() == 1 {
4162 let id = path.segments.last().unwrap().identifier;
4164 match self.primitive_type_table
4168 Some(&primitive_type) => {
4170 Some((DefPrimTy(primitive_type), LastMod(AllPublic)));
4174 .any(|s| !s.lifetimes.is_empty()) {
4175 self.session.span_err(path.span,
4176 "lifetime parameters \
4177 are not allowed on \
4179 } else if path.segments
4181 .any(|s| s.types.len() > 0) {
4182 self.session.span_err(path.span,
4183 "type parameters are \
4184 not allowed on this \
4196 match self.resolve_path(ty.id, path, TypeNS, true) {
4198 debug!("(resolving type) resolved `{}` to \
4200 token::get_ident(path.segments
4204 result_def = Some(def);
4211 Some(_) => {} // Continue.
4216 // Write the result into the def map.
4217 debug!("(resolving type) writing resolution for `{}` \
4219 self.path_idents_to_str(path),
4221 self.record_def(path_id, def);
4224 let msg = format!("use of undeclared type name `{}`",
4225 self.path_idents_to_str(path));
4226 self.resolve_error(ty.span, msg.as_slice());
4230 bounds.as_ref().map(|bound_vec| {
4231 for bound in bound_vec.iter() {
4232 self.resolve_type_parameter_bound(ty.id, bound);
4237 TyClosure(c, _) | TyProc(c) => {
4238 c.bounds.as_ref().map(|bounds| {
4239 for bound in bounds.iter() {
4240 self.resolve_type_parameter_bound(ty.id, bound);
4243 visit::walk_ty(self, ty, ());
4247 // Just resolve embedded types.
4248 visit::walk_ty(self, ty, ());
4253 fn resolve_pattern(&mut self,
4255 mode: PatternBindingMode,
4256 // Maps idents to the node ID for the (outermost)
4257 // pattern that binds them
4258 bindings_list: &mut HashMap<Name,NodeId>) {
4259 let pat_id = pattern.id;
4260 walk_pat(pattern, |pattern| {
4261 match pattern.node {
4262 PatIdent(binding_mode, ref path, _)
4263 if !path.global && path.segments.len() == 1 => {
4265 // The meaning of pat_ident with no type parameters
4266 // depends on whether an enum variant or unit-like struct
4267 // with that name is in scope. The probing lookup has to
4268 // be careful not to emit spurious errors. Only matching
4269 // patterns (match) can match nullary variants or
4270 // unit-like structs. For binding patterns (let), matching
4271 // such a value is simply disallowed (since it's rarely
4274 let ident = path.segments.get(0).identifier;
4275 let renamed = mtwt::resolve(ident);
4277 match self.resolve_bare_identifier_pattern(ident) {
4278 FoundStructOrEnumVariant(def, lp)
4279 if mode == RefutableMode => {
4280 debug!("(resolving pattern) resolving `{}` to \
4281 struct or enum variant",
4282 token::get_name(renamed));
4284 self.enforce_default_binding_mode(
4288 self.record_def(pattern.id, (def, lp));
4290 FoundStructOrEnumVariant(..) => {
4293 format!("declaration of `{}` shadows an enum \
4294 variant or unit-like struct in \
4296 token::get_name(renamed)).as_slice());
4298 FoundConst(def, lp) if mode == RefutableMode => {
4299 debug!("(resolving pattern) resolving `{}` to \
4301 token::get_name(renamed));
4303 self.enforce_default_binding_mode(
4307 self.record_def(pattern.id, (def, lp));
4310 self.resolve_error(pattern.span,
4311 "only irrefutable patterns \
4314 BareIdentifierPatternUnresolved => {
4315 debug!("(resolving pattern) binding `{}`",
4316 token::get_name(renamed));
4318 let def = match mode {
4320 // For pattern arms, we must use
4321 // `def_binding` definitions.
4323 DefBinding(pattern.id, binding_mode)
4325 LocalIrrefutableMode => {
4326 // But for locals, we use `def_local`.
4327 DefLocal(pattern.id, binding_mode)
4329 ArgumentIrrefutableMode => {
4330 // And for function arguments, `def_arg`.
4331 DefArg(pattern.id, binding_mode)
4335 // Record the definition so that later passes
4336 // will be able to distinguish variants from
4337 // locals in patterns.
4339 self.record_def(pattern.id, (def, LastMod(AllPublic)));
4341 // Add the binding to the local ribs, if it
4342 // doesn't already exist in the bindings list. (We
4343 // must not add it if it's in the bindings list
4344 // because that breaks the assumptions later
4345 // passes make about or-patterns.)
4347 if !bindings_list.contains_key(&renamed) {
4348 let this = &mut *self;
4349 let value_ribs = this.value_ribs.borrow();
4350 let length = value_ribs.len();
4351 let last_rib = value_ribs.get(
4353 last_rib.bindings.borrow_mut()
4354 .insert(renamed, DlDef(def));
4355 bindings_list.insert(renamed, pat_id);
4356 } else if bindings_list.find(&renamed) ==
4358 // Then this is a duplicate variable in the
4359 // same disjunction, which is an error.
4360 self.resolve_error(pattern.span,
4361 format!("identifier `{}` is bound \
4362 more than once in the same \
4364 path_to_str(path)).as_slice());
4366 // Else, not bound in the same pattern: do
4371 // Check the types in the path pattern.
4372 for ty in path.segments
4374 .flat_map(|seg| seg.types.iter()) {
4375 self.resolve_type(&**ty);
4379 PatIdent(binding_mode, ref path, _) => {
4380 // This must be an enum variant, struct, or constant.
4381 match self.resolve_path(pat_id, path, ValueNS, false) {
4382 Some(def @ (DefVariant(..), _)) |
4383 Some(def @ (DefStruct(..), _)) => {
4384 self.record_def(pattern.id, def);
4386 Some(def @ (DefStatic(..), _)) => {
4387 self.enforce_default_binding_mode(
4391 self.record_def(pattern.id, def);
4396 format!("`{}` is not an enum variant or constant",
4401 .identifier)).as_slice())
4404 self.resolve_error(path.span,
4405 "unresolved enum variant");
4409 // Check the types in the path pattern.
4410 for ty in path.segments
4412 .flat_map(|s| s.types.iter()) {
4413 self.resolve_type(&**ty);
4417 PatEnum(ref path, _) => {
4418 // This must be an enum variant, struct or const.
4419 match self.resolve_path(pat_id, path, ValueNS, false) {
4420 Some(def @ (DefFn(..), _)) |
4421 Some(def @ (DefVariant(..), _)) |
4422 Some(def @ (DefStruct(..), _)) |
4423 Some(def @ (DefStatic(..), _)) => {
4424 self.record_def(pattern.id, def);
4427 self.resolve_error(path.span,
4428 format!("`{}` is not an enum variant, struct or const",
4433 .identifier)).as_slice());
4436 self.resolve_error(path.span,
4437 format!("unresolved enum variant, struct or const `{}`",
4442 .identifier)).as_slice());
4446 // Check the types in the path pattern.
4447 for ty in path.segments
4449 .flat_map(|s| s.types.iter()) {
4450 self.resolve_type(&**ty);
4454 PatLit(ref expr) => {
4455 self.resolve_expr(&**expr);
4458 PatRange(ref first_expr, ref last_expr) => {
4459 self.resolve_expr(&**first_expr);
4460 self.resolve_expr(&**last_expr);
4463 PatStruct(ref path, _, _) => {
4464 match self.resolve_path(pat_id, path, TypeNS, false) {
4465 Some((DefTy(class_id), lp))
4466 if self.structs.contains_key(&class_id) => {
4467 let class_def = DefStruct(class_id);
4468 self.record_def(pattern.id, (class_def, lp));
4470 Some(definition @ (DefStruct(class_id), _)) => {
4471 assert!(self.structs.contains_key(&class_id));
4472 self.record_def(pattern.id, definition);
4474 Some(definition @ (DefVariant(_, variant_id, _), _))
4475 if self.structs.contains_key(&variant_id) => {
4476 self.record_def(pattern.id, definition);
4479 debug!("(resolving pattern) didn't find struct \
4480 def: {:?}", result);
4481 let msg = format!("`{}` does not name a structure",
4482 self.path_idents_to_str(path));
4483 self.resolve_error(path.span, msg.as_slice());
4496 fn resolve_bare_identifier_pattern(&mut self, name: Ident)
4497 -> BareIdentifierPatternResolution {
4498 let module = self.current_module.clone();
4499 match self.resolve_item_in_lexical_scope(module,
4502 Success((target, _)) => {
4503 debug!("(resolve bare identifier pattern) succeeded in \
4504 finding {} at {:?}",
4505 token::get_ident(name),
4506 target.bindings.value_def.borrow());
4507 match *target.bindings.value_def.borrow() {
4509 fail!("resolved name in the value namespace to a \
4510 set of name bindings with no def?!");
4513 // For the two success cases, this lookup can be
4514 // considered as not having a private component because
4515 // the lookup happened only within the current module.
4517 def @ DefVariant(..) | def @ DefStruct(..) => {
4518 return FoundStructOrEnumVariant(def, LastMod(AllPublic));
4520 def @ DefStatic(_, false) => {
4521 return FoundConst(def, LastMod(AllPublic));
4524 return BareIdentifierPatternUnresolved;
4532 fail!("unexpected indeterminate result");
4536 debug!("(resolve bare identifier pattern) failed to find {}",
4537 token::get_ident(name));
4538 return BareIdentifierPatternUnresolved;
4543 /// If `check_ribs` is true, checks the local definitions first; i.e.
4544 /// doesn't skip straight to the containing module.
4545 fn resolve_path(&mut self,
4548 namespace: Namespace,
4549 check_ribs: bool) -> Option<(Def, LastPrivate)> {
4550 // First, resolve the types.
4551 for ty in path.segments.iter().flat_map(|s| s.types.iter()) {
4552 self.resolve_type(&**ty);
4556 return self.resolve_crate_relative_path(path, namespace);
4559 let unqualified_def =
4560 self.resolve_identifier(path.segments
4567 if path.segments.len() > 1 {
4568 let def = self.resolve_module_relative_path(path, namespace);
4569 match (def, unqualified_def) {
4570 (Some((d, _)), Some((ud, _))) if d == ud => {
4572 .add_lint(UnnecessaryQualification,
4575 "unnecessary qualification".to_string());
4583 return unqualified_def;
4586 // resolve a single identifier (used as a varref)
4587 fn resolve_identifier(&mut self,
4589 namespace: Namespace,
4592 -> Option<(Def, LastPrivate)> {
4594 match self.resolve_identifier_in_local_ribs(identifier,
4598 return Some((def, LastMod(AllPublic)));
4606 return self.resolve_item_by_identifier_in_lexical_scope(identifier,
4610 // FIXME #4952: Merge me with resolve_name_in_module?
4611 fn resolve_definition_of_name_in_module(&mut self,
4612 containing_module: Rc<Module>,
4614 namespace: Namespace)
4616 // First, search children.
4617 self.populate_module_if_necessary(&containing_module);
4619 match containing_module.children.borrow().find(&name) {
4620 Some(child_name_bindings) => {
4621 match child_name_bindings.def_for_namespace(namespace) {
4623 // Found it. Stop the search here.
4624 let p = child_name_bindings.defined_in_public_namespace(
4626 let lp = if p {LastMod(AllPublic)} else {
4627 LastMod(DependsOn(def.def_id()))
4629 return ChildNameDefinition(def, lp);
4637 // Next, search import resolutions.
4638 match containing_module.import_resolutions.borrow().find(&name) {
4639 Some(import_resolution) if import_resolution.is_public => {
4640 match (*import_resolution).target_for_namespace(namespace) {
4642 match target.bindings.def_for_namespace(namespace) {
4645 let id = import_resolution.id(namespace);
4646 self.used_imports.insert((id, namespace));
4647 return ImportNameDefinition(def, LastMod(AllPublic));
4650 // This can happen with external impls, due to
4651 // the imperfect way we read the metadata.
4658 Some(..) | None => {} // Continue.
4661 // Finally, search through external children.
4662 if namespace == TypeNS {
4663 match containing_module.external_module_children.borrow()
4667 match module.def_id.get() {
4668 None => {} // Continue.
4670 let lp = if module.is_public {LastMod(AllPublic)} else {
4671 LastMod(DependsOn(def_id))
4673 return ChildNameDefinition(DefMod(def_id), lp);
4680 return NoNameDefinition;
4683 // resolve a "module-relative" path, e.g. a::b::c
4684 fn resolve_module_relative_path(&mut self,
4686 namespace: Namespace)
4687 -> Option<(Def, LastPrivate)> {
4688 let module_path_idents = path.segments.init().iter()
4689 .map(|ps| ps.identifier)
4690 .collect::<Vec<_>>();
4692 let containing_module;
4694 let module = self.current_module.clone();
4695 match self.resolve_module_path(module,
4696 module_path_idents.as_slice(),
4701 let msg = format!("use of undeclared module `{}`",
4702 self.idents_to_str(module_path_idents.as_slice()));
4703 self.resolve_error(path.span, msg.as_slice());
4708 fail!("indeterminate unexpected");
4711 Success((resulting_module, resulting_last_private)) => {
4712 containing_module = resulting_module;
4713 last_private = resulting_last_private;
4717 let ident = path.segments.last().unwrap().identifier;
4718 let def = match self.resolve_definition_of_name_in_module(containing_module.clone(),
4721 NoNameDefinition => {
4722 // We failed to resolve the name. Report an error.
4725 ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
4726 (def, last_private.or(lp))
4729 match containing_module.kind.get() {
4730 TraitModuleKind | ImplModuleKind => {
4731 match containing_module.def_id.get() {
4733 match self.method_map.borrow().find(&(ident.name, def_id)) {
4734 Some(x) if *x == SelfStatic => (),
4737 debug!("containing module was a trait or impl \
4738 and name was a method -> not resolved");
4751 /// Invariant: This must be called only during main resolution, not during
4752 /// import resolution.
4753 fn resolve_crate_relative_path(&mut self,
4755 namespace: Namespace)
4756 -> Option<(Def, LastPrivate)> {
4757 let module_path_idents = path.segments.init().iter()
4758 .map(|ps| ps.identifier)
4759 .collect::<Vec<_>>();
4761 let root_module = self.graph_root.get_module();
4763 let containing_module;
4765 match self.resolve_module_path_from_root(root_module,
4766 module_path_idents.as_slice(),
4770 LastMod(AllPublic)) {
4772 let msg = format!("use of undeclared module `::{}`",
4773 self.idents_to_str(module_path_idents.as_slice()));
4774 self.resolve_error(path.span, msg.as_slice());
4779 fail!("indeterminate unexpected");
4782 Success((resulting_module, resulting_last_private)) => {
4783 containing_module = resulting_module;
4784 last_private = resulting_last_private;
4788 let name = path.segments.last().unwrap().identifier.name;
4789 match self.resolve_definition_of_name_in_module(containing_module,
4792 NoNameDefinition => {
4793 // We failed to resolve the name. Report an error.
4796 ChildNameDefinition(def, lp) | ImportNameDefinition(def, lp) => {
4797 return Some((def, last_private.or(lp)));
4802 fn resolve_identifier_in_local_ribs(&mut self,
4804 namespace: Namespace,
4807 // Check the local set of ribs.
4808 let search_result = match namespace {
4810 let renamed = mtwt::resolve(ident);
4811 self.search_ribs(self.value_ribs.borrow().as_slice(),
4815 let name = ident.name;
4816 self.search_ribs(self.type_ribs.borrow().as_slice(), name, span)
4820 match search_result {
4821 Some(DlDef(def)) => {
4822 debug!("(resolving path in local ribs) resolved `{}` to \
4824 token::get_ident(ident),
4828 Some(DlField) | Some(DlImpl(_)) | None => {
4834 fn resolve_item_by_identifier_in_lexical_scope(&mut self,
4836 namespace: Namespace)
4837 -> Option<(Def, LastPrivate)> {
4839 let module = self.current_module.clone();
4840 match self.resolve_item_in_lexical_scope(module,
4843 Success((target, _)) => {
4844 match (*target.bindings).def_for_namespace(namespace) {
4846 // This can happen if we were looking for a type and
4847 // found a module instead. Modules don't have defs.
4848 debug!("(resolving item path by identifier in lexical \
4849 scope) failed to resolve {} after success...",
4850 token::get_ident(ident));
4854 debug!("(resolving item path in lexical scope) \
4855 resolved `{}` to item",
4856 token::get_ident(ident));
4857 // This lookup is "all public" because it only searched
4858 // for one identifier in the current module (couldn't
4859 // have passed through reexports or anything like that.
4860 return Some((def, LastMod(AllPublic)));
4865 fail!("unexpected indeterminate result");
4868 debug!("(resolving item path by identifier in lexical scope) \
4869 failed to resolve {}", token::get_ident(ident));
4875 fn with_no_errors<T>(&mut self, f: |&mut Resolver| -> T) -> T {
4876 self.emit_errors = false;
4878 self.emit_errors = true;
4882 fn resolve_error(&self, span: Span, s: &str) {
4883 if self.emit_errors {
4884 self.session.span_err(span, s);
4888 fn find_fallback_in_self_type(&mut self, name: Name) -> FallbackSuggestion {
4889 #[deriving(PartialEq)]
4890 enum FallbackChecks {
4895 fn extract_path_and_node_id(t: &Ty, allow: FallbackChecks)
4896 -> Option<(Path, NodeId, FallbackChecks)> {
4898 TyPath(ref path, _, node_id) => Some((path.clone(), node_id, allow)),
4899 TyPtr(mut_ty) => extract_path_and_node_id(&*mut_ty.ty, OnlyTraitAndStatics),
4900 TyRptr(_, mut_ty) => extract_path_and_node_id(&*mut_ty.ty, allow),
4901 // This doesn't handle the remaining `Ty` variants as they are not
4902 // that commonly the self_type, it might be interesting to provide
4903 // support for those in future.
4908 fn get_module(this: &mut Resolver, span: Span, ident_path: &[ast::Ident])
4909 -> Option<Rc<Module>> {
4910 let root = this.current_module.clone();
4911 let last_name = ident_path.last().unwrap().name;
4913 if ident_path.len() == 1 {
4914 match this.primitive_type_table.primitive_types.find(&last_name) {
4917 match this.current_module.children.borrow().find(&last_name) {
4918 Some(child) => child.get_module_if_available(),
4924 match this.resolve_module_path(root,
4925 ident_path.as_slice(),
4929 Success((module, _)) => Some(module),
4935 let (path, node_id, allowed) = match self.current_self_type {
4936 Some(ref ty) => match extract_path_and_node_id(ty, Everything) {
4938 None => return NoSuggestion,
4940 None => return NoSuggestion,
4943 if allowed == Everything {
4944 // Look for a field with the same name in the current self_type.
4945 match self.def_map.borrow().find(&node_id) {
4947 | Some(&DefStruct(did))
4948 | Some(&DefVariant(_, did, _)) => match self.structs.find(&did) {
4951 if fields.iter().any(|&field_name| name == field_name) {
4956 _ => {} // Self type didn't resolve properly
4960 let ident_path = path.segments.iter().map(|seg| seg.identifier).collect::<Vec<_>>();
4962 // Look for a method in the current self type's impl module.
4963 match get_module(self, path.span, ident_path.as_slice()) {
4964 Some(module) => match module.children.borrow().find(&name) {
4966 let p_str = self.path_idents_to_str(&path);
4967 match binding.def_for_namespace(ValueNS) {
4968 Some(DefStaticMethod(_, provenance, _)) => {
4970 FromImpl(_) => return StaticMethod(p_str),
4971 FromTrait(_) => unreachable!()
4974 Some(DefMethod(_, None)) if allowed == Everything => return Method,
4975 Some(DefMethod(_, Some(_))) => return TraitMethod,
4984 // Look for a method in the current trait.
4985 let method_map = self.method_map.borrow();
4986 match self.current_trait_ref {
4987 Some((did, ref trait_ref)) => {
4988 let path_str = self.path_idents_to_str(&trait_ref.path);
4990 match method_map.find(&(name, did)) {
4991 Some(&SelfStatic) => return StaticTraitMethod(path_str),
4992 Some(_) => return TraitMethod,
5002 fn find_best_match_for_name(&mut self, name: &str, max_distance: uint)
5004 let this = &mut *self;
5006 let mut maybes: Vec<token::InternedString> = Vec::new();
5007 let mut values: Vec<uint> = Vec::new();
5009 let mut j = this.value_ribs.borrow().len();
5012 let value_ribs = this.value_ribs.borrow();
5013 let bindings = value_ribs.get(j).bindings.borrow();
5014 for (&k, _) in bindings.iter() {
5015 maybes.push(token::get_name(k));
5016 values.push(uint::MAX);
5020 let mut smallest = 0;
5021 for (i, other) in maybes.iter().enumerate() {
5022 *values.get_mut(i) = name.lev_distance(other.get());
5024 if *values.get(i) <= *values.get(smallest) {
5029 if values.len() > 0 &&
5030 *values.get(smallest) != uint::MAX &&
5031 *values.get(smallest) < name.len() + 2 &&
5032 *values.get(smallest) <= max_distance &&
5033 name != maybes.get(smallest).get() {
5035 Some(maybes.get(smallest).get().to_string())
5042 fn resolve_expr(&mut self, expr: &Expr) {
5043 // First, record candidate traits for this expression if it could
5044 // result in the invocation of a method call.
5046 self.record_candidate_traits_for_expr_if_necessary(expr);
5048 // Next, resolve the node.
5050 // The interpretation of paths depends on whether the path has
5051 // multiple elements in it or not.
5053 ExprPath(ref path) => {
5054 // This is a local path in the value namespace. Walk through
5055 // scopes looking for it.
5057 match self.resolve_path(expr.id, path, ValueNS, true) {
5059 // Write the result into the def map.
5060 debug!("(resolving expr) resolved `{}`",
5061 self.path_idents_to_str(path));
5063 // First-class methods are not supported yet; error
5066 (DefMethod(..), _) => {
5067 self.resolve_error(expr.span,
5068 "first-class methods \
5069 are not supported");
5070 self.session.span_note(expr.span,
5078 self.record_def(expr.id, def);
5081 let wrong_name = self.path_idents_to_str(path);
5082 // Be helpful if the name refers to a struct
5083 // (The pattern matching def_tys where the id is in self.structs
5084 // matches on regular structs while excluding tuple- and enum-like
5085 // structs, which wouldn't result in this error.)
5086 match self.with_no_errors(|this|
5087 this.resolve_path(expr.id, path, TypeNS, false)) {
5088 Some((DefTy(struct_id), _))
5089 if self.structs.contains_key(&struct_id) => {
5090 self.resolve_error(expr.span,
5091 format!("`{}` is a structure name, but \
5093 uses it like a function name",
5094 wrong_name).as_slice());
5096 self.session.span_note(expr.span,
5097 format!("Did you mean to write: \
5098 `{} {{ /* fields */ }}`?",
5099 wrong_name).as_slice());
5103 let mut method_scope = false;
5104 self.value_ribs.borrow().iter().rev().advance(|rib| {
5105 let res = match *rib {
5106 Rib { bindings: _, kind: MethodRibKind(_, _) } => true,
5107 Rib { bindings: _, kind: ItemRibKind } => false,
5108 _ => return true, // Keep advancing
5112 false // Stop advancing
5115 if method_scope && token::get_name(self.self_ident.name).get()
5116 == wrong_name.as_slice() {
5119 "`self` is not available \
5120 in a static method. Maybe a \
5121 `self` argument is missing?");
5123 let name = path_to_ident(path).name;
5124 let mut msg = match self.find_fallback_in_self_type(name) {
5126 // limit search to 5 to reduce the number
5127 // of stupid suggestions
5128 self.find_best_match_for_name(wrong_name.as_slice(), 5)
5129 .map_or("".to_string(),
5130 |x| format!("`{}`", x))
5133 format!("`self.{}`", wrong_name),
5136 format!("to call `self.{}`", wrong_name),
5137 StaticTraitMethod(path_str)
5138 | StaticMethod(path_str) =>
5139 format!("to call `{}::{}`", path_str, wrong_name)
5143 msg = format!(" Did you mean {}?", msg)
5148 format!("unresolved name `{}`.{}",
5157 visit::walk_expr(self, expr, ());
5160 ExprFnBlock(fn_decl, block) |
5161 ExprProc(fn_decl, block) => {
5162 self.resolve_function(FunctionRibKind(expr.id, block.id),
5163 Some(fn_decl), NoTypeParameters,
5167 ExprStruct(ref path, _, _) => {
5168 // Resolve the path to the structure it goes to.
5169 match self.resolve_path(expr.id, path, TypeNS, false) {
5170 Some((DefTy(class_id), lp)) | Some((DefStruct(class_id), lp))
5171 if self.structs.contains_key(&class_id) => {
5172 let class_def = DefStruct(class_id);
5173 self.record_def(expr.id, (class_def, lp));
5175 Some(definition @ (DefVariant(_, class_id, _), _))
5176 if self.structs.contains_key(&class_id) => {
5177 self.record_def(expr.id, definition);
5180 debug!("(resolving expression) didn't find struct \
5181 def: {:?}", result);
5182 let msg = format!("`{}` does not name a structure",
5183 self.path_idents_to_str(path));
5184 self.resolve_error(path.span, msg.as_slice());
5188 visit::walk_expr(self, expr, ());
5191 ExprLoop(_, Some(label)) => {
5192 self.with_label_rib(|this| {
5193 let def_like = DlDef(DefLabel(expr.id));
5196 let label_ribs = this.label_ribs.borrow();
5197 let length = label_ribs.len();
5198 let rib = label_ribs.get(length - 1);
5199 let renamed = mtwt::resolve(label);
5200 rib.bindings.borrow_mut().insert(renamed, def_like);
5203 visit::walk_expr(this, expr, ());
5207 ExprForLoop(..) => fail!("non-desugared expr_for_loop"),
5209 ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
5210 let renamed = mtwt::resolve(label);
5211 match self.search_ribs(self.label_ribs.borrow().as_slice(),
5212 renamed, expr.span) {
5216 format!("use of undeclared label `{}`",
5217 token::get_ident(label)).as_slice())
5219 Some(DlDef(def @ DefLabel(_))) => {
5220 // Since this def is a label, it is never read.
5221 self.record_def(expr.id, (def, LastMod(AllPublic)))
5224 self.session.span_bug(expr.span,
5225 "label wasn't mapped to a \
5232 visit::walk_expr(self, expr, ());
5237 fn record_candidate_traits_for_expr_if_necessary(&mut self, expr: &Expr) {
5239 ExprField(_, ident, _) => {
5240 // FIXME(#6890): Even though you can't treat a method like a
5241 // field, we need to add any trait methods we find that match
5242 // the field name so that we can do some nice error reporting
5243 // later on in typeck.
5244 let traits = self.search_for_traits_containing_method(ident.name);
5245 self.trait_map.insert(expr.id, traits);
5247 ExprMethodCall(ident, _, _) => {
5248 debug!("(recording candidate traits for expr) recording \
5251 let traits = self.search_for_traits_containing_method(ident.node.name);
5252 self.trait_map.insert(expr.id, traits);
5260 fn search_for_traits_containing_method(&mut self, name: Name) -> Vec<DefId> {
5261 debug!("(searching for traits containing method) looking for '{}'",
5262 token::get_name(name));
5264 fn add_trait_info(found_traits: &mut Vec<DefId>,
5265 trait_def_id: DefId,
5267 debug!("(adding trait info) found trait {}:{} for method '{}'",
5270 token::get_name(name));
5271 found_traits.push(trait_def_id);
5274 let mut found_traits = Vec::new();
5275 let mut search_module = self.current_module.clone();
5277 // Look for the current trait.
5278 match self.current_trait_ref {
5279 Some((trait_def_id, _)) => {
5280 let method_map = self.method_map.borrow();
5282 if method_map.contains_key(&(name, trait_def_id)) {
5283 add_trait_info(&mut found_traits, trait_def_id, name);
5286 None => {} // Nothing to do.
5289 // Look for trait children.
5290 self.populate_module_if_necessary(&search_module);
5293 let method_map = self.method_map.borrow();
5294 for (_, child_names) in search_module.children.borrow().iter() {
5295 let def = match child_names.def_for_namespace(TypeNS) {
5299 let trait_def_id = match def {
5300 DefTrait(trait_def_id) => trait_def_id,
5303 if method_map.contains_key(&(name, trait_def_id)) {
5304 add_trait_info(&mut found_traits, trait_def_id, name);
5309 // Look for imports.
5310 for (_, import) in search_module.import_resolutions.borrow().iter() {
5311 let target = match import.target_for_namespace(TypeNS) {
5313 Some(target) => target,
5315 let did = match target.bindings.def_for_namespace(TypeNS) {
5316 Some(DefTrait(trait_def_id)) => trait_def_id,
5317 Some(..) | None => continue,
5319 if self.method_map.borrow().contains_key(&(name, did)) {
5320 add_trait_info(&mut found_traits, did, name);
5321 self.used_imports.insert((import.type_id, TypeNS));
5325 match search_module.parent_link.clone() {
5326 NoParentLink | ModuleParentLink(..) => break,
5327 BlockParentLink(parent_module, _) => {
5328 search_module = parent_module.upgrade().unwrap();
5336 fn record_def(&mut self, node_id: NodeId, (def, lp): (Def, LastPrivate)) {
5337 debug!("(recording def) recording {:?} for {:?}, last private {:?}",
5339 assert!(match lp {LastImport{..} => false, _ => true},
5340 "Import should only be used for `use` directives");
5341 self.last_private.insert(node_id, lp);
5342 self.def_map.borrow_mut().insert_or_update_with(node_id, def, |_, old_value| {
5343 // Resolve appears to "resolve" the same ID multiple
5344 // times, so here is a sanity check it at least comes to
5345 // the same conclusion! - nmatsakis
5346 if def != *old_value {
5348 .bug(format!("node_id {:?} resolved first to {:?} and \
5357 fn enforce_default_binding_mode(&mut self,
5359 pat_binding_mode: BindingMode,
5361 match pat_binding_mode {
5362 BindByValue(_) => {}
5364 self.resolve_error(pat.span,
5365 format!("cannot use `ref` binding mode \
5373 // Unused import checking
5375 // Although this is mostly a lint pass, it lives in here because it depends on
5376 // resolve data structures and because it finalises the privacy information for
5377 // `use` directives.
5380 fn check_for_unused_imports(&mut self, krate: &ast::Crate) {
5381 let mut visitor = UnusedImportCheckVisitor{ resolver: self };
5382 visit::walk_crate(&mut visitor, krate, ());
5385 fn check_for_item_unused_imports(&mut self, vi: &ViewItem) {
5386 // Ignore is_public import statements because there's no way to be sure
5387 // whether they're used or not. Also ignore imports with a dummy span
5388 // because this means that they were generated in some fashion by the
5389 // compiler and we don't need to consider them.
5390 if vi.vis == Public { return }
5391 if vi.span == DUMMY_SP { return }
5394 ViewItemExternCrate(..) => {} // ignore
5395 ViewItemUse(ref p) => {
5397 ViewPathSimple(_, _, id) => self.finalize_import(id, p.span),
5398 ViewPathList(_, ref list, _) => {
5399 for i in list.iter() {
5400 self.finalize_import(i.node.id, i.span);
5403 ViewPathGlob(_, id) => {
5404 if !self.used_imports.contains(&(id, TypeNS)) &&
5405 !self.used_imports.contains(&(id, ValueNS)) {
5407 .add_lint(UnusedImports,
5410 "unused import".to_string());
5418 // We have information about whether `use` (import) directives are actually used now.
5419 // If an import is not used at all, we signal a lint error. If an import is only used
5420 // for a single namespace, we remove the other namespace from the recorded privacy
5421 // information. That means in privacy.rs, we will only check imports and namespaces
5422 // which are used. In particular, this means that if an import could name either a
5423 // public or private item, we will check the correct thing, dependent on how the import
5425 fn finalize_import(&mut self, id: NodeId, span: Span) {
5426 debug!("finalizing import uses for {}",
5427 self.session.codemap().span_to_snippet(span));
5429 if !self.used_imports.contains(&(id, TypeNS)) &&
5430 !self.used_imports.contains(&(id, ValueNS)) {
5431 self.session.add_lint(UnusedImports,
5434 "unused import".to_string());
5437 let (v_priv, t_priv) = match self.last_private.find(&id) {
5445 fail!("we should only have LastImport for `use` directives")
5450 let mut v_used = if self.used_imports.contains(&(id, ValueNS)) {
5455 let t_used = if self.used_imports.contains(&(id, TypeNS)) {
5461 match (v_priv, t_priv) {
5462 // Since some items may be both in the value _and_ type namespaces (e.g., structs)
5463 // we might have two LastPrivates pointing at the same thing. There is no point
5464 // checking both, so lets not check the value one.
5465 (Some(DependsOn(def_v)), Some(DependsOn(def_t))) if def_v == def_t => v_used = Unused,
5469 self.last_private.insert(id, LastImport{value_priv: v_priv,
5472 type_used: t_used});
5478 // Diagnostics are not particularly efficient, because they're rarely
5482 /// A somewhat inefficient routine to obtain the name of a module.
5483 fn module_to_str(&mut self, module: &Module) -> String {
5484 let mut idents = Vec::new();
5486 fn collect_mod(idents: &mut Vec<ast::Ident>, module: &Module) {
5487 match module.parent_link {
5489 ModuleParentLink(ref module, name) => {
5491 collect_mod(idents, &*module.upgrade().unwrap());
5493 BlockParentLink(ref module, _) => {
5494 idents.push(special_idents::opaque);
5495 collect_mod(idents, &*module.upgrade().unwrap());
5499 collect_mod(&mut idents, module);
5501 if idents.len() == 0 {
5502 return "???".to_string();
5504 self.idents_to_str(idents.move_iter().rev()
5505 .collect::<Vec<ast::Ident>>()
5509 #[allow(dead_code)] // useful for debugging
5510 fn dump_module(&mut self, module_: Rc<Module>) {
5511 debug!("Dump of module `{}`:", self.module_to_str(&*module_));
5513 debug!("Children:");
5514 self.populate_module_if_necessary(&module_);
5515 for (&name, _) in module_.children.borrow().iter() {
5516 debug!("* {}", token::get_name(name));
5519 debug!("Import resolutions:");
5520 let import_resolutions = module_.import_resolutions.borrow();
5521 for (&name, import_resolution) in import_resolutions.iter() {
5523 match import_resolution.target_for_namespace(ValueNS) {
5524 None => { value_repr = "".to_string(); }
5526 value_repr = " value:?".to_string();
5532 match import_resolution.target_for_namespace(TypeNS) {
5533 None => { type_repr = "".to_string(); }
5535 type_repr = " type:?".to_string();
5540 debug!("* {}:{}{}", token::get_name(name), value_repr, type_repr);
5545 pub struct CrateMap {
5546 pub def_map: DefMap,
5547 pub exp_map2: ExportMap2,
5548 pub trait_map: TraitMap,
5549 pub external_exports: ExternalExports,
5550 pub last_private_map: LastPrivateMap,
5553 /// Entry point to crate resolution.
5554 pub fn resolve_crate(session: &Session,
5558 let mut resolver = Resolver::new(session, krate.span);
5559 resolver.resolve(krate);
5560 let Resolver { def_map, export_map2, trait_map, last_private,
5561 external_exports, .. } = resolver;
5564 exp_map2: export_map2,
5565 trait_map: trait_map,
5566 external_exports: external_exports,
5567 last_private_map: last_private,