Skip to content

Commit beeacdb

Browse files
Use custom trait instead of Into
1 parent fd8cf3c commit beeacdb

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/librustc_middle/ty/query/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,23 @@ 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+
/// An analogue of the `Into` trait that's intended only for query paramaters.
194+
///
195+
/// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
196+
/// user call `to_def_id` to convert between them everywhere else.
197+
pub trait IntoQueryParam<P> {
198+
fn into_query_param(self) -> P;
199+
}
200+
201+
impl<P> IntoQueryParam<P> for P {
202+
fn into_query_param(self) -> P {
203+
self
204+
}
205+
}
206+
207+
impl IntoQueryParam<DefId> for LocalDefId {
208+
fn into_query_param(self) -> DefId {
209+
self.to_def_id()
210+
}
211+
}

src/librustc_middle/ty/query/plumbing.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ macro_rules! define_queries {
243243
}
244244

245245
macro_rules! query_helper_param_ty {
246-
(DefId) => { impl Into<DefId> };
246+
(DefId) => { impl IntoQueryParam<DefId> };
247247
($K:ty) => { $K };
248248
}
249249

@@ -386,7 +386,7 @@ macro_rules! define_queries_inner {
386386
$($(#[$attr])*
387387
#[inline(always)]
388388
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
389-
ensure_query::<queries::$name<'_>, _>(self.tcx, key.into())
389+
ensure_query::<queries::$name<'_>, _>(self.tcx, key.into_query_param())
390390
})*
391391
}
392392

@@ -464,7 +464,7 @@ macro_rules! define_queries_inner {
464464
$($(#[$attr])*
465465
#[inline(always)]
466466
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V {
467-
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into())
467+
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param())
468468
})*
469469
}
470470

0 commit comments

Comments
 (0)