Skip to content

Commit 563ec85

Browse files
committed
Rename IntoQueryParam to IntoQueryKey and tweak some occurrences
All methods that accept `impl IntoQueryKey<_>` have been adjusted to consistently call `into_query_key` before doing anything else. When a function with a conversion trait calls another function with the same conversion trait, doing the conversion eagerly can reduce the overall number of instantiations.
1 parent b8b0dbd commit 563ec85

7 files changed

Lines changed: 84 additions & 70 deletions

File tree

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
11
use rustc_hir::OwnerId;
22
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId};
33

4-
/// An analogue of the `Into` trait that's intended only for query parameters.
4+
/// Argument-conversion trait used by some queries and other `TyCtxt` methods.
55
///
6-
/// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
7-
/// user call `to_def_id` to convert between them everywhere else.
8-
pub trait IntoQueryParam<P> {
9-
fn into_query_param(self) -> P;
6+
/// A function that accepts an `impl IntoQueryKey<DefId>` argument can be thought
7+
/// of as taking a [`DefId`], except that callers can also pass a [`LocalDefId`]
8+
/// or values of other narrower ID types, as long as they have a trivial conversion
9+
/// to `DefId`.
10+
///
11+
/// Using a dedicated trait instead of [`Into`] makes the purpose of the conversion
12+
/// more explicit, and makes occurrences easier to search for.
13+
pub trait IntoQueryKey<K> {
14+
/// Argument conversion from `Self` to `K`.
15+
/// This should always be a very cheap conversion, e.g. [`LocalDefId::to_def_id`].
16+
fn into_query_key(self) -> K;
1017
}
1118

12-
impl<P> IntoQueryParam<P> for P {
19+
/// Any type can be converted to itself.
20+
///
21+
/// This is useful in generic or macro-generated code where we don't know whether
22+
/// conversion is actually needed, so that we can do a conversion unconditionally.
23+
impl<K> IntoQueryKey<K> for K {
1324
#[inline(always)]
14-
fn into_query_param(self) -> P {
25+
fn into_query_key(self) -> K {
1526
self
1627
}
1728
}
1829

19-
impl IntoQueryParam<LocalDefId> for OwnerId {
30+
impl IntoQueryKey<LocalDefId> for OwnerId {
2031
#[inline(always)]
21-
fn into_query_param(self) -> LocalDefId {
32+
fn into_query_key(self) -> LocalDefId {
2233
self.def_id
2334
}
2435
}
2536

26-
impl IntoQueryParam<DefId> for LocalDefId {
37+
impl IntoQueryKey<DefId> for LocalDefId {
2738
#[inline(always)]
28-
fn into_query_param(self) -> DefId {
39+
fn into_query_key(self) -> DefId {
2940
self.to_def_id()
3041
}
3142
}
3243

33-
impl IntoQueryParam<DefId> for OwnerId {
44+
impl IntoQueryKey<DefId> for OwnerId {
3445
#[inline(always)]
35-
fn into_query_param(self) -> DefId {
46+
fn into_query_key(self) -> DefId {
3647
self.to_def_id()
3748
}
3849
}
3950

40-
impl IntoQueryParam<DefId> for ModDefId {
51+
impl IntoQueryKey<DefId> for ModDefId {
4152
#[inline(always)]
42-
fn into_query_param(self) -> DefId {
53+
fn into_query_key(self) -> DefId {
4354
self.to_def_id()
4455
}
4556
}
4657

47-
impl IntoQueryParam<DefId> for LocalModDefId {
58+
impl IntoQueryKey<DefId> for LocalModDefId {
4859
#[inline(always)]
49-
fn into_query_param(self) -> DefId {
60+
fn into_query_key(self) -> DefId {
5061
self.to_def_id()
5162
}
5263
}
5364

54-
impl IntoQueryParam<LocalDefId> for LocalModDefId {
65+
impl IntoQueryKey<LocalDefId> for LocalModDefId {
5566
#[inline(always)]
56-
fn into_query_param(self) -> LocalDefId {
67+
fn into_query_key(self) -> LocalDefId {
5768
self.into()
5869
}
5970
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::def_id::LocalDefId;
22

33
pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
4-
pub use self::into_query_key::IntoQueryParam;
4+
pub use self::into_query_key::IntoQueryKey;
55
pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter};
66
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
77
pub use self::plumbing::{

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ impl<'tcx> TyCtxt<'tcx> {
269269
}
270270

271271
macro_rules! query_helper_param_ty {
272-
(DefId) => { impl $crate::query::IntoQueryParam<DefId> };
273-
(LocalDefId) => { impl $crate::query::IntoQueryParam<LocalDefId> };
272+
(DefId) => { impl $crate::query::IntoQueryKey<DefId> };
273+
(LocalDefId) => { impl $crate::query::IntoQueryKey<LocalDefId> };
274274
($K:ty) => { $K };
275275
}
276276

@@ -411,7 +411,7 @@ macro_rules! define_callbacks {
411411
crate::query::inner::query_ensure_ok_or_done(
412412
self.tcx,
413413
&self.tcx.query_system.query_vtables.$name,
414-
$crate::query::IntoQueryParam::into_query_param(key),
414+
$crate::query::IntoQueryKey::into_query_key(key),
415415
$crate::query::EnsureMode::Ok,
416416
)
417417
}
@@ -431,7 +431,7 @@ macro_rules! define_callbacks {
431431
crate::query::inner::query_ensure_result(
432432
self.tcx,
433433
&self.tcx.query_system.query_vtables.$name,
434-
$crate::query::IntoQueryParam::into_query_param(key),
434+
$crate::query::IntoQueryKey::into_query_key(key),
435435
)
436436
}
437437
)*
@@ -445,7 +445,7 @@ macro_rules! define_callbacks {
445445
crate::query::inner::query_ensure_ok_or_done(
446446
self.tcx,
447447
&self.tcx.query_system.query_vtables.$name,
448-
$crate::query::IntoQueryParam::into_query_param(key),
448+
$crate::query::IntoQueryKey::into_query_key(key),
449449
$crate::query::EnsureMode::Done,
450450
);
451451
}
@@ -474,21 +474,21 @@ macro_rules! define_callbacks {
474474
self.tcx,
475475
self.span,
476476
&self.tcx.query_system.query_vtables.$name,
477-
$crate::query::IntoQueryParam::into_query_param(key),
477+
$crate::query::IntoQueryKey::into_query_key(key),
478478
))
479479
}
480480
)*
481481
}
482482

483483
$(
484484
#[cfg($feedable)]
485-
impl<'tcx, K: $crate::query::IntoQueryParam<$name::Key<'tcx>> + Copy>
485+
impl<'tcx, K: $crate::query::IntoQueryKey<$name::Key<'tcx>> + Copy>
486486
TyCtxtFeed<'tcx, K>
487487
{
488488
$(#[$attr])*
489489
#[inline(always)]
490490
pub fn $name(self, value: $name::ProvidedValue<'tcx>) {
491-
let key = self.key().into_query_param();
491+
let key = self.key().into_query_key();
492492
let erased_value = $name::provided_to_erased(self.tcx, value);
493493
$crate::query::inner::query_feed(
494494
self.tcx,

compiler/rustc_middle/src/ty/context.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature};
6161
use crate::middle::resolve_bound_vars;
6262
use crate::mir::interpret::{self, Allocation, ConstAllocation};
6363
use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
64-
use crate::query::{IntoQueryParam, LocalCrate, Providers, QuerySystem, TyCtxtAt};
64+
use crate::query::{IntoQueryKey, LocalCrate, Providers, QuerySystem, TyCtxtAt};
6565
use crate::thir::Thir;
6666
use crate::traits;
6767
use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques};
@@ -1111,10 +1111,9 @@ impl<'tcx> TyCtxt<'tcx> {
11111111
}
11121112

11131113
/// Check if the given `def_id` is a `type const` (mgca)
1114-
pub fn is_type_const<I: Copy + IntoQueryParam<DefId>>(self, def_id: I) -> bool {
1115-
// No need to call the query directly in this case always false.
1116-
let def_kind = self.def_kind(def_id.into_query_param());
1117-
match def_kind {
1114+
pub fn is_type_const(self, def_id: impl IntoQueryKey<DefId>) -> bool {
1115+
let def_id = def_id.into_query_key();
1116+
match self.def_kind(def_id) {
11181117
DefKind::Const { is_type_const } | DefKind::AssocConst { is_type_const } => {
11191118
is_type_const
11201119
}
@@ -1168,8 +1167,8 @@ impl<'tcx> TyCtxt<'tcx> {
11681167
self.features_query(())
11691168
}
11701169

1171-
pub fn def_key(self, id: impl IntoQueryParam<DefId>) -> rustc_hir::definitions::DefKey {
1172-
let id = id.into_query_param();
1170+
pub fn def_key(self, id: impl IntoQueryKey<DefId>) -> rustc_hir::definitions::DefKey {
1171+
let id = id.into_query_key();
11731172
// Accessing the DefKey is ok, since it is part of DefPathHash.
11741173
if let Some(id) = id.as_local() {
11751174
self.definitions_untracked().def_key(id)
@@ -2705,7 +2704,8 @@ impl<'tcx> TyCtxt<'tcx> {
27052704
self.sess.opts.unstable_opts.build_sdylib_interface
27062705
}
27072706

2708-
pub fn intrinsic(self, def_id: impl IntoQueryParam<DefId> + Copy) -> Option<ty::IntrinsicDef> {
2707+
pub fn intrinsic(self, def_id: impl IntoQueryKey<DefId>) -> Option<ty::IntrinsicDef> {
2708+
let def_id = def_id.into_query_key();
27092709
match self.def_kind(def_id) {
27102710
DefKind::Fn | DefKind::AssocFn => self.intrinsic_raw(def_id),
27112711
_ => None,
@@ -2774,10 +2774,8 @@ impl<'tcx> TyCtxt<'tcx> {
27742774
find_attr!(self, def_id, DoNotRecommend { .. })
27752775
}
27762776

2777-
pub fn is_trivial_const<P>(self, def_id: P) -> bool
2778-
where
2779-
P: IntoQueryParam<DefId>,
2780-
{
2777+
pub fn is_trivial_const(self, def_id: impl IntoQueryKey<DefId>) -> bool {
2778+
let def_id = def_id.into_query_key();
27812779
self.trivial_const(def_id).is_some()
27822780
}
27832781

compiler/rustc_middle/src/ty/context/impl_interner.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, search_graph};
1414

1515
use crate::dep_graph::{DepKind, DepNodeIndex};
1616
use crate::infer::canonical::CanonicalVarKinds;
17-
use crate::query::IntoQueryParam;
1817
use crate::traits::cache::WithDepNode;
1918
use crate::traits::solve::{
2019
self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect,
@@ -720,7 +719,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
720719
}
721720

722721
fn item_name(self, id: DefId) -> Symbol {
723-
let id = id.into_query_param();
724722
self.opt_item_name(id).unwrap_or_else(|| {
725723
bug!("item_name: no name for {:?}", self.def_path(id));
726724
})

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ use crate::ich::StableHashingContext;
117117
use crate::metadata::{AmbigModChild, ModChild};
118118
use crate::middle::privacy::EffectiveVisibilities;
119119
use crate::mir::{Body, CoroutineLayout, CoroutineSavedLocal, SourceInfo};
120-
use crate::query::{IntoQueryParam, Providers};
120+
use crate::query::{IntoQueryKey, Providers};
121121
use crate::ty;
122122
use crate::ty::codec::{TyDecoder, TyEncoder};
123123
pub use crate::ty::diagnostics::*;
@@ -1031,12 +1031,14 @@ impl<'tcx> TypingEnv<'tcx> {
10311031
/// converted to use proper canonical inputs instead.
10321032
pub fn non_body_analysis(
10331033
tcx: TyCtxt<'tcx>,
1034-
def_id: impl IntoQueryParam<DefId>,
1034+
def_id: impl IntoQueryKey<DefId>,
10351035
) -> TypingEnv<'tcx> {
1036+
let def_id = def_id.into_query_key();
10361037
TypingEnv { typing_mode: TypingMode::non_body_analysis(), param_env: tcx.param_env(def_id) }
10371038
}
10381039

1039-
pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryParam<DefId>) -> TypingEnv<'tcx> {
1040+
pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey<DefId>) -> TypingEnv<'tcx> {
1041+
let def_id = def_id.into_query_key();
10401042
tcx.typing_env_normalized_for_post_analysis(def_id)
10411043
}
10421044

@@ -1528,8 +1530,8 @@ impl<'tcx> TyCtxt<'tcx> {
15281530
}
15291531

15301532
/// Look up the name of a definition across crates. This does not look at HIR.
1531-
pub fn opt_item_name(self, def_id: impl IntoQueryParam<DefId>) -> Option<Symbol> {
1532-
let def_id = def_id.into_query_param();
1533+
pub fn opt_item_name(self, def_id: impl IntoQueryKey<DefId>) -> Option<Symbol> {
1534+
let def_id = def_id.into_query_key();
15331535
if let Some(cnum) = def_id.as_crate_root() {
15341536
Some(self.crate_name(cnum))
15351537
} else {
@@ -1549,8 +1551,8 @@ impl<'tcx> TyCtxt<'tcx> {
15491551
/// [`opt_item_name`] instead.
15501552
///
15511553
/// [`opt_item_name`]: Self::opt_item_name
1552-
pub fn item_name(self, id: impl IntoQueryParam<DefId>) -> Symbol {
1553-
let id = id.into_query_param();
1554+
pub fn item_name(self, id: impl IntoQueryKey<DefId>) -> Symbol {
1555+
let id = id.into_query_key();
15541556
self.opt_item_name(id).unwrap_or_else(|| {
15551557
bug!("item_name: no name for {:?}", self.def_path(id));
15561558
})
@@ -1559,8 +1561,8 @@ impl<'tcx> TyCtxt<'tcx> {
15591561
/// Look up the name and span of a definition.
15601562
///
15611563
/// See [`item_name`][Self::item_name] for more information.
1562-
pub fn opt_item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Option<Ident> {
1563-
let def_id = def_id.into_query_param();
1564+
pub fn opt_item_ident(self, def_id: impl IntoQueryKey<DefId>) -> Option<Ident> {
1565+
let def_id = def_id.into_query_key();
15641566
let def = self.opt_item_name(def_id)?;
15651567
let span = self
15661568
.def_ident_span(def_id)
@@ -1571,8 +1573,8 @@ impl<'tcx> TyCtxt<'tcx> {
15711573
/// Look up the name and span of a definition.
15721574
///
15731575
/// See [`item_name`][Self::item_name] for more information.
1574-
pub fn item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Ident {
1575-
let def_id = def_id.into_query_param();
1576+
pub fn item_ident(self, def_id: impl IntoQueryKey<DefId>) -> Ident {
1577+
let def_id = def_id.into_query_key();
15761578
self.opt_item_ident(def_id).unwrap_or_else(|| {
15771579
bug!("item_ident: no name for {:?}", self.def_path(def_id));
15781580
})
@@ -1902,8 +1904,9 @@ impl<'tcx> TyCtxt<'tcx> {
19021904
}
19031905

19041906
/// Returns the trait item that is implemented by the given item `DefId`.
1905-
pub fn trait_item_of(self, def_id: impl IntoQueryParam<DefId>) -> Option<DefId> {
1906-
self.opt_associated_item(def_id.into_query_param())?.trait_item_def_id()
1907+
pub fn trait_item_of(self, def_id: impl IntoQueryKey<DefId>) -> Option<DefId> {
1908+
let def_id = def_id.into_query_key();
1909+
self.opt_associated_item(def_id)?.trait_item_def_id()
19071910
}
19081911

19091912
/// If the given `DefId` is an associated item of a trait,
@@ -1915,8 +1918,8 @@ impl<'tcx> TyCtxt<'tcx> {
19151918
}
19161919
}
19171920

1918-
pub fn impl_is_of_trait(self, def_id: impl IntoQueryParam<DefId>) -> bool {
1919-
let def_id = def_id.into_query_param();
1921+
pub fn impl_is_of_trait(self, def_id: impl IntoQueryKey<DefId>) -> bool {
1922+
let def_id = def_id.into_query_key();
19201923
let DefKind::Impl { of_trait } = self.def_kind(def_id) else {
19211924
panic!("expected Impl for {def_id:?}");
19221925
};
@@ -1950,37 +1953,40 @@ impl<'tcx> TyCtxt<'tcx> {
19501953
}
19511954
}
19521955

1953-
pub fn impl_polarity(self, def_id: impl IntoQueryParam<DefId>) -> ty::ImplPolarity {
1956+
pub fn impl_polarity(self, def_id: impl IntoQueryKey<DefId>) -> ty::ImplPolarity {
1957+
let def_id = def_id.into_query_key();
19541958
self.impl_trait_header(def_id).polarity
19551959
}
19561960

19571961
/// Given an `impl_id`, return the trait it implements.
19581962
pub fn impl_trait_ref(
19591963
self,
1960-
def_id: impl IntoQueryParam<DefId>,
1964+
def_id: impl IntoQueryKey<DefId>,
19611965
) -> ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>> {
1966+
let def_id = def_id.into_query_key();
19621967
self.impl_trait_header(def_id).trait_ref
19631968
}
19641969

19651970
/// Given an `impl_id`, return the trait it implements.
19661971
/// Returns `None` if it is an inherent impl.
19671972
pub fn impl_opt_trait_ref(
19681973
self,
1969-
def_id: impl IntoQueryParam<DefId>,
1974+
def_id: impl IntoQueryKey<DefId>,
19701975
) -> Option<ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>>> {
1971-
let def_id = def_id.into_query_param();
1976+
let def_id = def_id.into_query_key();
19721977
self.impl_is_of_trait(def_id).then(|| self.impl_trait_ref(def_id))
19731978
}
19741979

19751980
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
1976-
pub fn impl_trait_id(self, def_id: impl IntoQueryParam<DefId>) -> DefId {
1981+
pub fn impl_trait_id(self, def_id: impl IntoQueryKey<DefId>) -> DefId {
1982+
let def_id = def_id.into_query_key();
19771983
self.impl_trait_ref(def_id).skip_binder().def_id
19781984
}
19791985

19801986
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
19811987
/// Returns `None` if it is an inherent impl.
1982-
pub fn impl_opt_trait_id(self, def_id: impl IntoQueryParam<DefId>) -> Option<DefId> {
1983-
let def_id = def_id.into_query_param();
1988+
pub fn impl_opt_trait_id(self, def_id: impl IntoQueryKey<DefId>) -> Option<DefId> {
1989+
let def_id = def_id.into_query_key();
19841990
self.impl_is_of_trait(def_id).then(|| self.impl_trait_id(def_id))
19851991
}
19861992

0 commit comments

Comments
 (0)