]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/hir-def/src/find_path.rs
Rollup merge of #101049 - JeanCASPAR:remove-span_fatal-from-ast_lowering, r=davidtwco
[rust.git] / src / tools / rust-analyzer / crates / hir-def / src / find_path.rs
1 //! An algorithm to find a path to refer to a certain item.
2
3 use std::iter;
4
5 use hir_expand::name::{known, AsName, Name};
6 use rustc_hash::FxHashSet;
7
8 use crate::{
9     db::DefDatabase,
10     item_scope::ItemInNs,
11     nameres::DefMap,
12     path::{ModPath, PathKind},
13     visibility::Visibility,
14     ModuleDefId, ModuleId,
15 };
16
17 /// Find a path that can be used to refer to a certain item. This can depend on
18 /// *from where* you're referring to the item, hence the `from` parameter.
19 pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
20     let _p = profile::span("find_path");
21     find_path_inner(db, item, from, None)
22 }
23
24 pub fn find_path_prefixed(
25     db: &dyn DefDatabase,
26     item: ItemInNs,
27     from: ModuleId,
28     prefix_kind: PrefixKind,
29 ) -> Option<ModPath> {
30     let _p = profile::span("find_path_prefixed");
31     find_path_inner(db, item, from, Some(prefix_kind))
32 }
33
34 const MAX_PATH_LEN: usize = 15;
35
36 trait ModPathExt {
37     fn starts_with_std(&self) -> bool;
38     fn can_start_with_std(&self) -> bool;
39 }
40
41 impl ModPathExt for ModPath {
42     fn starts_with_std(&self) -> bool {
43         self.segments().first() == Some(&known::std)
44     }
45
46     // Can we replace the first segment with `std::` and still get a valid, identical path?
47     fn can_start_with_std(&self) -> bool {
48         let first_segment = self.segments().first();
49         first_segment == Some(&known::alloc) || first_segment == Some(&known::core)
50     }
51 }
52
53 fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
54     if item == ItemInNs::Types(from.into()) {
55         // - if the item is the module we're in, use `self`
56         Some(ModPath::from_segments(PathKind::Super(0), None))
57     } else if let Some(parent_id) = def_map[from.local_id].parent {
58         // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
59         let parent_id = def_map.module_id(parent_id);
60         if item == ItemInNs::Types(ModuleDefId::ModuleId(parent_id)) {
61             Some(ModPath::from_segments(PathKind::Super(1), None))
62         } else {
63             None
64         }
65     } else {
66         None
67     }
68 }
69
70 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
71 pub enum PrefixKind {
72     /// Causes paths to always start with either `self`, `super`, `crate` or a crate-name.
73     /// This is the same as plain, just that paths will start with `self` iprepended f the path
74     /// starts with an identifier that is not a crate.
75     BySelf,
76     /// Causes paths to ignore imports in the local module.
77     Plain,
78     /// Causes paths to start with `crate` where applicable, effectively forcing paths to be absolute.
79     ByCrate,
80 }
81
82 impl PrefixKind {
83     #[inline]
84     fn prefix(self) -> PathKind {
85         match self {
86             PrefixKind::BySelf => PathKind::Super(0),
87             PrefixKind::Plain => PathKind::Plain,
88             PrefixKind::ByCrate => PathKind::Crate,
89         }
90     }
91
92     #[inline]
93     fn is_absolute(&self) -> bool {
94         self == &PrefixKind::ByCrate
95     }
96 }
97 /// Attempts to find a path to refer to the given `item` visible from the `from` ModuleId
98 fn find_path_inner(
99     db: &dyn DefDatabase,
100     item: ItemInNs,
101     from: ModuleId,
102     prefixed: Option<PrefixKind>,
103 ) -> Option<ModPath> {
104     // FIXME: Do fast path for std/core libs?
105
106     let mut visited_modules = FxHashSet::default();
107     let def_map = from.def_map(db);
108     find_path_inner_(db, &def_map, from, item, MAX_PATH_LEN, prefixed, &mut visited_modules)
109 }
110
111 fn find_path_inner_(
112     db: &dyn DefDatabase,
113     def_map: &DefMap,
114     from: ModuleId,
115     item: ItemInNs,
116     max_len: usize,
117     mut prefixed: Option<PrefixKind>,
118     visited_modules: &mut FxHashSet<ModuleId>,
119 ) -> Option<ModPath> {
120     if max_len == 0 {
121         return None;
122     }
123
124     // Base cases:
125
126     // - if the item is already in scope, return the name under which it is
127     let scope_name = def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
128         def_map[local_id].scope.name_of(item).map(|(name, _)| name.clone())
129     });
130     if prefixed.is_none() {
131         if let Some(scope_name) = scope_name {
132             return Some(ModPath::from_segments(PathKind::Plain, Some(scope_name)));
133         }
134     }
135
136     // - if the item is a builtin, it's in scope
137     if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item {
138         return Some(ModPath::from_segments(PathKind::Plain, Some(builtin.as_name())));
139     }
140
141     // - if the item is the crate root, return `crate`
142     let crate_root = def_map.crate_root(db);
143     if item == ItemInNs::Types(ModuleDefId::ModuleId(crate_root)) {
144         return Some(ModPath::from_segments(PathKind::Crate, None));
145     }
146
147     if prefixed.filter(PrefixKind::is_absolute).is_none() {
148         if let modpath @ Some(_) = check_self_super(&def_map, item, from) {
149             return modpath;
150         }
151     }
152
153     // - if the item is the crate root of a dependency crate, return the name from the extern prelude
154     let root_def_map = crate_root.def_map(db);
155     if let ItemInNs::Types(ModuleDefId::ModuleId(item)) = item {
156         for (name, &def_id) in root_def_map.extern_prelude() {
157             if item == def_id {
158                 let name = scope_name.unwrap_or_else(|| name.clone());
159
160                 let name_already_occupied_in_type_ns = def_map
161                     .with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
162                         def_map[local_id]
163                             .scope
164                             .type_(&name)
165                             .filter(|&(id, _)| id != ModuleDefId::ModuleId(def_id))
166                     })
167                     .is_some();
168                 let kind = if name_already_occupied_in_type_ns {
169                     cov_mark::hit!(ambiguous_crate_start);
170                     PathKind::Abs
171                 } else {
172                     PathKind::Plain
173                 };
174                 return Some(ModPath::from_segments(kind, Some(name)));
175             }
176         }
177     }
178
179     // - if the item is in the prelude, return the name from there
180     if let Some(prelude_module) = root_def_map.prelude() {
181         // Preludes in block DefMaps are ignored, only the crate DefMap is searched
182         let prelude_def_map = prelude_module.def_map(db);
183         let prelude_scope = &prelude_def_map[prelude_module.local_id].scope;
184         if let Some((name, vis)) = prelude_scope.name_of(item) {
185             if vis.is_visible_from(db, from) {
186                 return Some(ModPath::from_segments(PathKind::Plain, Some(name.clone())));
187             }
188         }
189     }
190
191     // Recursive case:
192     // - if the item is an enum variant, refer to it via the enum
193     if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
194         if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) {
195             let data = db.enum_data(variant.parent);
196             path.push_segment(data.variants[variant.local_id].name.clone());
197             return Some(path);
198         }
199         // If this doesn't work, it seems we have no way of referring to the
200         // enum; that's very weird, but there might still be a reexport of the
201         // variant somewhere
202     }
203
204     // - otherwise, look for modules containing (reexporting) it and import it from one of those
205     let prefer_no_std = db.crate_supports_no_std(crate_root.krate);
206     let mut best_path = None;
207     let mut best_path_len = max_len;
208
209     if item.krate(db) == Some(from.krate) {
210         // Item was defined in the same crate that wants to import it. It cannot be found in any
211         // dependency in this case.
212         // FIXME: this should have a fast path that doesn't look through the prelude again?
213         for (module_id, name) in find_local_import_locations(db, item, from) {
214             if !visited_modules.insert(module_id) {
215                 cov_mark::hit!(recursive_imports);
216                 continue;
217             }
218             if let Some(mut path) = find_path_inner_(
219                 db,
220                 def_map,
221                 from,
222                 ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
223                 best_path_len - 1,
224                 prefixed,
225                 visited_modules,
226             ) {
227                 path.push_segment(name);
228
229                 let new_path = match best_path {
230                     Some(best_path) => select_best_path(best_path, path, prefer_no_std),
231                     None => path,
232                 };
233                 best_path_len = new_path.len();
234                 best_path = Some(new_path);
235             }
236         }
237     } else {
238         // Item was defined in some upstream crate. This means that it must be exported from one,
239         // too (unless we can't name it at all). It could *also* be (re)exported by the same crate
240         // that wants to import it here, but we always prefer to use the external path here.
241
242         let crate_graph = db.crate_graph();
243         let extern_paths = crate_graph[from.krate].dependencies.iter().filter_map(|dep| {
244             let import_map = db.import_map(dep.crate_id);
245             import_map.import_info_for(item).and_then(|info| {
246                 // Determine best path for containing module and append last segment from `info`.
247                 // FIXME: we should guide this to look up the path locally, or from the same crate again?
248                 let mut path = find_path_inner_(
249                     db,
250                     def_map,
251                     from,
252                     ItemInNs::Types(ModuleDefId::ModuleId(info.container)),
253                     best_path_len - 1,
254                     prefixed,
255                     visited_modules,
256                 )?;
257                 cov_mark::hit!(partially_imported);
258                 path.push_segment(info.path.segments.last()?.clone());
259                 Some(path)
260             })
261         });
262
263         for path in extern_paths {
264             let new_path = match best_path {
265                 Some(best_path) => select_best_path(best_path, path, prefer_no_std),
266                 None => path,
267             };
268             best_path = Some(new_path);
269         }
270     }
271
272     // If the item is declared inside a block expression, don't use a prefix, as we don't handle
273     // that correctly (FIXME).
274     if let Some(item_module) = item.as_module_def_id().and_then(|did| did.module(db)) {
275         if item_module.def_map(db).block_id().is_some() && prefixed.is_some() {
276             cov_mark::hit!(prefixed_in_block_expression);
277             prefixed = Some(PrefixKind::Plain);
278         }
279     }
280
281     match prefixed.map(PrefixKind::prefix) {
282         Some(prefix) => best_path.or_else(|| {
283             scope_name.map(|scope_name| ModPath::from_segments(prefix, Some(scope_name)))
284         }),
285         None => best_path,
286     }
287 }
288
289 fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath {
290     if old_path.starts_with_std() && new_path.can_start_with_std() {
291         if prefer_no_std {
292             cov_mark::hit!(prefer_no_std_paths);
293             new_path
294         } else {
295             cov_mark::hit!(prefer_std_paths);
296             old_path
297         }
298     } else if new_path.starts_with_std() && old_path.can_start_with_std() {
299         if prefer_no_std {
300             cov_mark::hit!(prefer_no_std_paths);
301             old_path
302         } else {
303             cov_mark::hit!(prefer_std_paths);
304             new_path
305         }
306     } else if new_path.len() < old_path.len() {
307         new_path
308     } else {
309         old_path
310     }
311 }
312
313 /// Finds locations in `from.krate` from which `item` can be imported by `from`.
314 fn find_local_import_locations(
315     db: &dyn DefDatabase,
316     item: ItemInNs,
317     from: ModuleId,
318 ) -> Vec<(ModuleId, Name)> {
319     let _p = profile::span("find_local_import_locations");
320
321     // `from` can import anything below `from` with visibility of at least `from`, and anything
322     // above `from` with any visibility. That means we do not need to descend into private siblings
323     // of `from` (and similar).
324
325     let def_map = from.def_map(db);
326
327     // Compute the initial worklist. We start with all direct child modules of `from` as well as all
328     // of its (recursive) parent modules.
329     let data = &def_map[from.local_id];
330     let mut worklist =
331         data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>();
332     // FIXME: do we need to traverse out of block expressions here?
333     for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) {
334         worklist.push(ancestor);
335     }
336
337     let def_map = def_map.crate_root(db).def_map(db);
338
339     let mut seen: FxHashSet<_> = FxHashSet::default();
340
341     let mut locations = Vec::new();
342     while let Some(module) = worklist.pop() {
343         if !seen.insert(module) {
344             continue; // already processed this module
345         }
346
347         let ext_def_map;
348         let data = if module.krate == from.krate {
349             if module.block.is_some() {
350                 // Re-query the block's DefMap
351                 ext_def_map = module.def_map(db);
352                 &ext_def_map[module.local_id]
353             } else {
354                 // Reuse the root DefMap
355                 &def_map[module.local_id]
356             }
357         } else {
358             // The crate might reexport a module defined in another crate.
359             ext_def_map = module.def_map(db);
360             &ext_def_map[module.local_id]
361         };
362
363         if let Some((name, vis)) = data.scope.name_of(item) {
364             if vis.is_visible_from(db, from) {
365                 let is_private = match vis {
366                     Visibility::Module(private_to) => private_to.local_id == module.local_id,
367                     Visibility::Public => false,
368                 };
369                 let is_original_def = match item.as_module_def_id() {
370                     Some(module_def_id) => data.scope.declarations().any(|it| it == module_def_id),
371                     None => false,
372                 };
373
374                 // Ignore private imports. these could be used if we are
375                 // in a submodule of this module, but that's usually not
376                 // what the user wants; and if this module can import
377                 // the item and we're a submodule of it, so can we.
378                 // Also this keeps the cached data smaller.
379                 if !is_private || is_original_def {
380                     locations.push((module, name.clone()));
381                 }
382             }
383         }
384
385         // Descend into all modules visible from `from`.
386         for (ty, vis) in data.scope.types() {
387             if let ModuleDefId::ModuleId(module) = ty {
388                 if vis.is_visible_from(db, from) {
389                     worklist.push(module);
390                 }
391             }
392         }
393     }
394
395     locations
396 }
397
398 #[cfg(test)]
399 mod tests {
400     use base_db::fixture::WithFixture;
401     use hir_expand::hygiene::Hygiene;
402     use syntax::ast::AstNode;
403
404     use crate::test_db::TestDB;
405
406     use super::*;
407
408     /// `code` needs to contain a cursor marker; checks that `find_path` for the
409     /// item the `path` refers to returns that same path when called from the
410     /// module the cursor is in.
411     fn check_found_path_(ra_fixture: &str, path: &str, prefix_kind: Option<PrefixKind>) {
412         let (db, pos) = TestDB::with_position(ra_fixture);
413         let module = db.module_at_position(pos);
414         let parsed_path_file = syntax::SourceFile::parse(&format!("use {};", path));
415         let ast_path =
416             parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
417         let mod_path = ModPath::from_src(&db, ast_path, &Hygiene::new_unhygienic()).unwrap();
418
419         let def_map = module.def_map(&db);
420         let resolved = def_map
421             .resolve_path(
422                 &db,
423                 module.local_id,
424                 &mod_path,
425                 crate::item_scope::BuiltinShadowMode::Module,
426             )
427             .0
428             .take_types()
429             .unwrap();
430
431         let found_path = find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind);
432         assert_eq!(found_path, Some(mod_path), "{:?}", prefix_kind);
433     }
434
435     fn check_found_path(
436         ra_fixture: &str,
437         unprefixed: &str,
438         prefixed: &str,
439         absolute: &str,
440         self_prefixed: &str,
441     ) {
442         check_found_path_(ra_fixture, unprefixed, None);
443         check_found_path_(ra_fixture, prefixed, Some(PrefixKind::Plain));
444         check_found_path_(ra_fixture, absolute, Some(PrefixKind::ByCrate));
445         check_found_path_(ra_fixture, self_prefixed, Some(PrefixKind::BySelf));
446     }
447
448     #[test]
449     fn same_module() {
450         check_found_path(
451             r#"
452 struct S;
453 $0
454         "#,
455             "S",
456             "S",
457             "crate::S",
458             "self::S",
459         );
460     }
461
462     #[test]
463     fn enum_variant() {
464         check_found_path(
465             r#"
466 enum E { A }
467 $0
468         "#,
469             "E::A",
470             "E::A",
471             "E::A",
472             "E::A",
473         );
474     }
475
476     #[test]
477     fn sub_module() {
478         check_found_path(
479             r#"
480 mod foo {
481     pub struct S;
482 }
483 $0
484         "#,
485             "foo::S",
486             "foo::S",
487             "crate::foo::S",
488             "self::foo::S",
489         );
490     }
491
492     #[test]
493     fn super_module() {
494         check_found_path(
495             r#"
496 //- /main.rs
497 mod foo;
498 //- /foo.rs
499 mod bar;
500 struct S;
501 //- /foo/bar.rs
502 $0
503         "#,
504             "super::S",
505             "super::S",
506             "crate::foo::S",
507             "super::S",
508         );
509     }
510
511     #[test]
512     fn self_module() {
513         check_found_path(
514             r#"
515 //- /main.rs
516 mod foo;
517 //- /foo.rs
518 $0
519         "#,
520             "self",
521             "self",
522             "crate::foo",
523             "self",
524         );
525     }
526
527     #[test]
528     fn crate_root() {
529         check_found_path(
530             r#"
531 //- /main.rs
532 mod foo;
533 //- /foo.rs
534 $0
535         "#,
536             "crate",
537             "crate",
538             "crate",
539             "crate",
540         );
541     }
542
543     #[test]
544     fn same_crate() {
545         check_found_path(
546             r#"
547 //- /main.rs
548 mod foo;
549 struct S;
550 //- /foo.rs
551 $0
552         "#,
553             "crate::S",
554             "crate::S",
555             "crate::S",
556             "crate::S",
557         );
558     }
559
560     #[test]
561     fn different_crate() {
562         check_found_path(
563             r#"
564 //- /main.rs crate:main deps:std
565 $0
566 //- /std.rs crate:std
567 pub struct S;
568         "#,
569             "std::S",
570             "std::S",
571             "std::S",
572             "std::S",
573         );
574     }
575
576     #[test]
577     fn different_crate_renamed() {
578         check_found_path(
579             r#"
580 //- /main.rs crate:main deps:std
581 extern crate std as std_renamed;
582 $0
583 //- /std.rs crate:std
584 pub struct S;
585         "#,
586             "std_renamed::S",
587             "std_renamed::S",
588             "std_renamed::S",
589             "std_renamed::S",
590         );
591     }
592
593     #[test]
594     fn partially_imported() {
595         cov_mark::check!(partially_imported);
596         // Tests that short paths are used even for external items, when parts of the path are
597         // already in scope.
598         check_found_path(
599             r#"
600 //- /main.rs crate:main deps:syntax
601
602 use syntax::ast;
603 $0
604
605 //- /lib.rs crate:syntax
606 pub mod ast {
607     pub enum ModuleItem {
608         A, B, C,
609     }
610 }
611         "#,
612             "ast::ModuleItem",
613             "syntax::ast::ModuleItem",
614             "syntax::ast::ModuleItem",
615             "syntax::ast::ModuleItem",
616         );
617
618         check_found_path(
619             r#"
620 //- /main.rs crate:main deps:syntax
621 $0
622
623 //- /lib.rs crate:syntax
624 pub mod ast {
625     pub enum ModuleItem {
626         A, B, C,
627     }
628 }
629         "#,
630             "syntax::ast::ModuleItem",
631             "syntax::ast::ModuleItem",
632             "syntax::ast::ModuleItem",
633             "syntax::ast::ModuleItem",
634         );
635     }
636
637     #[test]
638     fn same_crate_reexport() {
639         check_found_path(
640             r#"
641 mod bar {
642     mod foo { pub(super) struct S; }
643     pub(crate) use foo::*;
644 }
645 $0
646         "#,
647             "bar::S",
648             "bar::S",
649             "crate::bar::S",
650             "self::bar::S",
651         );
652     }
653
654     #[test]
655     fn same_crate_reexport_rename() {
656         check_found_path(
657             r#"
658 mod bar {
659     mod foo { pub(super) struct S; }
660     pub(crate) use foo::S as U;
661 }
662 $0
663         "#,
664             "bar::U",
665             "bar::U",
666             "crate::bar::U",
667             "self::bar::U",
668         );
669     }
670
671     #[test]
672     fn different_crate_reexport() {
673         check_found_path(
674             r#"
675 //- /main.rs crate:main deps:std
676 $0
677 //- /std.rs crate:std deps:core
678 pub use core::S;
679 //- /core.rs crate:core
680 pub struct S;
681         "#,
682             "std::S",
683             "std::S",
684             "std::S",
685             "std::S",
686         );
687     }
688
689     #[test]
690     fn prelude() {
691         check_found_path(
692             r#"
693 //- /main.rs crate:main deps:std
694 $0
695 //- /std.rs crate:std
696 pub mod prelude {
697     pub mod rust_2018 {
698         pub struct S;
699     }
700 }
701         "#,
702             "S",
703             "S",
704             "S",
705             "S",
706         );
707     }
708
709     #[test]
710     fn enum_variant_from_prelude() {
711         let code = r#"
712 //- /main.rs crate:main deps:std
713 $0
714 //- /std.rs crate:std
715 pub mod prelude {
716     pub mod rust_2018 {
717         pub enum Option<T> { Some(T), None }
718         pub use Option::*;
719     }
720 }
721         "#;
722         check_found_path(code, "None", "None", "None", "None");
723         check_found_path(code, "Some", "Some", "Some", "Some");
724     }
725
726     #[test]
727     fn shortest_path() {
728         check_found_path(
729             r#"
730 //- /main.rs
731 pub mod foo;
732 pub mod baz;
733 struct S;
734 $0
735 //- /foo.rs
736 pub mod bar { pub struct S; }
737 //- /baz.rs
738 pub use crate::foo::bar::S;
739         "#,
740             "baz::S",
741             "baz::S",
742             "crate::baz::S",
743             "self::baz::S",
744         );
745     }
746
747     #[test]
748     fn discount_private_imports() {
749         check_found_path(
750             r#"
751 //- /main.rs
752 mod foo;
753 pub mod bar { pub struct S; }
754 use bar::S;
755 //- /foo.rs
756 $0
757         "#,
758             // crate::S would be shorter, but using private imports seems wrong
759             "crate::bar::S",
760             "crate::bar::S",
761             "crate::bar::S",
762             "crate::bar::S",
763         );
764     }
765
766     #[test]
767     fn import_cycle() {
768         check_found_path(
769             r#"
770 //- /main.rs
771 pub mod foo;
772 pub mod bar;
773 pub mod baz;
774 //- /bar.rs
775 $0
776 //- /foo.rs
777 pub use super::baz;
778 pub struct S;
779 //- /baz.rs
780 pub use super::foo;
781         "#,
782             "crate::foo::S",
783             "crate::foo::S",
784             "crate::foo::S",
785             "crate::foo::S",
786         );
787     }
788
789     #[test]
790     fn prefer_std_paths_over_alloc() {
791         cov_mark::check!(prefer_std_paths);
792         check_found_path(
793             r#"
794 //- /main.rs crate:main deps:alloc,std
795 $0
796
797 //- /std.rs crate:std deps:alloc
798 pub mod sync {
799     pub use alloc::sync::Arc;
800 }
801
802 //- /zzz.rs crate:alloc
803 pub mod sync {
804     pub struct Arc;
805 }
806         "#,
807             "std::sync::Arc",
808             "std::sync::Arc",
809             "std::sync::Arc",
810             "std::sync::Arc",
811         );
812     }
813
814     #[test]
815     fn prefer_core_paths_over_std() {
816         cov_mark::check!(prefer_no_std_paths);
817         check_found_path(
818             r#"
819 //- /main.rs crate:main deps:core,std
820 #![no_std]
821
822 $0
823
824 //- /std.rs crate:std deps:core
825
826 pub mod fmt {
827     pub use core::fmt::Error;
828 }
829
830 //- /zzz.rs crate:core
831
832 pub mod fmt {
833     pub struct Error;
834 }
835         "#,
836             "core::fmt::Error",
837             "core::fmt::Error",
838             "core::fmt::Error",
839             "core::fmt::Error",
840         );
841
842         // Should also work (on a best-effort basis) if `no_std` is conditional.
843         check_found_path(
844             r#"
845 //- /main.rs crate:main deps:core,std
846 #![cfg_attr(not(test), no_std)]
847
848 $0
849
850 //- /std.rs crate:std deps:core
851
852 pub mod fmt {
853     pub use core::fmt::Error;
854 }
855
856 //- /zzz.rs crate:core
857
858 pub mod fmt {
859     pub struct Error;
860 }
861         "#,
862             "core::fmt::Error",
863             "core::fmt::Error",
864             "core::fmt::Error",
865             "core::fmt::Error",
866         );
867     }
868
869     #[test]
870     fn prefer_alloc_paths_over_std() {
871         check_found_path(
872             r#"
873 //- /main.rs crate:main deps:alloc,std
874 #![no_std]
875
876 $0
877
878 //- /std.rs crate:std deps:alloc
879
880 pub mod sync {
881     pub use alloc::sync::Arc;
882 }
883
884 //- /zzz.rs crate:alloc
885
886 pub mod sync {
887     pub struct Arc;
888 }
889             "#,
890             "alloc::sync::Arc",
891             "alloc::sync::Arc",
892             "alloc::sync::Arc",
893             "alloc::sync::Arc",
894         );
895     }
896
897     #[test]
898     fn prefer_shorter_paths_if_not_alloc() {
899         check_found_path(
900             r#"
901 //- /main.rs crate:main deps:megaalloc,std
902 $0
903
904 //- /std.rs crate:std deps:megaalloc
905 pub mod sync {
906     pub use megaalloc::sync::Arc;
907 }
908
909 //- /zzz.rs crate:megaalloc
910 pub struct Arc;
911             "#,
912             "megaalloc::Arc",
913             "megaalloc::Arc",
914             "megaalloc::Arc",
915             "megaalloc::Arc",
916         );
917     }
918
919     #[test]
920     fn builtins_are_in_scope() {
921         let code = r#"
922 $0
923
924 pub mod primitive {
925     pub use u8;
926 }
927         "#;
928         check_found_path(code, "u8", "u8", "u8", "u8");
929         check_found_path(code, "u16", "u16", "u16", "u16");
930     }
931
932     #[test]
933     fn inner_items() {
934         check_found_path(
935             r#"
936 fn main() {
937     struct Inner {}
938     $0
939 }
940         "#,
941             "Inner",
942             "Inner",
943             "Inner",
944             "Inner",
945         );
946     }
947
948     #[test]
949     fn inner_items_from_outer_scope() {
950         check_found_path(
951             r#"
952 fn main() {
953     struct Struct {}
954     {
955         $0
956     }
957 }
958         "#,
959             "Struct",
960             "Struct",
961             "Struct",
962             "Struct",
963         );
964     }
965
966     #[test]
967     fn inner_items_from_inner_module() {
968         cov_mark::check!(prefixed_in_block_expression);
969         check_found_path(
970             r#"
971 fn main() {
972     mod module {
973         struct Struct {}
974     }
975     {
976         $0
977     }
978 }
979         "#,
980             "module::Struct",
981             "module::Struct",
982             "module::Struct",
983             "module::Struct",
984         );
985     }
986
987     #[test]
988     fn outer_items_with_inner_items_present() {
989         check_found_path(
990             r#"
991 mod module {
992     pub struct CompleteMe;
993 }
994
995 fn main() {
996     fn inner() {}
997     $0
998 }
999             "#,
1000             // FIXME: these could use fewer/better prefixes
1001             "module::CompleteMe",
1002             "crate::module::CompleteMe",
1003             "crate::module::CompleteMe",
1004             "crate::module::CompleteMe",
1005         )
1006     }
1007
1008     #[test]
1009     fn from_inside_module() {
1010         // This worked correctly, but the test suite logic was broken.
1011         cov_mark::check!(submodule_in_testdb);
1012         check_found_path(
1013             r#"
1014 mod baz {
1015     pub struct Foo {}
1016 }
1017
1018 mod bar {
1019     fn bar() {
1020         $0
1021     }
1022 }
1023             "#,
1024             "crate::baz::Foo",
1025             "crate::baz::Foo",
1026             "crate::baz::Foo",
1027             "crate::baz::Foo",
1028         )
1029     }
1030
1031     #[test]
1032     fn from_inside_module_with_inner_items() {
1033         check_found_path(
1034             r#"
1035 mod baz {
1036     pub struct Foo {}
1037 }
1038
1039 mod bar {
1040     fn bar() {
1041         fn inner() {}
1042         $0
1043     }
1044 }
1045             "#,
1046             "crate::baz::Foo",
1047             "crate::baz::Foo",
1048             "crate::baz::Foo",
1049             "crate::baz::Foo",
1050         )
1051     }
1052
1053     #[test]
1054     fn recursive_pub_mod_reexport() {
1055         cov_mark::check!(recursive_imports);
1056         check_found_path(
1057             r#"
1058 fn main() {
1059     let _ = 22_i32.as_name$0();
1060 }
1061
1062 pub mod name {
1063     pub trait AsName {
1064         fn as_name(&self) -> String;
1065     }
1066     impl AsName for i32 {
1067         fn as_name(&self) -> String {
1068             format!("Name: {}", self)
1069         }
1070     }
1071     pub use crate::name;
1072 }
1073 "#,
1074             "name::AsName",
1075             "name::AsName",
1076             "crate::name::AsName",
1077             "self::name::AsName",
1078         );
1079     }
1080
1081     #[test]
1082     fn extern_crate() {
1083         check_found_path(
1084             r#"
1085 //- /main.rs crate:main deps:dep
1086 $0
1087 //- /dep.rs crate:dep
1088 "#,
1089             "dep",
1090             "dep",
1091             "dep",
1092             "dep",
1093         );
1094
1095         check_found_path(
1096             r#"
1097 //- /main.rs crate:main deps:dep
1098 fn f() {
1099     fn inner() {}
1100     $0
1101 }
1102 //- /dep.rs crate:dep
1103 "#,
1104             "dep",
1105             "dep",
1106             "dep",
1107             "dep",
1108         );
1109     }
1110
1111     #[test]
1112     fn prelude_with_inner_items() {
1113         check_found_path(
1114             r#"
1115 //- /main.rs crate:main deps:std
1116 fn f() {
1117     fn inner() {}
1118     $0
1119 }
1120 //- /std.rs crate:std
1121 pub mod prelude {
1122     pub mod rust_2018 {
1123         pub enum Option { None }
1124         pub use Option::*;
1125     }
1126 }
1127         "#,
1128             "None",
1129             "None",
1130             "None",
1131             "None",
1132         );
1133     }
1134 }