11//! An algorithm to find a path to refer to a certain item.
22
3- use std:: { cmp:: Ordering , iter} ;
3+ use std:: { cell :: Cell , cmp:: Ordering , iter} ;
44
55use hir_expand:: {
66 name:: { known, AsName , Name } ,
@@ -49,6 +49,7 @@ pub fn find_path(
4949 ignore_local_imports,
5050 from,
5151 from_def_map : & from. def_map ( db) ,
52+ fuel : Cell :: new ( FIND_PATH_FUEL ) ,
5253 } ,
5354 item,
5455 MAX_PATH_LEN ,
@@ -70,6 +71,7 @@ fn zip_stability(a: Stability, b: Stability) -> Stability {
7071}
7172
7273const MAX_PATH_LEN : usize = 15 ;
74+ const FIND_PATH_FUEL : usize = 10000 ;
7375
7476#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
7577pub enum PrefixKind {
@@ -101,6 +103,7 @@ struct FindPathCtx<'db> {
101103 ignore_local_imports : bool ,
102104 from : ModuleId ,
103105 from_def_map : & ' db DefMap ,
106+ fuel : Cell < usize > ,
104107}
105108
106109/// Attempts to find a path to refer to the given `item` visible from the `from` ModuleId
@@ -314,6 +317,17 @@ fn calculate_best_path(
314317 // the item's name itself.
315318 return None ;
316319 }
320+ let fuel = ctx. fuel . get ( ) ;
321+ if fuel == 0 {
322+ // we ran out of fuel, so we stop searching here
323+ tracing:: warn!(
324+ "ran out of fuel while searching for a path for item {item:?} of krate {:?} from krate {:?}" ,
325+ item. krate( ctx. db) ,
326+ ctx. from. krate( )
327+ ) ;
328+ return None ;
329+ }
330+ ctx. fuel . set ( fuel - 1 ) ;
317331
318332 let mut best_path = None ;
319333 let mut best_path_len = max_len;
0 commit comments