Skip to content

Commit 2c4a23a

Browse files
authored
Unrolled build for #143849
Rollup merge of #143849 - lolbinarycat:rustdoc-priv-normalize-143222, r=GuillaumeGomez rustdoc: never link to unnamable items fixes #143222
2 parents 6c02dd4 + 05a62c8 commit 2c4a23a

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/librustdoc/html/format.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ pub(crate) enum HrefError {
368368
Private,
369369
// Not in external cache, href link should be in same page
370370
NotInExternalCache,
371+
/// Refers to an unnamable item, such as one defined within a function or const block.
372+
UnnamableItem,
371373
}
372374

373375
/// This function is to get the external macro path because they are not in the cache used in
@@ -479,6 +481,26 @@ fn generate_item_def_id_path(
479481
Ok((url_parts, shortty, fqp))
480482
}
481483

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, as long as they are visible
490+
DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
491+
// items in impls can be linked to,
492+
// as long as we can link to the item the impl is on.
493+
// since associated traits are not a thing,
494+
// it should not be possible to refer to an impl item if
495+
// the base type is not namable.
496+
DefKind::Impl { .. } => return false,
497+
// everything else does not have docs generated for it
498+
_ => return true,
499+
}
500+
}
501+
return false;
502+
}
503+
482504
fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
483505
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
484506
}
@@ -552,6 +574,9 @@ pub(crate) fn href_with_root_path(
552574
}
553575
_ => original_did,
554576
};
577+
if is_unnamable(cx.tcx(), did) {
578+
return Err(HrefError::UnnamableItem);
579+
}
555580
let cache = cx.cache();
556581
let relative_to = &cx.current;
557582

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub trait Assoc {
2+
type Ty;
3+
}
4+
5+
pub struct Foo(<Foo as crate::Assoc>::Ty);
6+
7+
const _X: () = {
8+
impl crate::Assoc for Foo {
9+
type Ty = Bar;
10+
}
11+
pub struct Bar;
12+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ compile-flags: -Z normalize-docs --document-private-items -Zunstable-options --show-type-layout
2+
//@ aux-build:wrap-unnamable-type.rs
3+
//@ build-aux-docs
4+
5+
// regression test for https://github.com/rust-lang/rust/issues/143222
6+
// makes sure normalizing docs does not cause us to link to unnamable types
7+
// in cross-crate reexports.
8+
9+
#![crate_name = "foo"]
10+
11+
extern crate wrap_unnamable_type as helper;
12+
13+
//@ has 'foo/struct.Foo.html'
14+
//@ !hasraw - 'struct.Bar.html'
15+
#[doc(inline)]
16+
pub use helper::Foo;

0 commit comments

Comments
 (0)