Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 62b5aff

Browse files
Make query helpers on TyCtxt take impl Into<DefId>
1 parent e82734e commit 62b5aff

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

src/librustc_middle/ty/query/plumbing.rs

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,73 @@ 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! define_query_helper {
246+
(TyCtxtAt<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => {
247+
$(#[$attr])*
248+
#[inline(always)]
249+
pub fn $name(self, key: impl Into<DefId>) -> $V {
250+
fn mono(this: TyCtxtAt<$tcx>, key: DefId) -> $V {
251+
get_query::<queries::$name<'_>, _>(this.tcx, this.span, key)
252+
}
253+
254+
mono(self, key.into())
255+
}
256+
};
257+
(TyCtxtAt<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => {
258+
$(#[$attr])*
259+
#[inline(always)]
260+
pub fn $name(self, key: $K) -> $V {
261+
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key)
262+
}
263+
};
264+
265+
(TyCtxt<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => {
266+
$(#[$attr])*
267+
#[inline(always)]
268+
pub fn $name(self, key: impl Into<DefId>) -> $V {
269+
self.at(DUMMY_SP).$name(key)
270+
}
271+
};
272+
(TyCtxt<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => {
273+
$(#[$attr])*
274+
#[inline(always)]
275+
pub fn $name(self, key: $K) -> $V {
276+
self.at(DUMMY_SP).$name(key)
277+
}
278+
};
279+
280+
(TyCtxtEnsure<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => {
281+
$(#[$attr])*
282+
#[inline(always)]
283+
pub fn $name(self, key: impl Into<DefId>) {
284+
fn mono(this: TyCtxtEnsure<$tcx>, key: DefId) {
285+
ensure_query::<queries::$name<'_>, _>(this.tcx, key)
286+
}
287+
288+
mono(self, key.into())
289+
}
290+
};
291+
(TyCtxtEnsure<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => {
292+
$(#[$attr])*
293+
#[inline(always)]
294+
pub fn $name(self, key: $K) {
295+
ensure_query::<queries::$name<'_>, _>(self.tcx, key)
296+
}
297+
};
298+
}
299+
245300
macro_rules! define_queries_inner {
246301
(<$tcx:tt>
247302
$($(#[$attr:meta])* category<$category:tt>
248-
[$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
303+
[$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*) => {
249304

250305
use std::mem;
251306
use crate::{
@@ -263,7 +318,7 @@ macro_rules! define_queries_inner {
263318
#[allow(nonstandard_style)]
264319
#[derive(Clone, Debug)]
265320
pub enum Query<$tcx> {
266-
$($(#[$attr])* $name($K)),*
321+
$($(#[$attr])* $name($($K)*)),*
267322
}
268323

269324
impl<$tcx> Query<$tcx> {
@@ -321,7 +376,7 @@ macro_rules! define_queries_inner {
321376
}
322377

323378
$(impl<$tcx> QueryConfig<TyCtxt<$tcx>> for queries::$name<$tcx> {
324-
type Key = $K;
379+
type Key = $($K)*;
325380
type Value = $V;
326381
const NAME: &'static str = stringify!($name);
327382
const CATEGORY: ProfileCategory = $category;
@@ -332,7 +387,7 @@ macro_rules! define_queries_inner {
332387
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
333388
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node;
334389

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

337392
#[inline(always)]
338393
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<TyCtxt<$tcx>, Self::Cache> {
@@ -377,12 +432,8 @@ macro_rules! define_queries_inner {
377432
pub tcx: TyCtxt<'tcx>,
378433
}
379434

380-
impl TyCtxtEnsure<$tcx> {
381-
$($(#[$attr])*
382-
#[inline(always)]
383-
pub fn $name(self, key: $K) {
384-
ensure_query::<queries::$name<'_>, _>(self.tcx, key)
385-
})*
435+
impl TyCtxtEnsure<'tcx> {
436+
$( define_query_helper!(TyCtxtEnsure<'tcx>, $(#[$attr])* $name($($K)*) -> $V); )*
386437
}
387438

388439
#[derive(Copy, Clone)]
@@ -419,11 +470,7 @@ macro_rules! define_queries_inner {
419470
}
420471
}
421472

422-
$($(#[$attr])*
423-
#[inline(always)]
424-
pub fn $name(self, key: $K) -> $V {
425-
self.at(DUMMY_SP).$name(key)
426-
})*
473+
$( define_query_helper!(TyCtxt<$tcx>, $(#[$attr])* $name($($K)*) -> $V); )*
427474

428475
/// All self-profiling events generated by the query engine use
429476
/// virtual `StringId`s for their `event_id`. This method makes all
@@ -456,16 +503,12 @@ macro_rules! define_queries_inner {
456503
}
457504

458505
impl TyCtxtAt<$tcx> {
459-
$($(#[$attr])*
460-
#[inline(always)]
461-
pub fn $name(self, key: $K) -> $V {
462-
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key)
463-
})*
506+
$( define_query_helper!(TyCtxtAt<$tcx>, $(#[$attr])* $name($($K)*) -> $V); )*
464507
}
465508

466509
define_provider_struct! {
467510
tcx: $tcx,
468-
input: ($(([$($modifiers)*] [$name] [$K] [$V]))*)
511+
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
469512
}
470513

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

0 commit comments

Comments
 (0)