@@ -155,10 +155,6 @@ fn find_path_for_module(
155155 module_id : ModuleId ,
156156 max_len : usize ,
157157) -> Option < ( ModPath , Stability ) > {
158- if max_len == 0 {
159- return None ;
160- }
161-
162158 if let Some ( crate_root) = module_id. as_crate_root ( ) {
163159 if crate_root == ctx. from . derive_crate_root ( ) {
164160 // - if the item is the crate root, return `crate`
@@ -314,6 +310,8 @@ fn calculate_best_path(
314310 max_len : usize ,
315311) -> Option < ( ModPath , Stability ) > {
316312 if max_len <= 1 {
313+ // recursive base case, we can't find a path prefix of length 0, one segment is occupied by
314+ // the item's name itself.
317315 return None ;
318316 }
319317
@@ -341,18 +339,18 @@ fn calculate_best_path(
341339 // Item was defined in the same crate that wants to import it. It cannot be found in any
342340 // dependency in this case.
343341 // FIXME: cache the `find_local_import_locations` output?
344- for ( module_id , name ) in find_local_import_locations ( db, item, ctx. from , ctx. from_def_map ) {
342+ find_local_import_locations ( db, item, ctx. from , ctx. from_def_map , |name , module_id| {
345343 if !visited_modules. insert ( module_id) {
346- continue ;
344+ return ;
347345 }
348346 // we are looking for paths of length up to best_path_len, any longer will make it be
349347 // less optimal. The -1 is due to us pushing name onto it afterwards.
350348 if let Some ( path) =
351349 find_path_for_module ( ctx, visited_modules, module_id, best_path_len - 1 )
352350 {
353- process ( path, name, & mut best_path_len) ;
351+ process ( path, name. clone ( ) , & mut best_path_len) ;
354352 }
355- }
353+ } )
356354 } else {
357355 // Item was defined in some upstream crate. This means that it must be exported from one,
358356 // too (unless we can't name it at all). It could *also* be (re)exported by the same crate
@@ -454,14 +452,14 @@ fn select_best_path(
454452 }
455453}
456454
457- // FIXME: Remove allocations
458455/// Finds locations in `from.krate` from which `item` can be imported by `from`.
459456fn find_local_import_locations (
460457 db : & dyn DefDatabase ,
461458 item : ItemInNs ,
462459 from : ModuleId ,
463460 def_map : & DefMap ,
464- ) -> Vec < ( ModuleId , Name ) > {
461+ mut cb : impl FnMut ( & Name , ModuleId ) ,
462+ ) {
465463 let _p = tracing:: span!( tracing:: Level :: INFO , "find_local_import_locations" ) . entered ( ) ;
466464
467465 // `from` can import anything below `from` with visibility of at least `from`, and anything
@@ -470,19 +468,17 @@ fn find_local_import_locations(
470468
471469 // Compute the initial worklist. We start with all direct child modules of `from` as well as all
472470 // of its (recursive) parent modules.
473- let data = & def_map[ from. local_id ] ;
474- let mut worklist =
475- data. children . values ( ) . map ( |child| def_map. module_id ( * child) ) . collect :: < Vec < _ > > ( ) ;
476- // FIXME: do we need to traverse out of block expressions here?
477- for ancestor in iter:: successors ( from. containing_module ( db) , |m| m. containing_module ( db) ) {
478- worklist. push ( ancestor) ;
479- }
471+ let mut worklist = def_map[ from. local_id ]
472+ . children
473+ . values ( )
474+ . map ( |child| def_map. module_id ( * child) )
475+ // FIXME: do we need to traverse out of block expressions here?
476+ . chain ( iter:: successors ( from. containing_module ( db) , |m| m. containing_module ( db) ) )
477+ . collect :: < Vec < _ > > ( ) ;
478+ let mut seen: FxHashSet < _ > = FxHashSet :: default ( ) ;
480479
481480 let def_map = def_map. crate_root ( ) . def_map ( db) ;
482481
483- let mut seen: FxHashSet < _ > = FxHashSet :: default ( ) ;
484-
485- let mut locations = Vec :: new ( ) ;
486482 while let Some ( module) = worklist. pop ( ) {
487483 if !seen. insert ( module) {
488484 continue ; // already processed this module
@@ -523,7 +519,7 @@ fn find_local_import_locations(
523519 // the item and we're a submodule of it, so can we.
524520 // Also this keeps the cached data smaller.
525521 if declared || is_pub_or_explicit {
526- locations . push ( ( module , name . clone ( ) ) ) ;
522+ cb ( name , module ) ;
527523 }
528524 }
529525 }
@@ -535,8 +531,6 @@ fn find_local_import_locations(
535531 }
536532 }
537533 }
538-
539- locations
540534}
541535
542536#[ cfg( test) ]
0 commit comments