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

Commit 4c6120c

Browse files
committed
rustc_metadata: Make attribute decoding slightly faster and stricter
Rename `CStore::item_attrs` -> `CStore::item_attrs_untracked` top follow conventions
1 parent 636fd49 commit 4c6120c

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,24 +1309,26 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13091309

13101310
fn get_item_attrs(
13111311
&'a self,
1312-
node_id: DefIndex,
1312+
id: DefIndex,
13131313
sess: &'a Session,
13141314
) -> impl Iterator<Item = ast::Attribute> + 'a {
1315-
// The attributes for a tuple struct/variant are attached to the definition, not the ctor;
1316-
// we assume that someone passing in a tuple struct ctor is actually wanting to
1317-
// look at the definition
1318-
let def_key = self.def_key(node_id);
1319-
let item_id = if def_key.disambiguated_data.data == DefPathData::Ctor {
1320-
def_key.parent.unwrap()
1321-
} else {
1322-
node_id
1323-
};
1324-
13251315
self.root
13261316
.tables
13271317
.attributes
1328-
.get(self, item_id)
1329-
.unwrap_or_else(Lazy::empty)
1318+
.get(self, id)
1319+
.unwrap_or_else(|| {
1320+
// Structure and variant constructors don't have any attributes encoded for them,
1321+
// but we assume that someone passing a constructor ID actually wants to look at
1322+
// the attributes on the corresponding struct or variant.
1323+
let def_key = self.def_key(id);
1324+
assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor);
1325+
let parent_id = def_key.parent.expect("no parent for a constructor");
1326+
self.root
1327+
.tables
1328+
.attributes
1329+
.get(self, parent_id)
1330+
.expect("no encoded attributes for a structure or variant")
1331+
})
13301332
.decode((self, sess))
13311333
}
13321334

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
145145
lookup_deprecation_entry => {
146146
cdata.get_deprecation(def_id.index).map(DeprecationEntry::external)
147147
}
148-
item_attrs => { tcx.arena.alloc_from_iter(
149-
cdata.get_item_attrs(def_id.index, tcx.sess)
150-
) }
148+
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
151149
fn_arg_names => { cdata.get_fn_param_names(tcx, def_id.index) }
152150
rendered_const => { cdata.get_rendered_const(def_id.index) }
153151
impl_parent => { cdata.get_parent_impl(def_id.index) }
@@ -470,7 +468,7 @@ impl CStore {
470468
self.get_crate_data(cnum).num_def_ids()
471469
}
472470

473-
pub fn item_attrs(&self, def_id: DefId, sess: &Session) -> Vec<ast::Attribute> {
471+
pub fn item_attrs_untracked(&self, def_id: DefId, sess: &Session) -> Vec<ast::Attribute> {
474472
self.get_crate_data(def_id.krate).get_item_attrs(def_id.index, sess).collect()
475473
}
476474

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,17 +895,19 @@ impl<'a> Resolver<'a> {
895895
// a note about editions
896896
let note = if let Some(did) = did {
897897
let requires_note = !did.is_local()
898-
&& this.cstore().item_attrs(did, this.session).iter().any(
899-
|attr| {
898+
&& this
899+
.cstore()
900+
.item_attrs_untracked(did, this.session)
901+
.iter()
902+
.any(|attr| {
900903
if attr.has_name(sym::rustc_diagnostic_item) {
901904
[sym::TryInto, sym::TryFrom, sym::FromIterator]
902905
.map(|x| Some(x))
903906
.contains(&attr.value_str())
904907
} else {
905908
false
906909
}
907-
},
908-
);
910+
});
909911

910912
requires_note.then(|| {
911913
format!(

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3420,7 +3420,7 @@ impl<'a> Resolver<'a> {
34203420

34213421
let attr = self
34223422
.cstore()
3423-
.item_attrs(def_id, self.session)
3423+
.item_attrs_untracked(def_id, self.session)
34243424
.into_iter()
34253425
.find(|a| a.has_name(sym::rustc_legacy_const_generics))?;
34263426
let mut ret = Vec::new();

0 commit comments

Comments
 (0)