Skip to content
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
70 changes: 70 additions & 0 deletions compiler/rustc_middle/src/query/into_query_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use rustc_hir::OwnerId;
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId};

/// Argument-conversion trait used by some queries and other `TyCtxt` methods.
///
/// A function that accepts an `impl IntoQueryKey<DefId>` argument can be thought
/// of as taking a [`DefId`], except that callers can also pass a [`LocalDefId`]
/// or values of other narrower ID types, as long as they have a trivial conversion
/// to `DefId`.
///
/// Using a dedicated trait instead of [`Into`] makes the purpose of the conversion
/// more explicit, and makes occurrences easier to search for.
pub trait IntoQueryKey<K> {
/// Argument conversion from `Self` to `K`.
/// This should always be a very cheap conversion, e.g. [`LocalDefId::to_def_id`].
fn into_query_key(self) -> K;
}

/// Any type can be converted to itself.
///
/// This is useful in generic or macro-generated code where we don't know whether
/// conversion is actually needed, so that we can do a conversion unconditionally.
impl<K> IntoQueryKey<K> for K {
#[inline(always)]
fn into_query_key(self) -> K {
self
}
}

impl IntoQueryKey<LocalDefId> for OwnerId {
#[inline(always)]
fn into_query_key(self) -> LocalDefId {
self.def_id
}
}

