Skip to content

Commit 14e0dab

Browse files
Unify item relative path computation in one function
1 parent f3c2483 commit 14e0dab

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

Diff for: src/librustdoc/clean/inline.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ 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
@@ -206,8 +220,7 @@ pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemT
206220

207221
let crate_name = cx.tcx.crate_name(did.krate);
208222

209-
let relative =
210-
cx.tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name());
223+
let relative = item_relative_path(cx.tcx, did);
211224
let fqn = if let ItemType::Macro = kind {
212225
// Check to see if it is a macro 2.0 or built-in macro
213226
if matches!(
@@ -218,7 +231,7 @@ pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemT
218231
) {
219232
once(crate_name).chain(relative).collect()
220233
} else {
221-
vec![crate_name, relative.last().expect("relative was empty")]
234+
vec![crate_name, *relative.last().expect("relative was empty")]
222235
}
223236
} else {
224237
once(crate_name).chain(relative).collect()

Diff for: 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...
@@ -680,18 +669,7 @@ fn generate_item_def_id_path(
680669
.unwrap_or(def_id);
681670
}
682671

683-
let relative: Vec<Symbol> = tcx
684-
.def_path(def_id)
685-
.data
686-
.into_iter()
687-
.filter_map(|elem| {
688-
// extern blocks (and a few others things) have an empty name.
689-
match elem.data.get_opt_name() {
690-
Some(s) if !s.is_empty() => Some(s),
691-
_ => None,
692-
}
693-
})
694-
.collect();
672+
let relative = clean::inline::item_relative_path(tcx, def_id);
695673
let fqp: Vec<Symbol> = once(crate_name).chain(relative).collect();
696674

697675
let def_kind = tcx.def_kind(def_id);

Diff for: tests/rustdoc/jump-to-non-local-method.rs

+37-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,47 @@
22

33
#![crate_name = "foo"]
44

5+
// @has 'src/foo/jump-to-non-local-method.rs.html'
6+
7+
// @has - '//a[@href="{{channel}}/core/sync/atomic/struct.AtomicIsize.html"]' 'std::sync::atomic::AtomicIsize'
58
use std::sync::atomic::AtomicIsize;
9+
// @has - '//a[@href="{{channel}}/std/io/trait.Read.html"]' 'std::io::Read'
10+
use std::io::Read;
11+
// @has - '//a[@href="{{channel}}/std/io/index.html"]' 'std::io'
12+
use std::io;
13+
// @has - '//a[@href="{{channel}}/std/process/fn.exit.html"]' 'std::process::exit'
14+
use std::process::exit;
15+
use std::cmp::Ordering;
16+
use std::marker::PhantomData;
617

7-
// @has 'src/foo/jump-to-non-local-method.rs.html'
8-
// @has - '//a[@href="https://doc.rust-lang.org/nightly/core/sync/atomic/struct.AtomicIsize.html#method.new"]' 'AtomicIsize::new'
18+
pub fn bar2<T: Read>(readable: T) {
19+
// @has - '//a[@href="{{channel}}/std/io/trait.Read.html#tymethod.read"]' 'read'
20+
let _ = readable.read(&mut []);
21+
}
922

1023
pub fn bar() {
24+
// @has - '//a[@href="{{channel}}/core/sync/atomic/struct.AtomicIsize.html#method.new"]' 'AtomicIsize::new'
1125
let _ = AtomicIsize::new(0);
12-
b();
26+
// @has - '//a[@href="#48"]' 'local_private'
27+
local_private();
28+
}
29+
30+
pub fn extern_call() {
31+
// @has - '//a[@href="{{channel}}/std/process/fn.exit.html"]' 'exit'
32+
exit(0);
33+
}
34+
35+
pub fn macro_call() -> Result<(), ()> {
36+
// @has - '//a[@href="{{channel}}/core/macro.try.html"]' 'try!'
37+
try!(Err(()));
38+
Ok(())
39+
}
40+
41+
pub fn variant() {
42+
// @has - '//a[@href="{{channel}}/core/cmp/enum.Ordering.html#variant.Less"]' 'Ordering::Less'
43+
let _ = Ordering::Less;
44+
// @has - '//a[@href="{{channel}}/core/marker/struct.PhantomData.html"]' 'PhantomData'
45+
let _: PhantomData::<usize> = PhantomData;
1346
}
1447

15-
fn b() {}
48+
fn local_private() {}

0 commit comments

Comments
 (0)