Skip to content

Add a per-item query just for define_opaque attributes #138500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let num_nodes = self.item_local_id_counter.as_usize();
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash, define_opaque };
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };

self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map })
self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map, define_opaque })
}

/// This method allocates a new `HirId` for the given `NodeId`.
Expand Down
16 changes: 9 additions & 7 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,18 +1307,13 @@ impl Attribute {
#[derive(Debug)]
pub struct AttributeMap<'tcx> {
pub map: SortedMap<ItemLocalId, &'tcx [Attribute]>,
/// Preprocessed `#[define_opaque]` attribute.
pub define_opaque: Option<&'tcx [(Span, LocalDefId)]>,
// Only present when the crate hash is needed.
pub opt_hash: Option<Fingerprint>,
}

impl<'tcx> AttributeMap<'tcx> {
pub const EMPTY: &'static AttributeMap<'static> = &AttributeMap {
map: SortedMap::new(),
opt_hash: Some(Fingerprint::ZERO),
define_opaque: None,
};
pub const EMPTY: &'static AttributeMap<'static> =
&AttributeMap { map: SortedMap::new(), opt_hash: Some(Fingerprint::ZERO) };

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

impl<'tcx> OwnerInfo<'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/stable_hash_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
// We ignore the `map` since it refers to information included in `opt_hash` which is
// hashed in the collector and used for the crate hash.
let AttributeMap { opt_hash, define_opaque: _, map: _ } = *self;
let AttributeMap { opt_hash, map: _ } = *self;
opt_hash.unwrap().hash_stable(hcx, hasher);
}
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,9 @@ pub fn provide(providers: &mut Providers) {
providers.in_scope_traits_map = |tcx, id| {
tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
};
providers.explicitly_defined_opaques = |tcx, id| {
tcx.hir_crate(()).owners[id.def_id]
.as_owner()
.and_then(|owner_info| Some(*owner_info.define_opaque.as_ref()?))
};
}
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,9 @@ rustc_queries! {
-> Option<&'tcx ItemLocalMap<Box<[TraitCandidate]>>> {
desc { "getting traits in scope at a block" }
}
query explicitly_defined_opaques(_: hir::OwnerId) -> Option<&'tcx [(Span, LocalDefId)]> {
desc { "getting the explicitly defined opaque types"}
}

/// Returns whether the impl or associated function has the `default` keyword.
query defaultness(def_id: DefId) -> hir::Defaultness {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ty_utils/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
#[instrument(level = "trace", skip(self))]
fn collect_taits_from_defines_attr(&mut self) {
let hir_id = self.tcx.local_def_id_to_hir_id(self.item);
if !hir_id.is_owner() {
let Some(owner_id) = hir_id.as_owner() else {
return;
}
let Some(defines) = self.tcx.hir_attr_map(hir_id.owner).define_opaque else {
};
let Some(defines) = self.tcx.explicitly_defined_opaques(owner_id) else {
return;
};
for &(span, define) in defines {
Expand Down
Loading