|
1 | 1 | use std::collections::hash_map::Entry; |
2 | 2 | use std::collections::BTreeMap; |
3 | 3 |
|
4 | | -use rustc_data_structures::fx::FxHashMap; |
| 4 | +use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; |
5 | 5 | use rustc_middle::ty::TyCtxt; |
6 | 6 | use rustc_span::symbol::Symbol; |
7 | 7 | use serde::ser::{Serialize, SerializeSeq, SerializeStruct, Serializer}; |
@@ -246,24 +246,55 @@ pub(crate) fn build_index<'tcx>( |
246 | 246 | where |
247 | 247 | S: Serializer, |
248 | 248 | { |
| 249 | + let mut extra_paths = FxHashMap::default(); |
| 250 | + // We need to keep the order of insertion, hence why we use an `IndexMap`. Then we will |
| 251 | + // insert these "extra paths" (which are paths of items from external crates) into the |
| 252 | + // `full_paths` list at the end. |
| 253 | + let mut revert_extra_paths = FxIndexMap::default(); |
249 | 254 | let mut mod_paths = FxHashMap::default(); |
250 | 255 | for (index, item) in self.items.iter().enumerate() { |
251 | 256 | if item.path.is_empty() { |
252 | 257 | continue; |
253 | 258 | } |
254 | 259 | mod_paths.insert(&item.path, index); |
255 | 260 | } |
256 | | - let paths = self |
257 | | - .paths |
258 | | - .iter() |
259 | | - .map(|(ty, path)| { |
260 | | - if path.len() < 2 { |
261 | | - return Paths { ty: *ty, name: path[0], path: None }; |
| 261 | + let mut paths = Vec::with_capacity(self.paths.len()); |
| 262 | + for (ty, path) in &self.paths { |
| 263 | + if path.len() < 2 { |
| 264 | + paths.push(Paths { ty: *ty, name: path[0], path: None }); |
| 265 | + continue; |
| 266 | + } |
| 267 | + let full_path = join_with_double_colon(&path[..path.len() - 1]); |
| 268 | + if let Some(index) = mod_paths.get(&full_path) { |
| 269 | + paths.push(Paths { ty: *ty, name: *path.last().unwrap(), path: Some(*index) }); |
| 270 | + continue; |
| 271 | + } |
| 272 | + // It means it comes from an external crate so the item and its path will be |
| 273 | + // stored into another array. |
| 274 | + // |
| 275 | + // `index` is put after the last `mod_paths` |
| 276 | + let index = extra_paths.len() + self.items.len(); |
| 277 | + if !revert_extra_paths.contains_key(&index) { |
| 278 | + revert_extra_paths.insert(index, full_path.clone()); |
| 279 | + } |
| 280 | + match extra_paths.entry(full_path) { |
| 281 | + Entry::Occupied(entry) => { |
| 282 | + paths.push(Paths { |
| 283 | + ty: *ty, |
| 284 | + name: *path.last().unwrap(), |
| 285 | + path: Some(*entry.get()), |
| 286 | + }); |
262 | 287 | } |
263 | | - let index = mod_paths.get(&join_with_double_colon(&path[..path.len() - 1])); |
264 | | - Paths { ty: *ty, name: *path.last().unwrap(), path: index.copied() } |
265 | | - }) |
266 | | - .collect::<Vec<_>>(); |
| 288 | + Entry::Vacant(entry) => { |
| 289 | + entry.insert(index); |
| 290 | + paths.push(Paths { |
| 291 | + ty: *ty, |
| 292 | + name: *path.last().unwrap(), |
| 293 | + path: Some(index), |
| 294 | + }); |
| 295 | + } |
| 296 | + } |
| 297 | + } |
267 | 298 |
|
268 | 299 | let mut names = Vec::with_capacity(self.items.len()); |
269 | 300 | let mut types = String::with_capacity(self.items.len()); |
@@ -322,6 +353,10 @@ pub(crate) fn build_index<'tcx>( |
322 | 353 | } |
323 | 354 | } |
324 | 355 |
|
| 356 | + for (index, path) in &revert_extra_paths { |
| 357 | + full_paths.push((*index, path)); |
| 358 | + } |
| 359 | + |
325 | 360 | let has_aliases = !self.aliases.is_empty(); |
326 | 361 | let mut crate_data = |
327 | 362 | serializer.serialize_struct("CrateData", if has_aliases { 9 } else { 8 })?; |
|
0 commit comments