impl IntoQueryKey<DefId> for LocalDefId {
#[inline(always)]
fn into_query_key(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryKey<DefId> for OwnerId {
#[inline(always)]
fn into_query_key(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryKey<DefId> for ModDefId {
#[inline(always)]
fn into_query_key(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryKey<DefId> for LocalModDefId {
#[inline(always)]
fn into_query_key(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryKey<LocalDefId> for LocalModDefId {
#[inline(always)]
fn into_query_key(self) -> LocalDefId {
self.into()
}
}
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use rustc_hir::def_id::LocalDefId;

pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
pub use self::into_query_key::IntoQueryKey;
pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter};
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
pub use self::plumbing::{
ActiveKeyStatus, CycleError, EnsureMode, IntoQueryParam, QueryMode, QueryState, QuerySystem,
QueryVTable, TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
ActiveKeyStatus, CycleError, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable,
TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
};
pub use self::stack::QueryStackFrame;
pub use crate::queries::Providers;
Expand All @@ -15,6 +16,7 @@ pub(crate) mod arena_cached;
mod caches;
pub mod erase;
pub(crate) mod inner;
mod into_query_key;
mod job;
mod keys;
pub(crate) mod modifiers;
Expand Down
82 changes: 8 additions & 74 deletions compiler/rustc_middle/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use rustc_data_structures::hash_table::HashTable;
use rustc_data_structures::sharded::Sharded;
use rustc_data_structures::sync::{AtomicU64, WorkerLocal};
use rustc_errors::Diag;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::hir_id::OwnerId;
use rustc_span::{Span, Spanned};
pub use sealed::IntoQueryParam;

use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
use crate::ich::StableHashingContext;
Expand Down Expand Up @@ -272,8 +269,8 @@ impl<'tcx> TyCtxt<'tcx> {
}

macro_rules! query_helper_param_ty {
(DefId) => { impl $crate::query::IntoQueryParam<DefId> };
(LocalDefId) => { impl $crate::query::IntoQueryParam<LocalDefId> };
(DefId) => { impl $crate::query::IntoQueryKey<DefId> };
(LocalDefId) => { impl $crate::query::IntoQueryKey<LocalDefId> };
($K:ty) => { $K };
}

Expand Down Expand Up @@ -414,7 +411,7 @@ macro_rules! define_callbacks {
crate::query::inner::query_ensure_ok_or_done(
self.tcx,
&self.tcx.query_system.query_vtables.$name,
$crate::query::IntoQueryParam::into_query_param(key),
$crate::query::IntoQueryKey::into_query_key(key),
$crate::query::EnsureMode::Ok,
)
}
Expand All @@ -434,7 +431,7 @@ macro_rules! define_callbacks {
crate::query::inner::query_ensure_result(
self.tcx,
&self.tcx.query_system.query_vtables.$name,
$crate::query::IntoQueryParam::into_query_param(key),
$crate::query::IntoQueryKey::into_query_key(key),
)
}
)*
Expand All @@ -448,7 +445,7 @@ macro_rules! define_callbacks {
crate::query::inner::query_ensure_ok_or_done(
self.tcx,
&self.tcx.query_system.query_vtables.$name,
$crate::query::IntoQueryParam::into_query_param(key),
$crate::query::IntoQueryKey::into_query_key(key),
$crate::query::EnsureMode::Done,
);
}
Expand Down Expand Up @@ -477,21 +474,21 @@ macro_rules! define_callbacks {
self.tcx,
self.span,
&self.tcx.query_system.query_vtables.$name,
$crate::query::IntoQueryParam::into_query_param(key),
$crate::query::IntoQueryKey::into_query_key(key),
))
}
)*
}

$(
#[cfg($feedable)]
impl<'tcx, K: $crate::query::IntoQueryParam<$name::Key<'tcx>> + Copy>
impl<'tcx, K: $crate::query::IntoQueryKey<$name::Key<'tcx>> + Copy>
TyCtxtFeed<'tcx, K>
{
$(#[$attr])*
#[inline(always)]
pub fn $name(self, value: $name::ProvidedValue<'tcx>) {
let key = self.key().into_query_param();
let key = self.key().into_query_key();
let erased_value = $name::provided_to_erased(self.tcx, value);
$crate::query::inner::query_feed(
self.tcx,
Expand Down Expand Up @@ -648,69 +645,6 @@ macro_rules! define_callbacks {
pub(crate) use define_callbacks;
pub(crate) use query_helper_param_ty;

mod sealed {
use rustc_hir::def_id::{LocalModDefId, ModDefId};

use super::{DefId, LocalDefId, OwnerId};

/// An analogue of the `Into` trait that's intended only for query parameters.
///
/// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
/// user call `to_def_id` to convert between them everywhere else.
pub trait IntoQueryParam<P> {
fn into_query_param(self) -> P;
}

impl<P> IntoQueryParam<P> for P {
#[inline(always)]
fn into_query_param(self) -> P {
self
}
}

impl IntoQueryParam<LocalDefId> for OwnerId {
#[inline(always)]
fn into_query_param(self) -> LocalDefId {
self.def_id
}
}

impl IntoQueryParam<DefId> for LocalDefId {
#[inline(always)]
fn into_query_param(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryParam<DefId> for OwnerId {
#[inline(always)]
fn into_query_param(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryParam<DefId> for ModDefId {
#[inline(always)]
fn into_query_param(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryParam<DefId> for LocalModDefId {
#[inline(always)]
fn into_query_param(self) -> DefId {
self.to_def_id()
}
}

impl IntoQueryParam<LocalDefId> for LocalModDefId {
#[inline(always)]
fn into_query_param(self) -> LocalDefId {
self.into()
}
}
}

#[cold]
pub(crate) fn default_query(name: &str, key: &dyn std::fmt::Debug) -> ! {
bug!(
Expand Down
22 changes: 10 additions & 12 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature};
use crate::middle::resolve_bound_vars;
use crate::mir::interpret::{self, Allocation, ConstAllocation};
use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
use crate::query::{IntoQueryParam, LocalCrate, Providers, QuerySystem, TyCtxtAt};
use crate::query::{IntoQueryKey, LocalCrate, Providers, QuerySystem, TyCtxtAt};
use crate::thir::Thir;
use crate::traits;
use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques};
Expand Down Expand Up @@ -1111,10 +1111,9 @@ impl<'tcx> TyCtxt<'tcx> {
}

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

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

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

pub fn is_trivial_const<P>(self, def_id: P) -> bool
where
P: IntoQueryParam<DefId>,
{
pub fn is_trivial_const(self, def_id: impl IntoQueryKey<DefId>) -> bool {
let def_id = def_id.into_query_key();
self.trivial_const(def_id).is_some()
}

Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/ty/context/impl_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, search_graph};

use crate::dep_graph::{DepKind, DepNodeIndex};
use crate::infer::canonical::CanonicalVarKinds;
use crate::query::IntoQueryParam;
use crate::traits::cache::WithDepNode;
use crate::traits::solve::{
self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect,
Expand Down Expand Up @@ -720,7 +719,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
}

fn item_name(self, id: DefId) -> Symbol {
let id = id.into_query_param();
self.opt_item_name(id).unwrap_or_else(|| {
bug!("item_name: no name for {:?}", self.def_path(id));
})
Expand Down
Loading
Loading