Skip to content

Commit 7906dc0

Browse files
jonas-schievinkehuss
authored andcommitted
Rollup merge of #81288 - camelid:fix-trait-item-vis, r=jyn514
rustdoc: Fix visibility of trait and impl items Fixes #81274. r? `@jyn514`
1 parent d1eff4c commit 7906dc0

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/librustdoc/clean/mod.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,10 @@ impl Clean<Item> for hir::TraitItem<'_> {
10961096
AssocTypeItem(bounds.clean(cx), default.clean(cx))
10971097
}
10981098
};
1099-
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1099+
let what_rustc_thinks =
1100+
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
1101+
// Trait items always inherit the trait's visibility -- we don't want to show `pub`.
1102+
Item { visibility: Inherited, ..what_rustc_thinks }
11001103
})
11011104
}
11021105
}
@@ -1124,7 +1127,21 @@ impl Clean<Item> for hir::ImplItem<'_> {
11241127
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
11251128
}
11261129
};
1127-
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1130+
1131+
let what_rustc_thinks =
1132+
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
1133+
let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(self.hir_id));
1134+
if let hir::ItemKind::Impl { of_trait, .. } = &parent_item.kind {
1135+
if of_trait.is_some() {
1136+
// Trait impl items always inherit the impl's visibility --
1137+
// we don't want to show `pub`.
1138+
Item { visibility: Inherited, ..what_rustc_thinks }
1139+
} else {
1140+
what_rustc_thinks
1141+
}
1142+
} else {
1143+
panic!("found impl item with non-impl parent {:?}", parent_item);
1144+
}
11281145
})
11291146
}
11301147
}

src/test/rustdoc/visibility.rs

+32
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,35 @@ mod a {
4242
struct FooBPriv;
4343
}
4444
}
45+
46+
// @has 'foo/trait.PubTrait.html' '//pre' 'pub trait PubTrait'
47+
//
48+
// @has 'foo/trait.PubTrait.html' '//pre' 'type Type;'
49+
// @!has 'foo/trait.PubTrait.html' '//pre' 'pub type Type;'
50+
//
51+
// @has 'foo/trait.PubTrait.html' '//pre' 'const CONST: usize;'
52+
// @!has 'foo/trait.PubTrait.html' '//pre' 'pub const CONST: usize;'
53+
//
54+
// @has 'foo/trait.PubTrait.html' '//pre' 'fn function();'
55+
// @!has 'foo/trait.PubTrait.html' '//pre' 'pub fn function();'
56+
57+
pub trait PubTrait {
58+
type Type;
59+
const CONST: usize;
60+
fn function();
61+
}
62+
63+
// @has 'foo/struct.FooPublic.html' '//code' 'type Type'
64+
// @!has 'foo/struct.FooPublic.html' '//code' 'pub type Type'
65+
//
66+
// @has 'foo/struct.FooPublic.html' '//code' 'const CONST: usize'
67+
// @!has 'foo/struct.FooPublic.html' '//code' 'pub const CONST: usize'
68+
//
69+
// @has 'foo/struct.FooPublic.html' '//code' 'fn function()'
70+
// @!has 'foo/struct.FooPublic.html' '//code' 'pub fn function()'
71+
72+
impl PubTrait for FooPublic {
73+
type Type = usize;
74+
const CONST: usize = 0;
75+
fn function() {}
76+
}

0 commit comments

Comments
 (0)