Skip to content

Commit 4e20847

Browse files
committed
rustdoc: Simplify some search index code
1 parent 2721e97 commit 4e20847

File tree

2 files changed

+36
-44
lines changed

2 files changed

+36
-44
lines changed

src/librustdoc/clean/types.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -1680,13 +1680,16 @@ impl Type {
16801680
}
16811681
}
16821682

1683-
fn inner_def_id(&self, cache: Option<&Cache>) -> Option<DefId> {
1683+
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1684+
///
1685+
/// [clean]: crate::clean
1686+
pub(crate) fn def_id(&self, cache: &Cache) -> Option<DefId> {
16841687
let t: PrimitiveType = match *self {
16851688
Type::Path { ref path } => return Some(path.def_id()),
16861689
DynTrait(ref bounds, _) => return bounds.get(0).map(|b| b.trait_.def_id()),
1687-
Primitive(p) => return cache.and_then(|c| c.primitive_locations.get(&p).cloned()),
1690+
Primitive(p) => return cache.primitive_locations.get(&p).cloned(),
16881691
BorrowedRef { type_: box Generic(..), .. } => PrimitiveType::Reference,
1689-
BorrowedRef { ref type_, .. } => return type_.inner_def_id(cache),
1692+
BorrowedRef { ref type_, .. } => return type_.def_id(cache),
16901693
Tuple(ref tys) => {
16911694
if tys.is_empty() {
16921695
PrimitiveType::Unit
@@ -1699,17 +1702,10 @@ impl Type {
16991702
Array(..) => PrimitiveType::Array,
17001703
Type::Pat(..) => PrimitiveType::Pat,
17011704
RawPointer(..) => PrimitiveType::RawPointer,
1702-
QPath(box QPathData { ref self_type, .. }) => return self_type.inner_def_id(cache),
1705+
QPath(box QPathData { ref self_type, .. }) => return self_type.def_id(cache),
17031706
Generic(_) | Infer | ImplTrait(_) => return None,
17041707
};
1705-
cache.and_then(|c| Primitive(t).def_id(c))
1706-
}
1707-
1708-
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1709-
///
1710-
/// [clean]: crate::clean
1711-
pub(crate) fn def_id(&self, cache: &Cache) -> Option<DefId> {
1712-
self.inner_def_id(Some(cache))
1708+
Primitive(t).def_id(cache)
17131709
}
17141710
}
17151711

src/librustdoc/formats/cache.rs

+28-32
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,13 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
442442
}
443443

444444
fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::Item, name: Symbol) {
445-
let (parent, is_inherent_impl_item) = match *item.kind {
446-
clean::StrippedItem(..) => ((None, None), false),
445+
let (parent, is_impl_child) = match *item.kind {
446+
clean::StrippedItem(..) => return,
447447
clean::AssocConstItem(..) | clean::AssocTypeItem(..)
448448
if cache.parent_stack.last().is_some_and(|parent| parent.is_trait_impl()) =>
449449
{
450450
// skip associated items in trait impls
451-
((None, None), false)
451+
return;
452452
}
453453
clean::TyMethodItem(..)
454454
| clean::TyAssocConstItem(..)
@@ -469,39 +469,35 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
469469
false,
470470
),
471471
clean::MethodItem(..) | clean::AssocConstItem(..) | clean::AssocTypeItem(..) => {
472-
if cache.parent_stack.is_empty() {
473-
((None, None), false)
474-
} else {
475-
let last = cache.parent_stack.last().expect("parent_stack is empty 2");
476-
let did = match &*last {
477-
ParentStackItem::Impl {
478-
// impl Trait for &T { fn method(self); }
479-
//
480-
// When generating a function index with the above shape, we want it
481-
// associated with `T`, not with the primitive reference type. It should
482-
// show up as `T::method`, rather than `reference::method`, in the search
483-
// results page.
484-
for_: clean::Type::BorrowedRef { type_, .. },
485-
..
486-
} => type_.def_id(&cache),
487-
ParentStackItem::Impl { for_, .. } => for_.def_id(&cache),
488-
ParentStackItem::Type(item_id) => item_id.as_def_id(),
489-
};
490-
let path = did
491-
.and_then(|did| cache.paths.get(&did))
492-
// The current stack not necessarily has correlation
493-
// for where the type was defined. On the other
494-
// hand, `paths` always has the right
495-
// information if present.
496-
.map(|(fqp, _)| &fqp[..fqp.len() - 1]);
497-
((did, path), true)
498-
}
472+
let last = cache.parent_stack.last().expect("parent_stack is empty 2");
473+
let did = match &*last {
474+
ParentStackItem::Impl {
475+
// impl Trait for &T { fn method(self); }
476+
//
477+
// When generating a function index with the above shape, we want it
478+
// associated with `T`, not with the primitive reference type. It should
479+
// show up as `T::method`, rather than `reference::method`, in the search
480+
// results page.
481+
for_: clean::Type::BorrowedRef { type_, .. },
482+
..
483+
} => type_.def_id(&cache),
484+
ParentStackItem::Impl { for_, .. } => for_.def_id(&cache),
485+
ParentStackItem::Type(item_id) => item_id.as_def_id(),
486+
};
487+
let path = did
488+
.and_then(|did| cache.paths.get(&did))
489+
// The current stack not necessarily has correlation
490+
// for where the type was defined. On the other
491+
// hand, `paths` always has the right
492+
// information if present.
493+
.map(|(fqp, _)| &fqp[..fqp.len() - 1]);
494+
((did, path), true)
499495
}
500496
_ => ((None, Some(&*cache.stack)), false),
501497
};
502498

503499
match parent {
504-
(parent, Some(path)) if is_inherent_impl_item || !cache.stripped_mod => {
500+
(parent, Some(path)) if is_impl_child || !cache.stripped_mod => {
505501
debug_assert!(!item.is_stripped());
506502

507503
// A crate has a module at its root, containing all items,
@@ -553,7 +549,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
553549
});
554550
}
555551
}
556-
(Some(parent), None) if is_inherent_impl_item => {
552+
(Some(parent), None) if is_impl_child => {
557553
// We have a parent, but we don't know where they're
558554
// defined yet. Wait for later to index this item.
559555
let impl_generics = clean_impl_generics(cache.parent_stack.last());

0 commit comments

Comments
 (0)