Skip to content

Commit cf41b1d

Browse files
Improve doc alias discovery
1 parent 7590c39 commit cf41b1d

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed

src/librustdoc/html/render.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub struct RenderInfo {
278278
/// Struct representing one entry in the JS search index. These are all emitted
279279
/// by hand to a large JS file at the end of cache-creation.
280280
#[derive(Debug)]
281-
struct IndexItem {
281+
pub struct IndexItem {
282282
ty: ItemType,
283283
name: String,
284284
path: String,
@@ -293,7 +293,12 @@ impl Serialize for IndexItem {
293293
where
294294
S: Serializer,
295295
{
296-
assert_eq!(self.parent.is_some(), self.parent_idx.is_some());
296+
assert_eq!(
297+
self.parent.is_some(),
298+
self.parent_idx.is_some(),
299+
"`{}` is missing idx",
300+
self.name
301+
);
297302

298303
(self.ty, &self.name, &self.path, &self.desc, self.parent_idx, &self.search_type)
299304
.serialize(serializer)
@@ -836,7 +841,7 @@ themePicker.onblur = handleThemeButtonsBlur;
836841
{
837842
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
838843
let mut output = String::with_capacity(100);
839-
for (alias, items) in &cx.cache.aliases {
844+
for (alias, items) in cx.cache.get_aliases() {
840845
if items.is_empty() {
841846
continue;
842847
}

src/librustdoc/html/render/cache.rs

+53-36
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ crate struct Cache {
120120

121121
/// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
122122
/// we need the alias element to have an array of items.
123-
pub(super) aliases: FxHashMap<String, Vec<IndexItem>>,
123+
pub(super) aliases: FxHashMap<String, Vec<usize>>,
124124
}
125125

126126
impl Cache {
@@ -311,7 +311,7 @@ impl DocFolder for Cache {
311311
};
312312

313313
match parent {
314-
(parent, Some(path)) if is_inherent_impl_item || (!self.stripped_mod) => {
314+
(parent, Some(path)) if is_inherent_impl_item || !self.stripped_mod => {
315315
debug_assert!(!item.is_stripped());
316316

317317
// A crate has a module at its root, containing all items,
@@ -327,6 +327,21 @@ impl DocFolder for Cache {
327327
parent_idx: None,
328328
search_type: get_index_search_type(&item),
329329
});
330+
331+
for alias in item
332+
.attrs
333+
.lists(sym::doc)
334+
.filter(|a| a.check_name(sym::alias))
335+
.filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
336+
.filter(|v| !v.is_empty())
337+
.collect::<FxHashSet<_>>()
338+
.into_iter()
339+
{
340+
self.aliases
341+
.entry(alias.to_lowercase())
342+
.or_insert(Vec::with_capacity(1))
343+
.push(self.search_index.len() - 1);
344+
}
330345
}
331346
}
332347
(Some(parent), None) if is_inherent_impl_item => {
@@ -363,6 +378,9 @@ impl DocFolder for Cache {
363378
| clean::MacroItem(..)
364379
| clean::ProcMacroItem(..)
365380
| clean::VariantItem(..)
381+
| clean::StructFieldItem(..)
382+
| clean::TyMethodItem(..)
383+
| clean::MethodItem(..)
366384
if !self.stripped_mod =>
367385
{
368386
// Re-exported items mean that the same id can show up twice
@@ -376,11 +394,8 @@ impl DocFolder for Cache {
376394
{
377395
self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
378396
}
379-
self.add_aliases(&item);
380397
}
381-
382398
clean::PrimitiveItem(..) => {
383-
self.add_aliases(&item);
384399
self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
385400
}
386401

@@ -489,36 +504,23 @@ impl DocFolder for Cache {
489504
}
490505

491506
impl Cache {
492-
fn add_aliases(&mut self, item: &clean::Item) {
493-
if item.def_id.index == CRATE_DEF_INDEX {
494-
return;
495-
}
496-
if let Some(ref item_name) = item.name {
497-
let path = self
498-
.paths
499-
.get(&item.def_id)
500-
.map(|p| p.0[..p.0.len() - 1].join("::"))
501-
.unwrap_or("std".to_owned());
502-
for alias in item
503-
.attrs
504-
.lists(sym::doc)
505-
.filter(|a| a.check_name(sym::alias))
506-
.filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
507-
.filter(|v| !v.is_empty())
508-
.collect::<FxHashSet<_>>()
509-
.into_iter()
510-
{
511-
self.aliases.entry(alias).or_insert(Vec::with_capacity(1)).push(IndexItem {
512-
ty: item.type_(),
513-
name: item_name.to_string(),
514-
path: path.clone(),
515-
desc: shorten(plain_summary_line(item.doc_value())),
516-
parent: None,
517-
parent_idx: None,
518-
search_type: get_index_search_type(&item),
519-
});
520-
}
521-
}
507+
pub fn get_aliases<'a>(&'a self) -> FxHashMap<String, Vec<&'a IndexItem>> {
508+
self.aliases
509+
.iter()
510+
.map(|(k, values)| {
511+
(
512+
k.clone(),
513+
values
514+
.iter()
515+
.filter(|v| {
516+
let x = &self.search_index[**v];
517+
x.parent_idx.is_some() == x.parent.is_some()
518+
})
519+
.map(|v| &self.search_index[*v])
520+
.collect::<Vec<_>>(),
521+
)
522+
})
523+
.collect()
522524
}
523525
}
524526

@@ -567,7 +569,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
567569
let mut crate_items = Vec::with_capacity(cache.search_index.len());
568570
let mut crate_paths = vec![];
569571

570-
let Cache { ref mut search_index, ref orphan_impl_items, ref paths, .. } = *cache;
572+
let Cache { ref mut search_index, ref orphan_impl_items, ref paths, ref mut aliases, .. } =
573+
*cache;
571574

572575
// Attach all orphan items to the type's definition if the type
573576
// has since been learned.
@@ -582,6 +585,20 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
582585
parent_idx: None,
583586
search_type: get_index_search_type(&item),
584587
});
588+
for alias in item
589+
.attrs
590+
.lists(sym::doc)
591+
.filter(|a| a.check_name(sym::alias))
592+
.filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
593+
.filter(|v| !v.is_empty())
594+
.collect::<FxHashSet<_>>()
595+
.into_iter()
596+
{
597+
aliases
598+
.entry(alias.to_lowercase())
599+
.or_insert(Vec::with_capacity(1))
600+
.push(search_index.len() - 1);
601+
}
585602
}
586603
}
587604

0 commit comments

Comments
 (0)