Skip to content

Commit

Permalink
Auto merge of rust-lang#127185 - camelid:query-has_attr, r=<try>
Browse files Browse the repository at this point in the history
Queryify `has_attr` to improve performance

Inspired by rust-lang#127144 (review)
and previous success in rust-lang#94897.

r? `@compiler-errors`
  • Loading branch information
bors committed Jun 30, 2024
2 parents ef3d6fd + a75363e commit 53af5a5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/query/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ impl Key for Option<Symbol> {
}
}

impl Key for (DefId, Symbol) {
type Cache<V> = DefaultCache<Self, V>;

fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}

/// Canonical query goals correspond to abstract trait operations that
/// are not tied to any crate in particular.
impl<'tcx, T: Clone> Key for Canonical<'tcx, T> {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,12 @@ rustc_queries! {
separate_provide_extern
}

/// This query backs the [`TyCtxt::has_attr`] function to make it faster.
/// You should use that function instead.
query has_attr_query(key: (DefId, Symbol)) -> bool {
desc { |tcx| "checking whether `{}` has attr {}", tcx.def_path_str(key.0), key.1 }
}

/// Determines whether an item is annotated with `doc(hidden)`.
query is_doc_hidden(def_id: DefId) -> bool {
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ impl<'tcx> TyCtxt<'tcx> {

/// Determines whether an item is annotated with an attribute.
pub fn has_attr(self, did: impl Into<DefId>, attr: Symbol) -> bool {
self.get_attrs(did, attr).next().is_some()
self.has_attr_query((did.into(), attr))
}

/// Determines whether an item is annotated with a multi-segement attribute
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
use rustc_index::bit_set::GrowableBitSet;
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable};
use rustc_session::Limit;
use rustc_span::sym;
use rustc_span::{sym, Symbol};
use rustc_target::abi::{Float, Integer, IntegerType, Size};
use rustc_target::spec::abi::Abi;
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -1829,6 +1829,12 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
val.fold_with(&mut visitor)
}

/// Determines whether an item is annotated with an attribute.
pub fn has_attr_query(tcx: TyCtxt<'_>, key: (DefId, Symbol)) -> bool {
let (did, attr) = key;
tcx.get_attrs(did, attr).next().is_some()
}

/// Determines whether an item is directly annotated with `doc(hidden)`.
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
tcx.get_attrs(def_id, sym::doc)
Expand Down Expand Up @@ -1865,6 +1871,7 @@ pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Intrinsi
pub fn provide(providers: &mut Providers) {
*providers = Providers {
reveal_opaque_types_in_bounds,
has_attr_query,
is_doc_hidden,
is_doc_notable_trait,
intrinsic_raw,
Expand Down

0 comments on commit 53af5a5

Please sign in to comment.