@@ -16,19 +16,25 @@ use crate::{
1616
1717/// Find a path that can be used to refer to a certain item. This can depend on
1818/// *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 > {
19+ pub fn find_path (
20+ db : & dyn DefDatabase ,
21+ item : ItemInNs ,
22+ from : ModuleId ,
23+ prefer_core : bool ,
24+ ) -> Option < ModPath > {
2025 let _p = profile:: span ( "find_path" ) ;
21- find_path_inner ( db, item, from, None )
26+ find_path_inner ( db, item, from, None , prefer_core )
2227}
2328
2429pub fn find_path_prefixed (
2530 db : & dyn DefDatabase ,
2631 item : ItemInNs ,
2732 from : ModuleId ,
2833 prefix_kind : PrefixKind ,
34+ prefer_core : bool ,
2935) -> Option < ModPath > {
3036 let _p = profile:: span ( "find_path_prefixed" ) ;
31- find_path_inner ( db, item, from, Some ( prefix_kind) )
37+ find_path_inner ( db, item, from, Some ( prefix_kind) , prefer_core )
3238}
3339
3440const MAX_PATH_LEN : usize = 15 ;
@@ -100,12 +106,22 @@ fn find_path_inner(
100106 item : ItemInNs ,
101107 from : ModuleId ,
102108 prefixed : Option < PrefixKind > ,
109+ prefer_core : bool ,
103110) -> Option < ModPath > {
104111 // FIXME: Do fast path for std/core libs?
105112
106113 let mut visited_modules = FxHashSet :: default ( ) ;
107114 let def_map = from. def_map ( db) ;
108- find_path_inner_ ( db, & def_map, from, item, MAX_PATH_LEN , prefixed, & mut visited_modules)
115+ find_path_inner_ (
116+ db,
117+ & def_map,
118+ from,
119+ item,
120+ MAX_PATH_LEN ,
121+ prefixed,
122+ & mut visited_modules,
123+ prefer_core,
124+ )
109125}
110126
111127fn find_path_inner_ (
@@ -116,6 +132,7 @@ fn find_path_inner_(
116132 max_len : usize ,
117133 mut prefixed : Option < PrefixKind > ,
118134 visited_modules : & mut FxHashSet < ModuleId > ,
135+ prefer_core : bool ,
119136) -> Option < ModPath > {
120137 if max_len == 0 {
121138 return None ;
@@ -191,7 +208,9 @@ fn find_path_inner_(
191208 // Recursive case:
192209 // - if the item is an enum variant, refer to it via the enum
193210 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) {
211+ if let Some ( mut path) =
212+ find_path ( db, ItemInNs :: Types ( variant. parent . into ( ) ) , from, prefer_core)
213+ {
195214 let data = db. enum_data ( variant. parent ) ;
196215 path. push_segment ( data. variants [ variant. local_id ] . name . clone ( ) ) ;
197216 return Some ( path) ;
@@ -202,7 +221,7 @@ fn find_path_inner_(
202221 }
203222
204223 // - 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 ) ;
224+ let prefer_no_std = prefer_core || db. crate_supports_no_std ( crate_root. krate ) ;
206225 let mut best_path = None ;
207226 let mut best_path_len = max_len;
208227
@@ -223,6 +242,7 @@ fn find_path_inner_(
223242 best_path_len - 1 ,
224243 prefixed,
225244 visited_modules,
245+ prefer_core,
226246 ) {
227247 path. push_segment ( name) ;
228248
@@ -253,6 +273,7 @@ fn find_path_inner_(
253273 best_path_len - 1 ,
254274 prefixed,
255275 visited_modules,
276+ prefer_core,
256277 ) ?;
257278 cov_mark:: hit!( partially_imported) ;
258279 path. push_segment ( info. path . segments . last ( ) ?. clone ( ) ) ;
@@ -428,7 +449,8 @@ mod tests {
428449 . take_types ( )
429450 . unwrap ( ) ;
430451
431- let found_path = find_path_inner ( & db, ItemInNs :: Types ( resolved) , module, prefix_kind) ;
452+ let found_path =
453+ find_path_inner ( & db, ItemInNs :: Types ( resolved) , module, prefix_kind, false ) ;
432454 assert_eq ! ( found_path, Some ( mod_path) , "{:?}" , prefix_kind) ;
433455 }
434456
0 commit comments