1
Fork 0

fix dead link for method in trait of blanket impl from third party crate

This commit is contained in:
François Mockers 2021-06-27 11:10:36 +02:00
parent 238fd72880
commit 53447d81d0
3 changed files with 47 additions and 31 deletions

View file

@ -472,7 +472,18 @@ impl clean::GenericArgs {
}
}
crate fn href(did: DefId, cx: &Context<'_>) -> Option<(String, ItemType, Vec<String>)> {
// Possible errors when computing href link source for a `DefId`
crate enum HrefError {
// `DefId` is in an unknown location. This seems to happen when building without dependencies
// but a trait from a dependency is still visible
UnknownLocation,
// Unavailable because private
Unavailable,
// Not in external cache, href link should be in same page
NotInExternalCache,
}
crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<String>), HrefError> {
let cache = &cx.cache();
let relative_to = &cx.current;
fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] {
@ -480,7 +491,7 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Option<(String, ItemType, Vec<Str
}
if !did.is_local() && !cache.access_levels.is_public(did) && !cache.document_private {
return None;
return Err(HrefError::Unavailable);
}
let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) {
@ -489,22 +500,25 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Option<(String, ItemType, Vec<Str
href_relative_parts(module_fqp, relative_to)
}),
None => {
let &(ref fqp, shortty) = cache.external_paths.get(&did)?;
let module_fqp = to_module_fqp(shortty, fqp);
(
fqp,
shortty,
match cache.extern_locations[&did.krate] {
ExternalLocation::Remote(ref s) => {
let s = s.trim_end_matches('/');
let mut s = vec![&s[..]];
s.extend(module_fqp[..].iter().map(String::as_str));
s
}
ExternalLocation::Local => href_relative_parts(module_fqp, relative_to),
ExternalLocation::Unknown => return None,
},
)
if let Some(&(ref fqp, shortty)) = cache.external_paths.get(&did) {
let module_fqp = to_module_fqp(shortty, fqp);
(
fqp,
shortty,
match cache.extern_locations[&did.krate] {
ExternalLocation::Remote(ref s) => {
let s = s.trim_end_matches('/');
let mut s = vec![&s[..]];
s.extend(module_fqp[..].iter().map(String::as_str));
s
}
ExternalLocation::Local => href_relative_parts(module_fqp, relative_to),
ExternalLocation::Unknown => return Err(HrefError::UnknownLocation),
},
)
} else {
return Err(HrefError::NotInExternalCache);
}
}
};
let last = &fqp.last().unwrap()[..];
@ -518,7 +532,7 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Option<(String, ItemType, Vec<Str
url_parts.push(&filename);
}
}
Some((url_parts.join("/"), shortty, fqp.to_vec()))
Ok((url_parts.join("/"), shortty, fqp.to_vec()))
}
/// Both paths should only be modules.
@ -567,7 +581,7 @@ fn resolved_path<'a, 'cx: 'a>(
write!(w, "{}{:#}", &last.name, last.args.print(cx))?;
} else {
let path = if use_absolute {
if let Some((_, _, fqp)) = href(did, cx) {
if let Ok((_, _, fqp)) = href(did, cx) {
format!(
"{}::{}",
fqp[..fqp.len() - 1].join("::"),
@ -675,7 +689,7 @@ crate fn anchor<'a, 'cx: 'a>(
) -> impl fmt::Display + 'a {
let parts = href(did.into(), cx);
display_fn(move |f| {
if let Some((url, short_ty, fqp)) = parts {
if let Ok((url, short_ty, fqp)) = parts {
write!(
f,
r#"<a class="{}" href="{}" title="{} {}">{}</a>"#,
@ -907,7 +921,7 @@ fn fmt_type<'cx>(
// look at).
box clean::ResolvedPath { did, .. } => {
match href(did.into(), cx) {
Some((ref url, _, ref path)) if !f.alternate() => {
Ok((ref url, _, ref path)) if !f.alternate() => {
write!(
f,
"<a class=\"type\" href=\"{url}#{shortty}.{name}\" \