Skip to content

Commit f57283d

Browse files
committed
rustdoc: actually never link to unnamable types
1 parent 94700f8 commit f57283d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/librustdoc/html/format.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,20 @@ fn generate_item_def_id_path(
481481
Ok((url_parts, shortty, fqp))
482482
}
483483

484+
/// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block.
485+
fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool {
486+
let mut cur_did = did;
487+
while let Some(parent) = tcx.opt_parent(cur_did) {
488+
match tcx.def_kind(parent) {
489+
// items defined in these can be linked to
490+
DefKind::Mod | DefKind::Impl { .. } | DefKind::ForeignMod => cur_did = parent,
491+
// everything else does not have docs generated for it
492+
_ => return true,
493+
}
494+
}
495+
return false;
496+
}
497+
484498
fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
485499
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
486500
}
@@ -500,13 +514,7 @@ fn url_parts(
500514
builder.extend(module_fqp.iter().copied());
501515
Ok(builder)
502516
}
503-
ExternalLocation::Local => {
504-
if module_fqp.iter().any(|sym| sym.as_str() == "_") {
505-
Err(HrefError::UnnamableItem)
506-
} else {
507-
Ok(href_relative_parts(module_fqp, relative_to))
508-
}
509-
}
517+
ExternalLocation::Local => Ok(href_relative_parts(module_fqp, relative_to)),
510518
ExternalLocation::Unknown => Err(HrefError::DocumentationNotBuilt),
511519
}
512520
}
@@ -560,6 +568,9 @@ pub(crate) fn href_with_root_path(
560568
}
561569
_ => original_did,
562570
};
571+
if is_unnamable(cx.tcx(), did) {
572+
return Err(HrefError::UnnamableItem);
573+
}
563574
let cache = cx.cache();
564575
let relative_to = &cx.current;
565576

0 commit comments

Comments
 (0)