make one Canonicalize impl for QueryResult

This lets us simplify a few type aliases.
This commit is contained in:
Niko Matsakis 2018-06-08 16:58:12 -04:00
parent 7358931a1c
commit be27a5a775
4 changed files with 37 additions and 49 deletions

View File

@ -32,15 +32,15 @@
//! [c]: https://rust-lang-nursery.github.io/rustc-guide/traits/canonicalization.html
use infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::sync::Lrc;
use serialize::UseSpecializedDecodable;
use std::fmt::Debug;
use std::ops::Index;
use syntax::codemap::Span;
use ty::{self, CanonicalVar, Lift, Region, Slice, TyCtxt};
use ty::subst::Kind;
use ty::fold::TypeFoldable;
use rustc_data_structures::indexed_vec::IndexVec;
use ty::subst::Kind;
use ty::{self, CanonicalVar, Lift, Region, Slice, TyCtxt};
mod canonicalizer;
@ -59,7 +59,7 @@ pub struct Canonical<'gcx, V> {
pub type CanonicalVarInfos<'gcx> = &'gcx Slice<CanonicalVarInfo>;
impl<'gcx> UseSpecializedDecodable for CanonicalVarInfos<'gcx> { }
impl<'gcx> UseSpecializedDecodable for CanonicalVarInfos<'gcx> {}
/// A set of values corresponding to the canonical variables from some
/// `Canonical`. You can give these values to
@ -124,6 +124,9 @@ pub struct QueryResult<'tcx, R> {
pub value: R,
}
pub type CanonicalizedQueryResult<'gcx, T> =
Lrc<Canonical<'gcx, QueryResult<'gcx, <T as Lift<'gcx>>::Lifted>>>;
/// Indicates whether or not we were able to prove the query to be
/// true.
#[derive(Copy, Clone, Debug)]
@ -246,9 +249,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
CanonicalVarKind::Ty(ty_kind) => {
let ty = match ty_kind {
CanonicalTyVarKind::General => {
self.next_ty_var(
TypeVariableOrigin::MiscVariable(span),
)
self.next_ty_var(TypeVariableOrigin::MiscVariable(span))
}
CanonicalTyVarKind::Int => self.tcx.mk_int_var(self.next_int_var_id()),
@ -258,9 +259,9 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
ty.into()
}
CanonicalVarKind::Region => {
self.next_region_var(RegionVariableOrigin::MiscVariable(span)).into()
}
CanonicalVarKind::Region => self
.next_region_var(RegionVariableOrigin::MiscVariable(span))
.into(),
}
}
}
@ -343,3 +344,19 @@ impl<'tcx> Index<CanonicalVar> for CanonicalVarValues<'tcx> {
&self.var_values[value]
}
}
impl<'gcx: 'tcx, 'tcx, T> Canonicalize<'gcx, 'tcx> for QueryResult<'tcx, T>
where
T: TypeFoldable<'tcx> + Lift<'gcx>,
T::Lifted: Debug,
{
// we ought to intern this, but I'm too lazy just now
type Canonicalized = Lrc<Canonical<'gcx, QueryResult<'gcx, T::Lifted>>>;
fn intern(
_gcx: TyCtxt<'_, 'gcx, 'gcx>,
value: Canonical<'gcx, Self::Lifted>,
) -> Self::Canonicalized {
Lrc::new(value)
}
}

View File

