Skip to content

Commit 9ca6d58

Browse files
committed
Do not print visibility in external traits
1 parent 89ebad5 commit 9ca6d58

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/librustdoc/clean/inline.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::clean::{self, Attributes, AttributesExt, GetDefId, ToSource};
1919
use crate::core::DocContext;
2020
use crate::formats::item_type::ItemType;
2121

22-
use super::Clean;
22+
use super::{Clean, Visibility};
2323

2424
type Attrs<'hir> = rustc_middle::ty::Attributes<'hir>;
2525

@@ -193,8 +193,18 @@ crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType)
193193
}
194194

195195
crate fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
196-
let trait_items =
197-
cx.tcx.associated_items(did).in_definition_order().map(|item| item.clean(cx)).collect();
196+
let trait_items = cx
197+
.tcx
198+
.associated_items(did)
199+
.in_definition_order()
200+
.map(|item| {
201+
// When building an external trait, the cleaned trait will have all items public,
202+
// which causes methods to have a `pub` prefix, which is invalid since items in traits
203+
// can not have a visibility prefix. Thus we override the visibility here manually.
204+
// See https://github.com/rust-lang/rust/issues/81274
205+
clean::Item { visibility: Visibility::Inherited, ..item.clean(cx) }
206+
})
207+
.collect();
198208

199209
let predicates = cx.tcx.predicates_of(did);
200210
let generics = (cx.tcx.generics_of(did), predicates).clean(cx);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub trait Bar {
2+
fn foo();
3+
}

src/test/rustdoc/trait-visibility.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// aux-build:trait-visibility.rs
2+
3+
#![crate_name = "foo"]
4+
5+
extern crate trait_visibility;
6+
7+
// @has foo/trait.Bar.html '//a[@href="#tymethod.foo"]/..' "fn foo()"
8+
pub use trait_visibility::Bar;

0 commit comments

Comments
 (0)