Skip to content

Commit 53af5a5

Browse files
committed
Auto merge of rust-lang#127185 - camelid:query-has_attr, r=<try>
Queryify `has_attr` to improve performance Inspired by rust-lang#127144 (review) and previous success in rust-lang#94897. r? `@compiler-errors`
2 parents ef3d6fd + a75363e commit 53af5a5

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

compiler/rustc_middle/src/query/keys.rs

+8
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ impl Key for Option<Symbol> {
484484
}
485485
}
486486

487+
impl Key for (DefId, Symbol) {
488+
type Cache<V> = DefaultCache<Self, V>;
489+
490+
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
491+
DUMMY_SP
492+
}
493+
}
494+
487495
/// Canonical query goals correspond to abstract trait operations that
488496
/// are not tied to any crate in particular.
489497
impl<'tcx, T: Clone> Key for Canonical<'tcx, T> {

compiler/rustc_middle/src/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,12 @@ rustc_queries! {
12261226
separate_provide_extern
12271227
}
12281228

1229+
/// This query backs the [`TyCtxt::has_attr`] function to make it faster.
1230+
/// You should use that function instead.
1231+
query has_attr_query(key: (DefId, Symbol)) -> bool {
1232+
desc { |tcx| "checking whether `{}` has attr {}", tcx.def_path_str(key.0), key.1 }
1233+
}
1234+
12291235
/// Determines whether an item is annotated with `doc(hidden)`.
12301236
query is_doc_hidden(def_id: DefId) -> bool {
12311237
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ impl<'tcx> TyCtxt<'tcx> {
18011801

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

18071807
/// Determines whether an item is annotated with a multi-segement attribute

compiler/rustc_middle/src/ty/util.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
1919
use rustc_index::bit_set::GrowableBitSet;
2020
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable};
2121
use rustc_session::Limit;
22-
use rustc_span::sym;
22+
use rustc_span::{sym, Symbol};
2323
use rustc_target::abi::{Float, Integer, IntegerType, Size};
2424
use rustc_target::spec::abi::Abi;
2525
use smallvec::{smallvec, SmallVec};
@@ -1829,6 +1829,12 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
18291829
val.fold_with(&mut visitor)
18301830
}
18311831

1832+
/// Determines whether an item is annotated with an attribute.
1833+
pub fn has_attr_query(tcx: TyCtxt<'_>, key: (DefId, Symbol)) -> bool {
1834+
let (did, attr) = key;
1835+
tcx.get_attrs(did, attr).next().is_some()
1836+
}
1837+
18321838
/// Determines whether an item is directly annotated with `doc(hidden)`.
18331839
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
18341840
tcx.get_attrs(def_id, sym::doc)
@@ -1865,6 +1871,7 @@ pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Intrinsi
18651871
pub fn provide(providers: &mut Providers) {
18661872
*providers = Providers {
18671873
reveal_opaque_types_in_bounds,
1874+
has_attr_query,
18681875
is_doc_hidden,
18691876
is_doc_notable_trait,
18701877
intrinsic_raw,

0 commit comments

Comments
 (0)