Skip to content

Commit 5613719

Browse files
Unify item relative path computation in one function
1 parent a0e426e commit 5613719

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

src/librustdoc/clean/inline.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,28 @@ pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [ast:
191191
cx.tcx.get_attrs_unchecked(did)
192192
}
193193

194+
pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<Symbol> {
195+
tcx.def_path(def_id)
196+
.data
197+
.into_iter()
198+
.filter_map(|elem| {
199+
// extern blocks (and a few others things) have an empty name.
200+
match elem.data.get_opt_name() {
201+
Some(s) if !s.is_empty() => Some(s),
202+
_ => None,
203+
}
204+
})
205+
.collect()
206+
}
207+
194208
/// Record an external fully qualified name in the external_paths cache.
195209
///
196210
/// These names are used later on by HTML rendering to generate things like
197211
/// source links back to the original item.
198212
pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
199213
let crate_name = cx.tcx.crate_name(did.krate);
200214

201-
let relative =
202-
cx.tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name());
215+
let relative = item_relative_path(cx.tcx, did);
203216
let fqn = if let ItemType::Macro = kind {
204217
// Check to see if it is a macro 2.0 or built-in macro
205218
if matches!(
@@ -210,7 +223,7 @@ pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemT
210223
) {
211224
once(crate_name).chain(relative).collect()
212225
} else {
213-
vec![crate_name, relative.last().expect("relative was empty")]
226+
vec![crate_name, *relative.last().expect("relative was empty")]
214227
}
215228
} else {
216229
once(crate_name).chain(relative).collect()

src/librustdoc/html/format.rs

+2-24
Original file line numberDiff line numberDiff line change
@@ -586,18 +586,7 @@ fn generate_macro_def_id_path(
586586
let crate_name = tcx.crate_name(def_id.krate);
587587
let cache = cx.cache();
588588

589-
let fqp: Vec<Symbol> = tcx
590-
.def_path(def_id)
591-
.data
592-
.into_iter()
593-
.filter_map(|elem| {
594-
// extern blocks (and a few others things) have an empty name.
595-
match elem.data.get_opt_name() {
596-
Some(s) if !s.is_empty() => Some(s),
597-
_ => None,
598-
}
599-
})
600-
.collect();
589+
let fqp = clean::inline::item_relative_path(tcx, def_id);
601590
let mut relative = fqp.iter().copied();
602591
let cstore = CStore::from_tcx(tcx);
603592
// We need this to prevent a `panic` when this function is used from intra doc links...
@@ -675,18 +664,7 @@ fn generate_item_def_id_path(
675664
.map(|adt| adt.did())
676665
.unwrap_or(def_id);
677666

678-
let relative: Vec<Symbol> = tcx
679-
.def_path(def_id)
680-
.data
681-
.into_iter()
682-
.filter_map(|elem| {
683-
// extern blocks (and a few others things) have an empty name.
684-
match elem.data.get_opt_name() {
685-
Some(s) if !s.is_empty() => Some(s),
686-
_ => None,
687-
}
688-
})
689-
.collect();
667+
let relative = clean::inline::item_relative_path(tcx, def_id);
690668
let fqp: Vec<Symbol> = once(crate_name).chain(relative).collect();
691669

692670
let shortty = tcx.def_kind(def_id).into();

0 commit comments

Comments
 (0)