1 // Copyright 2012 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.
14 use driver::session::Session;
15 use metadata::csearch::{each_path, get_method_names_if_trait};
16 use metadata::csearch::{get_static_methods_if_impl, get_struct_fields};
17 use metadata::csearch::{get_type_name_if_impl};
18 use metadata::cstore::find_extern_mod_stmt_cnum;
19 use metadata::decoder::{def_like, dl_def, dl_field, dl_impl};
20 use middle::lang_items::LanguageItems;
21 use middle::lint::{deny, allow, forbid, level, unused_imports, warn};
22 use middle::pat_util::{pat_bindings};
27 use syntax::ast::{RegionTyParamBound, TraitTyParamBound, _mod, add, arm};
28 use syntax::ast::{binding_mode, bitand, bitor, bitxor, blk};
29 use syntax::ast::{bind_infer, bind_by_ref, bind_by_copy};
30 use syntax::ast::{crate, crate_num, decl_item, def, def_arg, def_binding};
31 use syntax::ast::{def_const, def_foreign_mod, def_fn, def_id, def_label};
32 use syntax::ast::{def_local, def_mod, def_prim_ty, def_region, def_self};
33 use syntax::ast::{def_self_ty, def_static_method, def_struct, def_ty};
34 use syntax::ast::{def_ty_param, def_typaram_binder};
35 use syntax::ast::{def_upvar, def_use, def_variant, expr, expr_assign_op};
36 use syntax::ast::{expr_binary, expr_break, expr_cast, expr_field, expr_fn};
37 use syntax::ast::{expr_fn_block, expr_index, expr_method_call, expr_path};
38 use syntax::ast::{def_prim_ty, def_region, def_self, def_ty, def_ty_param};
39 use syntax::ast::{def_upvar, def_use, def_variant, div, eq};
40 use syntax::ast::{enum_variant_kind, expr, expr_again, expr_assign_op};
41 use syntax::ast::{expr_fn_block, expr_index, expr_loop};
42 use syntax::ast::{expr_path, expr_struct, expr_unary, fn_decl};
43 use syntax::ast::{foreign_item, foreign_item_const, foreign_item_fn, ge};
44 use syntax::ast::{gt, ident, impure_fn, inherited, item, item_struct};
45 use syntax::ast::{item_const, item_enum, item_fn, item_foreign_mod};
46 use syntax::ast::{item_impl, item_mac, item_mod, item_trait, item_ty, le};
47 use syntax::ast::{local, local_crate, lt, method, mode, module_ns, mul};
48 use syntax::ast::{named_field, ne, neg, node_id, pat, pat_enum, pat_ident};
49 use syntax::ast::{path, pat_box, pat_lit, pat_range, pat_rec, pat_struct};
50 use syntax::ast::{pat_tup, pat_uniq, pat_wild, prim_ty, private, provided};
51 use syntax::ast::{public, required, rem, self_ty_, shl, shr, stmt_decl};
52 use syntax::ast::{struct_dtor, struct_field, struct_variant_kind, sty_by_ref};
53 use syntax::ast::{sty_static, subtract, trait_ref, tuple_variant_kind, Ty};
54 use syntax::ast::{ty_bool, ty_char, ty_f, ty_f32, ty_f64, ty_float, ty_i};
55 use syntax::ast::{ty_i16, ty_i32, ty_i64, ty_i8, ty_int, ty_param, ty_path};
56 use syntax::ast::{ty_str, ty_u, ty_u16, ty_u32, ty_u64, ty_u8, ty_uint};
57 use syntax::ast::{type_value_ns, ty_param_bound, unnamed_field};
58 use syntax::ast::{variant, view_item, view_item_extern_mod};
59 use syntax::ast::{view_item_use, view_path_glob, view_path_list};
60 use syntax::ast::{view_path_simple, visibility, anonymous, named, not};
61 use syntax::ast::{unsafe_fn};
62 use syntax::ast_util::{def_id_of_def, local_def};
63 use syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
64 use syntax::ast_util::{Privacy, Public, Private};
65 use syntax::ast_util::{variant_visibility_to_privacy, visibility_to_privacy};
66 use syntax::attr::{attr_metas, contains_name, attrs_contains_name};
67 use syntax::parse::token::ident_interner;
68 use syntax::parse::token::special_idents;
69 use syntax::print::pprust::{pat_to_str, path_to_str};
70 use syntax::codemap::{span, dummy_sp};
71 use syntax::visit::{default_visitor, fk_method, mk_vt, Visitor, visit_block};
72 use syntax::visit::{visit_crate, visit_expr, visit_expr_opt, visit_fn};
73 use syntax::visit::{visit_foreign_item, visit_item, visit_method_helper};
74 use syntax::visit::{visit_mod, visit_ty, vt};
78 use option::{Some, get, is_some, is_none};
79 use str::{connect, split_str};
82 use std::list::{Cons, List, Nil};
83 use std::oldmap::HashMap;
87 pub type DefMap = HashMap<node_id,def>;
89 pub struct binding_info {
91 binding_mode: binding_mode,
94 // Map from the name in a pattern to its binding mode.
95 pub type BindingMap = HashMap<ident,binding_info>;
97 // Implementation resolution
99 // FIXME #4946: This kind of duplicates information kept in
100 // ty::method. Maybe it should go away.
102 pub type MethodInfo = {
109 pub type Impl = { did: def_id, ident: ident, methods: ~[@MethodInfo] };
111 // Trait method resolution
112 pub type TraitMap = @HashMap<node_id,@DVec<def_id>>;
114 // This is the replacement export map. It maps a module to all of the exports
116 pub type ExportMap2 = HashMap<node_id, ~[Export2]>;
119 name: ~str, // The name of the target.
120 def_id: def_id, // The definition of the target.
121 reexport: bool, // Whether this is a reexport.
125 pub enum PatternBindingMode {
127 LocalIrrefutableMode,
128 ArgumentIrrefutableMode(mode)
136 /// A NamespaceResult represents the result of resolving an import in
137 /// a particular namespace. The result is either definitely-resolved,
138 /// definitely- unresolved, or unknown.
139 pub enum NamespaceResult {
140 /// Means that resolve hasn't gathered enough information yet to determine
141 /// whether the name is bound in this namespace. (That is, it hasn't
142 /// resolved all `use` directives yet.)
144 /// Means that resolve has determined that the name is definitely
145 /// not bound in the namespace.
147 /// Means that resolve has determined that the name is bound in the Module
148 /// argument, and specified by the NameBindings argument.
149 BoundResult(@Module, @mut NameBindings)
152 pub impl NamespaceResult {
153 pure fn is_unknown() -> bool {
155 UnknownResult => true,
161 pub enum NameDefinition {
162 NoNameDefinition, //< The name was unbound.
163 ChildNameDefinition(def), //< The name identifies an immediate child.
164 ImportNameDefinition(def) //< The name identifies an import.
169 pub enum Mutability {
174 pub enum SelfBinding {
176 HasSelfBinding(node_id, bool /* is implicit */)
179 pub type ResolveVisitor = vt<()>;
182 pub enum ImportDirectiveNS {
187 /// Contains data for specific types of import directives.
188 pub enum ImportDirectiveSubclass {
189 SingleImport(ident /* target */, ident /* source */, ImportDirectiveNS),
193 /// The context that we thread through while building the reduced graph.
194 pub enum ReducedGraphParent {
195 ModuleReducedGraphParent(@Module)
198 pub enum ResolveResult<T> {
199 Failed, // Failed to resolve the name.
200 Indeterminate, // Couldn't determine due to unresolved globs.
201 Success(T) // Successfully resolved the import.
204 pub impl<T> ResolveResult<T> {
205 fn failed() -> bool {
206 match self { Failed => true, _ => false }
208 fn indeterminate() -> bool {
209 match self { Indeterminate => true, _ => false }
213 pub enum TypeParameters/& {
214 NoTypeParameters, //< No type parameters.
215 HasTypeParameters(&~[ty_param], //< Type parameters.
216 node_id, //< ID of the enclosing item
218 // The index to start numbering the type parameters at.
219 // This is zero if this is the outermost set of type
220 // parameters, or equal to the number of outer type
221 // parameters. For example, if we have:
224 // fn method<U>() { ... }
227 // The index at the method site will be 1, because the
228 // outer T had index 0.
232 // The kind of the rib used for type parameters.
236 // The rib kind controls the translation of argument or local definitions
237 // (`def_arg` or `def_local`) to upvars (`def_upvar`).
240 // No translation needs to be applied.
243 // We passed through a function scope at the given node ID. Translate
244 // upvars as appropriate.
245 FunctionRibKind(node_id /* func id */, node_id /* body id */),
247 // We passed through an impl or trait and are now in one of its
248 // methods. Allow references to ty params that that impl or trait
249 // binds. Disallow any other upvars (including other ty params that are
251 // parent; method itself
252 MethodRibKind(node_id, MethodSort),
254 // We passed through a function *item* scope. Disallow upvars.
255 OpaqueFunctionRibKind,
257 // We're in a constant item. Can't refer to dynamic stuff.
261 // Methods can be required or provided. Required methods only occur in traits.
262 pub enum MethodSort {
267 // The X-ray flag indicates that a context has the X-ray privilege, which
268 // allows it to reference private names. Currently, this is used for the test
271 // FIXME #4947: The X-ray flag is kind of questionable in the first
272 // place. It might be better to introduce an expr_xray_path instead.
276 NoXray, //< Private items cannot be accessed.
277 Xray //< Private items can be accessed.
280 pub enum UseLexicalScopeFlag {
285 pub enum SearchThroughModulesFlag {
286 DontSearchThroughModules,
290 pub enum ModulePrefixResult {
292 PrefixFound(@Module, uint)
296 pub enum AllowCapturingSelfFlag {
297 AllowCapturingSelf, //< The "self" definition can be captured.
298 DontAllowCapturingSelf, //< The "self" definition cannot be captured.
301 pub enum BareIdentifierPatternResolution {
302 FoundStructOrEnumVariant(def),
304 BareIdentifierPatternUnresolved
307 // Specifies how duplicates should be handled when adding a child item if
308 // another item exists with the same name in some namespace.
310 pub enum DuplicateCheckingMode {
311 ForbidDuplicateModules,
312 ForbidDuplicateTypes,
313 ForbidDuplicateValues,
314 ForbidDuplicateTypesAndValues,
318 // Returns the namespace associated with the given duplicate checking mode,
319 // or fails for OverwriteDuplicates. This is used for error messages.
320 pub fn namespace_for_duplicate_checking_mode(mode: DuplicateCheckingMode)
323 ForbidDuplicateModules | ForbidDuplicateTypes |
324 ForbidDuplicateTypesAndValues => TypeNS,
325 ForbidDuplicateValues => ValueNS,
326 OverwriteDuplicates => fail!(~"OverwriteDuplicates has no namespace")
332 bindings: HashMap<ident,def_like>,
336 pub fn Rib(kind: RibKind) -> Rib {
344 /// One import directive.
345 pub struct ImportDirective {
347 module_path: @DVec<ident>,
348 subclass: @ImportDirectiveSubclass,
352 pub fn ImportDirective(privacy: Privacy,
353 module_path: @DVec<ident>,
354 subclass: @ImportDirectiveSubclass,
359 module_path: module_path,
365 /// The item that an import resolves to.
367 target_module: @Module,
368 bindings: @mut NameBindings,
371 pub fn Target(target_module: @Module, bindings: @mut NameBindings) -> Target {
373 target_module: target_module,
378 /// An ImportResolution represents a particular `use` directive.
379 pub struct ImportResolution {
380 /// The privacy of this `use` directive (whether it's `use` or
385 // The number of outstanding references to this name. When this reaches
386 // zero, outside modules can count on the targets being correct. Before
387 // then, all bets are off; future imports could override this name.
389 outstanding_references: uint,
391 /// The value that this `use` directive names, if there is one.
392 value_target: Option<Target>,
393 /// The type that this `use` directive names, if there is one.
394 type_target: Option<Target>,
396 /// There exists one state per import statement
397 state: @mut ImportState,
400 pub fn ImportResolution(privacy: Privacy,
402 state: @mut ImportState) -> ImportResolution {
406 outstanding_references: 0,
413 pub impl ImportResolution {
414 fn target_for_namespace(namespace: Namespace) -> Option<Target> {
416 TypeNS => return copy self.type_target,
417 ValueNS => return copy self.value_target
422 pub struct ImportState {
427 pub fn ImportState() -> ImportState {
428 ImportState{ used: false, warned: false }
431 /// The link from a module up to its nearest parent node.
432 pub enum ParentLink {
434 ModuleParentLink(@Module, ident),
435 BlockParentLink(@Module, node_id)
438 /// The type of module this is.
439 pub enum ModuleKind {
446 /// One node in the tree of modules.
448 parent_link: ParentLink,
449 mut def_id: Option<def_id>,
452 children: HashMap<ident,@mut NameBindings>,
453 imports: DVec<@ImportDirective>,
455 // The anonymous children of this node. Anonymous children are pseudo-
456 // modules that are implicitly created around items contained within
459 // For example, if we have this:
467 // There will be an anonymous module created around `g` with the ID of the
468 // entry block for `f`.
470 anonymous_children: HashMap<node_id,@Module>,
472 // The status of resolving each import in this module.
473 import_resolutions: HashMap<ident,@mut ImportResolution>,
475 // The number of unresolved globs that this module exports.
476 mut glob_count: uint,
478 // The index of the import we're resolving.
479 mut resolved_import_count: uint,
482 pub fn Module(parent_link: ParentLink,
483 def_id: Option<def_id>,
487 parent_link: parent_link,
492 anonymous_children: HashMap(),
493 import_resolutions: HashMap(),
495 resolved_import_count: 0
500 fn all_imports_resolved() -> bool {
501 return self.imports.len() == self.resolved_import_count;
505 pub fn unused_import_lint_level(session: Session) -> level {
506 for session.opts.lint_opts.each |lint_option_pair| {
507 let (lint_type, lint_level) = *lint_option_pair;
508 if lint_type == unused_imports {
515 // Records a possibly-private type definition.
516 pub struct TypeNsDef {
518 module_def: Option<@Module>,
519 type_def: Option<def>
522 // Records a possibly-private value definition.
523 pub struct ValueNsDef {
528 // Records the definitions (at most one for each namespace) that a name is
530 pub struct NameBindings {
531 type_def: Option<TypeNsDef>, //< Meaning in type namespace.
532 value_def: Option<ValueNsDef>, //< Meaning in value namespace.
534 // For error reporting
535 // FIXME (#3783): Merge me into TypeNsDef and ValueNsDef.
536 type_span: Option<span>,
537 value_span: Option<span>,
540 pub impl NameBindings {
541 /// Creates a new module in this set of name bindings.
542 fn define_module(@mut self,
544 parent_link: ParentLink,
545 def_id: Option<def_id>,
548 // Merges the module with the existing type def or creates a new one.
549 let module_ = @Module(parent_link, def_id, kind);
550 match self.type_def {
552 self.type_def = Some(TypeNsDef {
554 module_def: Some(module_),
558 Some(copy type_def) => {
559 self.type_def = Some(TypeNsDef {
561 module_def: Some(module_),
566 self.type_span = Some(sp);
569 /// Records a type definition.
570 fn define_type(@mut self, privacy: Privacy, def: def, sp: span) {
571 // Merges the type with the existing type def or creates a new one.
572 match self.type_def {
574 self.type_def = Some(TypeNsDef {
580 Some(copy type_def) => {
581 self.type_def = Some(TypeNsDef {
588 self.type_span = Some(sp);
591 /// Records a value definition.
592 fn define_value(@mut self, privacy: Privacy, def: def, sp: span) {
593 self.value_def = Some(ValueNsDef { privacy: privacy, def: def });
594 self.value_span = Some(sp);
597 /// Returns the module node if applicable.
598 fn get_module_if_available() -> Option<@Module> {
599 match self.type_def {
600 Some(ref type_def) => (*type_def).module_def,
606 * Returns the module node. Fails if this node does not have a module
609 fn get_module(@mut self) -> @Module {
610 match self.get_module_if_available() {
612 fail!(~"get_module called on a node with no module \
615 Some(module_def) => module_def
619 fn defined_in_namespace(namespace: Namespace) -> bool {
621 TypeNS => return self.type_def.is_some(),
622 ValueNS => return self.value_def.is_some()
626 fn def_for_namespace(namespace: Namespace) -> Option<def> {
629 match self.type_def {
631 Some(ref type_def) => {
632 // FIXME (#3784): This is reallllly questionable.
633 // Perhaps the right thing to do is to merge def_mod
635 match (*type_def).type_def {
636 Some(type_def) => Some(type_def),
638 match (*type_def).module_def {
639 Some(module_def) => {
640 module_def.def_id.map(|def_id|
651 match self.value_def {
653 Some(value_def) => Some(value_def.def)
659 fn privacy_for_namespace(namespace: Namespace) -> Option<Privacy> {
662 match self.type_def {
664 Some(ref type_def) => Some((*type_def).privacy)
668 match self.value_def {
670 Some(value_def) => Some(value_def.privacy)
676 fn span_for_namespace(namespace: Namespace) -> Option<span> {
677 if self.defined_in_namespace(namespace) {
679 TypeNS => self.type_span,
680 ValueNS => self.value_span,
688 pub fn NameBindings() -> NameBindings {
697 /// Interns the names of the primitive types.
698 pub struct PrimitiveTypeTable {
699 primitive_types: HashMap<ident,prim_ty>,
702 pub impl PrimitiveTypeTable {
703 fn intern(intr: @ident_interner, string: @~str,
704 primitive_type: prim_ty) {
705 let ident = intr.intern(string);
706 self.primitive_types.insert(ident, primitive_type);
710 pub fn PrimitiveTypeTable(intr: @ident_interner) -> PrimitiveTypeTable {
711 let table = PrimitiveTypeTable {
712 primitive_types: HashMap()
715 table.intern(intr, @~"bool", ty_bool);
716 table.intern(intr, @~"char", ty_int(ty_char));
717 table.intern(intr, @~"float", ty_float(ty_f));
718 table.intern(intr, @~"f32", ty_float(ty_f32));
719 table.intern(intr, @~"f64", ty_float(ty_f64));
720 table.intern(intr, @~"int", ty_int(ty_i));
721 table.intern(intr, @~"i8", ty_int(ty_i8));
722 table.intern(intr, @~"i16", ty_int(ty_i16));
723 table.intern(intr, @~"i32", ty_int(ty_i32));
724 table.intern(intr, @~"i64", ty_int(ty_i64));
725 table.intern(intr, @~"str", ty_str);
726 table.intern(intr, @~"uint", ty_uint(ty_u));
727 table.intern(intr, @~"u8", ty_uint(ty_u8));
728 table.intern(intr, @~"u16", ty_uint(ty_u16));
729 table.intern(intr, @~"u32", ty_uint(ty_u32));
730 table.intern(intr, @~"u64", ty_uint(ty_u64));
736 pub fn namespace_to_str(ns: Namespace) -> ~str {
743 pub fn Resolver(session: Session,
744 lang_items: LanguageItems,
747 let graph_root = @mut NameBindings();
749 graph_root.define_module(Public,
751 Some(def_id { crate: 0, node: 0 }),
755 let current_module = graph_root.get_module();
757 let self = Resolver {
759 lang_items: copy lang_items,
762 // The outermost module has def ID 0; this is not reflected in the
765 graph_root: graph_root,
767 unused_import_lint_level: unused_import_lint_level(session),
769 trait_info: HashMap(),
772 unresolved_imports: 0,
774 current_module: current_module,
779 xray_context: NoXray,
780 current_trait_refs: None,
782 self_ident: special_idents::self_,
783 type_self_ident: special_idents::type_self,
785 primitive_type_table: @PrimitiveTypeTable(session.
786 parse_sess.interner),
788 namespaces: ~[ TypeNS, ValueNS ],
794 export_map2: HashMap(),
795 trait_map: @HashMap(),
803 /// The main resolver class.
804 pub struct Resolver {
806 lang_items: LanguageItems,
809 intr: @ident_interner,
811 graph_root: @mut NameBindings,
813 unused_import_lint_level: level,
815 trait_info: HashMap<def_id,@HashMap<ident,()>>,
816 structs: HashMap<def_id,()>,
818 // The number of imports that are currently unresolved.
819 mut unresolved_imports: uint,
821 // The module that represents the current item scope.
822 mut current_module: @Module,
824 // The current set of local scopes, for values.
825 // FIXME #4948: Reuse ribs to avoid allocation.
826 value_ribs: @DVec<@Rib>,
828 // The current set of local scopes, for types.
829 type_ribs: @DVec<@Rib>,
831 // The current set of local scopes, for labels.
832 label_ribs: @DVec<@Rib>,
834 // Whether the current context is an X-ray context. An X-ray context is
835 // allowed to access private names of any module.
836 mut xray_context: XrayFlag,
838 // The trait that the current context can refer to.
839 mut current_trait_refs: Option<@DVec<def_id>>,
841 // The ident for the keyword "self".
843 // The ident for the non-keyword "Self".
844 type_self_ident: ident,
846 // The idents for the primitive types.
847 primitive_type_table: @PrimitiveTypeTable,
849 // The four namespaces.
850 namespaces: ~[Namespace],
852 // The function that has attribute named 'main'
853 mut attr_main_fn: Option<(node_id, span)>,
854 // The functions named 'main'
855 mut main_fns: ~[Option<(node_id, span)>],
858 export_map2: ExportMap2,
863 /// The main name resolution procedure.
864 fn resolve(@self, this: @Resolver) {
865 self.build_reduced_graph(this);
866 self.session.abort_if_errors();
868 self.resolve_imports();
869 self.session.abort_if_errors();
871 self.record_exports();
872 self.session.abort_if_errors();
874 self.resolve_crate();
875 self.session.abort_if_errors();
877 self.check_duplicate_main();
878 self.check_for_unused_imports_if_necessary();
882 // Reduced graph building
884 // Here we build the "reduced graph": the graph of the module tree without
885 // any imports resolved.
888 /// Constructs the reduced graph for the entire crate.
889 fn build_reduced_graph(this: @Resolver) {
891 ModuleReducedGraphParent(self.graph_root.get_module());
892 visit_crate(*self.crate, initial_parent, mk_vt(@Visitor {
893 visit_item: |item, context, visitor|
894 (*this).build_reduced_graph_for_item(item, context, visitor),
896 visit_foreign_item: |foreign_item, context, visitor|
897 (*this).build_reduced_graph_for_foreign_item(foreign_item,
901 visit_view_item: |view_item, context, visitor|
902 (*this).build_reduced_graph_for_view_item(view_item,
906 visit_block: |block, context, visitor|
907 (*this).build_reduced_graph_for_block(block,
911 .. *default_visitor()
915 /// Returns the current module tracked by the reduced graph parent.
916 fn get_module_from_parent(reduced_graph_parent: ReducedGraphParent)
918 match reduced_graph_parent {
919 ModuleReducedGraphParent(module_) => {
926 * Adds a new child item to the module definition of the parent node and
927 * returns its corresponding name bindings as well as the current parent.
928 * Or, if we're inside a block, creates (or reuses) an anonymous module
929 * corresponding to the innermost block ID and returns the name bindings
930 * as well as the newly-created parent.
932 * If this node does not have a module definition and we are not inside
935 fn add_child(name: ident,
936 reduced_graph_parent: ReducedGraphParent,
937 duplicate_checking_mode: DuplicateCheckingMode,
938 // For printing errors
940 -> (@mut NameBindings, ReducedGraphParent) {
942 // If this is the immediate descendant of a module, then we add the
943 // child name directly. Otherwise, we create or reuse an anonymous
944 // module and add the child to that.
947 match reduced_graph_parent {
948 ModuleReducedGraphParent(parent_module) => {
949 module_ = parent_module;
953 // Add or reuse the child.
954 let new_parent = ModuleReducedGraphParent(module_);
955 match module_.children.find(&name) {
957 let child = @mut NameBindings();
958 module_.children.insert(name, child);
959 return (child, new_parent);
962 // Enforce the duplicate checking mode. If we're requesting
963 // duplicate module checking, check that there isn't a module
964 // in the module with the same name. If we're requesting
965 // duplicate type checking, check that there isn't a type in
966 // the module with the same name. If we're requesting
967 // duplicate value checking, check that there isn't a value in
968 // the module with the same name. If we're requesting
969 // duplicate type checking and duplicate value checking, check
970 // that there isn't a duplicate type and a duplicate value
971 // with the same name. If no duplicate checking was requested
972 // at all, do nothing.
974 let mut is_duplicate = false;
975 match duplicate_checking_mode {
976 ForbidDuplicateModules => {
978 child.get_module_if_available().is_some();
980 ForbidDuplicateTypes => {
981 match child.def_for_namespace(TypeNS) {
982 Some(def_mod(_)) | None => {}
983 Some(_) => is_duplicate = true
986 ForbidDuplicateValues => {
987 is_duplicate = child.defined_in_namespace(ValueNS);
989 ForbidDuplicateTypesAndValues => {
990 match child.def_for_namespace(TypeNS) {
991 Some(def_mod(_)) | None => {}
992 Some(_) => is_duplicate = true
994 if child.defined_in_namespace(ValueNS) {
998 OverwriteDuplicates => {}
1000 if duplicate_checking_mode != OverwriteDuplicates &&
1002 // Return an error here by looking up the namespace that
1003 // had the duplicate.
1004 let ns = namespace_for_duplicate_checking_mode(
1005 duplicate_checking_mode);
1006 self.session.span_err(sp,
1007 fmt!("duplicate definition of %s %s",
1008 namespace_to_str(ns),
1009 self.session.str_of(name)));
1010 do child.span_for_namespace(ns).iter() |sp| {
1011 self.session.span_note(*sp,
1012 fmt!("first definition of %s %s here:",
1013 namespace_to_str(ns),
1014 self.session.str_of(name)));
1017 return (child, new_parent);
1022 fn block_needs_anonymous_module(block: blk) -> bool {
1023 // If the block has view items, we need an anonymous module.
1024 if block.node.view_items.len() > 0 {
1028 // Check each statement.
1029 for block.node.stmts.each |statement| {
1030 match statement.node {
1031 stmt_decl(declaration, _) => {
1032 match declaration.node {
1047 // If we found neither view items nor items, we don't need to create
1048 // an anonymous module.
1053 fn get_parent_link(parent: ReducedGraphParent,
1054 name: ident) -> ParentLink {
1056 ModuleReducedGraphParent(module_) => {
1057 return ModuleParentLink(module_, name);
1062 /// Constructs the reduced graph for one item.
1063 fn build_reduced_graph_for_item(item: @item,
1064 parent: ReducedGraphParent,
1065 &&visitor: vt<ReducedGraphParent>) {
1066 let ident = item.ident;
1068 let privacy = visibility_to_privacy(item.vis);
1070 match /*bad*/copy item.node {
1071 item_mod(module_) => {
1072 let (name_bindings, new_parent) =
1073 self.add_child(ident, parent, ForbidDuplicateModules, sp);
1075 let parent_link = self.get_parent_link(new_parent, ident);
1076 let def_id = def_id { crate: 0, node: item.id };
1077 name_bindings.define_module(privacy,
1084 ModuleReducedGraphParent(name_bindings.get_module());
1086 visit_mod(module_, sp, item.id, new_parent, visitor);
1089 item_foreign_mod(fm) => {
1090 let new_parent = match fm.sort {
1092 let (name_bindings, new_parent) =
1093 self.add_child(ident, parent,
1094 ForbidDuplicateModules, sp);
1096 let parent_link = self.get_parent_link(new_parent,
1098 let def_id = def_id { crate: 0, node: item.id };
1099 name_bindings.define_module(privacy,
1105 ModuleReducedGraphParent(name_bindings.get_module())
1108 // For anon foreign mods, the contents just go in the
1113 visit_item(item, new_parent, visitor);
1116 // These items live in the value namespace.
1118 let (name_bindings, _) =
1119 self.add_child(ident, parent, ForbidDuplicateValues, sp);
1121 name_bindings.define_value
1122 (privacy, def_const(local_def(item.id)), sp);
1124 item_fn(_, purity, _, _) => {
1125 let (name_bindings, new_parent) =
1126 self.add_child(ident, parent, ForbidDuplicateValues, sp);
1128 let def = def_fn(local_def(item.id), purity);
1129 name_bindings.define_value(privacy, def, sp);
1130 visit_item(item, new_parent, visitor);
1133 // These items live in the type namespace.
1135 let (name_bindings, _) =
1136 self.add_child(ident, parent, ForbidDuplicateTypes, sp);
1138 name_bindings.define_type
1139 (privacy, def_ty(local_def(item.id)), sp);
1142 item_enum(ref enum_definition, _) => {
1143 let (name_bindings, new_parent) =
1144 self.add_child(ident, parent, ForbidDuplicateTypes, sp);
1146 name_bindings.define_type
1147 (privacy, def_ty(local_def(item.id)), sp);
1149 for (*enum_definition).variants.each |variant| {
1150 self.build_reduced_graph_for_variant(*variant,
1152 // inherited => privacy of the enum item
1153 variant_visibility_to_privacy(variant.node.vis,
1160 // These items live in both the type and value namespaces.
1161 item_struct(struct_def, _) => {
1162 let (name_bindings, new_parent) =
1163 self.add_child(ident, parent, ForbidDuplicateTypes, sp);
1165 name_bindings.define_type(
1166 privacy, def_ty(local_def(item.id)), sp);
1168 // If this struct is tuple-like or enum-like, define a name
1169 // in the value namespace.
1170 match struct_def.ctor_id {
1173 name_bindings.define_value(
1175 def_struct(local_def(ctor_id)),
1180 // Record the def ID of this struct.
1181 self.structs.insert(local_def(item.id), ());
1183 visit_item(item, new_parent, visitor);
1186 item_impl(_, trait_ref_opt, ty, methods) => {
1187 // If this implements an anonymous trait and it has static
1188 // methods, then add all the static methods within to a new
1189 // module, if the type was defined within this module.
1191 // FIXME (#3785): This is quite unsatisfactory. Perhaps we
1192 // should modify anonymous traits to only be implementable in
1193 // the same module that declared the type.
1195 // Bail out early if there are no static methods.
1196 let mut has_static_methods = false;
1197 for methods.each |method| {
1198 match method.self_ty.node {
1199 sty_static => has_static_methods = true,
1204 // If there are static methods, then create the module
1206 match (trait_ref_opt, ty) {
1207 (None, @Ty { node: ty_path(path, _), _ }) if
1208 has_static_methods && path.idents.len() == 1 => {
1209 // Create the module.
1210 let name = path_to_ident(path);
1211 let (name_bindings, new_parent) =
1212 self.add_child(name,
1214 ForbidDuplicateModules,
1217 let parent_link = self.get_parent_link(new_parent,
1219 let def_id = local_def(item.id);
1220 name_bindings.define_module(Public,
1226 let new_parent = ModuleReducedGraphParent(
1227 name_bindings.get_module());
1229 // For each static method...
1230 for methods.each |method| {
1231 match method.self_ty.node {
1233 // Add the static method to the
1235 let ident = method.ident;
1236 let (method_name_bindings, _) =
1240 ForbidDuplicateValues,
1242 let def = def_fn(local_def(method.id),
1244 method_name_bindings.define_value(
1245 Public, def, method.span);
1254 visit_item(item, parent, visitor);
1257 item_trait(_, _, ref methods) => {
1258 let (name_bindings, new_parent) =
1259 self.add_child(ident, parent, ForbidDuplicateTypes, sp);
1261 // If the trait has static methods, then add all the static
1262 // methods within to a new module.
1264 // We only need to create the module if the trait has static
1265 // methods, so check that first.
1266 let mut has_static_methods = false;
1267 for (*methods).each |method| {
1268 let ty_m = trait_method_to_ty_method(*method);
1269 match ty_m.self_ty.node {
1271 has_static_methods = true;
1278 // Create the module if necessary.
1279 let module_parent_opt;
1280 if has_static_methods {
1281 let parent_link = self.get_parent_link(parent, ident);
1282 name_bindings.define_module(privacy,
1284 Some(local_def(item.id)),
1287 module_parent_opt = Some(ModuleReducedGraphParent(
1288 name_bindings.get_module()));
1290 module_parent_opt = None;
1293 // Add the names of all the methods to the trait info.
1294 let method_names = @HashMap();
1295 for (*methods).each |method| {
1296 let ty_m = trait_method_to_ty_method(*method);
1298 let ident = ty_m.ident;
1299 // Add it to the trait info if not static,
1300 // add it as a name in the trait module otherwise.
1301 match ty_m.self_ty.node {
1303 let def = def_static_method(
1305 Some(local_def(item.id)),
1308 let (method_name_bindings, _) =
1309 self.add_child(ident,
1310 module_parent_opt.get(),
1311 ForbidDuplicateValues,
1313 method_name_bindings.define_value(Public,
1318 method_names.insert(ident, ());
1323 let def_id = local_def(item.id);
1324 self.trait_info.insert(def_id, method_names);
1326 name_bindings.define_type(privacy, def_ty(def_id), sp);
1327 visit_item(item, new_parent, visitor);
1331 fail!(~"item macros unimplemented")
1336 // Constructs the reduced graph for one variant. Variants exist in the
1337 // type and/or value namespaces.
1338 fn build_reduced_graph_for_variant(variant: variant,
1340 +parent_privacy: Privacy,
1341 parent: ReducedGraphParent,
1342 &&visitor: vt<ReducedGraphParent>) {
1344 let ident = variant.node.name;
1345 let (child, _) = self.add_child(ident, parent, ForbidDuplicateValues,
1349 match variant.node.vis {
1350 public => privacy = Public,
1351 private => privacy = Private,
1352 inherited => privacy = parent_privacy
1355 match variant.node.kind {
1356 tuple_variant_kind(_) => {
1357 child.define_value(privacy,
1358 def_variant(item_id,
1359 local_def(variant.node.id)),
1362 struct_variant_kind(_) => {
1363 child.define_type(privacy,
1364 def_variant(item_id,
1365 local_def(variant.node.id)),
1367 self.structs.insert(local_def(variant.node.id), ());
1369 enum_variant_kind(ref enum_definition) => {
1370 child.define_type(privacy,
1371 def_ty(local_def(variant.node.id)),
1373 for (*enum_definition).variants.each |variant| {
1374 self.build_reduced_graph_for_variant(*variant, item_id,
1383 * Constructs the reduced graph for one 'view item'. View items consist
1384 * of imports and use directives.
1386 fn build_reduced_graph_for_view_item(view_item: @view_item,
1387 parent: ReducedGraphParent,
1388 &&_visitor: vt<ReducedGraphParent>) {
1389 let privacy = visibility_to_privacy(view_item.vis);
1390 match /*bad*/copy view_item.node {
1391 view_item_use(view_paths) => {
1392 for view_paths.each |view_path| {
1393 // Extract and intern the module part of the path. For
1394 // globs and lists, the path is found directly in the AST;
1395 // for simple paths we have to munge the path a little.
1397 let module_path = @DVec();
1398 match view_path.node {
1399 view_path_simple(_, full_path, _, _) => {
1400 let path_len = full_path.idents.len();
1401 assert path_len != 0;
1403 for full_path.idents.eachi |i, ident| {
1404 if i != path_len - 1 {
1405 (*module_path).push(*ident);
1410 view_path_glob(module_ident_path, _) |
1411 view_path_list(module_ident_path, _, _) => {
1412 for module_ident_path.idents.each |ident| {
1413 (*module_path).push(*ident);
1418 // Build up the import directives.
1419 let module_ = self.get_module_from_parent(parent);
1420 let state = @mut ImportState();
1421 match view_path.node {
1422 view_path_simple(binding, full_path, ns, _) => {
1424 module_ns => TypeNSOnly,
1425 type_value_ns => AnyNS
1428 let source_ident = full_path.idents.last();
1429 let subclass = @SingleImport(binding,
1432 self.build_import_directive(privacy,
1439 view_path_list(_, ref source_idents, _) => {
1440 for (*source_idents).each |source_ident| {
1441 let name = source_ident.node.name;
1442 let subclass = @SingleImport(name,
1445 self.build_import_directive(privacy,
1453 view_path_glob(_, _) => {
1454 self.build_import_directive(privacy,
1465 view_item_extern_mod(name, _, node_id) => {
1466 match find_extern_mod_stmt_cnum(self.session.cstore, node_id) {
1468 let (child_name_bindings, new_parent) =
1469 self.add_child(name, parent, ForbidDuplicateTypes,
1472 let def_id = def_id { crate: crate_id, node: 0 };
1473 let parent_link = ModuleParentLink
1474 (self.get_module_from_parent(new_parent), name);
1476 child_name_bindings.define_module(privacy,
1481 self.build_reduced_graph_for_external_crate
1482 (child_name_bindings.get_module());
1492 /// Constructs the reduced graph for one foreign item.
1493 fn build_reduced_graph_for_foreign_item(foreign_item: @foreign_item,
1494 parent: ReducedGraphParent,
1496 vt<ReducedGraphParent>) {
1498 let name = foreign_item.ident;
1499 let (name_bindings, new_parent) =
1500 self.add_child(name, parent, ForbidDuplicateValues,
1503 match /*bad*/copy foreign_item.node {
1504 foreign_item_fn(_, _, type_parameters) => {
1505 let def = def_fn(local_def(foreign_item.id), unsafe_fn);
1506 name_bindings.define_value(Public, def, foreign_item.span);
1508 do self.with_type_parameter_rib
1509 (HasTypeParameters(&type_parameters, foreign_item.id,
1510 0, NormalRibKind)) {
1511 visit_foreign_item(foreign_item, new_parent, visitor);
1514 foreign_item_const(*) => {
1515 let def = def_const(local_def(foreign_item.id));
1516 name_bindings.define_value(Public, def, foreign_item.span);
1518 visit_foreign_item(foreign_item, new_parent, visitor);
1523 fn build_reduced_graph_for_block(block: blk,
1524 parent: ReducedGraphParent,
1525 &&visitor: vt<ReducedGraphParent>) {
1528 if self.block_needs_anonymous_module(block) {
1529 let block_id = block.node.id;
1531 debug!("(building reduced graph for block) creating a new \
1532 anonymous module for block %d",
1535 let parent_module = self.get_module_from_parent(parent);
1536 let new_module = @Module(BlockParentLink(parent_module, block_id),
1538 AnonymousModuleKind);
1539 parent_module.anonymous_children.insert(block_id, new_module);
1540 new_parent = ModuleReducedGraphParent(new_module);
1542 new_parent = parent;
1545 visit_block(block, new_parent, visitor);
1548 fn handle_external_def(def: def,
1549 modules: HashMap<def_id, @Module>,
1550 child_name_bindings: @mut NameBindings,
1553 new_parent: ReducedGraphParent) {
1555 def_mod(def_id) | def_foreign_mod(def_id) => {
1556 match copy child_name_bindings.type_def {
1557 Some(TypeNsDef { module_def: Some(copy module_def), _ }) => {
1558 debug!("(building reduced graph for external crate) \
1559 already created module");
1560 module_def.def_id = Some(def_id);
1561 modules.insert(def_id, module_def);
1564 debug!("(building reduced graph for \
1565 external crate) building module \
1567 let parent_link = self.get_parent_link(new_parent, ident);
1569 match modules.find(&def_id) {
1571 child_name_bindings.define_module(Public,
1576 modules.insert(def_id,
1577 child_name_bindings.get_module());
1579 Some(existing_module) => {
1580 // Create an import resolution to
1581 // avoid creating cycles in the
1585 @mut ImportResolution(Public,
1587 @mut ImportState());
1588 resolution.outstanding_references = 0;
1590 match existing_module.parent_link {
1592 BlockParentLink(*) => {
1593 fail!(~"can't happen");
1595 ModuleParentLink(parent_module, ident) => {
1596 let name_bindings = parent_module.children.get(
1598 resolution.type_target =
1599 Some(Target(parent_module, name_bindings));
1603 debug!("(building reduced graph for external crate) \
1604 ... creating import resolution");
1606 new_parent.import_resolutions.insert(ident, resolution);
1612 def_fn(*) | def_static_method(*) | def_const(*) |
1614 debug!("(building reduced graph for external \
1615 crate) building value %s", final_ident);
1616 child_name_bindings.define_value(Public, def, dummy_sp());
1619 debug!("(building reduced graph for external \
1620 crate) building type %s", final_ident);
1622 // If this is a trait, add all the method names
1623 // to the trait info.
1625 match get_method_names_if_trait(self.session.cstore, def_id) {
1629 Some(method_names) => {
1630 let interned_method_names = @HashMap();
1631 for method_names.each |method_data| {
1632 let (method_name, self_ty) = *method_data;
1633 debug!("(building reduced graph for \
1634 external crate) ... adding \
1636 self.session.str_of(method_name));
1638 // Add it to the trait info if not static.
1639 if self_ty != sty_static {
1640 interned_method_names.insert(method_name, ());
1643 self.trait_info.insert(def_id, interned_method_names);
1647 child_name_bindings.define_type(Public, def, dummy_sp());
1649 def_struct(def_id) => {
1650 debug!("(building reduced graph for external \
1651 crate) building type %s",
1653 child_name_bindings.define_type(Public, def, dummy_sp());
1654 self.structs.insert(def_id, ());
1656 def_self(*) | def_arg(*) | def_local(*) |
1657 def_prim_ty(*) | def_ty_param(*) | def_binding(*) |
1658 def_use(*) | def_upvar(*) | def_region(*) |
1659 def_typaram_binder(*) | def_label(*) | def_self_ty(*) => {
1660 fail!(fmt!("didn't expect `%?`", def));
1666 * Builds the reduced graph rooted at the 'use' directive for an external
1669 fn build_reduced_graph_for_external_crate(root: @Module) {
1670 let modules = HashMap();
1672 // Create all the items reachable by paths.
1673 for each_path(self.session.cstore, root.def_id.get().crate)
1674 |path_string, def_like| {
1676 debug!("(building reduced graph for external crate) found path \
1678 path_string, def_like);
1680 let mut pieces = split_str(path_string, ~"::");
1681 let final_ident_str = pieces.pop();
1682 let final_ident = self.session.ident_of(final_ident_str);
1684 // Find the module we need, creating modules along the way if we
1687 let mut current_module = root;
1688 for pieces.each |ident_str| {
1689 let ident = self.session.ident_of(/*bad*/copy *ident_str);
1690 // Create or reuse a graph node for the child.
1691 let (child_name_bindings, new_parent) =
1692 self.add_child(ident,
1693 ModuleReducedGraphParent(current_module),
1694 OverwriteDuplicates,
1697 // Define or reuse the module node.
1698 match child_name_bindings.type_def {
1700 debug!("(building reduced graph for external crate) \
1701 autovivifying missing type def %s",
1703 let parent_link = self.get_parent_link(new_parent,
1705 child_name_bindings.define_module(Public,
1711 Some(copy type_ns_def)
1712 if type_ns_def.module_def.is_none() => {
1713 debug!("(building reduced graph for external crate) \
1714 autovivifying missing module def %s",
1716 let parent_link = self.get_parent_link(new_parent,
1718 child_name_bindings.define_module(Public,
1724 _ => {} // Fall through.
1727 current_module = child_name_bindings.get_module();
1732 // Add the new child item.
1733 let (child_name_bindings, new_parent) =
1734 self.add_child(final_ident,
1735 ModuleReducedGraphParent(
1737 OverwriteDuplicates,
1740 self.handle_external_def(def,
1742 child_name_bindings,
1743 self.session.str_of(final_ident),
1748 // We only process static methods of impls here.
1749 match get_type_name_if_impl(self.session.cstore, def) {
1751 Some(final_ident) => {
1752 let static_methods_opt =
1753 get_static_methods_if_impl(
1754 self.session.cstore, def);
1755 match static_methods_opt {
1756 Some(ref static_methods) if
1757 static_methods.len() >= 1 => {
1758 debug!("(building reduced graph for \
1759 external crate) processing \
1760 static methods for type name %s",
1761 self.session.str_of(final_ident));
1763 let (child_name_bindings, new_parent) =
1764 self.add_child(final_ident,
1765 ModuleReducedGraphParent(
1767 OverwriteDuplicates,
1770 // Process the static methods. First,
1771 // create the module.
1773 match copy child_name_bindings.type_def {
1775 module_def: Some(copy module_def),
1778 // We already have a module. This
1780 type_module = module_def;
1784 self.get_parent_link(
1785 new_parent, final_ident);
1786 child_name_bindings.define_module(
1793 child_name_bindings.
1798 // Add each static method to the module.
1799 let new_parent = ModuleReducedGraphParent(
1801 for static_methods.each
1802 |static_method_info| {
1803 let ident = static_method_info.ident;
1804 debug!("(building reduced graph for \
1805 external crate) creating \
1806 static method '%s'",
1807 self.session.str_of(ident));
1809 let (method_name_bindings, _) =
1813 OverwriteDuplicates,
1816 static_method_info.def_id,
1817 static_method_info.purity);
1818 method_name_bindings.define_value(
1819 Public, def, dummy_sp());
1823 // Otherwise, do nothing.
1824 Some(_) | None => {}
1830 debug!("(building reduced graph for external crate) \
1837 /// Creates and adds an import directive to the given module.
1838 fn build_import_directive(privacy: Privacy,
1840 module_path: @DVec<ident>,
1841 subclass: @ImportDirectiveSubclass,
1843 state: @mut ImportState) {
1844 let directive = @ImportDirective(privacy, module_path,
1846 module_.imports.push(directive);
1848 // Bump the reference count on the name. Or, if this is a glob, set
1849 // the appropriate flag.
1852 SingleImport(target, _, _) => {
1853 debug!("(building import directive) building import \
1854 directive: privacy %? %s::%s",
1856 self.idents_to_str(module_path.get()),
1857 self.session.str_of(target));
1859 match module_.import_resolutions.find(&target) {
1860 Some(resolution) => {
1861 debug!("(building import directive) bumping \
1863 resolution.outstanding_references += 1;
1866 debug!("(building import directive) creating new");
1867 let resolution = @mut ImportResolution(privacy,
1870 let name = self.idents_to_str(module_path.get());
1871 // Don't warn about unused intrinsics because they're
1872 // automatically appended to all files
1873 if name == ~"intrinsic::rusti" {
1874 resolution.state.warned = true;
1876 resolution.outstanding_references = 1;
1877 module_.import_resolutions.insert(target, resolution);
1882 // Set the glob flag. This tells us that we don't know the
1883 // module's exports ahead of time.
1885 module_.glob_count += 1;
1889 self.unresolved_imports += 1;
1892 // Import resolution
1894 // This is a fixed-point algorithm. We resolve imports until our efforts
1895 // are stymied by an unresolved import; then we bail out of the current
1896 // module and continue. We terminate successfully once no more imports
1897 // remain or unsuccessfully when no forward progress in resolving imports
1901 * Resolves all imports for the crate. This method performs the fixed-
1904 fn resolve_imports() {
1906 let mut prev_unresolved_imports = 0;
1908 debug!("(resolving imports) iteration %u, %u imports left",
1909 i, self.unresolved_imports);
1911 let module_root = self.graph_root.get_module();
1912 self.resolve_imports_for_module_subtree(module_root);
1914 if self.unresolved_imports == 0 {
1915 debug!("(resolving imports) success");
1919 if self.unresolved_imports == prev_unresolved_imports {
1920 self.session.err(~"failed to resolve imports");
1921 self.report_unresolved_imports(module_root);
1926 prev_unresolved_imports = self.unresolved_imports;
1931 * Attempts to resolve imports for the given module and all of its
1934 fn resolve_imports_for_module_subtree(module_: @Module) {
1935 debug!("(resolving imports for module subtree) resolving %s",
1936 self.module_to_str(module_));
1937 self.resolve_imports_for_module(module_);
1939 for module_.children.each_value |&child_node| {
1940 match child_node.get_module_if_available() {
1944 Some(child_module) => {
1945 self.resolve_imports_for_module_subtree(child_module);
1950 for module_.anonymous_children.each_value |&child_module| {
1951 self.resolve_imports_for_module_subtree(child_module);
1955 /// Attempts to resolve imports for the given module only.
1956 fn resolve_imports_for_module(module_: @Module) {
1957 if (*module_).all_imports_resolved() {
1958 debug!("(resolving imports for module) all imports resolved for \
1960 self.module_to_str(module_));
1964 let import_count = module_.imports.len();
1965 while module_.resolved_import_count < import_count {
1966 let import_index = module_.resolved_import_count;
1967 let import_directive = module_.imports.get_elt(import_index);
1968 match self.resolve_import_for_module(module_, import_directive) {
1970 // We presumably emitted an error. Continue.
1971 let idents = import_directive.module_path.get();
1972 let msg = fmt!("failed to resolve import: %s",
1973 self.import_path_to_str(idents,
1974 *import_directive.subclass));
1975 self.session.span_err(import_directive.span, msg);
1978 // Bail out. We'll come around next time.
1986 module_.resolved_import_count += 1;
1990 fn idents_to_str(idents: ~[ident]) -> ~str {
1991 let ident_strs = idents.map(|&ident| self.session.str_of(ident));
1992 return str::connect(ident_strs, "::");
1995 fn import_directive_subclass_to_str(subclass: ImportDirectiveSubclass)
1998 SingleImport(_target, source, _ns) => self.session.str_of(source),
2003 fn import_path_to_str(idents: ~[ident], subclass: ImportDirectiveSubclass)
2005 if idents.is_empty() {
2006 self.import_directive_subclass_to_str(subclass)
2009 self.idents_to_str(idents),
2010 self.import_directive_subclass_to_str(subclass))
2015 * Attempts to resolve the given import. The return value indicates
2016 * failure if we're certain the name does not exist, indeterminate if we
2017 * don't know whether the name exists at the moment due to other
2018 * currently-unresolved imports, or success if we know the name exists.
2019 * If successful, the resolved bindings are written into the module.
2021 fn resolve_import_for_module(module_: @Module,
2022 import_directive: @ImportDirective)
2023 -> ResolveResult<()> {
2025 let mut resolution_result;
2026 let module_path = import_directive.module_path;
2028 debug!("(resolving import for module) resolving import `%s::...` in \
2030 self.idents_to_str((*module_path).get()),
2031 self.module_to_str(module_));
2033 // One-level renaming imports of the form `import foo = bar;` are
2034 // handled specially.
2036 if (*module_path).len() == 0 {
2038 self.resolve_one_level_renaming_import(module_,
2041 // First, resolve the module path for the directive, if necessary.
2042 match self.resolve_module_path_for_import(module_,
2044 DontUseLexicalScope,
2045 import_directive.span) {
2048 resolution_result = Failed;
2051 resolution_result = Indeterminate;
2053 Success(containing_module) => {
2054 // We found the module that the target is contained
2055 // within. Attempt to resolve the import within it.
2057 match *import_directive.subclass {
2058 SingleImport(target, source, AnyNS) => {
2060 self.resolve_single_import(module_,
2065 SingleImport(target, source, TypeNSOnly) => {
2067 self.resolve_single_module_import
2068 (module_, containing_module, target,
2072 let span = import_directive.span;
2073 let p = import_directive.privacy;
2075 self.resolve_glob_import(p,
2085 // Decrement the count of unresolved imports.
2086 match resolution_result {
2088 assert self.unresolved_imports >= 1;
2089 self.unresolved_imports -= 1;
2092 // Nothing to do here; just return the error.
2096 // Decrement the count of unresolved globs if necessary. But only if
2097 // the resolution result is indeterminate -- otherwise we'll stop
2098 // processing imports here. (See the loop in
2099 // resolve_imports_for_module.)
2101 if !resolution_result.indeterminate() {
2102 match *import_directive.subclass {
2104 assert module_.glob_count >= 1;
2105 module_.glob_count -= 1;
2107 SingleImport(*) => {
2113 return resolution_result;
2116 fn resolve_single_import(module_: @Module,
2117 containing_module: @Module,
2120 -> ResolveResult<()> {
2122 debug!("(resolving single import) resolving `%s` = `%s::%s` from \
2124 self.session.str_of(target),
2125 self.module_to_str(containing_module),
2126 self.session.str_of(source),
2127 self.module_to_str(module_));
2129 // We need to resolve both namespaces for this to succeed.
2131 // FIXME #4949: See if there's some way of handling namespaces in
2132 // a more generic way. We have two of them; it seems worth
2135 let mut value_result = UnknownResult;
2136 let mut type_result = UnknownResult;
2138 // Search for direct children of the containing module.
2139 match containing_module.children.find(&source) {
2143 Some(child_name_bindings) => {
2144 if (*child_name_bindings).defined_in_namespace(ValueNS) {
2145 value_result = BoundResult(containing_module,
2146 child_name_bindings);
2148 if (*child_name_bindings).defined_in_namespace(TypeNS) {
2149 type_result = BoundResult(containing_module,
2150 child_name_bindings);
2155 // Unless we managed to find a result in both namespaces (unlikely),
2156 // search imports as well.
2157 match (value_result, type_result) {
2158 (BoundResult(*), BoundResult(*)) => {
2162 // If there is an unresolved glob at this point in the
2163 // containing module, bail out. We don't know enough to be
2164 // able to resolve this import.
2166 if containing_module.glob_count > 0 {
2167 debug!("(resolving single import) unresolved glob; \
2169 return Indeterminate;
2172 // Now search the exported imports within the containing
2175 match containing_module.import_resolutions.find(&source) {
2177 // The containing module definitely doesn't have an
2178 // exported import with the name in question. We can
2179 // therefore accurately report that the names are
2182 if value_result.is_unknown() {
2183 value_result = UnboundResult;
2185 if type_result.is_unknown() {
2186 type_result = UnboundResult;
2189 Some(import_resolution)
2190 if import_resolution.outstanding_references
2193 fn get_binding(import_resolution:
2194 @mut ImportResolution,
2195 namespace: Namespace)
2196 -> NamespaceResult {
2198 // Import resolutions must be declared with "pub"
2199 // in order to be exported.
2200 if import_resolution.privacy == Private {
2201 return UnboundResult;
2204 match (*import_resolution).
2205 target_for_namespace(namespace) {
2207 return UnboundResult;
2210 import_resolution.state.used = true;
2211 return BoundResult(target.target_module,
2217 // The name is an import which has been fully
2218 // resolved. We can, therefore, just follow it.
2219 if value_result.is_unknown() {
2220 value_result = get_binding(import_resolution,
2223 if type_result.is_unknown() {
2224 type_result = get_binding(import_resolution,
2229 // The import is unresolved. Bail out.
2230 debug!("(resolving single import) unresolved import; \
2232 return Indeterminate;
2238 // We've successfully resolved the import. Write the results in.
2239 assert module_.import_resolutions.contains_key(&target);
2240 let import_resolution = module_.import_resolutions.get(&target);
2242 match value_result {
2243 BoundResult(target_module, name_bindings) => {
2244 import_resolution.value_target =
2245 Some(Target(target_module, name_bindings));
2247 UnboundResult => { /* Continue. */ }
2249 fail!(~"value result should be known at this point");
2253 BoundResult(target_module, name_bindings) => {
2254 import_resolution.type_target =
2255 Some(Target(target_module, name_bindings));
2257 UnboundResult => { /* Continue. */ }
2259 fail!(~"type result should be known at this point");
2263 let i = import_resolution;
2264 match (i.value_target, i.type_target) {
2265 // If this name wasn't found in either namespace, it's definitely
2267 (None, None) => { return Failed; }
2268 // If it's private, it's also unresolved.
2269 (Some(t), None) | (None, Some(t)) => {
2270 match t.bindings.type_def {
2271 Some(ref type_def) => {
2272 if type_def.privacy == Private {
2278 match t.bindings.value_def {
2279 Some(ref value_def) => {
2280 if value_def.privacy == Private {
2287 // It's also an error if there's both a type and a value with this
2288 // name, but both are private
2289 (Some(val), Some(ty)) => {
2290 match (val.bindings.value_def, ty.bindings.value_def) {
2291 (Some(ref value_def), Some(ref type_def)) =>
2292 if value_def.privacy == Private
2293 && type_def.privacy == Private {
2301 assert import_resolution.outstanding_references >= 1;
2302 import_resolution.outstanding_references -= 1;
2304 debug!("(resolving single import) successfully resolved import");
2308 fn resolve_single_module_import(module_: @Module,
2309 containing_module: @Module,
2312 -> ResolveResult<()> {
2314 debug!("(resolving single module import) resolving `%s` = `%s::%s` \
2316 self.session.str_of(target),
2317 self.module_to_str(containing_module),
2318 self.session.str_of(source),
2319 self.module_to_str(module_));
2321 // We need to resolve the module namespace for this to succeed.
2322 let mut module_result = UnknownResult;
2324 // Search for direct children of the containing module.
2325 match containing_module.children.find(&source) {
2329 Some(child_name_bindings) => {
2330 if (*child_name_bindings).defined_in_namespace(TypeNS) {
2331 module_result = BoundResult(containing_module,
2332 child_name_bindings);
2337 // Unless we managed to find a result, search imports as well.
2338 match module_result {
2343 // If there is an unresolved glob at this point in the
2344 // containing module, bail out. We don't know enough to be
2345 // able to resolve this import.
2347 if containing_module.glob_count > 0 {
2348 debug!("(resolving single module import) unresolved \
2349 glob; bailing out");
2350 return Indeterminate;
2353 // Now search the exported imports within the containing
2355 match containing_module.import_resolutions.find(&source) {
2357 // The containing module definitely doesn't have an
2358 // exported import with the name in question. We can
2359 // therefore accurately report that the names are
2362 if module_result.is_unknown() {
2363 module_result = UnboundResult;
2366 Some(import_resolution)
2367 if import_resolution.outstanding_references
2369 // The name is an import which has been fully
2370 // resolved. We can, therefore, just follow it.
2372 if module_result.is_unknown() {
2373 match (*import_resolution).target_for_namespace(
2376 module_result = UnboundResult;
2379 import_resolution.state.used = true;
2380 module_result = BoundResult
2381 (target.target_module,
2388 // The import is unresolved. Bail out.
2389 debug!("(resolving single module import) unresolved \
2390 import; bailing out");
2391 return Indeterminate;
2397 // We've successfully resolved the import. Write the results in.
2398 assert module_.import_resolutions.contains_key(&target);
2399 let import_resolution = module_.import_resolutions.get(&target);
2401 match module_result {
2402 BoundResult(target_module, name_bindings) => {
2403 debug!("(resolving single import) found module binding");
2404 import_resolution.type_target =
2405 Some(Target(target_module, name_bindings));
2408 debug!("(resolving single import) didn't find module \
2412 fail!(~"module result should be known at this point");
2416 let i = import_resolution;
2417 if i.type_target.is_none() {
2418 // If this name wasn't found in the type namespace, it's
2419 // definitely unresolved.
2423 assert import_resolution.outstanding_references >= 1;
2424 import_resolution.outstanding_references -= 1;
2426 debug!("(resolving single module import) successfully resolved \
2433 * Resolves a glob import. Note that this function cannot fail; it either
2434 * succeeds or bails out (as importing * from an empty module or a module
2435 * that exports nothing is valid).
2437 fn resolve_glob_import(privacy: Privacy,
2439 containing_module: @Module,
2441 -> ResolveResult<()> {
2442 // This function works in a highly imperative manner; it eagerly adds
2443 // everything it can to the list of import resolutions of the module
2445 debug!("(resolving glob import) resolving %? glob import", privacy);
2446 let state = @mut ImportState();
2448 // We must bail out if the node has unresolved imports of any kind
2449 // (including globs).
2450 if !(*containing_module).all_imports_resolved() {
2451 debug!("(resolving glob import) target module has unresolved \
2452 imports; bailing out");
2453 return Indeterminate;
2456 assert containing_module.glob_count == 0;
2458 // Add all resolved imports from the containing module.
2459 for containing_module.import_resolutions.each
2460 |&ident, &target_import_resolution| {
2462 debug!("(resolving glob import) writing module resolution \
2464 is_none(&target_import_resolution.type_target),
2465 self.module_to_str(module_));
2467 // Here we merge two import resolutions.
2468 match module_.import_resolutions.find(&ident) {
2470 // Simple: just copy the old import resolution.
2471 let new_import_resolution =
2472 @mut ImportResolution(privacy,
2473 target_import_resolution.span,
2475 new_import_resolution.value_target =
2476 copy target_import_resolution.value_target;
2477 new_import_resolution.type_target =
2478 copy target_import_resolution.type_target;
2480 module_.import_resolutions.insert
2481 (ident, new_import_resolution);
2483 Some(dest_import_resolution) => {
2484 // Merge the two import resolutions at a finer-grained
2487 match copy target_import_resolution.value_target {
2491 Some(value_target) => {
2492 dest_import_resolution.value_target =
2493 Some(copy value_target);
2496 match copy target_import_resolution.type_target {
2500 Some(type_target) => {
2501 dest_import_resolution.type_target =
2502 Some(copy type_target);
2509 // Add all children from the containing module.
2510 for containing_module.children.each |&ident, &name_bindings| {
2511 let mut dest_import_resolution;
2512 match module_.import_resolutions.find(&ident) {
2514 // Create a new import resolution from this child.
2515 dest_import_resolution = @mut ImportResolution(privacy,
2518 module_.import_resolutions.insert
2519 (ident, dest_import_resolution);
2521 Some(existing_import_resolution) => {
2522 dest_import_resolution = existing_import_resolution;
2527 debug!("(resolving glob import) writing resolution `%s` in `%s` \
2528 to `%s`, privacy=%?",
2529 self.session.str_of(ident),
2530 self.module_to_str(containing_module),
2531 self.module_to_str(module_),
2532 dest_import_resolution.privacy);
2534 // Merge the child item into the import resolution.
2535 if (*name_bindings).defined_in_namespace(ValueNS) {
2536 debug!("(resolving glob import) ... for value target");
2537 dest_import_resolution.value_target =
2538 Some(Target(containing_module, name_bindings));
2540 if (*name_bindings).defined_in_namespace(TypeNS) {
2541 debug!("(resolving glob import) ... for type target");
2542 dest_import_resolution.type_target =
2543 Some(Target(containing_module, name_bindings));
2547 debug!("(resolving glob import) successfully resolved import");
2551 fn resolve_module_path_from_root(module_: @Module,
2552 module_path: @DVec<ident>,
2555 -> ResolveResult<@Module> {
2556 let mut search_module = module_;
2557 let mut index = index;
2558 let module_path_len = (*module_path).len();
2560 // Resolve the module part of the path. This does not involve looking
2561 // upward though scope chains; we simply resolve names directly in
2562 // modules as we go.
2564 while index < module_path_len {
2565 let name = (*module_path).get_elt(index);
2566 match self.resolve_name_in_module(search_module,
2571 self.session.span_err(span, ~"unresolved name");
2575 debug!("(resolving module path for import) module \
2576 resolution is indeterminate: %s",
2577 self.session.str_of(name));
2578 return Indeterminate;
2580 Success(target) => {
2581 // Check to see whether there are type bindings, and, if
2582 // so, whether there is a module within.
2583 match target.bindings.type_def {
2584 Some(copy type_def) => {
2585 match type_def.module_def {
2588 self.session.span_err(span,
2596 Some(copy module_def) => {
2597 search_module = module_def;
2602 // There are no type bindings at all.
2603 self.session.span_err(span,
2604 fmt!("not a module: %s",
2605 self.session.str_of(
2616 return Success(search_module);
2620 * Attempts to resolve the module part of an import directive or path
2621 * rooted at the given module.
2623 fn resolve_module_path_for_import(module_: @Module,
2624 module_path: @DVec<ident>,
2625 use_lexical_scope: UseLexicalScopeFlag,
2627 -> ResolveResult<@Module> {
2629 let module_path_len = (*module_path).len();
2630 assert module_path_len > 0;
2632 debug!("(resolving module path for import) processing `%s` rooted at \
2634 self.idents_to_str((*module_path).get()),
2635 self.module_to_str(module_));
2637 // Resolve the module prefix, if any.
2638 let module_prefix_result = self.resolve_module_prefix(module_,
2641 let mut search_module;
2642 let mut start_index;
2643 match module_prefix_result {
2645 self.session.span_err(span, ~"unresolved name");
2649 debug!("(resolving module path for import) indeterminate; \
2651 return Indeterminate;
2653 Success(NoPrefixFound) => {
2654 // There was no prefix, so we're considering the first element
2655 // of the path. How we handle this depends on whether we were
2656 // instructed to use lexical scope or not.
2657 match use_lexical_scope {
2658 DontUseLexicalScope => {
2659 // This is a crate-relative path. We will start the
2660 // resolution process at index zero.
2661 search_module = self.graph_root.get_module();
2664 UseLexicalScope => {
2665 // This is not a crate-relative path. We resolve the
2666 // first component of the path in the current lexical
2667 // scope and then proceed to resolve below that.
2668 let result = self.resolve_module_in_lexical_scope(
2670 module_path.get_elt(0));
2673 self.session.span_err(span,
2674 ~"unresolved name");
2678 debug!("(resolving module path for import) \
2679 indeterminate; bailing");
2680 return Indeterminate;
2682 Success(containing_module) => {
2683 search_module = containing_module;
2690 Success(PrefixFound(containing_module, index)) => {
2691 search_module = containing_module;
2692 start_index = index;
2696 return self.resolve_module_path_from_root(search_module,
2702 fn resolve_item_in_lexical_scope(module_: @Module,
2704 namespace: Namespace,
2705 search_through_modules:
2706 SearchThroughModulesFlag)
2707 -> ResolveResult<Target> {
2709 debug!("(resolving item in lexical scope) resolving `%s` in \
2710 namespace %? in `%s`",
2711 self.session.str_of(name),
2713 self.module_to_str(module_));
2715 // The current module node is handled specially. First, check for
2716 // its immediate children.
2718 match module_.children.find(&name) {
2720 if (*name_bindings).defined_in_namespace(namespace) => {
2721 return Success(Target(module_, name_bindings));
2723 Some(_) | None => { /* Not found; continue. */ }
2726 // Now check for its import directives. We don't have to have resolved
2727 // all its imports in the usual way; this is because chains of
2728 // adjacent import statements are processed as though they mutated the
2731 match module_.import_resolutions.find(&name) {
2733 // Not found; continue.
2735 Some(import_resolution) => {
2736 match (*import_resolution).target_for_namespace(namespace) {
2738 // Not found; continue.
2739 debug!("(resolving item in lexical scope) found \
2740 import resolution, but not in namespace %?",
2744 import_resolution.state.used = true;
2745 return Success(copy target);
2751 // Finally, proceed up the scope chain looking for parent modules.
2752 let mut search_module = module_;
2754 // Go to the next parent.
2755 match search_module.parent_link {
2757 // No more parents. This module was unresolved.
2758 debug!("(resolving item in lexical scope) unresolved \
2762 ModuleParentLink(parent_module_node, _) => {
2763 match search_through_modules {
2764 DontSearchThroughModules => {
2765 match search_module.kind {
2766 NormalModuleKind => {
2767 // We stop the search here.
2768 debug!("(resolving item in lexical \
2769 scope) unresolved module: not \
2770 searching through module \
2776 AnonymousModuleKind => {
2777 search_module = parent_module_node;
2781 SearchThroughModules => {
2782 search_module = parent_module_node;
2786 BlockParentLink(parent_module_node, _) => {
2787 search_module = parent_module_node;
2791 // Resolve the name in the parent module.
2792 match self.resolve_name_in_module(search_module,
2797 // Continue up the search chain.
2800 // We couldn't see through the higher scope because of an
2801 // unresolved import higher up. Bail.
2803 debug!("(resolving item in lexical scope) indeterminate \
2804 higher scope; bailing");
2805 return Indeterminate;
2807 Success(target) => {
2808 // We found the module.
2809 return Success(copy target);
2815 /** Resolves a module name in the current lexical scope. */
2816 fn resolve_module_in_lexical_scope(module_: @Module, name: ident)
2817 -> ResolveResult<@Module> {
2818 // If this module is an anonymous module, resolve the item in the
2819 // lexical scope. Otherwise, resolve the item from the crate root.
2820 let resolve_result = self.resolve_item_in_lexical_scope(
2821 module_, name, TypeNS, DontSearchThroughModules);
2822 match resolve_result {
2823 Success(target) => {
2824 match target.bindings.type_def {
2825 Some(ref type_def) => {
2826 match (*type_def).module_def {
2828 error!("!!! (resolving module in lexical \
2829 scope) module wasn't actually a \
2833 Some(module_def) => {
2834 return Success(module_def);
2839 error!("!!! (resolving module in lexical scope) module
2840 wasn't actually a module!");
2846 debug!("(resolving module in lexical scope) indeterminate; \
2848 return Indeterminate;
2851 debug!("(resolving module in lexical scope) failed to \
2859 * Returns the nearest normal module parent of the given module.
2861 fn get_nearest_normal_module_parent(module_: @Module) -> Option<@Module> {
2862 let mut module_ = module_;
2864 match module_.parent_link {
2865 NoParentLink => return None,
2866 ModuleParentLink(new_module, _) |
2867 BlockParentLink(new_module, _) => {
2868 match new_module.kind {
2869 NormalModuleKind => return Some(new_module),
2872 AnonymousModuleKind => module_ = new_module,
2880 * Returns the nearest normal module parent of the given module, or the
2881 * module itself if it is a normal module.
2883 fn get_nearest_normal_module_parent_or_self(module_: @Module) -> @Module {
2884 match module_.kind {
2885 NormalModuleKind => return module_,
2886 ExternModuleKind | TraitModuleKind | AnonymousModuleKind => {
2887 match self.get_nearest_normal_module_parent(module_) {
2889 Some(new_module) => new_module
2896 * Resolves a "module prefix". A module prefix is one of (a) `self::`;
2897 * (b) some chain of `super::`.
2899 fn resolve_module_prefix(module_: @Module,
2900 module_path: @DVec<ident>)
2901 -> ResolveResult<ModulePrefixResult> {
2902 let interner = self.session.parse_sess.interner;
2904 // Start at the current module if we see `self` or `super`, or at the
2905 // top of the crate otherwise.
2906 let mut containing_module;
2908 if *interner.get(module_path.get_elt(0)) == ~"self" {
2910 self.get_nearest_normal_module_parent_or_self(module_);
2912 } else if *interner.get(module_path.get_elt(0)) == ~"super" {
2914 self.get_nearest_normal_module_parent_or_self(module_);
2915 i = 0; // We'll handle `super` below.
2917 return Success(NoPrefixFound);
2920 // Now loop through all the `super`s we find.
2921 while i < module_path.len() &&
2922 *interner.get(module_path.get_elt(i)) == ~"super" {
2923 debug!("(resolving module prefix) resolving `super` at %s",
2924 self.module_to_str(containing_module));
2925 match self.get_nearest_normal_module_parent(containing_module) {
2926 None => return Failed,
2927 Some(new_module) => {
2928 containing_module = new_module;
2934 debug!("(resolving module prefix) finished resolving prefix at %s",
2935 self.module_to_str(containing_module));
2937 return Success(PrefixFound(containing_module, i));
2941 * Attempts to resolve the supplied name in the given module for the
2942 * given namespace. If successful, returns the target corresponding to
2945 fn resolve_name_in_module(module_: @Module,
2947 namespace: Namespace,
2949 -> ResolveResult<Target> {
2950 debug!("(resolving name in module) resolving `%s` in `%s`",
2951 self.session.str_of(name),
2952 self.module_to_str(module_));
2954 // First, check the direct children of the module.
2955 match module_.children.find(&name) {
2957 if (*name_bindings).defined_in_namespace(namespace) => {
2959 debug!("(resolving name in module) found node as child");
2960 return Success(Target(module_, name_bindings));
2967 // Next, check the module's imports. If the module has a glob and
2968 // globs were not allowed, then we bail out; we don't know its imports
2970 if !allow_globs && module_.glob_count > 0 {
2971 debug!("(resolving name in module) module has glob; bailing out");
2972 return Indeterminate;
2975 // Otherwise, we check the list of resolved imports.
2976 match module_.import_resolutions.find(&name) {
2977 Some(import_resolution) => {
2978 if import_resolution.outstanding_references != 0 {
2979 debug!("(resolving name in module) import unresolved; \
2981 return Indeterminate;
2984 match (*import_resolution).target_for_namespace(namespace) {
2986 debug!("(resolving name in module) name found, but \
2987 not in namespace %?",
2991 debug!("(resolving name in module) resolved to \
2993 import_resolution.state.used = true;
2994 return Success(copy target);
3003 // We're out of luck.
3004 debug!("(resolving name in module) failed to resolve %s",
3005 self.session.str_of(name));
3010 * Resolves a one-level renaming import of the kind `import foo = bar;`
3011 * This needs special handling, as, unlike all of the other imports, it
3012 * needs to look in the scope chain for modules and non-modules alike.
3014 fn resolve_one_level_renaming_import(module_: @Module,
3015 import_directive: @ImportDirective)
3016 -> ResolveResult<()> {
3018 let mut target_name;
3019 let mut source_name;
3020 let allowable_namespaces;
3021 match *import_directive.subclass {
3022 SingleImport(target, source, namespaces) => {
3023 target_name = target;
3024 source_name = source;
3025 allowable_namespaces = namespaces;
3028 fail!(~"found `import *`, which is invalid");
3032 debug!("(resolving one-level naming result) resolving import `%s` = \
3034 self.session.str_of(target_name),
3035 self.session.str_of(source_name),
3036 self.module_to_str(module_));
3038 // Find the matching items in the lexical scope chain for every
3039 // namespace. If any of them come back indeterminate, this entire
3040 // import is indeterminate.
3042 let mut module_result;
3043 debug!("(resolving one-level naming result) searching for module");
3044 match self.resolve_item_in_lexical_scope(module_,
3047 SearchThroughModules) {
3049 debug!("(resolving one-level renaming import) didn't find \
3051 module_result = None;
3054 debug!("(resolving one-level renaming import) module result \
3055 is indeterminate; bailing");
3056 return Indeterminate;
3058 Success(name_bindings) => {
3059 debug!("(resolving one-level renaming import) module result \
3061 module_result = Some(copy name_bindings);
3065 let mut value_result;
3066 let mut type_result;
3067 if allowable_namespaces == TypeNSOnly {
3068 value_result = None;
3071 debug!("(resolving one-level naming result) searching for value");
3072 match self.resolve_item_in_lexical_scope(module_,
3075 SearchThroughModules) {
3078 debug!("(resolving one-level renaming import) didn't \
3079 find value result");
3080 value_result = None;
3083 debug!("(resolving one-level renaming import) value \
3084 result is indeterminate; bailing");
3085 return Indeterminate;
3087 Success(name_bindings) => {
3088 debug!("(resolving one-level renaming import) value \
3090 value_result = Some(copy name_bindings);
3094 debug!("(resolving one-level naming result) searching for type");
3095 match self.resolve_item_in_lexical_scope(module_,
3098 SearchThroughModules) {
3101 debug!("(resolving one-level renaming import) didn't \
3106 debug!("(resolving one-level renaming import) type \
3107 result is indeterminate; bailing");
3108 return Indeterminate;
3110 Success(name_bindings) => {
3111 debug!("(resolving one-level renaming import) type \
3113 type_result = Some(copy name_bindings);
3119 // NB: This one results in effects that may be somewhat surprising. It
3123 // impl foo for ... { ... }
3125 // impl foo for ... { ... }
3126 // import bar = foo;
3131 // results in only A::B::foo being aliased to A::B::bar, not A::foo
3132 // *and* A::B::foo being aliased to A::B::bar.
3135 // If nothing at all was found, that's an error.
3136 if is_none(&module_result) &&
3137 is_none(&value_result) &&
3138 is_none(&type_result) {
3140 self.session.span_err(import_directive.span,
3141 ~"unresolved import");
3145 // Otherwise, proceed and write in the bindings.
3146 match module_.import_resolutions.find(&target_name) {
3148 fail!(~"(resolving one-level renaming import) reduced graph \
3149 construction or glob importing should have created the \
3150 import resolution name by now");
3152 Some(import_resolution) => {
3153 debug!("(resolving one-level renaming import) writing module \
3154 result %? for `%s` into `%s`",
3155 is_none(&module_result),
3156 self.session.str_of(target_name),
3157 self.module_to_str(module_));
3159 import_resolution.value_target = value_result;
3160 import_resolution.type_target = type_result;
3162 assert import_resolution.outstanding_references >= 1;
3163 import_resolution.outstanding_references -= 1;
3167 debug!("(resolving one-level renaming import) successfully resolved");
3171 fn report_unresolved_imports(module_: @Module) {
3172 let index = module_.resolved_import_count;
3173 let import_count = module_.imports.len();
3174 if index != import_count {
3175 self.session.span_err(module_.imports.get_elt(index).span,
3176 ~"unresolved import");
3179 // Descend into children and anonymous children.
3180 for module_.children.each_value |&child_node| {
3181 match child_node.get_module_if_available() {
3185 Some(child_module) => {
3186 self.report_unresolved_imports(child_module);
3191 for module_.anonymous_children.each_value |&module_| {
3192 self.report_unresolved_imports(module_);
3198 // This pass simply determines what all "export" keywords refer to and
3199 // writes the results into the export map.
3201 // FIXME #4953 This pass will be removed once exports change to per-item.
3202 // Then this operation can simply be performed as part of item (or import)
3205 fn record_exports() {
3206 let root_module = self.graph_root.get_module();
3207 self.record_exports_for_module_subtree(root_module);
3210 fn record_exports_for_module_subtree(module_: @Module) {
3211 // If this isn't a local crate, then bail out. We don't need to record
3212 // exports for nonlocal crates.
3214 match module_.def_id {
3215 Some(def_id) if def_id.crate == local_crate => {
3217 debug!("(recording exports for module subtree) recording \
3218 exports for local module");
3221 // Record exports for the root module.
3222 debug!("(recording exports for module subtree) recording \
3223 exports for root module");
3227 debug!("(recording exports for module subtree) not recording \
3229 self.module_to_str(module_));
3234 self.record_exports_for_module(module_);
3236 for module_.children.each_value |&child_name_bindings| {
3237 match child_name_bindings.get_module_if_available() {
3241 Some(child_module) => {
3242 self.record_exports_for_module_subtree(child_module);
3247 for module_.anonymous_children.each_value |&child_module| {
3248 self.record_exports_for_module_subtree(child_module);
3252 fn record_exports_for_module(module_: @Module) {
3253 let mut exports2 = ~[];
3255 self.add_exports_for_module(&mut exports2, module_);
3256 match copy module_.def_id {
3258 self.export_map2.insert(def_id.node, exports2);
3259 debug!("(computing exports) writing exports for %d (some)",
3267 fn add_exports_of_namebindings(exports2: &mut ~[Export2],
3269 namebindings: @mut NameBindings,
3272 match (namebindings.def_for_namespace(ns),
3273 namebindings.privacy_for_namespace(ns)) {
3274 (Some(d), Some(Public)) => {
3275 debug!("(computing exports) YES: %s '%s' => %?",
3276 if reexport { ~"reexport" } else { ~"export"},
3277 self.session.str_of(ident),
3279 exports2.push(Export2 {
3281 name: self.session.str_of(ident),
3282 def_id: def_id_of_def(d)
3285 (Some(_), Some(privacy)) => {
3286 debug!("(computing reexports) NO: privacy %?", privacy);
3289 debug!("(computing reexports) NO: %?, %?", d_opt, p_opt);
3294 fn add_exports_for_module(exports2: &mut ~[Export2], module_: @Module) {
3295 for module_.children.each |ident, namebindings| {
3296 debug!("(computing exports) maybe export '%s'",
3297 self.session.str_of(*ident));
3298 self.add_exports_of_namebindings(&mut *exports2,
3303 self.add_exports_of_namebindings(&mut *exports2,
3310 for module_.import_resolutions.each |ident, importresolution| {
3311 if importresolution.privacy != Public {
3312 debug!("(computing exports) not reexporting private `%s`",
3313 self.session.str_of(*ident));
3316 for [ TypeNS, ValueNS ].each |ns| {
3317 match importresolution.target_for_namespace(*ns) {
3319 debug!("(computing exports) maybe reexport '%s'",
3320 self.session.str_of(*ident));
3321 self.add_exports_of_namebindings(&mut *exports2,
3335 // We maintain a list of value ribs and type ribs.
3337 // Simultaneously, we keep track of the current position in the module
3338 // graph in the `current_module` pointer. When we go to resolve a name in
3339 // the value or type namespaces, we first look through all the ribs and
3340 // then query the module graph. When we resolve a name in the module
3341 // namespace, we can skip all the ribs (since nested modules are not
3342 // allowed within blocks in Rust) and jump straight to the current module
3345 // Named implementations are handled separately. When we find a method
3346 // call, we consult the module node to find all of the implementations in
3347 // scope. This information is lazily cached in the module node. We then
3348 // generate a fake "implementation scope" containing all the
3349 // implementations thus found, for compatibility with old resolve pass.
3351 fn with_scope(name: Option<ident>, f: fn()) {
3352 let orig_module = self.current_module;
3354 // Move down in the graph.
3360 match orig_module.children.find(&name) {
3362 debug!("!!! (with scope) didn't find `%s` in `%s`",
3363 self.session.str_of(name),
3364 self.module_to_str(orig_module));
3366 Some(name_bindings) => {
3367 match (*name_bindings).get_module_if_available() {
3369 debug!("!!! (with scope) didn't find module \
3371 self.session.str_of(name),
3372 self.module_to_str(orig_module));
3375 self.current_module = module_;
3385 self.current_module = orig_module;
3388 // Wraps the given definition in the appropriate number of `def_upvar`
3391 fn upvarify(ribs: @DVec<@Rib>, rib_index: uint, def_like: def_like,
3392 span: span, allow_capturing_self: AllowCapturingSelfFlag)
3393 -> Option<def_like> {
3396 let mut is_ty_param;
3399 dl_def(d @ def_local(*)) | dl_def(d @ def_upvar(*)) |
3400 dl_def(d @ def_arg(*)) | dl_def(d @ def_binding(*)) => {
3402 is_ty_param = false;
3404 dl_def(d @ def_ty_param(*)) => {
3408 dl_def(d @ def_self(*))
3409 if allow_capturing_self == DontAllowCapturingSelf => {
3411 is_ty_param = false;
3414 return Some(def_like);
3418 let mut rib_index = rib_index + 1;
3419 while rib_index < (*ribs).len() {
3420 let rib = (*ribs).get_elt(rib_index);
3423 // Nothing to do. Continue.
3425 FunctionRibKind(function_id, body_id) => {
3427 def = def_upvar(def_id_of_def(def).node,
3433 MethodRibKind(item_id, _) => {
3434 // If the def is a ty param, and came from the parent
3437 def_ty_param(did, _) if self.def_map.find(&did.node)
3438 == Some(def_typaram_binder(item_id)) => {
3443 // This was an attempt to access an upvar inside a
3444 // named function item. This is not allowed, so we
3447 self.session.span_err(
3449 ~"attempted dynamic environment-capture");
3451 // This was an attempt to use a type parameter outside
3454 self.session.span_err(span,
3455 ~"attempt to use a type \
3456 argument out of scope");
3463 OpaqueFunctionRibKind => {
3465 // This was an attempt to access an upvar inside a
3466 // named function item. This is not allowed, so we
3469 self.session.span_err(
3471 ~"attempted dynamic environment-capture");
3473 // This was an attempt to use a type parameter outside
3476 self.session.span_err(span,
3477 ~"attempt to use a type \
3478 argument out of scope");
3483 ConstantItemRibKind => {
3484 // Still doesn't deal with upvars
3485 self.session.span_err(span,
3486 ~"attempt to use a non-constant \
3487 value in a constant");
3495 return Some(dl_def(def));
3498 fn search_ribs(ribs: @DVec<@Rib>, name: ident, span: span,
3499 allow_capturing_self: AllowCapturingSelfFlag)
3500 -> Option<def_like> {
3502 // FIXME #4950: This should not use a while loop.
3503 // FIXME #4950: Try caching?
3505 let mut i = (*ribs).len();
3508 let rib = (*ribs).get_elt(i);
3509 match rib.bindings.find(&name) {
3511 return self.upvarify(ribs, i, def_like, span,
3512 allow_capturing_self);
3523 fn resolve_crate(@self) {
3524 debug!("(resolving crate) starting");
3526 visit_crate(*self.crate, (), mk_vt(@Visitor {
3527 visit_item: |item, _context, visitor|
3528 self.resolve_item(item, visitor),
3529 visit_arm: |arm, _context, visitor|
3530 self.resolve_arm(arm, visitor),
3531 visit_block: |block, _context, visitor|
3532 self.resolve_block(block, visitor),
3533 visit_expr: |expr, _context, visitor|
3534 self.resolve_expr(expr, visitor),
3535 visit_local: |local, _context, visitor|
3536 self.resolve_local(local, visitor),
3537 visit_ty: |ty, _context, visitor|
3538 self.resolve_type(ty, visitor),
3539 .. *default_visitor()
3543 fn resolve_item(item: @item, visitor: ResolveVisitor) {
3544 debug!("(resolving item) resolving %s",
3545 self.session.str_of(item.ident));
3547 // Items with the !resolve_unexported attribute are X-ray contexts.
3548 // This is used to allow the test runner to run unexported tests.
3549 let orig_xray_flag = self.xray_context;
3550 if contains_name(attr_metas(/*bad*/copy item.attrs),
3551 ~"!resolve_unexported") {
3552 self.xray_context = Xray;
3555 match /*bad*/copy item.node {
3557 // enum item: resolve all the variants' discrs,
3558 // then resolve the ty params
3559 item_enum(ref enum_def, ref type_parameters) => {
3561 for (*enum_def).variants.each() |variant| {
3562 do variant.node.disr_expr.iter() |dis_expr| {
3563 // resolve the discriminator expr
3565 self.with_constant_rib(|| {
3566 self.resolve_expr(*dis_expr, visitor);
3571 // n.b. the discr expr gets visted twice.
3572 // but maybe it's okay since the first time will signal an
3573 // error if there is one? -- tjc
3574 do self.with_type_parameter_rib(
3576 type_parameters, item.id, 0, NormalRibKind)) {
3577 visit_item(item, (), visitor);
3581 item_ty(_, type_parameters) => {
3582 do self.with_type_parameter_rib
3583 (HasTypeParameters(&type_parameters, item.id, 0,
3587 visit_item(item, (), visitor);
3591 item_impl(type_parameters,
3595 self.resolve_implementation(item.id,
3604 item_trait(ref type_parameters, ref traits, ref methods) => {
3605 // Create a new rib for the self type.
3606 let self_type_rib = @Rib(NormalRibKind);
3607 (*self.type_ribs).push(self_type_rib);
3608 self_type_rib.bindings.insert(self.type_self_ident,
3609 dl_def(def_self_ty(item.id)));
3611 // Create a new rib for the trait-wide type parameters.
3612 do self.with_type_parameter_rib
3613 (HasTypeParameters(type_parameters, item.id, 0,
3616 self.resolve_type_parameters(/*bad*/copy *type_parameters,
3619 // Resolve derived traits.
3620 for traits.each |trt| {
3621 match self.resolve_path(trt.path, TypeNS, true,
3624 self.session.span_err(trt.path.span,
3625 ~"attempt to derive a \
3626 nonexistent trait"),
3628 // Write a mapping from the trait ID to the
3629 // definition of the trait into the definition
3632 debug!("(resolving trait) found trait def: \
3635 self.record_def(trt.ref_id, def);
3640 for (*methods).each |method| {
3641 // Create a new rib for the method-specific type
3644 // FIXME #4951: Do we need a node ID here?
3647 required(ref ty_m) => {
3648 do self.with_type_parameter_rib
3649 (HasTypeParameters(&(*ty_m).tps,
3651 type_parameters.len(),
3652 MethodRibKind(item.id, Required))) {
3654 // Resolve the method-specific type
3656 self.resolve_type_parameters(
3657 /*bad*/copy (*ty_m).tps,
3660 for (*ty_m).decl.inputs.each |argument| {
3661 self.resolve_type(argument.ty, visitor);
3664 self.resolve_type(ty_m.decl.output, visitor);
3668 self.resolve_method(MethodRibKind(item.id,
3671 type_parameters.len(),
3678 (*self.type_ribs).pop();
3681 item_struct(struct_def, ty_params) => {
3682 self.resolve_struct(item.id,
3684 /*bad*/copy struct_def.fields,
3689 item_mod(module_) => {
3690 do self.with_scope(Some(item.ident)) {
3691 self.resolve_module(module_, item.span, item.ident,
3696 item_foreign_mod(foreign_module) => {
3697 do self.with_scope(Some(item.ident)) {
3698 for foreign_module.items.each |foreign_item| {
3699 match /*bad*/copy foreign_item.node {
3700 foreign_item_fn(_, _, type_parameters) => {
3701 do self.with_type_parameter_rib
3702 (HasTypeParameters(&type_parameters,
3705 OpaqueFunctionRibKind))
3708 visit_foreign_item(*foreign_item, (),
3712 foreign_item_const(_) => {
3713 visit_foreign_item(*foreign_item, (),
3721 item_fn(ref fn_decl, _, ref ty_params, ref block) => {
3722 // If this is the main function, we must record it in the
3724 // FIXME #4404 android JNI hacks
3725 if !*self.session.building_library ||
3726 self.session.targ_cfg.os == session::os_android {
3728 if self.attr_main_fn.is_none() &&
3729 item.ident == special_idents::main {
3731 self.main_fns.push(Some((item.id, item.span)));
3734 if attrs_contains_name(item.attrs, ~"main") {
3735 if self.attr_main_fn.is_none() {
3736 self.attr_main_fn = Some((item.id, item.span));
3738 self.session.span_err(
3740 ~"multiple 'main' functions");
3745 self.resolve_function(OpaqueFunctionRibKind,
3746 Some(@/*bad*/copy *fn_decl),
3751 OpaqueFunctionRibKind),
3758 self.with_constant_rib(|| {
3759 visit_item(item, (), visitor);
3764 fail!(~"item macros unimplemented")
3768 self.xray_context = orig_xray_flag;
3771 fn with_type_parameter_rib(type_parameters: TypeParameters, f: fn()) {
3772 match type_parameters {
3773 HasTypeParameters(type_parameters, node_id, initial_index,
3776 let function_type_rib = @Rib(rib_kind);
3777 (*self.type_ribs).push(function_type_rib);
3779 for (*type_parameters).eachi |index, type_parameter| {
3780 let name = type_parameter.ident;
3781 debug!("with_type_parameter_rib: %d %d", node_id,
3783 let def_like = dl_def(def_ty_param
3784 (local_def(type_parameter.id),
3785 index + initial_index));
3786 // Associate this type parameter with
3787 // the item that bound it
3788 self.record_def(type_parameter.id,
3789 def_typaram_binder(node_id));
3790 (*function_type_rib).bindings.insert(name, def_like);
3794 NoTypeParameters => {
3801 match type_parameters {
3802 HasTypeParameters(*) => {
3803 (*self.type_ribs).pop();
3806 NoTypeParameters => {
3812 fn with_label_rib(f: fn()) {
3813 (*self.label_ribs).push(@Rib(NormalRibKind));
3815 (*self.label_ribs).pop();
3817 fn with_constant_rib(f: fn()) {
3818 (*self.value_ribs).push(@Rib(ConstantItemRibKind));
3820 (*self.value_ribs).pop();
3824 fn resolve_function(rib_kind: RibKind,
3825 optional_declaration: Option<@fn_decl>,
3826 type_parameters: TypeParameters,
3828 self_binding: SelfBinding,
3829 visitor: ResolveVisitor) {
3830 // Create a value rib for the function.
3831 let function_value_rib = @Rib(rib_kind);
3832 (*self.value_ribs).push(function_value_rib);
3834 // Create a label rib for the function.
3835 let function_label_rib = @Rib(rib_kind);
3836 (*self.label_ribs).push(function_label_rib);
3838 // If this function has type parameters, add them now.
3839 do self.with_type_parameter_rib(type_parameters) {
3840 // Resolve the type parameters.
3841 match type_parameters {
3842 NoTypeParameters => {
3845 HasTypeParameters(type_parameters, _, _, _) => {
3846 self.resolve_type_parameters(/*bad*/copy *type_parameters,
3851 // Add self to the rib, if necessary.
3852 match self_binding {
3856 HasSelfBinding(self_node_id, is_implicit) => {
3857 let def_like = dl_def(def_self(self_node_id,
3859 (*function_value_rib).bindings.insert(self.self_ident,
3864 // Add each argument to the rib.
3865 match optional_declaration {
3869 Some(declaration) => {
3870 for declaration.inputs.each |argument| {
3872 ArgumentIrrefutableMode(argument.mode);
3874 if argument.is_mutbl {Mutable} else {Immutable};
3875 self.resolve_pattern(argument.pat,
3881 self.resolve_type(argument.ty, visitor);
3883 debug!("(resolving function) recorded argument");
3886 self.resolve_type(declaration.output, visitor);
3890 // Resolve the function body.
3891 self.resolve_block(block, visitor);
3893 debug!("(resolving function) leaving function");
3896 (*self.label_ribs).pop();
3897 (*self.value_ribs).pop();
3900 fn resolve_type_parameters(type_parameters: ~[ty_param],
3901 visitor: ResolveVisitor) {
3902 for type_parameters.each |type_parameter| {
3903 for type_parameter.bounds.each |&bound| {
3905 TraitTyParamBound(ty) => self.resolve_type(ty, visitor),
3906 RegionTyParamBound => {}
3912 fn resolve_struct(id: node_id,
3913 type_parameters: @~[ty_param],
3914 fields: ~[@struct_field],
3915 optional_destructor: Option<struct_dtor>,
3916 visitor: ResolveVisitor) {
3917 // If applicable, create a rib for the type parameters.
3918 let borrowed_type_parameters: &~[ty_param] = &*type_parameters;
3919 do self.with_type_parameter_rib(HasTypeParameters
3920 (borrowed_type_parameters, id, 0,
3921 OpaqueFunctionRibKind)) {
3923 // Resolve the type parameters.
3924 self.resolve_type_parameters(/*bad*/copy *type_parameters,
3928 for fields.each |field| {
3929 self.resolve_type(field.node.ty, visitor);
3932 // Resolve the destructor, if applicable.
3933 match optional_destructor {
3937 Some(ref destructor) => {
3938 self.resolve_function(NormalRibKind,
3941 (*destructor).node.body,
3943 ((*destructor).node.self_id,
3951 // Does this really need to take a RibKind or is it always going
3952 // to be NormalRibKind?
3953 fn resolve_method(rib_kind: RibKind,
3955 outer_type_parameter_count: uint,
3956 visitor: ResolveVisitor) {
3957 let borrowed_method_type_parameters = &method.tps;
3958 let type_parameters =
3959 HasTypeParameters(borrowed_method_type_parameters,
3961 outer_type_parameter_count,
3963 // we only have self ty if it is a non static method
3964 let self_binding = match method.self_ty.node {
3965 sty_static => { NoSelfBinding }
3966 sty_by_ref => { HasSelfBinding(method.self_id, true) }
3967 _ => { HasSelfBinding(method.self_id, false) }
3970 self.resolve_function(rib_kind,
3971 Some(@/*bad*/copy method.decl),
3978 fn resolve_implementation(id: node_id,
3980 type_parameters: ~[ty_param],
3981 opt_trait_reference: Option<@trait_ref>,
3983 methods: ~[@method],
3984 visitor: ResolveVisitor) {
3985 // If applicable, create a rib for the type parameters.
3986 let outer_type_parameter_count = type_parameters.len();
3987 let borrowed_type_parameters: &~[ty_param] = &type_parameters;
3988 do self.with_type_parameter_rib(HasTypeParameters
3989 (borrowed_type_parameters, id, 0,
3991 // Resolve the type parameters.
3992 self.resolve_type_parameters(/*bad*/copy type_parameters,
3995 // Resolve the trait reference, if necessary.
3996 let original_trait_refs = self.current_trait_refs;
3997 match opt_trait_reference {
3998 Some(trait_reference) => {
3999 let new_trait_refs = @DVec();
4000 match self.resolve_path(
4001 trait_reference.path, TypeNS, true, visitor) {
4003 self.session.span_err(span,
4004 ~"attempt to implement an \
4008 self.record_def(trait_reference.ref_id, def);
4010 // Record the current trait reference.
4011 (*new_trait_refs).push(def_id_of_def(def));
4014 // Record the current set of trait references.
4015 self.current_trait_refs = Some(new_trait_refs);
4020 // Resolve the self type.
4021 self.resolve_type(self_type, visitor);
4023 for methods.each |method| {
4024 // We also need a new scope for the method-specific
4026 self.resolve_method(MethodRibKind(
4028 Provided(method.id)),
4030 outer_type_parameter_count,
4033 let borrowed_type_parameters = &method.tps;
4034 self.resolve_function(MethodRibKind(
4036 Provided(method.id)),
4039 (borrowed_type_parameters,
4041 outer_type_parameter_count,
4044 HasSelfBinding(method.self_id),
4049 // Restore the original trait references.
4050 self.current_trait_refs = original_trait_refs;
4054 fn resolve_module(module_: _mod, span: span, _name: ident, id: node_id,
4055 visitor: ResolveVisitor) {
4057 // Write the implementations in scope into the module metadata.
4058 debug!("(resolving module) resolving module ID %d", id);
4059 visit_mod(module_, span, id, (), visitor);
4062 fn resolve_local(local: @local, visitor: ResolveVisitor) {
4063 let mutability = if local.node.is_mutbl {Mutable} else {Immutable};
4065 // Resolve the type.
4066 self.resolve_type(local.node.ty, visitor);
4068 // Resolve the initializer, if necessary.
4069 match local.node.init {
4073 Some(initializer) => {
4074 self.resolve_expr(initializer, visitor);
4078 // Resolve the pattern.
4079 self.resolve_pattern(local.node.pat, LocalIrrefutableMode, mutability,
4083 fn binding_mode_map(pat: @pat) -> BindingMap {
4084 let result = HashMap();
4085 do pat_bindings(self.def_map, pat) |binding_mode, _id, sp, path| {
4086 let ident = path_to_ident(path);
4087 result.insert(ident,
4088 binding_info {span: sp,
4089 binding_mode: binding_mode});
4094 fn check_consistent_bindings(arm: arm) {
4095 if arm.pats.len() == 0 { return; }
4096 let map_0 = self.binding_mode_map(arm.pats[0]);
4097 for arm.pats.eachi() |i, p| {
4098 let map_i = self.binding_mode_map(*p);
4100 for map_0.each |&key, &binding_0| {
4101 match map_i.find(&key) {
4103 self.session.span_err(
4105 fmt!("variable `%s` from pattern #1 is \
4106 not bound in pattern #%u",
4107 self.session.str_of(key), i + 1));
4109 Some(binding_i) => {
4110 if binding_0.binding_mode != binding_i.binding_mode {
4111 self.session.span_err(
4113 fmt!("variable `%s` is bound with different \
4114 mode in pattern #%u than in pattern #1",
4115 self.session.str_of(key), i + 1));
4121 for map_i.each |&key, &binding| {
4122 if !map_0.contains_key(&key) {
4123 self.session.span_err(
4125 fmt!("variable `%s` from pattern #%u is \
4126 not bound in pattern #1",
4127 self.session.str_of(key), i + 1));
4133 fn resolve_arm(arm: arm, visitor: ResolveVisitor) {
4134 (*self.value_ribs).push(@Rib(NormalRibKind));
4136 let bindings_list = HashMap();
4137 for arm.pats.each |pattern| {
4138 self.resolve_pattern(*pattern, RefutableMode, Immutable,
4139 Some(bindings_list), visitor);
4142 // This has to happen *after* we determine which
4143 // pat_idents are variants
4144 self.check_consistent_bindings(arm);
4146 visit_expr_opt(arm.guard, (), visitor);
4147 self.resolve_block(arm.body, visitor);
4149 (*self.value_ribs).pop();
4152 fn resolve_block(block: blk, visitor: ResolveVisitor) {
4153 debug!("(resolving block) entering block");
4154 (*self.value_ribs).push(@Rib(NormalRibKind));
4156 // Move down in the graph, if there's an anonymous module rooted here.
4157 let orig_module = self.current_module;
4158 match self.current_module.anonymous_children.find(&block.node.id) {
4159 None => { /* Nothing to do. */ }
4160 Some(anonymous_module) => {
4161 debug!("(resolving block) found anonymous module, moving \
4163 self.current_module = anonymous_module;
4167 // Descend into the block.
4168 visit_block(block, (), visitor);
4171 self.current_module = orig_module;
4173 (*self.value_ribs).pop();
4174 debug!("(resolving block) leaving block");
4177 fn resolve_type(ty: @Ty, visitor: ResolveVisitor) {
4179 // Like path expressions, the interpretation of path types depends
4180 // on whether the path has multiple elements in it or not.
4182 ty_path(path, path_id) => {
4183 // This is a path in the type namespace. Walk through scopes
4184 // scopes looking for it.
4185 let mut result_def = None;
4187 // First, check to see whether the name is a primitive type.
4188 if path.idents.len() == 1 {
4189 let name = path.idents.last();
4191 match self.primitive_type_table
4195 Some(primitive_type) => {
4197 Some(def_prim_ty(primitive_type));
4207 match self.resolve_path(path, TypeNS, true, visitor) {
4209 debug!("(resolving type) resolved `%s` to \
4211 self.session.str_of(
4212 path.idents.last()),
4214 result_def = Some(def);
4226 match copy result_def {
4228 // Write the result into the def map.
4229 debug!("(resolving type) writing resolution for `%s` \
4231 self.idents_to_str(path.idents),
4233 self.record_def(path_id, def);
4236 self.session.span_err
4237 (ty.span, fmt!("use of undeclared type name `%s`",
4238 self.idents_to_str(path.idents)));
4244 // Just resolve embedded types.
4245 visit_ty(ty, (), visitor);
4250 fn resolve_pattern(pattern: @pat,
4251 mode: PatternBindingMode,
4252 mutability: Mutability,
4253 // Maps idents to the node ID for the (outermost)
4254 // pattern that binds them
4255 bindings_list: Option<HashMap<ident,node_id>>,
4256 visitor: ResolveVisitor) {
4258 let pat_id = pattern.id;
4259 do walk_pat(pattern) |pattern| {
4260 match pattern.node {
4261 pat_ident(binding_mode, path, _)
4262 if !path.global && path.idents.len() == 1 => {
4264 // The meaning of pat_ident with no type parameters
4265 // depends on whether an enum variant or unit-like struct
4266 // with that name is in scope. The probing lookup has to
4267 // be careful not to emit spurious errors. Only matching
4268 // patterns (match) can match nullary variants or
4269 // unit-like structs. For binding patterns (let), matching
4270 // such a value is simply disallowed (since it's rarely
4273 let ident = path.idents[0];
4275 match self.resolve_bare_identifier_pattern(ident) {
4276 FoundStructOrEnumVariant(def)
4277 if mode == RefutableMode => {
4278 debug!("(resolving pattern) resolving `%s` to \
4279 struct or enum variant",
4280 self.session.str_of(ident));
4282 self.enforce_default_binding_mode(
4286 self.record_def(pattern.id, def);
4288 FoundStructOrEnumVariant(_) => {
4289 self.session.span_err(pattern.span,
4290 fmt!("declaration of `%s` \
4292 variant or unit-like \
4297 FoundConst(def) if mode == RefutableMode => {
4298 debug!("(resolving pattern) resolving `%s` to \
4300 self.session.str_of(ident));
4302 self.enforce_default_binding_mode(
4306 self.record_def(pattern.id, def);
4309 self.session.span_err(pattern.span,
4310 ~"only refutable patterns \
4313 BareIdentifierPatternUnresolved => {
4314 debug!("(resolving pattern) binding `%s`",
4315 self.session.str_of(ident));
4317 let is_mutable = mutability == Mutable;
4319 let def = match mode {
4321 // For pattern arms, we must use
4322 // `def_binding` definitions.
4324 def_binding(pattern.id, binding_mode)
4326 LocalIrrefutableMode => {
4327 // But for locals, we use `def_local`.
4328 def_local(pattern.id, is_mutable)
4330 ArgumentIrrefutableMode(argument_mode) => {
4331 // And for function arguments, `def_arg`.
4332 def_arg(pattern.id, argument_mode,
4337 // Record the definition so that later passes
4338 // will be able to distinguish variants from
4339 // locals in patterns.
4341 self.record_def(pattern.id, def);
4343 // Add the binding to the local ribs, if it
4344 // doesn't already exist in the bindings list. (We
4345 // must not add it if it's in the bindings list
4346 // because that breaks the assumptions later
4347 // passes make about or-patterns.)
4349 match bindings_list {
4351 if !bindings_list.contains_key(&ident)
4353 let last_rib = (*self.value_ribs).last();
4354 last_rib.bindings.insert(ident,
4356 bindings_list.insert(ident, pat_id);
4359 if b.find(&ident) == Some(pat_id) {
4360 // Then this is a duplicate variable
4361 // in the same disjunct, which is an
4363 self.session.span_err(pattern.span,
4364 fmt!("Identifier %s is bound more \
4365 than once in the same pattern",
4366 path_to_str(path, self.session
4369 // Not bound in the same pattern: do nothing
4372 let last_rib = (*self.value_ribs).last();
4373 last_rib.bindings.insert(ident,
4380 // Check the types in the path pattern.
4381 for path.types.each |ty| {
4382 self.resolve_type(*ty, visitor);
4386 pat_ident(_, path, _) | pat_enum(path, _) => {
4387 // These two must be enum variants or structs.
4388 match self.resolve_path(path, ValueNS, false, visitor) {
4389 Some(def @ def_variant(*)) |
4390 Some(def @ def_struct(*)) => {
4391 self.record_def(pattern.id, def);
4394 self.session.span_err(
4396 fmt!("not an enum variant: %s",
4397 self.session.str_of(
4398 path.idents.last())));
4401 self.session.span_err(path.span,
4402 ~"unresolved enum variant");
4406 // Check the types in the path pattern.
4407 for path.types.each |ty| {
4408 self.resolve_type(*ty, visitor);
4413 self.resolve_expr(expr, visitor);
4416 pat_range(first_expr, last_expr) => {
4417 self.resolve_expr(first_expr, visitor);
4418 self.resolve_expr(last_expr, visitor);
4421 pat_struct(path, _, _) => {
4422 match self.resolve_path(path, TypeNS, false, visitor) {
4423 Some(def_ty(class_id))
4424 if self.structs.contains_key(&class_id)
4426 let class_def = def_struct(class_id);
4427 self.record_def(pattern.id, class_def);
4429 Some(definition @ def_struct(class_id))
4430 if self.structs.contains_key(&class_id)
4432 self.record_def(pattern.id, definition);
4434 Some(definition @ def_variant(_, variant_id))
4435 if self.structs.contains_key(&variant_id)
4437 self.record_def(pattern.id, definition);
4440 debug!("(resolving pattern) didn't find struct \
4442 self.session.span_err(
4444 fmt!("`%s` does not name a structure",
4445 self.idents_to_str(path.idents)));
4457 fn resolve_bare_identifier_pattern(name: ident)
4458 -> BareIdentifierPatternResolution {
4459 match self.resolve_item_in_lexical_scope(self.current_module,
4462 SearchThroughModules) {
4463 Success(target) => {
4464 match target.bindings.value_def {
4466 fail!(~"resolved name in the value namespace to a \
4467 set of name bindings with no def?!");
4471 def @ def_variant(*) | def @ def_struct(*) => {
4472 return FoundStructOrEnumVariant(def);
4474 def @ def_const(*) => {
4475 return FoundConst(def);
4478 return BareIdentifierPatternUnresolved;
4486 fail!(~"unexpected indeterminate result");
4490 return BareIdentifierPatternUnresolved;
4496 * If `check_ribs` is true, checks the local definitions first; i.e.
4497 * doesn't skip straight to the containing module.
4499 fn resolve_path(path: @path,
4500 namespace: Namespace,
4502 visitor: ResolveVisitor)
4504 // First, resolve the types.
4505 for path.types.each |ty| {
4506 self.resolve_type(*ty, visitor);
4510 return self.resolve_crate_relative_path(path,
4515 if path.idents.len() > 1 {
4516 return self.resolve_module_relative_path(path,
4521 return self.resolve_identifier(path.idents.last(),
4527 fn resolve_identifier(identifier: ident,
4528 namespace: Namespace,
4534 match self.resolve_identifier_in_local_ribs(identifier,
4546 return self.resolve_item_by_identifier_in_lexical_scope(identifier,
4550 // FIXME #4952: Merge me with resolve_name_in_module?
4551 fn resolve_definition_of_name_in_module(containing_module: @Module,
4553 namespace: Namespace,
4557 // First, search children.
4558 match containing_module.children.find(&name) {
4559 Some(child_name_bindings) => {
4560 match (child_name_bindings.def_for_namespace(namespace),
4561 child_name_bindings.privacy_for_namespace(namespace)) {
4562 (Some(def), Some(Public)) => {
4563 // Found it. Stop the search here.
4564 return ChildNameDefinition(def);
4566 (Some(def), _) if xray == Xray => {
4567 // Found it. Stop the search here.
4568 return ChildNameDefinition(def);
4570 (Some(_), _) | (None, _) => {
4580 // Next, search import resolutions.
4581 match containing_module.import_resolutions.find(&name) {
4582 Some(import_resolution) if import_resolution.privacy == Public ||
4584 match (*import_resolution).target_for_namespace(namespace) {
4586 match (target.bindings.def_for_namespace(namespace),
4587 target.bindings.privacy_for_namespace(
4589 (Some(def), Some(Public)) => {
4591 import_resolution.state.used = true;
4592 return ImportNameDefinition(def);
4594 (Some(_), _) | (None, _) => {
4595 // This can happen with external impls, due to
4596 // the imperfect way we read the metadata.
4598 return NoNameDefinition;
4603 return NoNameDefinition;
4608 return NoNameDefinition;
4613 fn intern_module_part_of_path(path: @path) -> @DVec<ident> {
4614 let module_path_idents = @DVec();
4615 for path.idents.eachi |index, ident| {
4616 if index == path.idents.len() - 1 {
4620 (*module_path_idents).push(*ident);
4623 return module_path_idents;
4626 fn resolve_module_relative_path(path: @path,
4628 namespace: Namespace)
4631 let module_path_idents = self.intern_module_part_of_path(path);
4633 let mut containing_module;
4634 match self.resolve_module_path_for_import(self.current_module,
4639 self.session.span_err(path.span,
4640 fmt!("use of undeclared module `%s`",
4642 (*module_path_idents).get())));
4647 fail!(~"indeterminate unexpected");
4650 Success(resulting_module) => {
4651 containing_module = resulting_module;
4655 let name = path.idents.last();
4656 match self.resolve_definition_of_name_in_module(containing_module,
4660 NoNameDefinition => {
4661 // We failed to resolve the name. Report an error.
4664 ChildNameDefinition(def) | ImportNameDefinition(def) => {
4670 fn resolve_crate_relative_path(path: @path,
4672 namespace: Namespace)
4675 let module_path_idents = self.intern_module_part_of_path(path);
4677 let root_module = self.graph_root.get_module();
4679 let mut containing_module;
4680 match self.resolve_module_path_from_root(root_module,
4686 self.session.span_err(path.span,
4687 fmt!("use of undeclared module `::%s`",
4689 ((*module_path_idents).get())));
4694 fail!(~"indeterminate unexpected");
4697 Success(resulting_module) => {
4698 containing_module = resulting_module;
4702 let name = path.idents.last();
4703 match self.resolve_definition_of_name_in_module(containing_module,
4707 NoNameDefinition => {
4708 // We failed to resolve the name. Report an error.
4711 ChildNameDefinition(def) | ImportNameDefinition(def) => {
4717 fn resolve_identifier_in_local_ribs(ident: ident,
4718 namespace: Namespace,
4721 // Check the local set of ribs.
4722 let mut search_result;
4725 search_result = self.search_ribs(self.value_ribs, ident, span,
4726 DontAllowCapturingSelf);
4729 search_result = self.search_ribs(self.type_ribs, ident, span,
4730 AllowCapturingSelf);
4734 match copy search_result {
4735 Some(dl_def(def)) => {
4736 debug!("(resolving path in local ribs) resolved `%s` to \
4738 self.session.str_of(ident),
4742 Some(dl_field) | Some(dl_impl(_)) | None => {
4748 fn resolve_item_by_identifier_in_lexical_scope(ident: ident,
4749 namespace: Namespace)
4752 match self.resolve_item_in_lexical_scope(self.current_module,
4755 DontSearchThroughModules) {
4756 Success(target) => {
4757 match (*target.bindings).def_for_namespace(namespace) {
4759 // This can happen if we were looking for a type and
4760 // found a module instead. Modules don't have defs.
4764 debug!("(resolving item path in lexical scope) \
4765 resolved `%s` to item",
4766 self.session.str_of(ident));
4772 fail!(~"unexpected indeterminate result");
4780 fn name_exists_in_scope_struct(name: &str) -> bool {
4781 let mut i = self.type_ribs.len();
4784 let rib = self.type_ribs.get_elt(i);
4786 MethodRibKind(node_id, _) =>
4787 for vec::each(self.crate.node.module.items) |item| {
4788 if item.id == node_id {
4790 item_struct(class_def, _) => {
4791 for vec::each(class_def.fields) |field| {
4792 match field.node.kind {
4793 unnamed_field => {},
4794 named_field(ident, _, _) => {
4795 if str::eq_slice(self.session.str_of(ident),
4813 fn resolve_expr(expr: @expr, visitor: ResolveVisitor) {
4814 // First, record candidate traits for this expression if it could
4815 // result in the invocation of a method call.
4817 self.record_candidate_traits_for_expr_if_necessary(expr);
4819 // Next, resolve the node.
4821 // The interpretation of paths depends on whether the path has
4822 // multiple elements in it or not.
4824 expr_path(path) => {
4825 // This is a local path in the value namespace. Walk through
4826 // scopes looking for it.
4828 match self.resolve_path(path, ValueNS, true, visitor) {
4830 // Write the result into the def map.
4831 debug!("(resolving expr) resolved `%s`",
4832 self.idents_to_str(path.idents));
4833 self.record_def(expr.id, def);
4836 let wrong_name = self.idents_to_str(
4837 /*bad*/copy path.idents);
4838 if self.name_exists_in_scope_struct(wrong_name) {
4839 self.session.span_err(expr.span,
4840 fmt!("unresolved name: `%s`. \
4841 Did you mean: `self.%s`?",
4846 self.session.span_err(expr.span,
4847 fmt!("unresolved name: %s",
4853 visit_expr(expr, (), visitor);
4856 expr_fn(_, ref fn_decl, ref block, _) |
4857 expr_fn_block(ref fn_decl, ref block) => {
4858 self.resolve_function(FunctionRibKind(expr.id, block.node.id),
4859 Some(@/*bad*/copy *fn_decl),
4866 expr_struct(path, _, _) => {
4867 // Resolve the path to the structure it goes to.
4868 match self.resolve_path(path, TypeNS, false, visitor) {
4869 Some(def_ty(class_id)) | Some(def_struct(class_id))
4870 if self.structs.contains_key(&class_id) => {
4871 let class_def = def_struct(class_id);
4872 self.record_def(expr.id, class_def);
4874 Some(definition @ def_variant(_, class_id))
4875 if self.structs.contains_key(&class_id) => {
4876 self.record_def(expr.id, definition);
4879 self.session.span_err(
4881 fmt!("`%s` does not name a structure",
4882 self.idents_to_str(path.idents)));
4886 visit_expr(expr, (), visitor);
4889 expr_loop(_, Some(label)) => {
4890 do self.with_label_rib {
4891 let def_like = dl_def(def_label(expr.id));
4892 self.label_ribs.last().bindings.insert(label, def_like);
4894 visit_expr(expr, (), visitor);
4898 expr_break(Some(label)) | expr_again(Some(label)) => {
4899 match self.search_ribs(self.label_ribs, label, expr.span,
4900 DontAllowCapturingSelf) {
4902 self.session.span_err(expr.span,
4903 fmt!("use of undeclared label \
4904 `%s`", self.session.str_of(
4906 Some(dl_def(def @ def_label(_))) =>
4907 self.record_def(expr.id, def),
4909 self.session.span_bug(expr.span,
4910 ~"label wasn't mapped to a \
4916 visit_expr(expr, (), visitor);
4921 fn record_candidate_traits_for_expr_if_necessary(expr: @expr) {
4923 expr_field(_, ident, _) => {
4924 let traits = self.search_for_traits_containing_method(ident);
4925 self.trait_map.insert(expr.id, traits);
4927 expr_method_call(_, ident, _, _, _) => {
4928 let traits = self.search_for_traits_containing_method(ident);
4929 self.trait_map.insert(expr.id, traits);
4931 expr_binary(add, _, _) | expr_assign_op(add, _, _) => {
4932 self.add_fixed_trait_for_expr(expr.id,
4933 self.lang_items.add_trait());
4935 expr_binary(subtract, _, _) | expr_assign_op(subtract, _, _) => {
4936 self.add_fixed_trait_for_expr(expr.id,
4937 self.lang_items.sub_trait());
4939 expr_binary(mul, _, _) | expr_assign_op(mul, _, _) => {
4940 self.add_fixed_trait_for_expr(expr.id,
4941 self.lang_items.mul_trait());
4943 expr_binary(div, _, _) | expr_assign_op(div, _, _) => {
4944 self.add_fixed_trait_for_expr(expr.id,
4945 self.lang_items.div_trait());
4947 expr_binary(rem, _, _) | expr_assign_op(rem, _, _) => {
4948 self.add_fixed_trait_for_expr(expr.id,
4949 self.lang_items.modulo_trait());
4951 expr_binary(bitxor, _, _) | expr_assign_op(bitxor, _, _) => {
4952 self.add_fixed_trait_for_expr(expr.id,
4953 self.lang_items.bitxor_trait());
4955 expr_binary(bitand, _, _) | expr_assign_op(bitand, _, _) => {
4956 self.add_fixed_trait_for_expr(expr.id,
4957 self.lang_items.bitand_trait());
4959 expr_binary(bitor, _, _) | expr_assign_op(bitor, _, _) => {
4960 self.add_fixed_trait_for_expr(expr.id,
4961 self.lang_items.bitor_trait());
4963 expr_binary(shl, _, _) | expr_assign_op(shl, _, _) => {
4964 self.add_fixed_trait_for_expr(expr.id,
4965 self.lang_items.shl_trait());
4967 expr_binary(shr, _, _) | expr_assign_op(shr, _, _) => {
4968 self.add_fixed_trait_for_expr(expr.id,
4969 self.lang_items.shr_trait());
4971 expr_binary(lt, _, _) | expr_binary(le, _, _) |
4972 expr_binary(ge, _, _) | expr_binary(gt, _, _) => {
4973 self.add_fixed_trait_for_expr(expr.id,
4974 self.lang_items.ord_trait());
4976 expr_binary(eq, _, _) | expr_binary(ne, _, _) => {
4977 self.add_fixed_trait_for_expr(expr.id,
4978 self.lang_items.eq_trait());
4980 expr_unary(neg, _) => {
4981 self.add_fixed_trait_for_expr(expr.id,
4982 self.lang_items.neg_trait());
4984 expr_unary(not, _) => {
4985 self.add_fixed_trait_for_expr(expr.id,
4986 self.lang_items.not_trait());
4989 self.add_fixed_trait_for_expr(expr.id,
4990 self.lang_items.index_trait());
4998 fn search_for_traits_containing_method(name: ident) -> @DVec<def_id> {
4999 debug!("(searching for traits containing method) looking for '%s'",
5000 self.session.str_of(name));
5002 let found_traits = @DVec();
5003 let mut search_module = self.current_module;
5005 // Look for the current trait.
5006 match copy self.current_trait_refs {
5007 Some(trait_def_ids) => {
5008 for trait_def_ids.each |trait_def_id| {
5009 self.add_trait_info_if_containing_method(
5010 found_traits, *trait_def_id, name);
5018 // Look for trait children.
5019 for search_module.children.each_value |&child_name_bindings| {
5020 match child_name_bindings.def_for_namespace(TypeNS) {
5023 def_ty(trait_def_id) => {
5024 self.add_trait_info_if_containing_method(
5025 found_traits, trait_def_id, name);
5038 // Look for imports.
5039 for search_module.import_resolutions.each_value
5040 |&import_resolution| {
5042 match import_resolution.target_for_namespace(TypeNS) {
5047 match target.bindings.def_for_namespace(TypeNS) {
5050 def_ty(trait_def_id) => {
5052 add_trait_info_if_containing_method(
5053 found_traits, trait_def_id, name);
5055 import_resolution.state.used =
5072 // Move to the next parent.
5073 match search_module.parent_link {
5078 ModuleParentLink(parent_module, _) |
5079 BlockParentLink(parent_module, _) => {
5080 search_module = parent_module;
5085 return found_traits;
5088 fn add_trait_info_if_containing_method(found_traits: @DVec<def_id>,
5089 trait_def_id: def_id,
5090 name: ident) -> bool {
5092 debug!("(adding trait info if containing method) trying trait %d:%d \
5096 self.session.str_of(name));
5098 match self.trait_info.find(&trait_def_id) {
5099 Some(trait_info) if trait_info.contains_key(&name) => {
5100 debug!("(adding trait info if containing method) found trait \
5101 %d:%d for method '%s'",
5104 self.session.str_of(name));
5105 (*found_traits).push(trait_def_id);
5114 fn add_fixed_trait_for_expr(expr_id: node_id, +trait_id: def_id) {
5115 let traits = @DVec();
5116 traits.push(trait_id);
5117 self.trait_map.insert(expr_id, traits);
5120 fn record_def(node_id: node_id, def: def) {
5121 debug!("(recording def) recording %? for %?", def, node_id);
5122 self.def_map.insert(node_id, def);
5125 fn enforce_default_binding_mode(pat: @pat,
5126 pat_binding_mode: binding_mode,
5128 match pat_binding_mode {
5131 self.session.span_err(
5133 fmt!("cannot use `copy` binding mode with %s",
5137 self.session.span_err(
5139 fmt!("cannot use `ref` binding mode with %s",
5146 // main function checking
5148 // be sure that there is only one main function
5150 fn check_duplicate_main() {
5151 if self.attr_main_fn.is_none() {
5152 if self.main_fns.len() >= 1u {
5154 while i < self.main_fns.len() {
5155 let (_, dup_main_span) =
5156 option::unwrap(self.main_fns[i]);
5157 self.session.span_err(
5159 ~"multiple 'main' functions");
5162 *self.session.main_fn = self.main_fns[0];
5165 *self.session.main_fn = self.attr_main_fn;
5170 // Unused import checking
5172 // Although this is a lint pass, it lives in here because it depends on
5173 // resolve data structures.
5176 fn check_for_unused_imports_if_necessary() {
5177 if self.unused_import_lint_level == allow {
5181 let root_module = self.graph_root.get_module();
5182 self.check_for_unused_imports_in_module_subtree(root_module);
5185 fn check_for_unused_imports_in_module_subtree(module_: @Module) {
5186 // If this isn't a local crate, then bail out. We don't need to check
5187 // for unused imports in external crates.
5189 match module_.def_id {
5190 Some(def_id) if def_id.crate == local_crate => {
5194 // Check for unused imports in the root module.
5198 debug!("(checking for unused imports in module subtree) not \
5199 checking for unused imports for `%s`",
5200 self.module_to_str(module_));
5205 self.check_for_unused_imports_in_module(module_);
5207 for module_.children.each_value |&child_name_bindings| {
5208 match (*child_name_bindings).get_module_if_available() {
5212 Some(child_module) => {
5213 self.check_for_unused_imports_in_module_subtree
5219 for module_.anonymous_children.each_value |&child_module| {
5220 self.check_for_unused_imports_in_module_subtree(child_module);
5224 fn check_for_unused_imports_in_module(module_: @Module) {
5225 for module_.import_resolutions.each_value |&import_resolution| {
5226 // Ignore dummy spans for things like automatically injected
5227 // imports for the prelude, and also don't warn about the same
5228 // import statement being unused more than once.
5229 if !import_resolution.state.used &&
5230 !import_resolution.state.warned &&
5231 import_resolution.span != dummy_sp() {
5232 import_resolution.state.warned = true;
5233 match self.unused_import_lint_level {
5235 self.session.span_warn(copy import_resolution.span,
5239 self.session.span_err(copy import_resolution.span,
5243 self.session.span_bug(copy import_resolution.span,
5244 ~"shouldn't be here if lint \
5256 // Diagnostics are not particularly efficient, because they're rarely
5260 /// A somewhat inefficient routine to obtain the name of a module.
5261 fn module_to_str(module_: @Module) -> ~str {
5262 let idents = DVec();
5263 let mut current_module = module_;
5265 match current_module.parent_link {
5269 ModuleParentLink(module_, name) => {
5271 current_module = module_;
5273 BlockParentLink(module_, _) => {
5274 idents.push(special_idents::opaque);
5275 current_module = module_;
5280 if idents.len() == 0 {
5283 return self.idents_to_str(vec::reversed(idents.get()));
5286 fn dump_module(module_: @Module) {
5287 debug!("Dump of module `%s`:", self.module_to_str(module_));
5289 debug!("Children:");
5290 for module_.children.each_key |&name| {
5291 debug!("* %s", self.session.str_of(name));
5294 debug!("Import resolutions:");
5295 for module_.import_resolutions.each |&name, &import_resolution| {
5297 match (*import_resolution).target_for_namespace(ValueNS) {
5298 None => { value_repr = ~""; }
5300 value_repr = ~" value:?";
5306 match (*import_resolution).target_for_namespace(TypeNS) {
5307 None => { type_repr = ~""; }
5309 type_repr = ~" type:?";
5314 debug!("* %s:%s%s", self.session.str_of(name),
5315 value_repr, type_repr);
5320 /// Entry point to crate resolution.
5321 pub fn resolve_crate(session: Session,
5322 lang_items: LanguageItems,
5326 exp_map2: ExportMap2,
5329 let resolver = @Resolver(session, lang_items, crate);
5330 resolver.resolve(resolver);
5332 def_map: resolver.def_map,
5333 exp_map2: resolver.export_map2,
5334 trait_map: resolver.trait_map