@ -19,11 +19,13 @@
use infer::canonical::substitute::substitute_value;
use infer::canonical::{
Canonical, CanonicalVarValues, Canonicalize, Certainty, QueryRegionConstraint, QueryResult,
Canonical, CanonicalVarValues, Canonicalize, CanonicalizedQueryResult, Certainty,
QueryRegionConstraint, QueryResult,
};
use infer::region_constraints::{Constraint, RegionConstraintData};
use infer::{InferCtxt, InferOk, InferResult, RegionObligation};
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::indexed_vec::IndexVec;
use std::fmt::Debug;
use syntax::ast;
use traits::query::NoSolution;
@ -31,12 +33,7 @@ use traits::{FulfillmentContext, TraitEngine};
use traits::{Obligation, ObligationCause, PredicateObligation};
use ty::fold::TypeFoldable;
use ty::subst::{Kind, UnpackedKind};
use ty::{self, CanonicalVar, TyCtxt};
use rustc_data_structures::indexed_vec::IndexVec;
type CanonicalizedQueryResult<'gcx, 'tcx, T> =
<QueryResult<'tcx, T> as Canonicalize<'gcx, 'tcx>>::Canonicalized;
use ty::{self, CanonicalVar, Lift, TyCtxt};
impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
/// This method is meant to be invoked as the final step of a canonical query
@ -63,10 +60,10 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
inference_vars: CanonicalVarValues<'tcx>,
answer: T,
fulfill_cx: &mut FulfillmentContext<'tcx>,
) -> Result<CanonicalizedQueryResult<'gcx, 'tcx, T>, NoSolution>
) -> Result<CanonicalizedQueryResult<'gcx, T>, NoSolution>
where
T: Debug,
QueryResult<'tcx, T>: Canonicalize<'gcx, 'tcx>,
T: Debug + Lift<'gcx> + TypeFoldable<'tcx>,
T::Lifted: Debug,
{
let query_result = self.make_query_result(inference_vars, answer, fulfill_cx)?;
let (canonical_result, _) = self.canonicalize_response(&query_result);

View File

@ -9,13 +9,12 @@
// except according to those terms.
use infer::at::At;
use infer::canonical::{Canonical, Canonicalize, QueryResult};
use infer::canonical::{Canonical, Canonicalize};
use infer::InferOk;
use std::iter::FromIterator;
use traits::query::CanonicalTyGoal;
use ty::{self, Ty, TyCtxt};
use ty::subst::Kind;
use rustc_data_structures::sync::Lrc;
impl<'cx, 'gcx, 'tcx> At<'cx, 'gcx, 'tcx> {
/// Given a type `ty` of some value being dropped, computes a set
@ -181,18 +180,6 @@ impl_stable_hash_for!(struct DropckOutlivesResult<'tcx> {
kinds, overflows
});
impl<'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for QueryResult<'tcx, DropckOutlivesResult<'tcx>> {
// we ought to intern this, but I'm too lazy just now
type Canonicalized = Lrc<Canonical<'gcx, QueryResult<'gcx, DropckOutlivesResult<'gcx>>>>;
fn intern(
_gcx: TyCtxt<'_, 'gcx, 'gcx>,
value: Canonical<'gcx, Self::Lifted>,
) -> Self::Canonicalized {
Lrc::new(value)
}
}
impl_stable_hash_for!(struct DtorckConstraint<'tcx> {
outlives,
dtorck_types,

View File

@ -14,10 +14,9 @@
use infer::{InferCtxt, InferOk};
use infer::at::At;
use infer::canonical::{Canonical, Canonicalize, QueryResult};
use infer::canonical::{Canonical, Canonicalize};
use middle::const_val::ConstVal;
use mir::interpret::GlobalId;
use rustc_data_structures::sync::Lrc;
use traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
use traits::query::CanonicalProjectionGoal;
use traits::project::Normalized;
@ -262,18 +261,6 @@ impl<'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for ty::ParamEnvAnd<'tcx, ty::Pr
}
}
impl<'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for QueryResult<'tcx, NormalizationResult<'tcx>> {
// we ought to intern this, but I'm too lazy just now
type Canonicalized = Lrc<Canonical<'gcx, QueryResult<'gcx, NormalizationResult<'gcx>>>>;
fn intern(
_gcx: TyCtxt<'_, 'gcx, 'gcx>,
value: Canonical<'gcx, Self::Lifted>,
) -> Self::Canonicalized {
Lrc::new(value)
}
}
impl_stable_hash_for!(struct NormalizationResult<'tcx> {
normalized_ty
});