From 5ddcd09b53989f0a3c3fcefa5ba3ee9d40e60064 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 9 Feb 2018 10:39:46 -0500 Subject: [PATCH] add `TypeRelation` and `Lift` impls for `Kind` --- src/librustc/ty/relate.rs | 33 +++++++++++++++++++++++---------- src/librustc/ty/subst.rs | 13 ++++++++++++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/librustc/ty/relate.rs b/src/librustc/ty/relate.rs index 0ca2769aa17b..7f48f8c431fa 100644 --- a/src/librustc/ty/relate.rs +++ b/src/librustc/ty/relate.rs @@ -16,7 +16,7 @@ use hir::def_id::DefId; use middle::const_val::ConstVal; use traits::Reveal; -use ty::subst::{UnpackedKind, Substs}; +use ty::subst::{Kind, UnpackedKind, Substs}; use ty::{self, Ty, TyCtxt, TypeFoldable}; use ty::error::{ExpectedFound, TypeError}; use mir::interpret::{GlobalId, Value, PrimVal}; @@ -142,15 +142,7 @@ pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R, let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| { let variance = variances.map_or(ty::Invariant, |v| v[i]); - match (a.unpack(), b.unpack()) { - (UnpackedKind::Lifetime(a_lt), UnpackedKind::Lifetime(b_lt)) => { - Ok(relation.relate_with_variance(variance, &a_lt, &b_lt)?.into()) - } - (UnpackedKind::Type(a_ty), UnpackedKind::Type(b_ty)) => { - Ok(relation.relate_with_variance(variance, &a_ty, &b_ty)?.into()) - } - (UnpackedKind::Lifetime(_), _) | (UnpackedKind::Type(_), _) => bug!() - } + relation.relate_with_variance(variance, a, b) }); Ok(tcx.mk_substs(params)?) @@ -693,6 +685,27 @@ impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Box { } } +impl<'tcx> Relate<'tcx> for Kind<'tcx> { + fn relate<'a, 'gcx, R>( + relation: &mut R, + a: &Kind<'tcx>, + b: &Kind<'tcx> + ) -> RelateResult<'tcx, Kind<'tcx>> + where + R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a, + { + match (a.unpack(), b.unpack()) { + (UnpackedKind::Lifetime(a_lt), UnpackedKind::Lifetime(b_lt)) => { + Ok(relation.relate(&a_lt, &b_lt)?.into()) + } + (UnpackedKind::Type(a_ty), UnpackedKind::Type(b_ty)) => { + Ok(relation.relate(&a_ty, &b_ty)?.into()) + } + (UnpackedKind::Lifetime(_), _) | (UnpackedKind::Type(_), _) => bug!() + } + } +} + /////////////////////////////////////////////////////////////////////////// // Error handling diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 5e3417e98c2e..e70c72335aa3 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -11,7 +11,7 @@ // Type substitutions. use hir::def_id::DefId; -use ty::{self, Slice, Region, Ty, TyCtxt}; +use ty::{self, Lift, Slice, Region, Ty, TyCtxt}; use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; use serialize::{self, Encodable, Encoder, Decodable, Decoder}; @@ -113,6 +113,17 @@ impl<'tcx> fmt::Display for Kind<'tcx> { } } +impl<'a, 'tcx> Lift<'tcx> for Kind<'a> { + type Lifted = Kind<'tcx>; + + fn lift_to_tcx<'cx, 'gcx>(&self, tcx: TyCtxt<'cx, 'gcx, 'tcx>) -> Option { + match self.unpack() { + UnpackedKind::Lifetime(a) => a.lift_to_tcx(tcx).map(|a| a.into()), + UnpackedKind::Type(a) => a.lift_to_tcx(tcx).map(|a| a.into()), + } + } +} + impl<'tcx> TypeFoldable<'tcx> for Kind<'tcx> { fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { match self.unpack() {