Skip to content

Commit 72d6fde

Browse files
GuillaumeGomezlqd
authored andcommitted
Remove doc comments only for private items or some specific doc comments
1 parent b12b65c commit 72d6fde

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::rmeta::def_path_hash_map::DefPathHashMapRef;
33
use crate::rmeta::table::TableBuilder;
44
use crate::rmeta::*;
55

6+
use rustc_ast::Attribute;
67
use rustc_data_structures::fingerprint::Fingerprint;
78
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
89
use rustc_data_structures::memmap::{Mmap, MmapMut};
@@ -764,6 +765,26 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
764765
}
765766
}
766767

768+
#[inline]
769+
fn should_encode_attr(
770+
tcx: TyCtxt<'_>,
771+
attr: &Attribute,
772+
def_id: LocalDefId,
773+
is_def_id_public: &mut Option<bool>,
774+
) -> bool {
775+
if rustc_feature::is_builtin_only_local(attr.name_or_empty()) {
776+
false
777+
} else if attr.doc_str().is_some() {
778+
*is_def_id_public.get_or_insert_with(|| {
779+
tcx.privacy_access_levels(()).get_effective_vis(def_id).is_some()
780+
})
781+
} else if attr.has_name(sym::doc) {
782+
attr.meta_item_list().map(|l| l.iter().any(|l| !l.has_name(sym::inline))).unwrap_or(false)
783+
} else {
784+
true
785+
}
786+
}
787+
767788
fn should_encode_visibility(def_kind: DefKind) -> bool {
768789
match def_kind {
769790
DefKind::Mod
@@ -1126,12 +1147,14 @@ fn should_encode_trait_impl_trait_tys<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) ->
11261147

11271148
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11281149
fn encode_attrs(&mut self, def_id: LocalDefId) {
1129-
let mut attrs = self
1130-
.tcx
1150+
let tcx = self.tcx;
1151+
let mut is_public: Option<bool> = None;
1152+
1153+
let mut attrs = tcx
11311154
.hir()
1132-
.attrs(self.tcx.hir().local_def_id_to_hir_id(def_id))
1155+
.attrs(tcx.hir().local_def_id_to_hir_id(def_id))
11331156
.iter()
1134-
.filter(|attr| !rustc_feature::is_builtin_only_local(attr.name_or_empty()));
1157+
.filter(move |attr| should_encode_attr(tcx, attr, def_id, &mut is_public));
11351158

11361159
record_array!(self.tables.attributes[def_id.to_def_id()] <- attrs.clone());
11371160
if attrs.any(|attr| attr.may_have_doc_links()) {

0 commit comments

Comments
 (0)