Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 74b6d29

Browse files
committed
don't encode only locally used attrs
1 parent 87fd70c commit 74b6d29

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ pub enum AttributeDuplicates {
147147
FutureWarnPreceding,
148148
}
149149

150+
/// A conveniece macro to deal with `$($expr)?`.
151+
macro_rules! or_default {
152+
($default:expr,) => {
153+
$default
154+
};
155+
($default:expr, $next:expr) => {
156+
$next
157+
};
158+
}
159+
150160
/// A convenience macro for constructing attribute templates.
151161
/// E.g., `template!(Word, List: "description")` means that the attribute
152162
/// supports forms `#[attr]` and `#[attr(description)]`.
@@ -168,9 +178,10 @@ macro_rules! template {
168178
}
169179

170180
macro_rules! ungated {
171-
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(,)?) => {
181+
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)? $(,)?) => {
172182
BuiltinAttribute {
173183
name: sym::$attr,
184+
only_local: or_default!(false, $($only_local)?),
174185
type_: $typ,
175186
template: $tpl,
176187
gate: Ungated,
@@ -180,18 +191,20 @@ macro_rules! ungated {
180191
}
181192

182193
macro_rules! gated {
183-
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $gate:ident, $msg:expr $(,)?) => {
194+
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)?, $gate:ident, $msg:expr $(,)?) => {
184195
BuiltinAttribute {
185196
name: sym::$attr,
197+
only_local: or_default!(false, $($only_local)?),
186198
type_: $typ,
187199
template: $tpl,
188200
duplicates: $duplicates,
189201
gate: Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)),
190202
}
191203
};
192-
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $msg:expr $(,)?) => {
204+
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)?, $msg:expr $(,)?) => {
193205
BuiltinAttribute {
194206
name: sym::$attr,
207+
only_local: or_default!(false, $($only_local)?),
195208
type_: $typ,
196209
template: $tpl,
197210
duplicates: $duplicates,
@@ -201,12 +214,13 @@ macro_rules! gated {
201214
}
202215

203216
macro_rules! rustc_attr {
204-
(TEST, $attr:ident, $typ:expr, $tpl:expr, $duplicate:expr $(,)?) => {
217+
(TEST, $attr:ident, $typ:expr, $tpl:expr, $duplicate:expr $(, @only_local: $only_local:expr)? $(,)?) => {
205218
rustc_attr!(
206219
$attr,
207220
$typ,
208221
$tpl,
209222
$duplicate,
223+
$(@only_local: $only_local,)?
210224
concat!(
211225
"the `#[",
212226
stringify!($attr),
@@ -215,9 +229,10 @@ macro_rules! rustc_attr {
215229
),
216230
)
217231
};
218-
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $msg:expr $(,)?) => {
232+
($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)?, $msg:expr $(,)?) => {
219233
BuiltinAttribute {
220234
name: sym::$attr,
235+
only_local: or_default!(false, $($only_local)?),
221236
type_: $typ,
222237
template: $tpl,
223238
duplicates: $duplicates,
@@ -237,6 +252,10 @@ const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never b
237252

238253
pub struct BuiltinAttribute {
239254
pub name: Symbol,
255+
/// Whether this attribute is only used in the local crate.
256+
///
257+
/// If so, it is not encoded in the crate metadata.
258+
pub only_local: bool,
240259
pub type_: AttributeType,
241260
pub template: AttributeTemplate,
242261
pub duplicates: AttributeDuplicates,
@@ -295,7 +314,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
295314
ungated!(must_use, Normal, template!(Word, NameValueStr: "reason"), FutureWarnFollowing),
296315
gated!(
297316
must_not_suspend, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing,
298-
must_not_suspend, experimental!(must_not_suspend)
317+
experimental!(must_not_suspend)
299318
),
300319
ungated!(
301320
deprecated, Normal,
@@ -394,6 +413,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
394413
// Plugins:
395414
BuiltinAttribute {
396415
name: sym::plugin,
416+
only_local: false,
397417
type_: CrateLevel,
398418
template: template!(List: "name"),
399419
duplicates: DuplicatesOk,
@@ -475,7 +495,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
475495
),
476496
// DuplicatesOk since it has its own validation
477497
ungated!(
478-
stable, Normal, template!(List: r#"feature = "name", since = "version""#), DuplicatesOk
498+
stable, Normal, template!(List: r#"feature = "name", since = "version""#), DuplicatesOk,
479499
),
480500
ungated!(
481501
unstable, Normal,
@@ -633,7 +653,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
633653
// Internal attributes, Misc:
634654
// ==========================================================================
635655
gated!(
636-
lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, lang_items,
656+
lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, @only_local: true, lang_items,
637657
"language items are subject to change",
638658
),
639659
rustc_attr!(
@@ -642,11 +662,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
642662
"#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
643663
),
644664
rustc_attr!(
645-
rustc_coherence_is_core, AttributeType::CrateLevel, template!(Word), ErrorFollowing,
665+
rustc_coherence_is_core, AttributeType::CrateLevel, template!(Word), ErrorFollowing, @only_local: true,
646666
"#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."
647667
),
648668
rustc_attr!(
649-
rustc_allow_incoherent_impl, AttributeType::Normal, template!(Word), ErrorFollowing,
669+
rustc_allow_incoherent_impl, AttributeType::Normal, template!(Word), ErrorFollowing, @only_local: true,
650670
"#[rustc_allow_incoherent_impl] has to be added to all impl items of an incoherent inherent impl."
651671
),
652672
rustc_attr!(
@@ -656,6 +676,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
656676
),
657677
BuiltinAttribute {
658678
name: sym::rustc_diagnostic_item,
679+
// FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.
680+
only_local: false,
659681
type_: Normal,
660682
template: template!(NameValueStr: "name"),
661683
duplicates: ErrorFollowing,
@@ -676,7 +698,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
676698
"unboxed_closures are still evolving",
677699
),
678700
rustc_attr!(
679-
rustc_inherit_overflow_checks, Normal, template!(Word), WarnFollowing,
701+
rustc_inherit_overflow_checks, Normal, template!(Word), WarnFollowing, @only_local: true,
680702
"the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
681703
overflow checking behavior of several libcore functions that are inlined \
682704
across crates and will never be stable",
@@ -778,6 +800,10 @@ pub fn is_builtin_attr_name(name: Symbol) -> bool {
778800
BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
779801
}
780802

803+
pub fn is_builtin_only_local(name: Symbol) -> bool {
804+
BUILTIN_ATTRIBUTE_MAP.get(&name).map_or(false, |attr| attr.only_local)
805+
}
806+
781807
pub static BUILTIN_ATTRIBUTE_MAP: SyncLazy<FxHashMap<Symbol, &BuiltinAttribute>> =
782808
SyncLazy::new(|| {
783809
let mut map = FxHashMap::default();

compiler/rustc_feature/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ pub use accepted::ACCEPTED_FEATURES;
149149
pub use active::{Features, ACTIVE_FEATURES, INCOMPATIBLE_FEATURES};
150150
pub use builtin_attrs::AttributeDuplicates;
151151
pub use builtin_attrs::{
152-
deprecated_attributes, find_gated_cfg, is_builtin_attr_name, AttributeGate, AttributeTemplate,
153-
AttributeType, BuiltinAttribute, GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
152+
deprecated_attributes, find_gated_cfg, is_builtin_attr_name, is_builtin_only_local,
153+
AttributeGate, AttributeTemplate, AttributeType, BuiltinAttribute, GatedCfg,
154+
BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
154155
};
155156
pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES};

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -986,9 +986,13 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
986986

987987
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
988988
fn encode_attrs(&mut self, def_id: DefId) {
989-
let attrs = self.tcx.get_attrs(def_id);
990-
record!(self.tables.attributes[def_id] <- attrs);
991-
if attrs.iter().any(|attr| attr.may_have_doc_links()) {
989+
let mut attrs = self
990+
.tcx
991+
.get_attrs(def_id)
992+
.iter()
993+
.filter(|attr| !rustc_feature::is_builtin_only_local(attr.name_or_empty()));
994+
record!(self.tables.attributes[def_id] <- attrs.clone());
995+
if attrs.any(|attr| attr.may_have_doc_links()) {
992996
self.tables.may_have_doc_links.set(def_id.index, ());
993997
}
994998
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,9 @@ rustc_queries! {
10721072
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }
10731073
}
10741074

1075+
/// Returns the attributes on the item at `def_id`.
1076+
///
1077+
/// Do not use this directly, use `tcx.get_attrs` instead.
10751078
query item_attrs(def_id: DefId) -> &'tcx [ast::Attribute] {
10761079
desc { |tcx| "collecting attributes of `{}`", tcx.def_path_str(def_id) }
10771080
separate_provide_extern

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,10 @@ impl<'tcx> TyCtxt<'tcx> {
21872187
}
21882188

21892189
/// Gets the attributes of a definition.
2190+
///
2191+
/// Note that attributes which are be relevant while
2192+
/// the current local crate are not stored in the crate metadata
2193+
/// and therefore cannot be accessed outside of that crate.
21902194
pub fn get_attrs(self, did: DefId) -> Attributes<'tcx> {
21912195
if let Some(did) = did.as_local() {
21922196
self.hir().attrs(self.hir().local_def_id_to_hir_id(did))

0 commit comments

Comments
 (0)