Skip to content

Commit 6bf40d9

Browse files
committed
Auto merge of rust-lang#138500 - oli-obk:per-item-query-define-opaque, r=<try>
Add a per-item query just for `define_opaque` attributes follow up to rust-lang#128440 (comment) This wasn't done in the previous PR to make sure we can gauge the perf effect independently of the other changes
2 parents f7b4354 + fe58a05 commit 6bf40d9

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -625,9 +625,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
625625
let num_nodes = self.item_local_id_counter.as_usize();
626626
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
627627
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
628-
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash, define_opaque };
628+
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };
629629

630-
self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map })
630+
self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map, define_opaque })
631631
}
632632

633633
/// This method allocates a new `HirId` for the given `NodeId`.

compiler/rustc_hir/src/hir.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1307,18 +1307,13 @@ impl Attribute {
13071307
#[derive(Debug)]
13081308
pub struct AttributeMap<'tcx> {
13091309
pub map: SortedMap<ItemLocalId, &'tcx [Attribute]>,
1310-
/// Preprocessed `#[define_opaque]` attribute.
1311-
pub define_opaque: Option<&'tcx [(Span, LocalDefId)]>,
13121310
// Only present when the crate hash is needed.
13131311
pub opt_hash: Option<Fingerprint>,
13141312
}
13151313

13161314
impl<'tcx> AttributeMap<'tcx> {
1317-
pub const EMPTY: &'static AttributeMap<'static> = &AttributeMap {
1318-
map: SortedMap::new(),
1319-
opt_hash: Some(Fingerprint::ZERO),
1320-
define_opaque: None,
1321-
};
1315+
pub const EMPTY: &'static AttributeMap<'static> =
1316+
&AttributeMap { map: SortedMap::new(), opt_hash: Some(Fingerprint::ZERO) };
13221317

13231318
#[inline]
13241319
pub fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
@@ -1382,6 +1377,13 @@ pub struct OwnerInfo<'hir> {
13821377
/// Map indicating what traits are in scope for places where this
13831378
/// is relevant; generated by resolve.
13841379
pub trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
1380+
/// Preprocessed `#[define_opaque]` attribute.
1381+
/// `None` means there was no attribute.
1382+
/// `Some(&[])` means there was an attribute, but it had no entries.
1383+
/// This can be used to remove the opaques defined by default, e.g. on
1384+
/// associated methods which automatically pick up opaques in associated types
1385+
/// if they mention that associated type in their signature.
1386+
pub define_opaque: Option<&'hir [(Span, LocalDefId)]>,
13851387
}
13861388

13871389
impl<'tcx> OwnerInfo<'tcx> {

compiler/rustc_hir/src/stable_hash_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
106106
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
107107
// We ignore the `map` since it refers to information included in `opt_hash` which is
108108
// hashed in the collector and used for the crate hash.
109-
let AttributeMap { opt_hash, define_opaque: _, map: _ } = *self;
109+
let AttributeMap { opt_hash, map: _ } = *self;
110110
opt_hash.unwrap().hash_stable(hcx, hasher);
111111
}
112112
}

compiler/rustc_middle/src/hir/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,9 @@ pub fn provide(providers: &mut Providers) {
238238
providers.in_scope_traits_map = |tcx, id| {
239239
tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
240240
};
241+
providers.explicitly_defined_opaques = |tcx, id| {
242+
tcx.hir_crate(()).owners[id.def_id]
243+
.as_owner()
244+
.and_then(|owner_info| Some(*owner_info.define_opaque.as_ref()?))
245+
};
241246
}

compiler/rustc_middle/src/query/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,9 @@ rustc_queries! {
17241724
-> Option<&'tcx ItemLocalMap<Box<[TraitCandidate]>>> {
17251725
desc { "getting traits in scope at a block" }
17261726
}
1727+
query explicitly_defined_opaques(_: hir::OwnerId) -> Option<&'tcx [(Span, LocalDefId)]> {
1728+
desc { "getting the explicitly defined opaque types"}
1729+
}
17271730

17281731
/// Returns whether the impl or associated function has the `default` keyword.
17291732
query defaultness(def_id: DefId) -> hir::Defaultness {

compiler/rustc_ty_utils/src/opaque_types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
184184
#[instrument(level = "trace", skip(self))]
185185
fn collect_taits_from_defines_attr(&mut self) {
186186
let hir_id = self.tcx.local_def_id_to_hir_id(self.item);
187-
if !hir_id.is_owner() {
187+
let Some(owner_id) = hir_id.as_owner() else {
188188
return;
189-
}
190-
let Some(defines) = self.tcx.hir_attr_map(hir_id.owner).define_opaque else {
189+
};
190+
let Some(defines) = self.tcx.explicitly_defined_opaques(owner_id) else {
191191
return;
192192
};
193193
for &(span, define) in defines {

0 commit comments

Comments
 (0)