Skip to content

Commit fd69fa8

Browse files
Add missing root_path when generating links using href
1 parent 1a48d1a commit fd69fa8

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/librustdoc/html/format.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,11 @@ crate enum HrefError {
484484
NotInExternalCache,
485485
}
486486

487-
crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<String>), HrefError> {
487+
crate fn href_with_root_path(
488+
did: DefId,
489+
cx: &Context<'_>,
490+
root_path: Option<&str>,
491+
) -> Result<(String, ItemType, Vec<String>), HrefError> {
488492
let cache = &cx.cache();
489493
let relative_to = &cx.current;
490494
fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] {
@@ -495,6 +499,7 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
495499
return Err(HrefError::Private);
496500
}
497501

502+
let mut is_remote = false;
498503
let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) {
499504
Some(&(ref fqp, shortty)) => (fqp, shortty, {
500505
let module_fqp = to_module_fqp(shortty, fqp);
@@ -508,6 +513,7 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
508513
shortty,
509514
match cache.extern_locations[&did.krate] {
510515
ExternalLocation::Remote(ref s) => {
516+
is_remote = true;
511517
let s = s.trim_end_matches('/');
512518
let mut s = vec![s];
513519
s.extend(module_fqp[..].iter().map(String::as_str));
@@ -522,6 +528,12 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
522528
}
523529
}
524530
};
531+
if !is_remote {
532+
if let Some(root_path) = root_path {
533+
let root = root_path.trim_end_matches('/');
534+
url_parts.insert(0, root);
535+
}
536+
}
525537
let last = &fqp.last().unwrap()[..];
526538
let filename;
527539
match shortty {
@@ -536,6 +548,10 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Str
536548
Ok((url_parts.join("/"), shortty, fqp.to_vec()))
537549
}
538550

551+
crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<String>), HrefError> {
552+
href_with_root_path(did, cx, None)
553+
}
554+
539555
/// Both paths should only be modules.
540556
/// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will
541557
/// both need `../iter/trait.Iterator.html` to get at the iterator trait.

src/librustdoc/html/highlight.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,19 @@ fn string<T: Display>(
580580
if let Some(href) =
581581
context_info.context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
582582
let context = context_info.context;
583+
// FIXME: later on, it'd be nice to provide two links (if possible) for all items:
584+
// one to the documentation page and one to the source definition.
585+
// FIXME: currently, external items only generate a link to their documentation,
586+
// a link to their definition can be generated using this:
587+
// https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338
583588
match href {
584589
LinkFromSrc::Local(span) => context
585590
.href_from_span(*span)
586591
.map(|s| format!("{}{}", context_info.root_path, s)),
587592
LinkFromSrc::External(def_id) => {
588-
format::href(*def_id, context).map(|(url, _, _)| url)
593+
format::href_with_root_path(*def_id, context, Some(context_info.root_path))
594+
.ok()
595+
.map(|(url, _, _)| url)
589596
}
590597
}
591598
})

src/librustdoc/html/render/span_map.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl<'tcx> SpanMapVisitor<'tcx> {
110110
Some(def_id)
111111
}
112112
Res::Local(_) => None,
113+
Res::Err => return,
113114
_ => return,
114115
};
115116
if let Some(span) = self.tcx.hir().res_span(path.res) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct SourceCode;

src/test/rustdoc/check-source-code-urls-to-def.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// compile-flags: -Zunstable-options --generate-link-to-definition
2+
// aux-build:source_code.rs
3+
// build-aux-docs
24

35
#![crate_name = "foo"]
46

7+
extern crate source_code;
8+
59
// @has 'src/foo/check-source-code-urls-to-def.rs.html'
610

711
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#1-17"]' 'bar'
@@ -23,13 +27,14 @@ impl Foo {
2327
fn babar() {}
2428

2529
// @has - '//a[@href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html"]' 'String'
26-
// @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#17"]' 5
27-
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar) {
30+
// @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#21"]' 5
31+
// @has - '//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
32+
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
2833
let x = 12;
2934
let y: Foo = Foo;
3035
let z: Bar = bar::Bar { field: Foo };
3136
babar();
32-
// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#20"]' 'hello'
37+
// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#24"]' 'hello'
3338
y.hello();
3439
}
3540

0 commit comments

Comments
 (0)