Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Fix visibility of trait and impl items #81288

Merged
merged 1 commit into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,10 @@ impl Clean<Item> for hir::TraitItem<'_> {
AssocTypeItem(bounds.clean(cx), default.clean(cx))
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
let what_rustc_thinks =
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
// Trait items always inherit the trait's visibility -- we don't want to show `pub`.
Item { visibility: Inherited, ..what_rustc_thinks }
})
}
}
Expand Down Expand Up @@ -1131,7 +1134,21 @@ impl Clean<Item> for hir::ImplItem<'_> {
)
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)

let what_rustc_thinks =
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(self.hir_id));
if let hir::ItemKind::Impl(impl_) = &parent_item.kind {
if impl_.of_trait.is_some() {
// Trait impl items always inherit the impl's visibility --
// we don't want to show `pub`.
Item { visibility: Inherited, ..what_rustc_thinks }
} else {
what_rustc_thinks
}
} else {
panic!("found impl item with non-impl parent {:?}", parent_item);
}
})
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/rustdoc/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,35 @@ mod a {
struct FooBPriv;
}
}

// @has 'foo/trait.PubTrait.html' '//pre' 'pub trait PubTrait'
//
// @has 'foo/trait.PubTrait.html' '//pre' 'type Type;'
// @!has 'foo/trait.PubTrait.html' '//pre' 'pub type Type;'
//
// @has 'foo/trait.PubTrait.html' '//pre' 'const CONST: usize;'
// @!has 'foo/trait.PubTrait.html' '//pre' 'pub const CONST: usize;'
//
// @has 'foo/trait.PubTrait.html' '//pre' 'fn function();'
// @!has 'foo/trait.PubTrait.html' '//pre' 'pub fn function();'

pub trait PubTrait {
type Type;
const CONST: usize;
fn function();
}

// @has 'foo/struct.FooPublic.html' '//code' 'type Type'
// @!has 'foo/struct.FooPublic.html' '//code' 'pub type Type'
//
// @has 'foo/struct.FooPublic.html' '//code' 'const CONST: usize'
// @!has 'foo/struct.FooPublic.html' '//code' 'pub const CONST: usize'
//
// @has 'foo/struct.FooPublic.html' '//code' 'fn function()'
// @!has 'foo/struct.FooPublic.html' '//code' 'pub fn function()'

impl PubTrait for FooPublic {
type Type = usize;
const CONST: usize = 0;
fn function() {}
}