@@ -48,7 +48,7 @@ pub fn find_path(
4848 )
4949}
5050
51- #[ derive( Copy , Clone , Debug ) ]
51+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
5252enum Stability {
5353 Unstable ,
5454 Stable ,
@@ -322,12 +322,14 @@ fn calculate_best_path(
322322 None => * best_path = Some ( new_path) ,
323323 } ;
324324
325- if item. krate ( ctx. db ) == Some ( ctx. from . krate ) {
326- let mut best_path_len = max_len;
325+ let db = ctx. db ;
326+
327+ let mut best_path_len = max_len;
328+ if item. krate ( db) == Some ( ctx. from . krate ) {
327329 // Item was defined in the same crate that wants to import it. It cannot be found in any
328330 // dependency in this case.
329331 // FIXME: cache the `find_local_import_locations` output?
330- for ( module_id, name) in find_local_import_locations ( ctx . db , item, ctx. from ) {
332+ for ( module_id, name) in find_local_import_locations ( db, item, ctx. from ) {
331333 if !visited_modules. insert ( module_id) {
332334 continue ;
333335 }
@@ -342,7 +344,9 @@ fn calculate_best_path(
342344 Some ( best_path) => select_best_path ( best_path, path, ctx. cfg ) ,
343345 None => path,
344346 } ;
345- best_path_len = new_path. 0 . len ( ) ;
347+ if new_path. 1 == Stable {
348+ best_path_len = new_path. 0 . len ( ) ;
349+ }
346350 update_best_path ( & mut best_path, new_path) ;
347351 }
348352 }
@@ -351,8 +355,8 @@ fn calculate_best_path(
351355 // too (unless we can't name it at all). It could *also* be (re)exported by the same crate
352356 // that wants to import it here, but we always prefer to use the external path here.
353357
354- for dep in & ctx . db . crate_graph ( ) [ ctx. from . krate ] . dependencies {
355- let import_map = ctx . db . import_map ( dep. crate_id ) ;
358+ for dep in & db. crate_graph ( ) [ ctx. from . krate ] . dependencies {
359+ let import_map = db. import_map ( dep. crate_id ) ;
356360 let Some ( import_info_for) = import_map. import_info_for ( item) else { continue } ;
357361 for info in import_info_for {
358362 if info. is_doc_hidden {
@@ -367,8 +371,7 @@ fn calculate_best_path(
367371 def_map,
368372 visited_modules,
369373 info. container ,
370- max_len - 1 ,
371- // fixme shouldnt we consider the best path length here?
374+ best_path_len - 1 ,
372375 ) ;
373376 let Some ( ( mut path, path_stability) ) = path else {
374377 continue ;
@@ -381,19 +384,22 @@ fn calculate_best_path(
381384 zip_stability ( path_stability, if info. is_unstable { Unstable } else { Stable } ) ,
382385 ) ;
383386
384- let new_path_with_stab = match best_path. take ( ) {
387+ let new_path = match best_path. take ( ) {
385388 Some ( best_path) => select_best_path ( best_path, path_with_stab, ctx. cfg ) ,
386389 None => path_with_stab,
387390 } ;
388- update_best_path ( & mut best_path, new_path_with_stab) ;
391+ if new_path. 1 == Stable {
392+ best_path_len = new_path. 0 . len ( ) ;
393+ }
394+ update_best_path ( & mut best_path, new_path) ;
389395 }
390396 }
391397 }
392398 best_path
393399}
394400
395401/// Select the best (most relevant) path between two paths.
396- /// This accounts for stability, path length whether std should be chosen over alloc/core paths as
402+ /// This accounts for stability, path length whether, std should be chosen over alloc/core paths as
397403/// well as ignoring prelude like paths or not.
398404fn select_best_path (
399405 old_path @ ( _, old_stability) : ( ModPath , Stability ) ,
0 commit comments