Skip to content

Commit ba9d01b

Browse files
committed
Auto merge of #98450 - lqd:doc-metadata, r=lqd,GuillaumeGomez
Remove more attributes from metadata A lot of the attributes that are currently stored in the metadata aren't used at all. The biggest metadata usage comes from the doc attributes currently but they are needed by rustdoc so we only removed the ones that cannot be used in downstream crates (doc comments on private items). r? `@ghost`
2 parents 657f246 + 41263d2 commit ba9d01b

File tree

5 files changed

+111
-14
lines changed

5 files changed

+111
-14
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,24 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
296296

297297
// Lints:
298298
ungated!(
299-
warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
299+
warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
300+
DuplicatesOk, @only_local: true,
300301
),
301302
ungated!(
302-
allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
303+
allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
304+
DuplicatesOk, @only_local: true,
303305
),
304306
gated!(
305307
expect, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk,
306308
lint_reasons, experimental!(expect)
307309
),
308310
ungated!(
309-
forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
311+
forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
312+
DuplicatesOk, @only_local: true,
310313
),
311314
ungated!(
312-
deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
315+
deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
316+
DuplicatesOk, @only_local: true,
313317
),
314318
ungated!(must_use, Normal, template!(Word, NameValueStr: "reason"), FutureWarnFollowing),
315319
gated!(
@@ -340,7 +344,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
340344
),
341345
ungated!(link_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
342346
ungated!(no_link, Normal, template!(Word), WarnFollowing),
343-
ungated!(repr, Normal, template!(List: "C"), DuplicatesOk),
347+
ungated!(repr, Normal, template!(List: "C"), DuplicatesOk, @only_local: true),
344348
ungated!(export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
345349
ungated!(link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
346350
ungated!(no_mangle, Normal, template!(Word), WarnFollowing, @only_local: true),
@@ -382,7 +386,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
382386
ungated!(inline, Normal, template!(Word, List: "always|never"), FutureWarnFollowing, @only_local: true),
383387
ungated!(cold, Normal, template!(Word), WarnFollowing, @only_local: true),
384388
ungated!(no_builtins, CrateLevel, template!(Word), WarnFollowing),
385-
ungated!(target_feature, Normal, template!(List: r#"enable = "name""#), DuplicatesOk),
389+
ungated!(
390+
target_feature, Normal, template!(List: r#"enable = "name""#),
391+
DuplicatesOk, @only_local: true,
392+
),
386393
ungated!(track_caller, Normal, template!(Word), WarnFollowing),
387394
gated!(
388395
no_sanitize, Normal,
@@ -488,18 +495,24 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
488495
// Internal attributes: Stability, deprecation, and unsafe:
489496
// ==========================================================================
490497

491-
ungated!(feature, CrateLevel, template!(List: "name1, name2, ..."), DuplicatesOk),
498+
ungated!(
499+
feature, CrateLevel,
500+
template!(List: "name1, name2, ..."), DuplicatesOk, @only_local: true,
501+
),
492502
// DuplicatesOk since it has its own validation
493503
ungated!(
494-
stable, Normal, template!(List: r#"feature = "name", since = "version""#), DuplicatesOk,
504+
stable, Normal,
505+
template!(List: r#"feature = "name", since = "version""#), DuplicatesOk, @only_local: true,
495506
),
496507
ungated!(
497508
unstable, Normal,
498509
template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk,
499510
),
500511
ungated!(rustc_const_unstable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
501-
ungated!(rustc_const_stable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
502-
ungated!(rustc_safe_intrinsic, Normal, template!(Word), DuplicatesOk),
512+
ungated!(
513+
rustc_const_stable, Normal,
514+
template!(List: r#"feature = "name""#), DuplicatesOk, @only_local: true,
515+
),
503516
ungated!(
504517
rustc_default_body_unstable, Normal,
505518
template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk
@@ -517,6 +530,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
517530
allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
518531
"allow_internal_unsafe side-steps the unsafe_code lint",
519532
),
533+
ungated!(rustc_safe_intrinsic, Normal, template!(Word), DuplicatesOk),
520534
rustc_attr!(rustc_allowed_through_unstable_modules, Normal, template!(Word), WarnFollowing,
521535
"rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
522536
through unstable paths"),
@@ -823,6 +837,8 @@ pub fn is_builtin_attr_name(name: Symbol) -> bool {
823837
BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
824838
}
825839

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

compiler/rustc_metadata/src/rmeta/encoder.rs

+41-4
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,40 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
764765
}
765766
}
766767

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.
777+
#[inline]
778+
fn should_encode_attr(
779+
tcx: TyCtxt<'_>,
780+
attr: &Attribute,
781+
def_id: LocalDefId,
782+
is_def_id_public: &mut Option<bool>,
783+
) -> bool {
784+
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.
786+
false
787+
} 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.
790+
*is_def_id_public.get_or_insert_with(|| {
791+
tcx.privacy_access_levels(()).get_effective_vis(def_id).is_some()
792+
})
793+
} 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.
796+
attr.meta_item_list().map(|l| l.iter().any(|l| !l.has_name(sym::inline))).unwrap_or(false)
797+
} else {
798+
true
799+
}
800+
}
801+
767802
fn should_encode_visibility(def_kind: DefKind) -> bool {
768803
match def_kind {
769804
DefKind::Mod
@@ -1126,12 +1161,14 @@ fn should_encode_trait_impl_trait_tys<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) ->
11261161

11271162
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11281163
fn encode_attrs(&mut self, def_id: LocalDefId) {
1129-
let mut attrs = self
1130-
.tcx
1164+
let tcx = self.tcx;
1165+
let mut is_public: Option<bool> = None;
1166+
1167+
let mut attrs = tcx
11311168
.hir()
1132-
.attrs(self.tcx.hir().local_def_id_to_hir_id(def_id))
1169+
.attrs(tcx.hir().local_def_id_to_hir_id(def_id))
11331170
.iter()
1134-
.filter(|attr| !rustc_feature::is_builtin_only_local(attr.name_or_empty()));
1171+
.filter(move |attr| should_encode_attr(tcx, attr, def_id, &mut is_public));
11351172

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

src/test/ui/attr-from-macro.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// aux-build:attr-from-macro.rs
2+
// run-pass
3+
4+
extern crate attr_from_macro;
5+
6+
attr_from_macro::creator! {
7+
struct Foo;
8+
enum Bar;
9+
enum FooBar;
10+
}
11+
12+
fn main() {
13+
// Checking the `repr(u32)` on the enum.
14+
assert_eq!(4, std::mem::size_of::<Bar>());
15+
// Checking the `repr(u16)` on the enum.
16+
assert_eq!(2, std::mem::size_of::<FooBar>());
17+
18+
// Checking the Debug impl on the types.
19+
eprintln!("{:?} {:?} {:?}", Foo, Bar::A, FooBar::A);
20+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[macro_export]
2+
macro_rules! creator {
3+
(struct $name1:ident; enum $name2:ident; enum $name3:ident;) => {
4+
#[derive(Debug)]
5+
pub struct $name1;
6+
7+
#[derive(Debug)]
8+
#[repr(u32)]
9+
pub enum $name2 { A }
10+
11+
#[derive(Debug)]
12+
#[repr(u16)]
13+
pub enum $name3 { A }
14+
}
15+
}

src/test/ui/query-visibility.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
// Check that it doesn't panic when `Input` gets its visibility checked.
3+
4+
#![crate_type = "lib"]
5+
6+
pub trait Layer<
7+
/// Hello.
8+
Input,
9+
> {}

0 commit comments

Comments
 (0)