-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Always hide private fields in aliased type #124939
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_middle::ty::Visibility; | ||
|
||
use crate::clean; | ||
use crate::clean::Item; | ||
use crate::core::DocContext; | ||
use crate::fold::{strip_item, DocFolder}; | ||
use crate::passes::Pass; | ||
|
||
pub(crate) const STRIP_ALIASED_NON_LOCAL: Pass = Pass { | ||
name: "strip-aliased-non-local", | ||
run: strip_aliased_non_local, | ||
description: "strips all non-local private aliased items from the output", | ||
}; | ||
|
||
fn strip_aliased_non_local(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate { | ||
let mut stripper = AliasedNonLocalStripper { tcx: cx.tcx }; | ||
stripper.fold_crate(krate) | ||
} | ||
|
||
struct AliasedNonLocalStripper<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
} | ||
|
||
impl<'tcx> DocFolder for AliasedNonLocalStripper<'tcx> { | ||
fn fold_item(&mut self, i: Item) -> Option<Item> { | ||
Some(match *i.kind { | ||
clean::TypeAliasItem(..) => { | ||
let mut stripper = NonLocalStripper { tcx: self.tcx }; | ||
// don't call `fold_item` as that could strip the type-alias it-self | ||
// which we don't want to strip out | ||
stripper.fold_item_recur(i) | ||
} | ||
_ => self.fold_item_recur(i), | ||
}) | ||
} | ||
} | ||
|
||
struct NonLocalStripper<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
} | ||
|
||
impl<'tcx> DocFolder for NonLocalStripper<'tcx> { | ||
fn fold_item(&mut self, i: Item) -> Option<Item> { | ||
// If not local, we want to respect the original visibility of | ||
// the field and not the one given by the user for the currrent crate. | ||
// | ||
// FIXME(#125009): Not-local should probably consider same Cargo workspace | ||
if !i.def_id().map_or(true, |did| did.is_local()) { | ||
if i.visibility(self.tcx) != Some(Visibility::Public) || i.is_doc_hidden() { | ||
return Some(strip_item(i)); | ||
} | ||
} | ||
|
||
Some(self.fold_item_recur(i)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//! This test makes sure that with never show the inner fields in the | ||
//! aliased type view of type alias. | ||
|
||
//@ compile-flags: -Z unstable-options --document-private-items | ||
|
||
#![crate_name = "foo"] | ||
|
||
use std::collections::BTreeMap; | ||
|
||
// @has 'foo/type.FooBar.html' '//*[@class="rust item-decl"]/code' 'struct FooBar { /* private fields */ }' | ||
pub type FooBar = BTreeMap<u32, String>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//! This test makes sure that with never show the inner fields in the | ||
//! aliased type view of type alias. | ||
|
||
#![crate_name = "foo"] | ||
|
||
use std::collections::BTreeMap; | ||
|
||
// @has 'foo/type.FooBar.html' '//*[@class="rust item-decl"]/code' 'struct FooBar { /* private fields */ }' | ||
pub type FooBar = BTreeMap<u32, String>; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This caused all cross-crate type-aliased enums to stop rendering:
Because
visibility()
here returnsNone
.I'll put up a PR to fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much appreciated, thanks!