diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2018-02-09 10:39:46 -0500 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2018-03-13 11:21:29 -0400 |
| commit | 5ddcd09b53989f0a3c3fcefa5ba3ee9d40e60064 (patch) | |
| tree | 8e16682b3b7af8a7a7f626fd60dd6cafb29a66f1 | |
| parent | 23837c1901ab234ee6bc806744cd47d5a652cdfc (diff) | |
| download | rust-5ddcd09b53989f0a3c3fcefa5ba3ee9d40e60064.tar.gz rust-5ddcd09b53989f0a3c3fcefa5ba3ee9d40e60064.zip | |
add `TypeRelation` and `Lift` impls for `Kind`
| -rw-r--r-- | src/librustc/ty/relate.rs | 33 | ||||
| -rw-r--r-- | 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 0ca2769aa17..7f48f8c431f 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<T> { } } +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 5e3417e98c2..e70c72335aa 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<Self::Lifted> { + 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() { |
