@@ -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]
769778fn 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