From 62b5aff1aefdb2c444c814946202665a760f9f2d Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Thu, 9 Apr 2020 10:29:08 -0700 Subject: [PATCH 1/8] Make query helpers on `TyCtxt` take `impl Into` --- src/librustc_middle/ty/query/plumbing.rs | 89 ++++++++++++++++++------ 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/src/librustc_middle/ty/query/plumbing.rs b/src/librustc_middle/ty/query/plumbing.rs index 1bb392f436fc6..612507711fdb8 100644 --- a/src/librustc_middle/ty/query/plumbing.rs +++ b/src/librustc_middle/ty/query/plumbing.rs @@ -234,18 +234,73 @@ macro_rules! hash_result { macro_rules! define_queries { (<$tcx:tt> $($category:tt { - $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)* + $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)* },)*) => { define_queries_inner! { <$tcx> - $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($K) -> $V,)*)* + $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($($K)*) -> $V,)*)* } } } +macro_rules! define_query_helper { + (TyCtxtAt<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => { + $(#[$attr])* + #[inline(always)] + pub fn $name(self, key: impl Into) -> $V { + fn mono(this: TyCtxtAt<$tcx>, key: DefId) -> $V { + get_query::, _>(this.tcx, this.span, key) + } + + mono(self, key.into()) + } + }; + (TyCtxtAt<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => { + $(#[$attr])* + #[inline(always)] + pub fn $name(self, key: $K) -> $V { + get_query::, _>(self.tcx, self.span, key) + } + }; + + (TyCtxt<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => { + $(#[$attr])* + #[inline(always)] + pub fn $name(self, key: impl Into) -> $V { + self.at(DUMMY_SP).$name(key) + } + }; + (TyCtxt<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => { + $(#[$attr])* + #[inline(always)] + pub fn $name(self, key: $K) -> $V { + self.at(DUMMY_SP).$name(key) + } + }; + + (TyCtxtEnsure<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => { + $(#[$attr])* + #[inline(always)] + pub fn $name(self, key: impl Into) { + fn mono(this: TyCtxtEnsure<$tcx>, key: DefId) { + ensure_query::, _>(this.tcx, key) + } + + mono(self, key.into()) + } + }; + (TyCtxtEnsure<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => { + $(#[$attr])* + #[inline(always)] + pub fn $name(self, key: $K) { + ensure_query::, _>(self.tcx, key) + } + }; +} + macro_rules! define_queries_inner { (<$tcx:tt> $($(#[$attr:meta])* category<$category:tt> - [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*) => { + [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*) => { use std::mem; use crate::{ @@ -263,7 +318,7 @@ macro_rules! define_queries_inner { #[allow(nonstandard_style)] #[derive(Clone, Debug)] pub enum Query<$tcx> { - $($(#[$attr])* $name($K)),* + $($(#[$attr])* $name($($K)*)),* } impl<$tcx> Query<$tcx> { @@ -321,7 +376,7 @@ macro_rules! define_queries_inner { } $(impl<$tcx> QueryConfig> for queries::$name<$tcx> { - type Key = $K; + type Key = $($K)*; type Value = $V; const NAME: &'static str = stringify!($name); const CATEGORY: ProfileCategory = $category; @@ -332,7 +387,7 @@ macro_rules! define_queries_inner { const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]); const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node; - type Cache = query_storage!([$($modifiers)*][$K, $V]); + type Cache = query_storage!([$($modifiers)*][$($K)*, $V]); #[inline(always)] fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState, Self::Cache> { @@ -377,12 +432,8 @@ macro_rules! define_queries_inner { pub tcx: TyCtxt<'tcx>, } - impl TyCtxtEnsure<$tcx> { - $($(#[$attr])* - #[inline(always)] - pub fn $name(self, key: $K) { - ensure_query::, _>(self.tcx, key) - })* + impl TyCtxtEnsure<'tcx> { + $( define_query_helper!(TyCtxtEnsure<'tcx>, $(#[$attr])* $name($($K)*) -> $V); )* } #[derive(Copy, Clone)] @@ -419,11 +470,7 @@ macro_rules! define_queries_inner { } } - $($(#[$attr])* - #[inline(always)] - pub fn $name(self, key: $K) -> $V { - self.at(DUMMY_SP).$name(key) - })* + $( define_query_helper!(TyCtxt<$tcx>, $(#[$attr])* $name($($K)*) -> $V); )* /// All self-profiling events generated by the query engine use /// virtual `StringId`s for their `event_id`. This method makes all @@ -456,16 +503,12 @@ macro_rules! define_queries_inner { } impl TyCtxtAt<$tcx> { - $($(#[$attr])* - #[inline(always)] - pub fn $name(self, key: $K) -> $V { - get_query::, _>(self.tcx, self.span, key) - })* + $( define_query_helper!(TyCtxtAt<$tcx>, $(#[$attr])* $name($($K)*) -> $V); )* } define_provider_struct! { tcx: $tcx, - input: ($(([$($modifiers)*] [$name] [$K] [$V]))*) + input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*) } impl<$tcx> Copy for Providers<$tcx> {} From 10ed50125454bcd166ee540cad47b91e76e343c4 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Thu, 9 Apr 2020 10:29:37 -0700 Subject: [PATCH 2/8] Impl `From` for `DefId` --- src/librustc_span/def_id.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs index fad9f2f613012..a3909904e8ce7 100644 --- a/src/librustc_span/def_id.rs +++ b/src/librustc_span/def_id.rs @@ -237,6 +237,12 @@ impl fmt::Debug for LocalDefId { } } +impl From for DefId { + fn from(v: LocalDefId) -> Self { + v.to_def_id() + } +} + impl rustc_serialize::UseSpecializedEncodable for LocalDefId {} impl rustc_serialize::UseSpecializedDecodable for LocalDefId {} From 0d4639d77c55a0146a0aa468fd7985da843d7380 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Thu, 9 Apr 2020 10:29:55 -0700 Subject: [PATCH 3/8] Fix inference fallout --- src/librustc_middle/ty/trait_def.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_middle/ty/trait_def.rs b/src/librustc_middle/ty/trait_def.rs index ed9054fcffd91..912f8be1d3342 100644 --- a/src/librustc_middle/ty/trait_def.rs +++ b/src/librustc_middle/ty/trait_def.rs @@ -193,7 +193,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> &Trai let mut impls = TraitImpls::default(); { - let mut add_impl = |impl_def_id| { + let mut add_impl = |impl_def_id: DefId| { let impl_self_ty = tcx.type_of(impl_def_id); if impl_def_id.is_local() && impl_self_ty.references_error() { return; From 6e5a21069dc7e975b2ed9ca821e4f419ba011d97 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Thu, 9 Apr 2020 10:30:21 -0700 Subject: [PATCH 4/8] Remove call to `to_def_id` before calling query helper --- src/librustc_typeck/check/wfcheck.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 5bfad558b6b36..6b6e2bb329fc9 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -335,7 +335,7 @@ fn for_id(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) -> CheckWfFcxBuilder<'_> inherited: Inherited::build(tcx, def_id), id, span, - param_env: tcx.param_env(def_id.to_def_id()), + param_env: tcx.param_env(def_id), } } From fd8cf3c5e3adffc5ef147c8a55d3532481172c42 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 10 Apr 2020 13:44:15 -0700 Subject: [PATCH 5/8] Simplify macro --- src/librustc_middle/ty/query/plumbing.rs | 76 ++++++------------------ 1 file changed, 19 insertions(+), 57 deletions(-) diff --git a/src/librustc_middle/ty/query/plumbing.rs b/src/librustc_middle/ty/query/plumbing.rs index 612507711fdb8..f3a49438f5b2f 100644 --- a/src/librustc_middle/ty/query/plumbing.rs +++ b/src/librustc_middle/ty/query/plumbing.rs @@ -242,59 +242,9 @@ macro_rules! define_queries { } } -macro_rules! define_query_helper { - (TyCtxtAt<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => { - $(#[$attr])* - #[inline(always)] - pub fn $name(self, key: impl Into) -> $V { - fn mono(this: TyCtxtAt<$tcx>, key: DefId) -> $V { - get_query::, _>(this.tcx, this.span, key) - } - - mono(self, key.into()) - } - }; - (TyCtxtAt<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => { - $(#[$attr])* - #[inline(always)] - pub fn $name(self, key: $K) -> $V { - get_query::, _>(self.tcx, self.span, key) - } - }; - - (TyCtxt<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => { - $(#[$attr])* - #[inline(always)] - pub fn $name(self, key: impl Into) -> $V { - self.at(DUMMY_SP).$name(key) - } - }; - (TyCtxt<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => { - $(#[$attr])* - #[inline(always)] - pub fn $name(self, key: $K) -> $V { - self.at(DUMMY_SP).$name(key) - } - }; - - (TyCtxtEnsure<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => { - $(#[$attr])* - #[inline(always)] - pub fn $name(self, key: impl Into) { - fn mono(this: TyCtxtEnsure<$tcx>, key: DefId) { - ensure_query::, _>(this.tcx, key) - } - - mono(self, key.into()) - } - }; - (TyCtxtEnsure<$tcx:tt>, $(#[$attr:meta])* $name:ident($K:ty) -> $V:ty) => { - $(#[$attr])* - #[inline(always)] - pub fn $name(self, key: $K) { - ensure_query::, _>(self.tcx, key) - } - }; +macro_rules! query_helper_param_ty { + (DefId) => { impl Into }; + ($K:ty) => { $K }; } macro_rules! define_queries_inner { @@ -432,8 +382,12 @@ macro_rules! define_queries_inner { pub tcx: TyCtxt<'tcx>, } - impl TyCtxtEnsure<'tcx> { - $( define_query_helper!(TyCtxtEnsure<'tcx>, $(#[$attr])* $name($($K)*) -> $V); )* + impl TyCtxtEnsure<$tcx> { + $($(#[$attr])* + #[inline(always)] + pub fn $name(self, key: query_helper_param_ty!($($K)*)) { + ensure_query::, _>(self.tcx, key.into()) + })* } #[derive(Copy, Clone)] @@ -470,7 +424,11 @@ macro_rules! define_queries_inner { } } - $( define_query_helper!(TyCtxt<$tcx>, $(#[$attr])* $name($($K)*) -> $V); )* + $($(#[$attr])* + #[inline(always)] + pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V { + self.at(DUMMY_SP).$name(key) + })* /// All self-profiling events generated by the query engine use /// virtual `StringId`s for their `event_id`. This method makes all @@ -503,7 +461,11 @@ macro_rules! define_queries_inner { } impl TyCtxtAt<$tcx> { - $( define_query_helper!(TyCtxtAt<$tcx>, $(#[$attr])* $name($($K)*) -> $V); )* + $($(#[$attr])* + #[inline(always)] + pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V { + get_query::, _>(self.tcx, self.span, key.into()) + })* } define_provider_struct! { From beeacdb2a7165b90ee3ed1e8221c9eaad6b010ea Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 10 Apr 2020 13:54:06 -0700 Subject: [PATCH 6/8] Use custom trait instead of `Into` --- src/librustc_middle/ty/query/mod.rs | 20 ++++++++++++++++++++ src/librustc_middle/ty/query/plumbing.rs | 6 +++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/librustc_middle/ty/query/mod.rs b/src/librustc_middle/ty/query/mod.rs index 9986eb88dc326..9f04cff4d2fb9 100644 --- a/src/librustc_middle/ty/query/mod.rs +++ b/src/librustc_middle/ty/query/mod.rs @@ -189,3 +189,23 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) { rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx) } + +/// An analogue of the `Into` trait that's intended only for query paramaters. +/// +/// 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

{ + fn into_query_param(self) -> P; +} + +impl

IntoQueryParam

for P { + fn into_query_param(self) -> P { + self + } +} + +impl IntoQueryParam for LocalDefId { + fn into_query_param(self) -> DefId { + self.to_def_id() + } +} diff --git a/src/librustc_middle/ty/query/plumbing.rs b/src/librustc_middle/ty/query/plumbing.rs index f3a49438f5b2f..068322b08b7a3 100644 --- a/src/librustc_middle/ty/query/plumbing.rs +++ b/src/librustc_middle/ty/query/plumbing.rs @@ -243,7 +243,7 @@ macro_rules! define_queries { } macro_rules! query_helper_param_ty { - (DefId) => { impl Into }; + (DefId) => { impl IntoQueryParam }; ($K:ty) => { $K }; } @@ -386,7 +386,7 @@ macro_rules! define_queries_inner { $($(#[$attr])* #[inline(always)] pub fn $name(self, key: query_helper_param_ty!($($K)*)) { - ensure_query::, _>(self.tcx, key.into()) + ensure_query::, _>(self.tcx, key.into_query_param()) })* } @@ -464,7 +464,7 @@ macro_rules! define_queries_inner { $($(#[$attr])* #[inline(always)] pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V { - get_query::, _>(self.tcx, self.span, key.into()) + get_query::, _>(self.tcx, self.span, key.into_query_param()) })* } From f0c77337e1510e9fb494fc459bdfea03ba2e166f Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 10 Apr 2020 14:06:57 -0700 Subject: [PATCH 7/8] Apply suggestions from review --- src/librustc_middle/ty/query/mod.rs | 34 +++++++++++++++++------------ src/librustc_span/def_id.rs | 6 ----- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/librustc_middle/ty/query/mod.rs b/src/librustc_middle/ty/query/mod.rs index 9f04cff4d2fb9..76e519c990ed5 100644 --- a/src/librustc_middle/ty/query/mod.rs +++ b/src/librustc_middle/ty/query/mod.rs @@ -190,22 +190,28 @@ pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &De rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx) } -/// An analogue of the `Into` trait that's intended only for query paramaters. -/// -/// 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

{ - fn into_query_param(self) -> P; -} +mod sealed { + use super::{DefId, LocalDefId}; + + /// An analogue of the `Into` trait that's intended only for query paramaters. + /// + /// 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

{ + fn into_query_param(self) -> P; + } -impl

IntoQueryParam

for P { - fn into_query_param(self) -> P { - self + impl

IntoQueryParam

for P { + fn into_query_param(self) -> P { + self + } } -} -impl IntoQueryParam for LocalDefId { - fn into_query_param(self) -> DefId { - self.to_def_id() + impl IntoQueryParam for LocalDefId { + fn into_query_param(self) -> DefId { + self.to_def_id() + } } } + +use sealed::IntoQueryParam; diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs index a3909904e8ce7..fad9f2f613012 100644 --- a/src/librustc_span/def_id.rs +++ b/src/librustc_span/def_id.rs @@ -237,12 +237,6 @@ impl fmt::Debug for LocalDefId { } } -impl From for DefId { - fn from(v: LocalDefId) -> Self { - v.to_def_id() - } -} - impl rustc_serialize::UseSpecializedEncodable for LocalDefId {} impl rustc_serialize::UseSpecializedDecodable for LocalDefId {} From 04c91a0bba32d7c361b17ee8c5c0b3cc12c0d42b Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Sat, 11 Apr 2020 16:29:50 -0700 Subject: [PATCH 8/8] Add `#[inline(always)]` to `into_query_param` --- src/librustc_middle/ty/query/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustc_middle/ty/query/mod.rs b/src/librustc_middle/ty/query/mod.rs index 76e519c990ed5..899479e65a732 100644 --- a/src/librustc_middle/ty/query/mod.rs +++ b/src/librustc_middle/ty/query/mod.rs @@ -202,12 +202,14 @@ mod sealed { } impl

IntoQueryParam

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