From 977f3fc9403624def9a2d030e5542f73bd26bb1f Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 19 Jun 2018 15:00:55 -0400 Subject: [PATCH] introduce `QueryKey` separation --- src/librustc/traits/query/type_op/eq.rs | 5 +++++ src/librustc/traits/query/type_op/mod.rs | 10 +++++++--- src/librustc/traits/query/type_op/normalize.rs | 5 +++++ src/librustc/traits/query/type_op/prove_predicate.rs | 5 +++++ src/librustc/traits/query/type_op/subtype.rs | 5 +++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/librustc/traits/query/type_op/eq.rs b/src/librustc/traits/query/type_op/eq.rs index cc5f72151841..b70a5307097b 100644 --- a/src/librustc/traits/query/type_op/eq.rs +++ b/src/librustc/traits/query/type_op/eq.rs @@ -26,6 +26,7 @@ impl<'tcx> Eq<'tcx> { } impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Eq<'tcx> { + type QueryKey = Self; type QueryResult = (); fn trivial_noop(self, _tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result { @@ -36,6 +37,10 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Eq<'tcx> { } } + fn into_query_key(self) -> Self { + self + } + fn param_env(&self) -> ty::ParamEnv<'tcx> { self.param_env } diff --git a/src/librustc/traits/query/type_op/mod.rs b/src/librustc/traits/query/type_op/mod.rs index 4addb6c027b9..41d33338db45 100644 --- a/src/librustc/traits/query/type_op/mod.rs +++ b/src/librustc/traits/query/type_op/mod.rs @@ -103,18 +103,21 @@ pub trait TypeOp<'gcx, 'tcx>: Sized + fmt::Debug { } } -pub trait QueryTypeOp<'gcx: 'tcx, 'tcx>: TypeFoldable<'tcx> + Lift<'gcx> { +pub trait QueryTypeOp<'gcx: 'tcx, 'tcx>: fmt::Debug + Sized { + type QueryKey: TypeFoldable<'tcx> + Lift<'gcx>; type QueryResult: TypeFoldable<'tcx> + Lift<'gcx>; /// Micro-optimization: returns `Ok(x)` if we can trivially /// produce the output, else returns `Err(self)` back. fn trivial_noop(self, tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result; + fn into_query_key(self) -> Self::QueryKey; + fn param_env(&self) -> ParamEnv<'tcx>; fn perform_query( tcx: TyCtxt<'_, 'gcx, 'tcx>, - canonicalized: Canonicalized<'gcx, Self>, + canonicalized: Canonicalized<'gcx, Self::QueryKey>, ) -> Fallible>; /// "Upcasts" a lifted query result (which is in the gcx lifetime) @@ -149,7 +152,8 @@ where // `canonicalize_hr_query_hack` here because of things like // the subtype query, which go awry around `'static` // otherwise. - let (canonical_self, canonical_var_values) = infcx.canonicalize_hr_query_hack(&self); + let query_key = self.into_query_key(); + let (canonical_self, canonical_var_values) = infcx.canonicalize_hr_query_hack(&query_key); let canonical_result = Q::perform_query(infcx.tcx, canonical_self)?; // FIXME: This is not the most efficient setup. The diff --git a/src/librustc/traits/query/type_op/normalize.rs b/src/librustc/traits/query/type_op/normalize.rs index 80bcc20f842c..8c5bbe05616a 100644 --- a/src/librustc/traits/query/type_op/normalize.rs +++ b/src/librustc/traits/query/type_op/normalize.rs @@ -33,6 +33,7 @@ impl<'gcx: 'tcx, 'tcx, T> super::QueryTypeOp<'gcx, 'tcx> for Normalize<'tcx, T> where T: Normalizable<'gcx, 'tcx>, { + type QueryKey = Self; type QueryResult = T; fn trivial_noop(self, _tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result { @@ -43,6 +44,10 @@ where } } + fn into_query_key(self) -> Self { + self + } + fn param_env(&self) -> ParamEnv<'tcx> { self.param_env } diff --git a/src/librustc/traits/query/type_op/prove_predicate.rs b/src/librustc/traits/query/type_op/prove_predicate.rs index d2714803eaec..1d02fe9a1131 100644 --- a/src/librustc/traits/query/type_op/prove_predicate.rs +++ b/src/librustc/traits/query/type_op/prove_predicate.rs @@ -28,12 +28,17 @@ impl<'tcx> ProvePredicate<'tcx> { } impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for ProvePredicate<'tcx> { + type QueryKey = Self; type QueryResult = (); fn trivial_noop(self, _tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result { Err(self) } + fn into_query_key(self) -> Self { + self + } + fn param_env(&self) -> ParamEnv<'tcx> { self.param_env } diff --git a/src/librustc/traits/query/type_op/subtype.rs b/src/librustc/traits/query/type_op/subtype.rs index 7bb1eda4eecf..0842b78ce8b1 100644 --- a/src/librustc/traits/query/type_op/subtype.rs +++ b/src/librustc/traits/query/type_op/subtype.rs @@ -30,6 +30,7 @@ impl<'tcx> Subtype<'tcx> { } impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Subtype<'tcx> { + type QueryKey = Self; type QueryResult = (); fn trivial_noop(self, _tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result<(), Self> { @@ -40,6 +41,10 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Subtype<'tcx> { } } + fn into_query_key(self) -> Self { + self + } + fn param_env(&self) -> ParamEnv<'tcx> { self.param_env }