]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/resolve_imports.rs
15c9528d7ef1e9fbf8a1b734f1bd883e8e31acd1
[rust.git] / src / librustc_resolve / resolve_imports.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use self::ImportDirectiveSubclass::*;
12
13 use DefModifiers;
14 use Module;
15 use Namespace::{self, TypeNS, ValueNS};
16 use {NameBinding, NameBindingKind, PrivacyError};
17 use ResolveResult;
18 use ResolveResult::*;
19 use Resolver;
20 use UseLexicalScopeFlag;
21 use {names_to_string, module_to_string};
22 use {resolve_error, ResolutionError};
23
24 use build_reduced_graph;
25
26 use rustc::lint;
27 use rustc::middle::def::*;
28
29 use syntax::ast::{NodeId, Name};
30 use syntax::attr::AttrMetaMethods;
31 use syntax::codemap::Span;
32 use syntax::util::lev_distance::find_best_match_for_name;
33
34 use std::mem::replace;
35 use std::cell::Cell;
36
37 /// Contains data for specific types of import directives.
38 #[derive(Clone, Debug)]
39 pub enum ImportDirectiveSubclass {
40     SingleImport {
41         target: Name,
42         source: Name,
43         type_determined: Cell<bool>,
44         value_determined: Cell<bool>,
45     },
46     GlobImport,
47 }
48
49 impl ImportDirectiveSubclass {
50     pub fn single(target: Name, source: Name) -> Self {
51         SingleImport {
52             target: target,
53             source: source,
54             type_determined: Cell::new(false),
55             value_determined: Cell::new(false),
56         }
57     }
58 }
59
60 /// Whether an import can be shadowed by another import.
61 #[derive(Debug,PartialEq,Clone,Copy)]
62 pub enum Shadowable {
63     Always,
64     Never,
65 }
66
67 /// One import directive.
68 #[derive(Debug,Clone)]
69 pub struct ImportDirective {
70     pub module_path: Vec<Name>,
71     pub subclass: ImportDirectiveSubclass,
72     pub span: Span,
73     pub id: NodeId,
74     pub is_public: bool, // see note in ImportResolutionPerNamespace about how to use this
75     pub shadowable: Shadowable,
76 }
77
78 impl ImportDirective {
79     pub fn new(module_path: Vec<Name>,
80                subclass: ImportDirectiveSubclass,
81                span: Span,
82                id: NodeId,
83                is_public: bool,
84                shadowable: Shadowable)
85                -> ImportDirective {
86         ImportDirective {
87             module_path: module_path,
88             subclass: subclass,
89             span: span,
90             id: id,
91             is_public: is_public,
92             shadowable: shadowable,
93         }
94     }
95
96     // Given the binding to which this directive resolves in a particular namespace,
97     // this returns the binding for the name this directive defines in that namespace.
98     fn import<'a>(&self,
99                   binding: &'a NameBinding<'a>,
100                   privacy_error: Option<Box<PrivacyError<'a>>>) -> NameBinding<'a> {
101         let mut modifiers = match self.is_public {
102             true => DefModifiers::PUBLIC | DefModifiers::IMPORTABLE,
103             false => DefModifiers::empty(),
104         };
105         if let GlobImport = self.subclass {
106             modifiers = modifiers | DefModifiers::GLOB_IMPORTED;
107         }
108         if self.shadowable == Shadowable::Always {
109             modifiers = modifiers | DefModifiers::PRELUDE;
110         }
111
112         NameBinding {
113             kind: NameBindingKind::Import {
114                 binding: binding,
115                 id: self.id,
116                 privacy_error: privacy_error,
117             },
118             span: Some(self.span),
119             modifiers: modifiers,
120         }
121     }
122 }
123
124 #[derive(Clone, Default)]
125 /// Records information about the resolution of a name in a module.
126 pub struct NameResolution<'a> {
127     /// The number of unresolved single imports that could define the name.
128     pub outstanding_references: usize,
129     /// The least shadowable known binding for this name, or None if there are no known bindings.
130     pub binding: Option<&'a NameBinding<'a>>,
131 }
132
133 impl<'a> NameResolution<'a> {
134     pub fn result(&self, outstanding_globs: usize) -> ResolveResult<&'a NameBinding<'a>> {
135         // If no unresolved imports (single or glob) can define the name, self.binding is final.
136         if self.outstanding_references == 0 && outstanding_globs == 0 {
137             return self.binding.map(Success).unwrap_or(Failed(None));
138         }
139
140         if let Some(binding) = self.binding {
141             // Single imports will never be shadowable by other single or glob imports.
142             if !binding.defined_with(DefModifiers::GLOB_IMPORTED) { return Success(binding); }
143             // Non-PRELUDE glob imports will never be shadowable by other glob imports.
144             if self.outstanding_references == 0 && !binding.defined_with(DefModifiers::PRELUDE) {
145                 return Success(binding);
146             }
147         }
148
149         Indeterminate
150     }
151
152     // Define the name or return the existing binding if there is a collision.
153     pub fn try_define(&mut self, binding: &'a NameBinding<'a>) -> Result<(), &'a NameBinding<'a>> {
154         let is_prelude = |binding: &NameBinding| binding.defined_with(DefModifiers::PRELUDE);
155         let old_binding = match self.binding {
156             Some(_) if is_prelude(binding) => return Ok(()),
157             Some(old_binding) if !is_prelude(old_binding) => old_binding,
158             _ => { self.binding = Some(binding); return Ok(()); }
159         };
160
161         // FIXME #31337: We currently allow items to shadow glob-imported re-exports.
162         if !old_binding.is_import() && binding.defined_with(DefModifiers::GLOB_IMPORTED) {
163             if let NameBindingKind::Import { binding, .. } = binding.kind {
164                 if binding.is_import() { return Ok(()); }
165             }
166         }
167
168         Err(old_binding)
169     }
170 }
171
172 struct ImportResolvingError<'a> {
173     /// Module where the error happened
174     source_module: Module<'a>,
175     import_directive: &'a ImportDirective,
176     span: Span,
177     help: String,
178 }
179
180 struct ImportResolver<'a, 'b: 'a, 'tcx: 'b> {
181     resolver: &'a mut Resolver<'b, 'tcx>,
182 }
183
184 impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
185     // Import resolution
186     //
187     // This is a fixed-point algorithm. We resolve imports until our efforts
188     // are stymied by an unresolved import; then we bail out of the current
189     // module and continue. We terminate successfully once no more imports
190     // remain or unsuccessfully when no forward progress in resolving imports
191     // is made.
192
193     /// Resolves all imports for the crate. This method performs the fixed-
194     /// point iteration.
195     fn resolve_imports(&mut self) {
196         let mut i = 0;
197         let mut prev_unresolved_imports = 0;
198         let mut errors = Vec::new();
199
200         loop {
201             debug!("(resolving imports) iteration {}, {} imports left",
202                    i,
203                    self.resolver.unresolved_imports);
204
205             self.resolve_imports_for_module_subtree(self.resolver.graph_root, &mut errors);
206
207             if self.resolver.unresolved_imports == 0 {
208                 debug!("(resolving imports) success");
209                 break;
210             }
211
212             if self.resolver.unresolved_imports == prev_unresolved_imports {
213                 // resolving failed
214                 if errors.len() > 0 {
215                     for e in errors {
216                         self.import_resolving_error(e)
217                     }
218                 } else {
219                     // Report unresolved imports only if no hard error was already reported
220                     // to avoid generating multiple errors on the same import.
221                     // Imports that are still indeterminate at this point are actually blocked
222                     // by errored imports, so there is no point reporting them.
223                     self.resolver.report_unresolved_imports(self.resolver.graph_root);
224                 }
225                 break;
226             }
227
228             i += 1;
229             prev_unresolved_imports = self.resolver.unresolved_imports;
230         }
231     }
232
233     /// Resolves an `ImportResolvingError` into the correct enum discriminant
234     /// and passes that on to `resolve_error`.
235     fn import_resolving_error(&self, e: ImportResolvingError<'b>) {
236         // If it's a single failed import then create a "fake" import
237         // resolution for it so that later resolve stages won't complain.
238         if let SingleImport { target, .. } = e.import_directive.subclass {
239             let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding {
240                 modifiers: DefModifiers::PRELUDE,
241                 kind: NameBindingKind::Def(Def::Err),
242                 span: None,
243             });
244             let dummy_binding = e.import_directive.import(dummy_binding, None);
245
246             let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding.clone());
247             let _ = e.source_module.try_define_child(target, TypeNS, dummy_binding);
248         }
249
250         let path = import_path_to_string(&e.import_directive.module_path,
251                                          &e.import_directive.subclass);
252
253         resolve_error(self.resolver,
254                       e.span,
255                       ResolutionError::UnresolvedImport(Some((&path, &e.help))));
256     }
257
258     /// Attempts to resolve imports for the given module and all of its
259     /// submodules.
260     fn resolve_imports_for_module_subtree(&mut self,
261                                           module_: Module<'b>,
262                                           errors: &mut Vec<ImportResolvingError<'b>>) {
263         debug!("(resolving imports for module subtree) resolving {}",
264                module_to_string(&module_));
265         let orig_module = replace(&mut self.resolver.current_module, module_);
266         self.resolve_imports_for_module(module_, errors);
267         self.resolver.current_module = orig_module;
268
269         for (_, child_module) in module_.module_children.borrow().iter() {
270             self.resolve_imports_for_module_subtree(child_module, errors);
271         }
272     }
273
274     /// Attempts to resolve imports for the given module only.
275     fn resolve_imports_for_module(&mut self,
276                                   module: Module<'b>,
277                                   errors: &mut Vec<ImportResolvingError<'b>>) {
278         let mut imports = Vec::new();
279         let mut unresolved_imports = module.unresolved_imports.borrow_mut();
280         ::std::mem::swap(&mut imports, &mut unresolved_imports);
281
282         for import_directive in imports {
283             match self.resolve_import_for_module(module, &import_directive) {
284                 Failed(err) => {
285                     let (span, help) = match err {
286                         Some((span, msg)) => (span, format!(". {}", msg)),
287                         None => (import_directive.span, String::new()),
288                     };
289                     errors.push(ImportResolvingError {
290                         source_module: module,
291                         import_directive: import_directive,
292                         span: span,
293                         help: help,
294                     });
295                 }
296                 Indeterminate => unresolved_imports.push(import_directive),
297                 Success(()) => {}
298             }
299         }
300     }
301
302     /// Attempts to resolve the given import. The return value indicates
303     /// failure if we're certain the name does not exist, indeterminate if we
304     /// don't know whether the name exists at the moment due to other
305     /// currently-unresolved imports, or success if we know the name exists.
306     /// If successful, the resolved bindings are written into the module.
307     fn resolve_import_for_module(&mut self,
308                                  module_: Module<'b>,
309                                  import_directive: &ImportDirective)
310                                  -> ResolveResult<()> {
311         debug!("(resolving import for module) resolving import `{}::...` in `{}`",
312                names_to_string(&import_directive.module_path),
313                module_to_string(&module_));
314
315         self.resolver
316             .resolve_module_path(module_,
317                                  &import_directive.module_path,
318                                  UseLexicalScopeFlag::DontUseLexicalScope,
319                                  import_directive.span)
320             .and_then(|containing_module| {
321                 // We found the module that the target is contained
322                 // within. Attempt to resolve the import within it.
323                 self.resolve_import(module_, containing_module, import_directive)
324             })
325             .and_then(|()| {
326                 // Decrement the count of unresolved imports.
327                 assert!(self.resolver.unresolved_imports >= 1);
328                 self.resolver.unresolved_imports -= 1;
329
330                 if let GlobImport = import_directive.subclass {
331                     module_.dec_glob_count();
332                     if import_directive.is_public {
333                         module_.dec_pub_glob_count();
334                     }
335                 }
336                 if import_directive.is_public {
337                     module_.dec_pub_count();
338                 }
339                 Success(())
340             })
341     }
342
343     fn resolve_import(&mut self,
344                       module_: Module<'b>,
345                       target_module: Module<'b>,
346                       directive: &ImportDirective)
347                       -> ResolveResult<()> {
348         let (source, target, value_determined, type_determined) = match directive.subclass {
349             SingleImport { source, target, ref value_determined, ref type_determined } =>
350                 (source, target, value_determined, type_determined),
351             GlobImport => return self.resolve_glob_import(module_, target_module, directive),
352         };
353
354         // We need to resolve both namespaces for this to succeed.
355         let (value_result, type_result) = {
356             let mut resolve_in_ns = |ns, determined: bool| {
357                 // Temporarily count the directive as determined so that the resolution fails
358                 // (as opposed to being indeterminate) when it can only be defined by the directive.
359                 if !determined { module_.decrement_outstanding_references_for(target, ns) }
360                 let result =
361                     self.resolver.resolve_name_in_module(target_module, source, ns, false, true);
362                 if !determined { module_.increment_outstanding_references_for(target, ns) }
363                 result
364             };
365             (resolve_in_ns(ValueNS, value_determined.get()),
366              resolve_in_ns(TypeNS, type_determined.get()))
367         };
368
369         for &(ns, result, determined) in &[(ValueNS, &value_result, value_determined),
370                                            (TypeNS, &type_result, type_determined)] {
371             if determined.get() { continue }
372             if let Indeterminate = *result { continue }
373
374             determined.set(true);
375             if let Success(binding) = *result {
376                 if !binding.defined_with(DefModifiers::IMPORTABLE) {
377                     let msg = format!("`{}` is not directly importable", target);
378                     span_err!(self.resolver.session, directive.span, E0253, "{}", &msg);
379                 }
380
381                 let privacy_error = if !self.resolver.is_visible(binding, target_module) {
382                     Some(Box::new(PrivacyError(directive.span, source, binding)))
383                 } else {
384                     None
385                 };
386
387                 self.define(module_, target, ns, directive.import(binding, privacy_error));
388             }
389             module_.decrement_outstanding_references_for(target, ns);
390         }
391
392         match (&value_result, &type_result) {
393             (&Indeterminate, _) | (_, &Indeterminate) => return Indeterminate,
394             (&Failed(_), &Failed(_)) => {
395                 let children = target_module.resolutions.borrow();
396                 let names = children.keys().map(|&(ref name, _)| name);
397                 let lev_suggestion = match find_best_match_for_name(names, &source.as_str(), None) {
398                     Some(name) => format!(". Did you mean to use `{}`?", name),
399                     None => "".to_owned(),
400                 };
401                 let msg = format!("There is no `{}` in `{}`{}",
402                                   source,
403                                   module_to_string(target_module), lev_suggestion);
404                 return Failed(Some((directive.span, msg)));
405             }
406             _ => (),
407         }
408
409         match (&value_result, &type_result) {
410             (&Success(name_binding), _) if !name_binding.is_import() &&
411                                            directive.is_public &&
412                                            !name_binding.is_public() => {
413                 let msg = format!("`{}` is private, and cannot be reexported", source);
414                 let note_msg = format!("consider marking `{}` as `pub` in the imported module",
415                                         source);
416                 struct_span_err!(self.resolver.session, directive.span, E0364, "{}", &msg)
417                     .span_note(directive.span, &note_msg)
418                     .emit();
419             }
420
421             (_, &Success(name_binding)) if !name_binding.is_import() && directive.is_public => {
422                 if !name_binding.is_public() {
423                     if name_binding.is_extern_crate() {
424                         let msg = format!("extern crate `{}` is private, and cannot be reexported \
425                                            (error E0364), consider declaring with `pub`",
426                                            source);
427                         self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
428                                                        directive.id,
429                                                        directive.span,
430                                                        msg);
431                     } else {
432                         let msg = format!("`{}` is private, and cannot be reexported", source);
433                         let note_msg =
434                             format!("consider declaring type or module `{}` with `pub`", source);
435                         struct_span_err!(self.resolver.session, directive.span, E0365, "{}", &msg)
436                             .span_note(directive.span, &note_msg)
437                             .emit();
438                     }
439                 } else if name_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
440                     let msg = format!("variant `{}` is private, and cannot be reexported \
441                                        (error E0364), consider declaring its enum as `pub`",
442                                        source);
443                     self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
444                                                    directive.id,
445                                                    directive.span,
446                                                    msg);
447                 }
448             }
449
450             _ => {}
451         }
452
453         // Report a privacy error here if all successful namespaces are privacy errors.
454         let mut privacy_error = None;
455         for &ns in &[ValueNS, TypeNS] {
456             privacy_error = match module_.resolve_name(target, ns, true) {
457                 Success(&NameBinding {
458                     kind: NameBindingKind::Import { ref privacy_error, .. }, ..
459                 }) => privacy_error.as_ref().map(|error| (**error).clone()),
460                 _ => continue,
461             };
462             if privacy_error.is_none() { break }
463         }
464         privacy_error.map(|error| self.resolver.privacy_errors.push(error));
465
466         // Record what this import resolves to for later uses in documentation,
467         // this may resolve to either a value or a type, but for documentation
468         // purposes it's good enough to just favor one over the other.
469         let def = match type_result.success().and_then(NameBinding::def) {
470             Some(def) => def,
471             None => value_result.success().and_then(NameBinding::def).unwrap(),
472         };
473         let path_resolution = PathResolution { base_def: def, depth: 0 };
474         self.resolver.def_map.borrow_mut().insert(directive.id, path_resolution);
475
476         debug!("(resolving single import) successfully resolved import");
477         return Success(());
478     }
479
480     // Resolves a glob import. Note that this function cannot fail; it either
481     // succeeds or bails out (as importing * from an empty module or a module
482     // that exports nothing is valid). target_module is the module we are
483     // actually importing, i.e., `foo` in `use foo::*`.
484     fn resolve_glob_import(&mut self,
485                            module_: Module<'b>,
486                            target_module: Module<'b>,
487                            directive: &ImportDirective)
488                            -> ResolveResult<()> {
489         // We must bail out if the node has unresolved imports of any kind (including globs).
490         if target_module.pub_count.get() > 0 {
491             debug!("(resolving glob import) target module has unresolved pub imports; bailing out");
492             return Indeterminate;
493         }
494
495         if module_.def_id() == target_module.def_id() {
496             // This means we are trying to glob import a module into itself, and it is a no-go
497             let msg = "Cannot glob-import a module into itself.".into();
498             return Failed(Some((directive.span, msg)));
499         }
500
501         // Add all children from the containing module.
502         build_reduced_graph::populate_module_if_necessary(self.resolver, target_module);
503         target_module.for_each_child(|name, ns, binding| {
504             if !binding.defined_with(DefModifiers::IMPORTABLE | DefModifiers::PUBLIC) { return }
505             self.define(module_, name, ns, directive.import(binding, None));
506
507             if ns == TypeNS && directive.is_public &&
508                binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
509                 let msg = format!("variant `{}` is private, and cannot be reexported (error \
510                                    E0364), consider declaring its enum as `pub`", name);
511                 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
512                                                directive.id,
513                                                directive.span,
514                                                msg);
515             }
516         });
517
518         // Record the destination of this import
519         if let Some(did) = target_module.def_id() {
520             self.resolver.def_map.borrow_mut().insert(directive.id,
521                                                       PathResolution {
522                                                           base_def: Def::Mod(did),
523                                                           depth: 0,
524                                                       });
525         }
526
527         debug!("(resolving glob import) successfully resolved import");
528         return Success(());
529     }
530
531     fn define(&mut self,
532               parent: Module<'b>,
533               name: Name,
534               ns: Namespace,
535               binding: NameBinding<'b>) {
536         if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
537             self.report_conflict(name, ns, &binding, old_binding);
538         } else if binding.is_public() { // Add to the export map
539             if let (Some(parent_def_id), Some(def)) = (parent.def_id(), binding.def()) {
540                 let parent_node_id = self.resolver.ast_map.as_local_node_id(parent_def_id).unwrap();
541                 let export = Export { name: name, def_id: def.def_id() };
542                 self.resolver.export_map.entry(parent_node_id).or_insert(Vec::new()).push(export);
543             }
544         }
545     }
546
547     fn report_conflict(&mut self,
548                        name: Name,
549                        ns: Namespace,
550                        binding: &NameBinding,
551                        old_binding: &NameBinding) {
552         // Error on the second of two conflicting imports
553         if old_binding.is_import() && binding.is_import() &&
554            old_binding.span.unwrap().lo > binding.span.unwrap().lo {
555             self.report_conflict(name, ns, old_binding, binding);
556             return;
557         }
558
559         if old_binding.is_extern_crate() {
560             let msg = format!("import `{0}` conflicts with imported crate \
561                                in this module (maybe you meant `use {0}::*`?)",
562                               name);
563             span_err!(self.resolver.session, binding.span.unwrap(), E0254, "{}", &msg);
564         } else if old_binding.is_import() {
565             let ns_word = match (ns, old_binding.module()) {
566                 (ValueNS, _) => "value",
567                 (TypeNS, Some(module)) if module.is_normal() => "module",
568                 (TypeNS, Some(module)) if module.is_trait() => "trait",
569                 (TypeNS, _) => "type",
570             };
571             let mut err = struct_span_err!(self.resolver.session,
572                                            binding.span.unwrap(),
573                                            E0252,
574                                            "a {} named `{}` has already been imported \
575                                             in this module",
576                                            ns_word,
577                                            name);
578             err.span_note(old_binding.span.unwrap(),
579                           &format!("previous import of `{}` here", name));
580             err.emit();
581         } else if ns == ValueNS { // Check for item conflicts in the value namespace
582             let mut err = struct_span_err!(self.resolver.session,
583                                            binding.span.unwrap(),
584                                            E0255,
585                                            "import `{}` conflicts with value in this module",
586                                            name);
587             err.span_note(old_binding.span.unwrap(), "conflicting value here");
588             err.emit();
589         } else { // Check for item conflicts in the type namespace
590             let (what, note) = match old_binding.module() {
591                 Some(ref module) if module.is_normal() =>
592                     ("existing submodule", "note conflicting module here"),
593                 Some(ref module) if module.is_trait() =>
594                     ("trait in this module", "note conflicting trait here"),
595                 _ => ("type in this module", "note conflicting type here"),
596             };
597             let mut err = struct_span_err!(self.resolver.session,
598                                            binding.span.unwrap(),
599                                            E0256,
600                                            "import `{}` conflicts with {}",
601                                            name,
602                                            what);
603             err.span_note(old_binding.span.unwrap(), note);
604             err.emit();
605         }
606     }
607 }
608
609 fn import_path_to_string(names: &[Name], subclass: &ImportDirectiveSubclass) -> String {
610     if names.is_empty() {
611         import_directive_subclass_to_string(subclass)
612     } else {
613         (format!("{}::{}",
614                  names_to_string(names),
615                  import_directive_subclass_to_string(subclass)))
616             .to_string()
617     }
618 }
619
620 fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> String {
621     match *subclass {
622         SingleImport { source, .. } => source.to_string(),
623         GlobImport => "*".to_string(),
624     }
625 }
626
627 pub fn resolve_imports(resolver: &mut Resolver) {
628     let mut import_resolver = ImportResolver { resolver: resolver };
629     import_resolver.resolve_imports();
630 }