Skip to content

Commit 07301e3

Browse files
authored
Rollup merge of #74077 - sethp:docs/fix-intra-doc-primitive-link, r=jyn514
Use relative path for local links to primitives Else, links to `char::foo` would point into `/path/to/src/libcore/std/primitive.char.html#method.foo`. Split out from #73804.
2 parents 65ac394 + 56b6b44 commit 07301e3

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

src/librustdoc/clean/types.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ impl Attributes {
628628
/// Cache must be populated before call
629629
pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
630630
use crate::html::format::href;
631+
use crate::html::render::CURRENT_DEPTH;
631632

632633
self.links
633634
.iter()
@@ -648,12 +649,13 @@ impl Attributes {
648649
if let Some(ref fragment) = *fragment {
649650
let cache = cache();
650651
let url = match cache.extern_locations.get(krate) {
651-
Some(&(_, ref src, ExternalLocation::Local)) => {
652-
src.to_str().expect("invalid file path")
652+
Some(&(_, _, ExternalLocation::Local)) => {
653+
let depth = CURRENT_DEPTH.with(|l| l.get());
654+
"../".repeat(depth)
653655
}
654-
Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
656+
Some(&(_, _, ExternalLocation::Remote(ref s))) => s.to_string(),
655657
Some(&(_, _, ExternalLocation::Unknown)) | None => {
656-
"https://doc.rust-lang.org/nightly"
658+
String::from("https://doc.rust-lang.org/nightly")
657659
}
658660
};
659661
// This is a primitive so the url is done "by hand".

src/test/rustdoc/auxiliary/my-core.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(no_core, lang_items)]
2+
#![no_core]
3+
#![crate_type="rlib"]
4+
5+
#[lang = "char"]
6+
impl char {
7+
pub fn len_utf8(self) -> usize {
8+
42
9+
}
10+
}
11+
12+
#[lang = "sized"]
13+
pub trait Sized {}
14+
15+
#[lang = "clone"]
16+
pub trait Clone: Sized {}
17+
18+
#[lang = "copy"]
19+
pub trait Copy: Clone {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// aux-build:my-core.rs
2+
// build-aux-docs
3+
// ignore-cross-compile
4+
// ignore-windows
5+
// ignore-tidy-linelength
6+
7+
#![deny(intra_doc_link_resolution_failure)]
8+
#![feature(no_core, lang_items)]
9+
#![no_core]
10+
#![crate_type = "rlib"]
11+
12+
// @has intra_link_prim_methods_external_core/index.html
13+
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html"]' 'char'
14+
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html#method.len_utf8"]' 'char::len_utf8'
15+
16+
//! A [`char`] and its [`char::len_utf8`].
17+
18+
extern crate my_core;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![deny(intra_doc_link_resolution_failure)]
2+
#![feature(no_core, lang_items)]
3+
#![no_core]
4+
#![crate_type = "rlib"]
5+
6+
// ignore-tidy-linelength
7+
8+
// @has intra_link_prim_methods_local/index.html
9+
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html"]' 'char'
10+
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html#method.len_utf8"]' 'char::len_utf8'
11+
12+
//! A [`char`] and its [`char::len_utf8`].
13+
14+
#[lang = "char"]
15+
impl char {
16+
pub fn len_utf8(self) -> usize {
17+
42
18+
}
19+
}
20+
21+
#[lang = "sized"]
22+
pub trait Sized {}
23+
24+
#[lang = "clone"]
25+
pub trait Clone: Sized {}
26+
27+
#[lang = "copy"]
28+
pub trait Copy: Clone {}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
#![deny(intra_doc_link_resolution_failure)]
22

3+
// ignore-tidy-linelength
4+
5+
// @has intra_link_prim_methods/index.html
6+
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html"]' 'char'
7+
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html#method.len_utf8"]' 'char::len_utf8'
8+
39
//! A [`char`] and its [`char::len_utf8`].

0 commit comments

Comments
 (0)