Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,14 @@ impl ExternalCrate {
if !url.ends_with('/') {
url.push('/');
}
Remote(url)
let is_absolute = url.starts_with('/')
|| url.split_once(':').is_some_and(|(scheme, _)| {
scheme.bytes().next().is_some_and(|b| b.is_ascii_alphabetic())
&& scheme
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
});
Comment on lines 206 to 212
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me wonder whether we should do further sanity checking here, eg ensuring that url does not have query or fragment parts - but that can be dealt with as a separate issue/PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, worth a follow-up.

Remote { url, is_absolute }
}

// See if there's documentation generated into the local directory
Expand Down Expand Up @@ -316,7 +323,7 @@ impl ExternalCrate {
#[derive(Debug)]
pub(crate) enum ExternalLocation {
/// Remote URL root of the external crate
Remote(String),
Remote { url: String, is_absolute: bool },
/// This external crate can be found in the local doc/ folder
Local,
/// The external crate could not be found.
Expand Down
53 changes: 32 additions & 21 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,10 @@ fn generate_macro_def_id_path(
}

let url = match cache.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
// `ExternalLocation::Remote` always end with a `/`.
format!("{s}{path}", path = fmt::from_fn(|f| path.iter().joined("/", f)))
ExternalLocation::Remote { ref url, is_absolute } => {
let mut prefix = remote_url_prefix(url, is_absolute, cx.current.len());
prefix.extend(path.iter().copied());
prefix.finish()
}
ExternalLocation::Local => {
// `root_path` always end with a `/`.
Expand Down Expand Up @@ -458,10 +459,10 @@ fn generate_item_def_id_path(

let shortty = ItemType::from_def_id(def_id, tcx);
let module_fqp = to_module_fqp(shortty, &fqp);
let mut is_remote = false;
let mut is_absolute = false;

let url_parts = url_parts(cx.cache(), def_id, module_fqp, &cx.current, &mut is_remote)?;
let mut url_parts = make_href(root_path, shortty, url_parts, &fqp, is_remote);
let url_parts = url_parts(cx.cache(), def_id, module_fqp, &cx.current, &mut is_absolute)?;
let mut url_parts = make_href(root_path, shortty, url_parts, &fqp, is_absolute);
if def_id != original_def_id {
let kind = ItemType::from_def_id(original_def_id, tcx);
url_parts = format!("{url_parts}#{kind}.{}", tcx.item_name(original_def_id))
Expand Down Expand Up @@ -493,18 +494,29 @@ fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
}

fn remote_url_prefix(url: &str, is_absolute: bool, depth: usize) -> UrlPartsBuilder {
let url = url.trim_end_matches('/');
if is_absolute {
UrlPartsBuilder::singleton(url)
} else {
let extra = depth.saturating_sub(1);
let mut b: UrlPartsBuilder = iter::repeat_n("..", extra).collect();
b.push(url);
b
}
}

fn url_parts(
cache: &Cache,
def_id: DefId,
module_fqp: &[Symbol],
relative_to: &[Symbol],
is_remote: &mut bool,
is_absolute: &mut bool,
) -> Result<UrlPartsBuilder, HrefError> {
match cache.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
*is_remote = true;
let s = s.trim_end_matches('/');
let mut builder = UrlPartsBuilder::singleton(s);
ExternalLocation::Remote { ref url, is_absolute: abs } => {
*is_absolute = abs;
let mut builder = remote_url_prefix(url, abs, relative_to.len());
builder.extend(module_fqp.iter().copied());
Ok(builder)
}
Expand All @@ -518,9 +530,10 @@ fn make_href(
shortty: ItemType,
mut url_parts: UrlPartsBuilder,
fqp: &[Symbol],
is_remote: bool,
is_absolute: bool,
) -> String {
if !is_remote && let Some(root_path) = root_path {
// FIXME: relative extern URLs may break when prefixed with root_path
if !is_absolute && let Some(root_path) = root_path {
let root = root_path.trim_end_matches('/');
url_parts.push_front(root);
}
Expand Down Expand Up @@ -583,7 +596,7 @@ pub(crate) fn href_with_root_path(
}
}

let mut is_remote = false;
let mut is_absolute = false;
let (fqp, shortty, url_parts) = match cache.paths.get(&did) {
Some(&(ref fqp, shortty)) => (fqp, shortty, {
let module_fqp = to_module_fqp(shortty, fqp.as_slice());
Expand All @@ -597,7 +610,7 @@ pub(crate) fn href_with_root_path(
let def_id_to_get = if root_path.is_some() { original_did } else { did };
if let Some(&(ref fqp, shortty)) = cache.external_paths.get(&def_id_to_get) {
let module_fqp = to_module_fqp(shortty, fqp);
(fqp, shortty, url_parts(cache, did, module_fqp, relative_to, &mut is_remote)?)
(fqp, shortty, url_parts(cache, did, module_fqp, relative_to, &mut is_absolute)?)
} else if matches!(def_kind, DefKind::Macro(_)) {
return generate_macro_def_id_path(did, cx, root_path);
} else if did.is_local() {
Expand All @@ -608,7 +621,7 @@ pub(crate) fn href_with_root_path(
}
};
Ok(HrefInfo {
url: make_href(root_path, shortty, url_parts, fqp, is_remote),
url: make_href(root_path, shortty, url_parts, fqp, is_absolute),
kind: shortty,
rust_path: fqp.clone(),
})
Expand Down Expand Up @@ -762,12 +775,10 @@ fn primitive_link_fragment(
}
Some(&def_id) => {
let loc = match m.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
ExternalLocation::Remote { ref url, is_absolute } => {
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
let builder: UrlPartsBuilder =
[s.as_str().trim_end_matches('/'), cname_sym.as_str()]
.into_iter()
.collect();
let mut builder = remote_url_prefix(url, is_absolute, cx.current.len());
builder.push(cname_sym.as_str());
Some(builder)
}
ExternalLocation::Local => {
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,9 @@ impl<'tcx> Context<'tcx> {
let e = ExternalCrate { crate_num: cnum };
(e.name(self.tcx()), e.src_root(self.tcx()))
}
ExternalLocation::Remote(ref s) => {
root = s.to_string();
ExternalLocation::Remote { ref url, .. } => {
// FIXME: relative extern URLs are not depth-adjusted for source pages
root = url.to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can't properly handle relative extern locations here for now, could we add a FIXME: comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added FIXME comments on both sites.

let e = ExternalCrate { crate_num: cnum };
(e.name(self.tcx()), e.src_root(self.tcx()))
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
types::ExternalCrate {
name: e.name(self.tcx).to_string(),
html_root_url: match external_location {
ExternalLocation::Remote(s) => Some(s.clone()),
// FIXME: relative extern URLs are not resolved here
ExternalLocation::Remote { url, .. } => Some(url.clone()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, perhaps add a FIXME: here?

_ => None,
},
path: self
Expand Down
27 changes: 27 additions & 0 deletions tests/rustdoc-html/extern/extern-html-root-url-relative.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While looking for more bugs, I wrote a few test cases that you probably want to incorporate. All of them passed, so it seems fine. Just bringing them up:

diff --git a/tests/rustdoc-html/extern/extern-html-root-url-relative.rs b/tests/rustdoc-html/extern/extern-html-root-url-relative.rs
index df6ebf1aedd..ba2b50c6bf2 100644
--- a/tests/rustdoc-html/extern/extern-html-root-url-relative.rs
+++ b/tests/rustdoc-html/extern/extern-html-root-url-relative.rs
@@ -1,4 +1,4 @@
-//@ compile-flags:-Z unstable-options --extern-html-root-url core=../ --extern-html-root-takes-precedence
+//@ compile-flags:-Z unstable-options --extern-html-root-url core=../ --extern-html-root-takes-precedence --generate-link-to-definition
 
 // At depth 1 (top-level), the href should be ../core/...
 //@ has extern_html_root_url_relative/index.html
@@ -9,7 +9,19 @@
 // At depth 2 (inside a module), the href should be ../../core/...
 pub mod nested {
     //@ has extern_html_root_url_relative/nested/index.html
-    //@ has - '//a/@href' '../../core/iter/index.html'
+    //@ has - '//a/@href' '../../core/future/index.html'
     #[doc(no_inline)]
-    pub use std::iter;
+    pub use std::future;
 }
+
+// Also depth 2, but for an intra-doc link.
+//@ has extern_html_root_url_relative/intra_doc_link/index.html
+//@ has - '//a/@href' '../../core/ptr/fn.write.html'
+/// [write](<core::ptr::write()>)
+pub mod intra_doc_link {
+}
+
+// link-to-definition
+//@ has src/extern_html_root_url_relative/extern-html-root-url-relative.rs.html
+//@ has - '//a/@href' '../../core/iter/index.html'
+//@ has - '//a/@href' '../../core/future/index.html'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, thanks for the extra coverage.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//@ compile-flags:-Z unstable-options --extern-html-root-url core=../ --extern-html-root-takes-precedence --generate-link-to-definition

// At depth 1 (top-level), the href should be ../core/...
//@ has extern_html_root_url_relative/index.html
//@ has - '//a/@href' '../core/iter/index.html'
#[doc(no_inline)]
pub use std::iter;

// At depth 2 (inside a module), the href should be ../../core/...
pub mod nested {
//@ has extern_html_root_url_relative/nested/index.html
//@ has - '//a/@href' '../../core/future/index.html'
#[doc(no_inline)]
pub use std::future;
}

// Also depth 2, but for an intra-doc link.
//@ has extern_html_root_url_relative/intra_doc_link/index.html
//@ has - '//a/@href' '../../core/ptr/fn.write.html'
/// [write](<core::ptr::write()>)
pub mod intra_doc_link {
}

// link-to-definition
//@ has src/extern_html_root_url_relative/extern-html-root-url-relative.rs.html
//@ has - '//a/@href' '../../core/iter/index.html'
//@ has - '//a/@href' '../../core/future/index.html'
Loading