Skip to content

Commit 5179ebe

Browse files
committed
Auto merge of #70961 - ecstatic-morse:into-def-id, r=eddyb
Take `impl Into<DefId>` for query methods on `TyCtxt` Alternative implementation of #70956. cc #70853.
2 parents d28a464 + 04c91a0 commit 5179ebe

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

src/librustc_middle/ty/query/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,31 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool
189189
pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
190190
rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx)
191191
}
192+
193+
mod sealed {
194+
use super::{DefId, LocalDefId};
195+
196+
/// An analogue of the `Into` trait that's intended only for query paramaters.
197+
///
198+
/// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
199+
/// user call `to_def_id` to convert between them everywhere else.
200+
pub trait IntoQueryParam<P> {
201+
fn into_query_param(self) -> P;
202+
}
203+
204+
impl<P> IntoQueryParam<P> for P {
205+
#[inline(always)]
206+
fn into_query_param(self) -> P {
207+
self
208+
}
209+
}
210+
211+
impl IntoQueryParam<DefId> for LocalDefId {
212+
#[inline(always)]
213+
fn into_query_param(self) -> DefId {
214+
self.to_def_id()
215+
}
216+
}
217+
}
218+
219+
use sealed::IntoQueryParam;

src/librustc_middle/ty/query/plumbing.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,23 @@ macro_rules! hash_result {
234234

235235
macro_rules! define_queries {
236236
(<$tcx:tt> $($category:tt {
237-
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*
237+
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*
238238
},)*) => {
239239
define_queries_inner! { <$tcx>
240-
$($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($K) -> $V,)*)*
240+
$($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($($K)*) -> $V,)*)*
241241
}
242242
}
243243
}
244244

245+
macro_rules! query_helper_param_ty {
246+
(DefId) => { impl IntoQueryParam<DefId> };
247+
($K:ty) => { $K };
248+
}
249+
245250
macro_rules! define_queries_inner {
246251
(<$tcx:tt>
247252
$($(#[$attr:meta])* category<$category:tt>
248-
[$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
253+
[$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*) => {
249254

250255
use std::mem;
251256
use crate::{
@@ -263,7 +268,7 @@ macro_rules! define_queries_inner {
263268
#[allow(nonstandard_style)]
264269
#[derive(Clone, Debug)]
265270
pub enum Query<$tcx> {
266-
$($(#[$attr])* $name($K)),*
271+
$($(#[$attr])* $name($($K)*)),*
267272
}
268273

269274
impl<$tcx> Query<$tcx> {
@@ -321,7 +326,7 @@ macro_rules! define_queries_inner {
321326
}
322327

323328
$(impl<$tcx> QueryConfig<TyCtxt<$tcx>> for queries::$name<$tcx> {
324-
type Key = $K;
329+
type Key = $($K)*;
325330
type Value = $V;
326331
const NAME: &'static str = stringify!($name);
327332
const CATEGORY: ProfileCategory = $category;
@@ -332,7 +337,7 @@ macro_rules! define_queries_inner {
332337
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
333338
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node;
334339

335-
type Cache = query_storage!([$($modifiers)*][$K, $V]);
340+
type Cache = query_storage!([$($modifiers)*][$($K)*, $V]);
336341

337342
#[inline(always)]
338343
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<TyCtxt<$tcx>, Self::Cache> {
@@ -380,8 +385,8 @@ macro_rules! define_queries_inner {
380385
impl TyCtxtEnsure<$tcx> {
381386
$($(#[$attr])*
382387
#[inline(always)]
383-
pub fn $name(self, key: $K) {
384-
ensure_query::<queries::$name<'_>, _>(self.tcx, key)
388+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
389+
ensure_query::<queries::$name<'_>, _>(self.tcx, key.into_query_param())
385390
})*
386391
}
387392

@@ -421,7 +426,7 @@ macro_rules! define_queries_inner {
421426

422427
$($(#[$attr])*
423428
#[inline(always)]
424-
pub fn $name(self, key: $K) -> $V {
429+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V {
425430
self.at(DUMMY_SP).$name(key)
426431
})*
427432

@@ -458,14 +463,14 @@ macro_rules! define_queries_inner {
458463
impl TyCtxtAt<$tcx> {
459464
$($(#[$attr])*
460465
#[inline(always)]
461-
pub fn $name(self, key: $K) -> $V {
462-
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key)
466+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V {
467+
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param())
463468
})*
464469
}
465470

466471
define_provider_struct! {
467472
tcx: $tcx,
468-
input: ($(([$($modifiers)*] [$name] [$K] [$V]))*)
473+
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
469474
}
470475

471476
impl<$tcx> Copy for Providers<$tcx> {}

src/librustc_middle/ty/trait_def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> &Trai
193193
let mut impls = TraitImpls::default();
194194

195195
{
196-
let mut add_impl = |impl_def_id| {
196+
let mut add_impl = |impl_def_id: DefId| {
197197
let impl_self_ty = tcx.type_of(impl_def_id);
198198
if impl_def_id.is_local() && impl_self_ty.references_error() {
199199
return;

src/librustc_typeck/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ fn for_id(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) -> CheckWfFcxBuilder<'_>
335335
inherited: Inherited::build(tcx, def_id),
336336
id,
337337
span,
338-
param_env: tcx.param_env(def_id.to_def_id()),
338+
param_env: tcx.param_env(def_id),
339339
}
340340
}
341341

0 commit comments

Comments
 (0)