Skip to content

Commit 4923b5f

Browse files
Rollup merge of rust-lang#154028 - Zalathar:into-query-key, r=nnethercote
Rename trait `IntoQueryParam` to `IntoQueryKey` This is in line with the general trend towards consistently referring to a query method parameter/argument as a “key”. In addition to the actual renaming, this PR also moves the trait out of `plumbing` and into its own file, and tweaks some of the occurrences of the trait to be more consistent with each other. r? nnethercote
2 parents 24a26d0 + 563ec85 commit 4923b5f

7 files changed

Lines changed: 127 additions & 118 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use rustc_hir::OwnerId;
2+
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId};
3+
4+
/// Argument-conversion trait used by some queries and other `TyCtxt` methods.
5+
///
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;
17+
}
18+
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 {
24+
#[inline(always)]
25+
fn into_query_key(self) -> K {
26+
self
27+
}
28+
}
29+
30+
impl IntoQueryKey<LocalDefId> for OwnerId {
31+
#[inline(always)]
32+
fn into_query_key(self) -> LocalDefId {
33+
self.def_id
34+
}
35+
}
36+
37+
impl IntoQueryKey<DefId> for LocalDefId {
38+
#[inline(always)]
39+
fn into_query_key(self) -> DefId {
40+
self.to_def_id()
41+
}
42+
}
43+
44+
impl IntoQueryKey<DefId> for OwnerId {
45+
#[inline(always)]
46+
fn into_query_key(self) -> DefId {
47+
self.to_def_id()
48+
}
49+
}
50+
51+
impl IntoQueryKey<DefId> for ModDefId {
52+
#[inline(always)]
53+
fn into_query_key(self) -> DefId {
54+
self.to_def_id()
55+
}
56+
}
57+
58+
impl IntoQueryKey<DefId> for LocalModDefId {
59+
#[inline(always)]
60+
fn into_query_key(self) -> DefId {
61+
self.to_def_id()
62+
}
63+
}
64+
65+
impl IntoQueryKey<LocalDefId> for LocalModDefId {
66+
#[inline(always)]
67+
fn into_query_key(self) -> LocalDefId {
68+
self.into()
69+
}
70+
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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::IntoQueryKey;
45
pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter};
56
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
67
pub use self::plumbing::{
7-
ActiveKeyStatus, CycleError, EnsureMode, IntoQueryParam, QueryMode, QueryState, QuerySystem,
8-
QueryVTable, TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
8+
ActiveKeyStatus, CycleError, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable,
9+
TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
910
};
1011
pub use self::stack::QueryStackFrame;
1112
pub use crate::queries::Providers;
@@ -15,6 +16,7 @@ pub(crate) mod arena_cached;
1516
mod caches;
1617
pub mod erase;
1718
pub(crate) mod inner;
19+
mod into_query_key;
1820
mod job;
1921
mod keys;
2022
pub(crate) mod modifiers;

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use rustc_data_structures::hash_table::HashTable;
66
use rustc_data_structures::sharded::Sharded;
77
use rustc_data_structures::sync::{AtomicU64, WorkerLocal};
88
use rustc_errors::Diag;
9-
use rustc_hir::def_id::{DefId, LocalDefId};
10-
use rustc_hir::hir_id::OwnerId;
119
use rustc_span::{Span, Spanned};
12-
pub use sealed::IntoQueryParam;
1310

1411
use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
1512
use crate::ich::StableHashingContext;
@@ -272,8 +269,8 @@ impl<'tcx> TyCtxt<'tcx> {
272269
}
273270

274271
macro_rules! query_helper_param_ty {
275-
(DefId) => { impl $crate::query::IntoQueryParam<DefId> };
276-
(LocalDefId) => { impl $crate::query::IntoQueryParam<LocalDefId> };
272+
(DefId) => { impl $crate::query::IntoQueryKey<DefId> };
273+
(LocalDefId) => { impl $crate::query::IntoQueryKey<LocalDefId> };
277274
($K:ty) => { $K };
278275
}
279276

