Skip to content

Commit 561332e

Browse files
Feed and only optionally encode item attrs if theyre present
1 parent 6cbf87c commit 561332e

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,19 +1141,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11411141
.tables
11421142
.attributes
11431143
.get(self, id)
1144-
.unwrap_or_else(|| {
1144+
.or_else(|| {
11451145
// Structure and variant constructors don't have any attributes encoded for them,
11461146
// but we assume that someone passing a constructor ID actually wants to look at
11471147
// the attributes on the corresponding struct or variant.
11481148
let def_key = self.def_key(id);
1149-
assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor);
1150-
let parent_id = def_key.parent.expect("no parent for a constructor");
1151-
self.root
1152-
.tables
1153-
.attributes
1154-
.get(self, parent_id)
1155-
.expect("no encoded attributes for a structure or variant")
1149+
if def_key.disambiguated_data.data == DefPathData::Ctor {
1150+
let parent_id = def_key.parent.expect("no parent for a constructor");
1151+
self.root.tables.attributes.get(self, parent_id)
1152+
} else {
1153+
None
1154+
}
11561155
})
1156+
.unwrap_or_else(|| LazyArray::default())
11571157
.decode((self, sess))
11581158
}
11591159

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,13 +1284,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12841284
is_exported: tcx.effective_visibilities(()).is_exported(def_id),
12851285
is_doc_hidden: false,
12861286
};
1287-
let attr_iter = tcx
1288-
.opt_local_def_id_to_hir_id(def_id)
1289-
.map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id))
1290-
.iter()
1291-
.filter(|attr| analyze_attr(attr, &mut state));
12921287

1293-
record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter);
1288+
let item_attrs = tcx.item_attrs(def_id);
1289+
if !item_attrs.is_empty() {
1290+
let attr_iter = item_attrs.iter().filter(|attr| analyze_attr(attr, &mut state));
1291+
record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter);
1292+
}
12941293

12951294
let mut attr_flags = AttrFlags::empty();
12961295
if state.is_doc_hidden {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ rustc_queries! {
12141214
query item_attrs(def_id: DefId) -> &'tcx [ast::Attribute] {
12151215
desc { |tcx| "collecting attributes of `{}`", tcx.def_path_str(def_id) }
12161216
separate_provide_extern
1217+
feedable
12171218
}
12181219

12191220
query codegen_fn_attrs(def_id: DefId) -> &'tcx CodegenFnAttrs {

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ fn associated_type_for_impl_trait_in_trait(
256256
let local_def_id = trait_assoc_ty.def_id();
257257
let def_id = local_def_id.to_def_id();
258258

259+
trait_assoc_ty.item_attrs(&[]);
260+
259261
trait_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
260262

261263
// There's no HIR associated with this new synthesized `def_id`, so feed
@@ -351,6 +353,8 @@ fn associated_type_for_impl_trait_in_impl(
351353
let local_def_id = impl_assoc_ty.def_id();
352354
let def_id = local_def_id.to_def_id();
353355

356+
impl_assoc_ty.item_attrs(&[]);
357+
354358
impl_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
355359

356360
// There's no HIR associated with this new synthesized `def_id`, so feed

0 commit comments

Comments
 (0)