Skip to content

Commit 3859cec

Browse files
committed
---
yaml --- r: 225991 b: refs/heads/stable c: 1f70a2e h: refs/heads/master i: 225989: f5e7e56 225987: 56d599b 225983: a0c6e67 v: v3
1 parent c0a28c9 commit 3859cec

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ refs/heads/tmp: e5d90d98402475b6e154ce216f9efcb80da1a747
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: 1fe32ca12c51afcd761d9962f51a74ff0d07a591
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: d8952e7932823a3f3aa9d4af0ab9fa6f08b18cef
32+
refs/heads/stable: 1f70a2e370e2c0d69403ccadc6b55caee27a73c1
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
3434
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b

branches/stable/src/librustc/middle/ty.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,128 @@ impl<'tcx> ctxt<'tcx> {
802802
pub fn free_region_map(&self, id: NodeId) -> FreeRegionMap {
803803
self.free_region_maps.borrow()[&id].clone()
804804
}
805+
806+
pub fn lift<T: Lift<'tcx>>(&self, value: &T) -> Option<T::Lifted> {
807+
value.lift_to_tcx(self)
808+
}
809+
}
810+
811+
/// A trait implemented for all X<'a> types which can be safely and
812+
/// efficiently converted to X<'tcx> as long as they are part of the
813+
/// provided ty::ctxt<'tcx>.
814+
/// This can be done, for example, for Ty<'tcx> or &'tcx Substs<'tcx>
815+
/// by looking them up in their respective interners.
816+
pub trait Lift<'tcx> {
817+
type Lifted;
818+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<Self::Lifted>;
819+
}
820+
821+
impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
822+
type Lifted = (A::Lifted, B::Lifted);
823+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<Self::Lifted> {
824+
tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
825+
}
826+
}
827+
828+
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
829+
type Lifted = Vec<T::Lifted>;
830+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<Self::Lifted> {
831+
let mut result = Vec::with_capacity(self.len());
832+
for x in self {
833+
if let Some(value) = tcx.lift(x) {
834+
result.push(value);
835+
} else {
836+
return None;
837+
}
838+
}
839+
Some(result)
840+
}
841+
}
842+
843+
impl<'tcx> Lift<'tcx> for Region {
844+
type Lifted = Self;
845+
fn lift_to_tcx(&self, _: &ctxt<'tcx>) -> Option<Region> {
846+
Some(*self)
847+
}
848+
}
849+
850+
impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
851+
type Lifted = Ty<'tcx>;
852+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<Ty<'tcx>> {
853+
if let Some(&ty) = tcx.interner.borrow().get(&self.sty) {
854+
if *self as *const _ == ty as *const _ {
855+
return Some(ty);
856+
}
857+
}
858+
None
859+
}
860+
}
861+
862+
impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
863+
type Lifted = &'tcx Substs<'tcx>;
864+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<&'tcx Substs<'tcx>> {
865+
if let Some(&substs) = tcx.substs_interner.borrow().get(*self) {
866+
if *self as *const _ == substs as *const _ {
867+
return Some(substs);
868+
}
869+
}
870+
None
871+
}
872+
}
873+
874+
impl<'a, 'tcx> Lift<'tcx> for TraitRef<'a> {
875+
type Lifted = TraitRef<'tcx>;
876+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<TraitRef<'tcx>> {
877+
tcx.lift(&self.substs).map(|substs| TraitRef {
878+
def_id: self.def_id,
879+
substs: substs
880+
})
881+
}
882+
}
883+
884+
impl<'a, 'tcx> Lift<'tcx> for TraitPredicate<'a> {
885+
type Lifted = TraitPredicate<'tcx>;
886+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<TraitPredicate<'tcx>> {
887+
tcx.lift(&self.trait_ref).map(|trait_ref| TraitPredicate {
888+
trait_ref: trait_ref
889+
})
890+
}
891+
}
892+
893+
impl<'a, 'tcx> Lift<'tcx> for EquatePredicate<'a> {
894+
type Lifted = EquatePredicate<'tcx>;
895+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<EquatePredicate<'tcx>> {
896+
tcx.lift(&(self.0, self.1)).map(|(a, b)| EquatePredicate(a, b))
897+
}
898+
}
899+
900+
impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for OutlivesPredicate<A, B> {
901+
type Lifted = OutlivesPredicate<A::Lifted, B::Lifted>;
902+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<Self::Lifted> {
903+
tcx.lift(&(self.0, self.1)).map(|(a, b)| OutlivesPredicate(a, b))
904+
}
905+
}
906+
907+
impl<'a, 'tcx> Lift<'tcx> for ProjectionPredicate<'a> {
908+
type Lifted = ProjectionPredicate<'tcx>;
909+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<ProjectionPredicate<'tcx>> {
910+
tcx.lift(&(self.projection_ty.trait_ref, self.ty)).map(|(trait_ref, ty)| {
911+
ProjectionPredicate {
912+
projection_ty: ProjectionTy {
913+
trait_ref: trait_ref,
914+
item_name: self.projection_ty.item_name
915+
},
916+
ty: ty
917+
}
918+
})
919+
}
920+
}
921+
922+
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Binder<T> {
923+
type Lifted = Binder<T::Lifted>;
924+
fn lift_to_tcx(&self, tcx: &ctxt<'tcx>) -> Option<Self::Lifted> {
925+
tcx.lift(&self.0).map(|x| Binder(x))
926+
}
805927
}
806928

807929
pub mod tls {

0 commit comments

Comments
 (0)