Skip to content

Commit ebfdd11

Browse files
committed
rustdoc: Fix a couple of issues with src links to external crates
- src links/redirects to extern fn from another crate had an extra '/'. - src links to `pub use` of a crate module had an extra '/'. - src links to renamed reexports from another crate used the new name for the link but should use the original name.
1 parent 3313e50 commit ebfdd11

File tree

6 files changed

+83
-8
lines changed

6 files changed

+83
-8
lines changed

src/librustdoc/clean/inline.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,14 @@ pub fn load_attrs<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
143143
pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) {
144144
if let Some(tcx) = cx.tcx_opt() {
145145
let crate_name = tcx.sess.cstore.crate_name(did.krate).to_string();
146-
let relative = tcx.def_path(did).data.into_iter().map(|elem| {
147-
elem.data.to_string()
146+
let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| {
147+
// extern blocks have an empty name
148+
let s = elem.data.to_string();
149+
if !s.is_empty() {
150+
Some(s)
151+
} else {
152+
None
153+
}
148154
});
149155
let fqn = once(crate_name).chain(relative).collect();
150156
cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind));

src/librustdoc/html/render.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1519,20 +1519,23 @@ impl<'a> Item<'a> {
15191519
// located, then we return `None`.
15201520
} else {
15211521
let cache = cache();
1522-
let path = match cache.external_paths.get(&self.item.def_id) {
1522+
let external_path = match cache.external_paths.get(&self.item.def_id) {
15231523
Some(path) => path,
15241524
None => return None,
15251525
};
1526-
let root = match cache.extern_locations.get(&self.item.def_id.krate) {
1526+
let mut path = match cache.extern_locations.get(&self.item.def_id.krate) {
15271527
Some(&(_, Remote(ref s))) => s.to_string(),
15281528
Some(&(_, Local)) => self.cx.root_path.clone(),
15291529
Some(&(_, Unknown)) => return None,
15301530
None => return None,
15311531
};
1532-
Some(format!("{root}{path}/{file}?gotosrc={goto}",
1533-
root = root,
1534-
path = path[..path.len() - 1].join("/"),
1535-
file = item_path(shortty(self.item), self.item.name.as_ref().unwrap()),
1532+
for item in &external_path[..external_path.len() - 1] {
1533+
path.push_str(item);
1534+
path.push_str("/");
1535+
}
1536+
Some(format!("{path}{file}?gotosrc={goto}",
1537+
path = path,
1538+
file = item_path(shortty(self.item), external_path.last().unwrap()),
15361539
goto = self.item.def_id.index.as_usize()))
15371540
}
15381541
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern {
12+
pub fn extern_c_fn();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub struct Foo;

src/test/rustdoc/issue-34274.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-34274.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
#![crate_name = "foo"]
16+
17+
extern crate issue_34274;
18+
19+
// @has foo/fn.extern_c_fn.html '//a/@href' '../issue_34274/fn.extern_c_fn.html?gotosrc='
20+
pub use issue_34274::extern_c_fn;
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:src-links-external.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
#![crate_name = "foo"]
16+
17+
extern crate src_links_external;
18+
19+
// @has foo/bar/index.html '//a/@href' '../src_links_external/index.html?gotosrc='
20+
pub use src_links_external as bar;
21+
22+
// @has foo/bar/struct.Foo.html '//a/@href' '../src_links_external/struct.Foo.html?gotosrc='

0 commit comments

Comments
 (0)