Skip to content

Commit ae5108a

Browse files
GuillaumeGomezlqd
authored andcommitted
Add code comments and documentation
1 parent 72d6fde commit ae5108a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ pub fn is_builtin_attr_name(name: Symbol) -> bool {
837837
BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
838838
}
839839

840+
/// Whether this builtin attribute is only used in the local crate.
841+
/// If so, it is not encoded in the crate metadata.
840842
pub fn is_builtin_only_local(name: Symbol) -> bool {
841843
BUILTIN_ATTRIBUTE_MAP.get(&name).map_or(false, |attr| attr.only_local)
842844
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+14
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
765765
}
766766
}
767767

768+
/// Returns whether an attribute needs to be recorded in metadata, that is, if it's usable and
769+
/// useful in downstream crates. Local-only attributes are an obvious example, but some
770+
/// rustdoc-specific attributes can equally be of use while documenting the current crate only.
771+
///
772+
/// Removing these superfluous attributes speeds up compilation by making the metadata smaller.
773+
///
774+
/// Note: the `is_def_id_public` parameter is used to cache whether the given `DefId` has a public
775+
/// visibility: this is a piece of data that can be computed once per defid, and not once per
776+
/// attribute. Some attributes would only be usable downstream if they are public.
768777
#[inline]
769778
fn should_encode_attr(
770779
tcx: TyCtxt<'_>,
@@ -773,12 +782,17 @@ fn should_encode_attr(
773782
is_def_id_public: &mut Option<bool>,
774783
) -> bool {
775784
if rustc_feature::is_builtin_only_local(attr.name_or_empty()) {
785+
// Attributes marked local-only don't need to be encoded for downstream crates.
776786
false
777787
} else if attr.doc_str().is_some() {
788+
// We keep all public doc comments because they might be "imported" into downstream crates
789+
// if they use `#[doc(inline)]` to copy an item's documentation into their own.
778790
*is_def_id_public.get_or_insert_with(|| {
779791
tcx.privacy_access_levels(()).get_effective_vis(def_id).is_some()
780792
})
781793
} else if attr.has_name(sym::doc) {
794+
// If this is a `doc` attribute, and it's marked `inline` (as in `#[doc(inline)]`), we can
795+
// remove it. It won't be inlinable in downstream crates.
782796
attr.meta_item_list().map(|l| l.iter().any(|l| !l.has_name(sym::inline))).unwrap_or(false)
783797
} else {
784798
true

0 commit comments

Comments
 (0)