@@ -414,7 +411,7 @@ macro_rules! define_callbacks {
414411
crate::query::inner::query_ensure_ok_or_done(
415412
self.tcx,
416413
&self.tcx.query_system.query_vtables.$name,
417-
$crate::query::IntoQueryParam::into_query_param(key),
414+
$crate::query::IntoQueryKey::into_query_key(key),
418415
$crate::query::EnsureMode::Ok,
419416
)
420417
}
@@ -434,7 +431,7 @@ macro_rules! define_callbacks {
434431
crate::query::inner::query_ensure_result(
435432
self.tcx,
436433
&self.tcx.query_system.query_vtables.$name,
437-
$crate::query::IntoQueryParam::into_query_param(key),
434+
$crate::query::IntoQueryKey::into_query_key(key),
438435
)
439436
}
440437
)*
@@ -448,7 +445,7 @@ macro_rules! define_callbacks {
448445
crate::query::inner::query_ensure_ok_or_done(
449446
self.tcx,
450447
&self.tcx.query_system.query_vtables.$name,
451-
$crate::query::IntoQueryParam::into_query_param(key),
448+
$crate::query::IntoQueryKey::into_query_key(key),
452449
$crate::query::EnsureMode::Done,
453450
);
454451
}
@@ -477,21 +474,21 @@ macro_rules! define_callbacks {
477474
self.tcx,
478475
self.span,
479476
&self.tcx.query_system.query_vtables.$name,
480-
$crate::query::IntoQueryParam::into_query_param(key),
477+
$crate::query::IntoQueryKey::into_query_key(key),
481478
))
482479
}
483480
)*
484481
}
485482

486483
$(
487484
#[cfg($feedable)]
488-
impl<'tcx, K: $crate::query::IntoQueryParam<$name::Key<'tcx>> + Copy>
485+
impl<'tcx, K: $crate::query::IntoQueryKey<$name::Key<'tcx>> + Copy>
489486
TyCtxtFeed<'tcx, K>
490487
{
491488
$(#[$attr])*
492489
#[inline(always)]
493490
pub fn $name(self, value: $name::ProvidedValue<'tcx>) {
494-
let key = self.key().into_query_param();
491+
let key = self.key().into_query_key();
495492
let erased_value = $name::provided_to_erased(self.tcx, value);
496493
$crate::query::inner::query_feed(
497494
self.tcx,
@@ -648,69 +645,6 @@ macro_rules! define_callbacks {
648645
pub(crate) use define_callbacks;
649646
pub(crate) use query_helper_param_ty;
650647

651-
mod sealed {
652-
use rustc_hir::def_id::{LocalModDefId, ModDefId};
653-
654-
use super::{DefId, LocalDefId, OwnerId};
655-
656-
/// An analogue of the `Into` trait that's intended only for query parameters.
657-
///
658-
/// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
659-
/// user call `to_def_id` to convert between them everywhere else.
660-
pub trait IntoQueryParam<P> {
661-
fn into_query_param(self) -> P;
662-
}
663-
664-
impl<P> IntoQueryParam<P> for P {
665-
#[inline(always)]
666-
fn into_query_param(self) -> P {
667-
self
668-
}
669-
}
670-
671-
impl IntoQueryParam<LocalDefId> for OwnerId {
672-
#[inline(always)]
673-
fn into_query_param(self) -> LocalDefId {
674-
self.def_id
675-
}
676-
}
677-
678-
impl IntoQueryParam<DefId> for LocalDefId {
679-
#[inline(always)]
680-
fn into_query_param(self) -> DefId {
681-
self.to_def_id()
682-
}
683-
}
684-
685-
impl IntoQueryParam<DefId> for OwnerId {
686-
#[inline(always)]
687-
fn into_query_param(self) -> DefId {
688-
self.to_def_id()
689-
}
690-
}
691-
692-
impl IntoQueryParam<DefId> for ModDefId {
693-
#[inline(always)]
694-
fn into_query_param(self) -> DefId {
695-
self.to_def_id()
696-
}
697-
}
698-
699-
impl IntoQueryParam<DefId> for LocalModDefId {
700-
#[inline(always)]
701-
fn into_query_param(self) -> DefId {
702-
self.to_def_id()
703-
}
704-
}
705-
706-
impl IntoQueryParam<LocalDefId> for LocalModDefId {
707-
#[inline(always)]
708-
fn into_query_param(self) -> LocalDefId {
709-
self.into()
710-
}
711-
}
712-
}
713-
714648
#[cold]
715649
pub(crate) fn default_query(name: &str, key: &dyn std::fmt::Debug) -> ! {
716650
bug!(

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
})

0 commit comments

Comments
 (0)