Rollup merge of #84832 - Stupremee:dont-print-vis-in-external-traits, r=jyn514

Do not print visibility in external traits

This PR fixes the bug that caused traits, which were re-exported, having visibility modifiers in front of methods, which is invalid.

It would be nice to add a test for this, but I don't even know if tests with multiple crates are possible.

Resolves #81274
This commit is contained in:
Dylan DPC 2021-05-03 00:32:45 +02:00 committed by GitHub
commit 83c49d09b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -19,7 +19,7 @@ use crate::clean::{self, Attributes, AttributesExt, GetDefId, ToSource};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;
use super::Clean;
use super::{Clean, Visibility};
type Attrs<'hir> = rustc_middle::ty::Attributes<'hir>;
@ -193,8 +193,18 @@ crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType)
}
crate fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
let trait_items =
cx.tcx.associated_items(did).in_definition_order().map(|item| item.clean(cx)).collect();
let trait_items = cx
.tcx
.associated_items(did)
.in_definition_order()
.map(|item| {
// When building an external trait, the cleaned trait will have all items public,
// which causes methods to have a `pub` prefix, which is invalid since items in traits
// can not have a visibility prefix. Thus we override the visibility here manually.
// See https://github.com/rust-lang/rust/issues/81274
clean::Item { visibility: Visibility::Inherited, ..item.clean(cx) }
})
.collect();
let predicates = cx.tcx.predicates_of(did);
let generics = (cx.tcx.generics_of(did), predicates).clean(cx);

View File

@ -0,0 +1,3 @@
pub trait Bar {
fn foo();
}

View File

@ -0,0 +1,8 @@
// aux-build:trait-visibility.rs
#![crate_name = "foo"]
extern crate trait_visibility;
// @has foo/trait.Bar.html '//a[@href="#tymethod.foo"]/..' "fn foo()"
pub use trait_visibility::Bar;