Skip to content

Commit

Permalink
Rollup merge of #84832 - Stupremee:dont-print-vis-in-external-traits,…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
Dylan-DPC authored May 2, 2021
2 parents b0c7e64 + 9ca6d58 commit 83c49d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/test/rustdoc/auxiliary/trait-visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait Bar {
fn foo();
}
8 changes: 8 additions & 0 deletions src/test/rustdoc/trait-visibility.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 83c49d0

Please sign in to comment.