]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/find_path.rs
Merge #7615
[rust.git] / crates / hir_def / src / find_path.rs
1 //! An algorithm to find a path to refer to a certain item.
2
3 use hir_expand::name::{known, AsName, Name};
4 use rustc_hash::FxHashSet;
5 use test_utils::mark;
6
7 use crate::nameres::DefMap;
8 use crate::{
9     db::DefDatabase,
10     item_scope::ItemInNs,
11     path::{ModPath, PathKind},
12     visibility::Visibility,
13     ModuleDefId, ModuleId,
14 };
15
16 /// Find a path that can be used to refer to a certain item. This can depend on
17 /// *from where* you're referring to the item, hence the `from` parameter.
18 pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
19     let _p = profile::span("find_path");
20     find_path_inner(db, item, from, MAX_PATH_LEN, None)
21 }
22
23 pub fn find_path_prefixed(
24     db: &dyn DefDatabase,
25     item: ItemInNs,
26     from: ModuleId,
27     prefix_kind: PrefixKind,
28 ) -> Option<ModPath> {
29     let _p = profile::span("find_path_prefixed");
30     find_path_inner(db, item, from, MAX_PATH_LEN, Some(prefix_kind))
31 }
32
33 const MAX_PATH_LEN: usize = 15;
34
35 impl ModPath {
36     fn starts_with_std(&self) -> bool {
37         self.segments().first() == Some(&known::std)
38     }
39
40     // When std library is present, paths starting with `std::`
41     // should be preferred over paths starting with `core::` and `alloc::`
42     fn can_start_with_std(&self) -> bool {
43         let first_segment = self.segments().first();
44         first_segment == Some(&known::alloc) || first_segment == Some(&known::core)
45     }
46 }
47
48 fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
49     if item == ItemInNs::Types(from.into()) {
50         // - if the item is the module we're in, use `self`
51         Some(ModPath::from_segments(PathKind::Super(0), Vec::new()))
52     } else if let Some(parent_id) = def_map[from.local_id].parent {
53         // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
54         let parent_id = def_map.module_id(parent_id);
55         if item == ItemInNs::Types(ModuleDefId::ModuleId(parent_id)) {
56             Some(ModPath::from_segments(PathKind::Super(1), Vec::new()))
57         } else {
58             None
59         }
60     } else {
61         None
62     }
63 }
64
65 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
66 pub enum PrefixKind {
67     /// Causes paths to always start with either `self`, `super`, `crate` or a crate-name.
68     /// This is the same as plain, just that paths will start with `self` iprepended f the path
69     /// starts with an identifier that is not a crate.
70     BySelf,
71     /// Causes paths to ignore imports in the local module.
72     Plain,
73     /// Causes paths to start with `crate` where applicable, effectively forcing paths to be absolute.
74     ByCrate,
75 }
76
77 impl PrefixKind {
78     #[inline]
79     fn prefix(self) -> PathKind {
80         match self {
81             PrefixKind::BySelf => PathKind::Super(0),
82             PrefixKind::Plain => PathKind::Plain,
83             PrefixKind::ByCrate => PathKind::Crate,
84         }
85     }
86
87     #[inline]
88     fn is_absolute(&self) -> bool {
89         self == &PrefixKind::ByCrate
90     }
91 }
92
93 fn find_path_inner(
94     db: &dyn DefDatabase,
95     item: ItemInNs,
96     from: ModuleId,
97     max_len: usize,
98     prefixed: Option<PrefixKind>,
99 ) -> Option<ModPath> {
100     if max_len == 0 {
101         return None;
102     }
103
104     // Base cases:
105
106     // - if the item is already in scope, return the name under which it is
107     let def_map = from.def_map(db);
108     let scope_name = def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
109         def_map[local_id].scope.name_of(item).map(|(name, _)| name.clone())
110     });
111     if prefixed.is_none() && scope_name.is_some() {
112         return scope_name
113             .map(|scope_name| ModPath::from_segments(PathKind::Plain, vec![scope_name]));
114     }
115
116     // - if the item is the crate root, return `crate`
117     let root = def_map.module_id(def_map.root());
118     if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() {
119         return Some(ModPath::from_segments(PathKind::Crate, Vec::new()));
120     }
121
122     if prefixed.filter(PrefixKind::is_absolute).is_none() {
123         if let modpath @ Some(_) = check_self_super(&def_map, item, from) {
124             return modpath;
125         }
126     }
127
128     // - if the item is the crate root of a dependency crate, return the name from the extern prelude
129     for (name, def_id) in def_map.extern_prelude() {
130         if item == ItemInNs::Types(*def_id) {
131             let name = scope_name.unwrap_or_else(|| name.clone());
132             return Some(ModPath::from_segments(PathKind::Plain, vec![name]));
133         }
134     }
135
136     // - if the item is in the prelude, return the name from there
137     if let Some(prelude_module) = def_map.prelude() {
138         let prelude_def_map = prelude_module.def_map(db);
139         let prelude_scope: &crate::item_scope::ItemScope =
140             &prelude_def_map[prelude_module.local_id].scope;
141         if let Some((name, vis)) = prelude_scope.name_of(item) {
142             if vis.is_visible_from(db, from) {
143                 return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
144             }
145         }
146     }
147
148     // - if the item is a builtin, it's in scope
149     if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item {
150         return Some(ModPath::from_segments(PathKind::Plain, vec![builtin.as_name()]));
151     }
152
153     // Recursive case:
154     // - if the item is an enum variant, refer to it via the enum
155     if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
156         if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) {
157             let data = db.enum_data(variant.parent);
158             path.push_segment(data.variants[variant.local_id].name.clone());
159             return Some(path);
160         }
161         // If this doesn't work, it seems we have no way of referring to the
162         // enum; that's very weird, but there might still be a reexport of the
163         // variant somewhere
164     }
165
166     // - otherwise, look for modules containing (reexporting) it and import it from one of those
167
168     let crate_root = def_map.module_id(def_map.root());
169     let crate_attrs = db.attrs(crate_root.into());
170     let prefer_no_std = crate_attrs.by_key("no_std").exists();
171     let mut best_path = None;
172     let mut best_path_len = max_len;
173
174     if item.krate(db) == Some(from.krate) {
175         // Item was defined in the same crate that wants to import it. It cannot be found in any
176         // dependency in this case.
177
178         let local_imports = find_local_import_locations(db, item, from);
179         for (module_id, name) in local_imports {
180             if let Some(mut path) = find_path_inner(
181                 db,
182                 ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
183                 from,
184                 best_path_len - 1,
185                 prefixed,
186             ) {
187                 path.push_segment(name);
188
189                 let new_path = if let Some(best_path) = best_path {
190                     select_best_path(best_path, path, prefer_no_std)
191                 } else {
192                     path
193                 };
194                 best_path_len = new_path.len();
195                 best_path = Some(new_path);
196             }
197         }
198     } else {
199         // Item was defined in some upstream crate. This means that it must be exported from one,
200         // too (unless we can't name it at all). It could *also* be (re)exported by the same crate
201         // that wants to import it here, but we always prefer to use the external path here.
202
203         let crate_graph = db.crate_graph();
204         let extern_paths = crate_graph[from.krate].dependencies.iter().filter_map(|dep| {
205             let import_map = db.import_map(dep.crate_id);
206             import_map.import_info_for(item).and_then(|info| {
207                 // Determine best path for containing module and append last segment from `info`.
208                 let mut path = find_path_inner(
209                     db,
210                     ItemInNs::Types(ModuleDefId::ModuleId(info.container)),
211                     from,
212                     best_path_len - 1,
213                     prefixed,
214                 )?;
215                 mark::hit!(partially_imported);
216                 path.push_segment(info.path.segments.last().unwrap().clone());
217                 Some(path)
218             })
219         });
220
221         for path in extern_paths {
222             let new_path = if let Some(best_path) = best_path {
223                 select_best_path(best_path, path, prefer_no_std)
224             } else {
225                 path
226             };
227             best_path = Some(new_path);
228         }
229     }
230
231     if let Some(mut prefix) = prefixed.map(PrefixKind::prefix) {
232         if matches!(prefix, PathKind::Crate | PathKind::Super(0)) && def_map.block_id().is_some() {
233             // Inner items cannot be referred to via `crate::` or `self::` paths.
234             prefix = PathKind::Plain;
235         }
236
237         best_path.or_else(|| {
238             scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name]))
239         })
240     } else {
241         best_path
242     }
243 }
244
245 fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath {
246     if old_path.starts_with_std() && new_path.can_start_with_std() {
247         if prefer_no_std {
248             mark::hit!(prefer_no_std_paths);
249             new_path
250         } else {
251             mark::hit!(prefer_std_paths);
252             old_path
253         }
254     } else if new_path.starts_with_std() && old_path.can_start_with_std() {
255         if prefer_no_std {
256             mark::hit!(prefer_no_std_paths);
257             old_path
258         } else {
259             mark::hit!(prefer_std_paths);
260             new_path
261         }
262     } else if new_path.len() < old_path.len() {
263         new_path
264     } else {
265         old_path
266     }
267 }
268
269 /// Finds locations in `from.krate` from which `item` can be imported by `from`.
270 fn find_local_import_locations(
271     db: &dyn DefDatabase,
272     item: ItemInNs,
273     from: ModuleId,
274 ) -> Vec<(ModuleId, Name)> {
275     let _p = profile::span("find_local_import_locations");
276
277     // `from` can import anything below `from` with visibility of at least `from`, and anything
278     // above `from` with any visibility. That means we do not need to descend into private siblings
279     // of `from` (and similar).
280
281     let def_map = from.def_map(db);
282
283     // Compute the initial worklist. We start with all direct child modules of `from` as well as all
284     // of its (recursive) parent modules.
285     let data = &def_map[from.local_id];
286     let mut worklist =
287         data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>();
288     let mut parent = data.parent;
289     while let Some(p) = parent {
290         worklist.push(def_map.module_id(p));
291         parent = def_map[p].parent;
292     }
293
294     let mut seen: FxHashSet<_> = FxHashSet::default();
295
296     let mut locations = Vec::new();
297     while let Some(module) = worklist.pop() {
298         if !seen.insert(module) {
299             continue; // already processed this module
300         }
301
302         let ext_def_map;
303         let data = if module.krate == from.krate {
304             &def_map[module.local_id]
305         } else {
306             // The crate might reexport a module defined in another crate.
307             ext_def_map = module.def_map(db);
308             &ext_def_map[module.local_id]
309         };
310
311         if let Some((name, vis)) = data.scope.name_of(item) {
312             if vis.is_visible_from(db, from) {
313                 let is_private = if let Visibility::Module(private_to) = vis {
314                     private_to.local_id == module.local_id
315                 } else {
316                     false
317                 };
318                 let is_original_def = if let Some(module_def_id) = item.as_module_def_id() {
319                     data.scope.declarations().any(|it| it == module_def_id)
320                 } else {
321                     false
322                 };
323
324                 // Ignore private imports. these could be used if we are
325                 // in a submodule of this module, but that's usually not
326                 // what the user wants; and if this module can import
327                 // the item and we're a submodule of it, so can we.
328                 // Also this keeps the cached data smaller.
329                 if !is_private || is_original_def {
330                     locations.push((module, name.clone()));
331                 }
332             }
333         }
334
335         // Descend into all modules visible from `from`.
336         for (_, per_ns) in data.scope.entries() {
337             if let Some((ModuleDefId::ModuleId(module), vis)) = per_ns.take_types_vis() {
338                 if vis.is_visible_from(db, from) {
339                     worklist.push(module);
340                 }
341             }
342         }
343     }
344
345     locations
346 }
347
348 #[cfg(test)]
349 mod tests {
350     use base_db::fixture::WithFixture;
351     use hir_expand::hygiene::Hygiene;
352     use syntax::ast::AstNode;
353     use test_utils::mark;
354
355     use crate::test_db::TestDB;
356
357     use super::*;
358
359     /// `code` needs to contain a cursor marker; checks that `find_path` for the
360     /// item the `path` refers to returns that same path when called from the
361     /// module the cursor is in.
362     fn check_found_path_(ra_fixture: &str, path: &str, prefix_kind: Option<PrefixKind>) {
363         let (db, pos) = TestDB::with_position(ra_fixture);
364         let module = db.module_at_position(pos);
365         let parsed_path_file = syntax::SourceFile::parse(&format!("use {};", path));
366         let ast_path =
367             parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
368         let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap();
369
370         let def_map = module.def_map(&db);
371         let resolved = def_map
372             .resolve_path(
373                 &db,
374                 module.local_id,
375                 &mod_path,
376                 crate::item_scope::BuiltinShadowMode::Module,
377             )
378             .0
379             .take_types()
380             .unwrap();
381
382         let found_path =
383             find_path_inner(&db, ItemInNs::Types(resolved), module, MAX_PATH_LEN, prefix_kind);
384         assert_eq!(found_path, Some(mod_path), "{:?}", prefix_kind);
385     }
386
387     fn check_found_path(
388         ra_fixture: &str,
389         unprefixed: &str,
390         prefixed: &str,
391         absolute: &str,
392         self_prefixed: &str,
393     ) {
394         check_found_path_(ra_fixture, unprefixed, None);
395         check_found_path_(ra_fixture, prefixed, Some(PrefixKind::Plain));
396         check_found_path_(ra_fixture, absolute, Some(PrefixKind::ByCrate));
397         check_found_path_(ra_fixture, self_prefixed, Some(PrefixKind::BySelf));
398     }
399
400     #[test]
401     fn same_module() {
402         let code = r#"
403             //- /main.rs
404             struct S;
405             $0
406         "#;
407         check_found_path(code, "S", "S", "crate::S", "self::S");
408     }
409
410     #[test]
411     fn enum_variant() {
412         let code = r#"
413             //- /main.rs
414             enum E { A }
415             $0
416         "#;
417         check_found_path(code, "E::A", "E::A", "E::A", "E::A");
418     }
419
420     #[test]
421     fn sub_module() {
422         let code = r#"
423             //- /main.rs
424             mod foo {
425                 pub struct S;
426             }
427             $0
428         "#;
429         check_found_path(code, "foo::S", "foo::S", "crate::foo::S", "self::foo::S");
430     }
431
432     #[test]
433     fn super_module() {
434         let code = r#"
435             //- /main.rs
436             mod foo;
437             //- /foo.rs
438             mod bar;
439             struct S;
440             //- /foo/bar.rs
441             $0
442         "#;
443         check_found_path(code, "super::S", "super::S", "crate::foo::S", "super::S");
444     }
445
446     #[test]
447     fn self_module() {
448         let code = r#"
449             //- /main.rs
450             mod foo;
451             //- /foo.rs
452             $0
453         "#;
454         check_found_path(code, "self", "self", "crate::foo", "self");
455     }
456
457     #[test]
458     fn crate_root() {
459         let code = r#"
460             //- /main.rs
461             mod foo;
462             //- /foo.rs
463             $0
464         "#;
465         check_found_path(code, "crate", "crate", "crate", "crate");
466     }
467
468     #[test]
469     fn same_crate() {
470         let code = r#"
471             //- /main.rs
472             mod foo;
473             struct S;
474             //- /foo.rs
475             $0
476         "#;
477         check_found_path(code, "crate::S", "crate::S", "crate::S", "crate::S");
478     }
479
480     #[test]
481     fn different_crate() {
482         let code = r#"
483             //- /main.rs crate:main deps:std
484             $0
485             //- /std.rs crate:std
486             pub struct S;
487         "#;
488         check_found_path(code, "std::S", "std::S", "std::S", "std::S");
489     }
490
491     #[test]
492     fn different_crate_renamed() {
493         let code = r#"
494             //- /main.rs crate:main deps:std
495             extern crate std as std_renamed;
496             $0
497             //- /std.rs crate:std
498             pub struct S;
499         "#;
500         check_found_path(
501             code,
502             "std_renamed::S",
503             "std_renamed::S",
504             "std_renamed::S",
505             "std_renamed::S",
506         );
507     }
508
509     #[test]
510     fn partially_imported() {
511         mark::check!(partially_imported);
512         // Tests that short paths are used even for external items, when parts of the path are
513         // already in scope.
514         let code = r#"
515             //- /main.rs crate:main deps:syntax
516
517             use syntax::ast;
518             $0
519
520             //- /lib.rs crate:syntax
521             pub mod ast {
522                 pub enum ModuleItem {
523                     A, B, C,
524                 }
525             }
526         "#;
527         check_found_path(
528             code,
529             "ast::ModuleItem",
530             "syntax::ast::ModuleItem",
531             "syntax::ast::ModuleItem",
532             "syntax::ast::ModuleItem",
533         );
534
535         let code = r#"
536             //- /main.rs crate:main deps:syntax
537
538             $0
539
540             //- /lib.rs crate:syntax
541             pub mod ast {
542                 pub enum ModuleItem {
543                     A, B, C,
544                 }
545             }
546         "#;
547         check_found_path(
548             code,
549             "syntax::ast::ModuleItem",
550             "syntax::ast::ModuleItem",
551             "syntax::ast::ModuleItem",
552             "syntax::ast::ModuleItem",
553         );
554     }
555
556     #[test]
557     fn same_crate_reexport() {
558         let code = r#"
559             //- /main.rs
560             mod bar {
561                 mod foo { pub(super) struct S; }
562                 pub(crate) use foo::*;
563             }
564             $0
565         "#;
566         check_found_path(code, "bar::S", "bar::S", "crate::bar::S", "self::bar::S");
567     }
568
569     #[test]
570     fn same_crate_reexport_rename() {
571         let code = r#"
572             //- /main.rs
573             mod bar {
574                 mod foo { pub(super) struct S; }
575                 pub(crate) use foo::S as U;
576             }
577             $0
578         "#;
579         check_found_path(code, "bar::U", "bar::U", "crate::bar::U", "self::bar::U");
580     }
581
582     #[test]
583     fn different_crate_reexport() {
584         let code = r#"
585             //- /main.rs crate:main deps:std
586             $0
587             //- /std.rs crate:std deps:core
588             pub use core::S;
589             //- /core.rs crate:core
590             pub struct S;
591         "#;
592         check_found_path(code, "std::S", "std::S", "std::S", "std::S");
593     }
594
595     #[test]
596     fn prelude() {
597         let code = r#"
598             //- /main.rs crate:main deps:std
599             $0
600             //- /std.rs crate:std
601             pub mod prelude { pub struct S; }
602             #[prelude_import]
603             pub use prelude::*;
604         "#;
605         check_found_path(code, "S", "S", "S", "S");
606     }
607
608     #[test]
609     fn enum_variant_from_prelude() {
610         let code = r#"
611             //- /main.rs crate:main deps:std
612             $0
613             //- /std.rs crate:std
614             pub mod prelude {
615                 pub enum Option<T> { Some(T), None }
616                 pub use Option::*;
617             }
618             #[prelude_import]
619             pub use prelude::*;
620         "#;
621         check_found_path(code, "None", "None", "None", "None");
622         check_found_path(code, "Some", "Some", "Some", "Some");
623     }
624
625     #[test]
626     fn shortest_path() {
627         let code = r#"
628             //- /main.rs
629             pub mod foo;
630             pub mod baz;
631             struct S;
632             $0
633             //- /foo.rs
634             pub mod bar { pub struct S; }
635             //- /baz.rs
636             pub use crate::foo::bar::S;
637         "#;
638         check_found_path(code, "baz::S", "baz::S", "crate::baz::S", "self::baz::S");
639     }
640
641     #[test]
642     fn discount_private_imports() {
643         let code = r#"
644             //- /main.rs
645             mod foo;
646             pub mod bar { pub struct S; }
647             use bar::S;
648             //- /foo.rs
649             $0
650         "#;
651         // crate::S would be shorter, but using private imports seems wrong
652         check_found_path(code, "crate::bar::S", "crate::bar::S", "crate::bar::S", "crate::bar::S");
653     }
654
655     #[test]
656     fn import_cycle() {
657         let code = r#"
658             //- /main.rs
659             pub mod foo;
660             pub mod bar;
661             pub mod baz;
662             //- /bar.rs
663             $0
664             //- /foo.rs
665             pub use super::baz;
666             pub struct S;
667             //- /baz.rs
668             pub use super::foo;
669         "#;
670         check_found_path(code, "crate::foo::S", "crate::foo::S", "crate::foo::S", "crate::foo::S");
671     }
672
673     #[test]
674     fn prefer_std_paths_over_alloc() {
675         mark::check!(prefer_std_paths);
676         let code = r#"
677         //- /main.rs crate:main deps:alloc,std
678         $0
679
680         //- /std.rs crate:std deps:alloc
681         pub mod sync {
682             pub use alloc::sync::Arc;
683         }
684
685         //- /zzz.rs crate:alloc
686         pub mod sync {
687             pub struct Arc;
688         }
689         "#;
690         check_found_path(
691             code,
692             "std::sync::Arc",
693             "std::sync::Arc",
694             "std::sync::Arc",
695             "std::sync::Arc",
696         );
697     }
698
699     #[test]
700     fn prefer_core_paths_over_std() {
701         mark::check!(prefer_no_std_paths);
702         let code = r#"
703         //- /main.rs crate:main deps:core,std
704         #![no_std]
705
706         $0
707
708         //- /std.rs crate:std deps:core
709
710         pub mod fmt {
711             pub use core::fmt::Error;
712         }
713
714         //- /zzz.rs crate:core
715
716         pub mod fmt {
717             pub struct Error;
718         }
719         "#;
720         check_found_path(
721             code,
722             "core::fmt::Error",
723             "core::fmt::Error",
724             "core::fmt::Error",
725             "core::fmt::Error",
726         );
727     }
728
729     #[test]
730     fn prefer_alloc_paths_over_std() {
731         let code = r#"
732         //- /main.rs crate:main deps:alloc,std
733         #![no_std]
734
735         $0
736
737         //- /std.rs crate:std deps:alloc
738
739         pub mod sync {
740             pub use alloc::sync::Arc;
741         }
742
743         //- /zzz.rs crate:alloc
744
745         pub mod sync {
746             pub struct Arc;
747         }
748         "#;
749         check_found_path(
750             code,
751             "alloc::sync::Arc",
752             "alloc::sync::Arc",
753             "alloc::sync::Arc",
754             "alloc::sync::Arc",
755         );
756     }
757
758     #[test]
759     fn prefer_shorter_paths_if_not_alloc() {
760         let code = r#"
761         //- /main.rs crate:main deps:megaalloc,std
762         $0
763
764         //- /std.rs crate:std deps:megaalloc
765         pub mod sync {
766             pub use megaalloc::sync::Arc;
767         }
768
769         //- /zzz.rs crate:megaalloc
770         pub struct Arc;
771         "#;
772         check_found_path(
773             code,
774             "megaalloc::Arc",
775             "megaalloc::Arc",
776             "megaalloc::Arc",
777             "megaalloc::Arc",
778         );
779     }
780
781     #[test]
782     fn builtins_are_in_scope() {
783         let code = r#"
784         //- /main.rs
785         $0
786
787         pub mod primitive {
788             pub use u8;
789         }
790         "#;
791         check_found_path(code, "u8", "u8", "u8", "u8");
792         check_found_path(code, "u16", "u16", "u16", "u16");
793     }
794
795     #[test]
796     fn inner_items() {
797         check_found_path(
798             r#"
799             fn main() {
800                 struct Inner {}
801                 $0
802             }
803         "#,
804             "Inner",
805             "Inner",
806             "Inner",
807             "Inner",
808         );
809     }
810
811     #[test]
812     fn inner_items_from_outer_scope() {
813         check_found_path(
814             r#"
815             fn main() {
816                 struct Struct {}
817                 {
818                     $0
819                 }
820             }
821         "#,
822             "Struct",
823             "Struct",
824             "Struct",
825             "Struct",
826         );
827     }
828
829     #[test]
830     fn inner_items_from_inner_module() {
831         check_found_path(
832             r#"
833             fn main() {
834                 mod module {
835                     struct Struct {}
836                 }
837                 {
838                     $0
839                 }
840             }
841         "#,
842             "module::Struct",
843             "module::Struct",
844             "module::Struct",
845             "module::Struct",
846         );
847     }
848
849     #[test]
850     #[ignore]
851     fn inner_items_from_parent_module() {
852         // FIXME: ItemTree currently associates all inner items with `main`. Luckily, this sort of
853         // code is very rare, so this isn't terrible.
854         // To fix it, we should probably build dedicated `ItemTree`s for inner items, and not store
855         // them in the file's main ItemTree. This would also allow us to stop parsing function
856         // bodies when we only want to compute the crate's main DefMap.
857         check_found_path(
858             r#"
859             fn main() {
860                 struct Struct {}
861                 mod module {
862                     $0
863                 }
864             }
865         "#,
866             "super::Struct",
867             "super::Struct",
868             "super::Struct",
869             "super::Struct",
870         );
871     }
872 }