Skip to content

Commit 93c5ebb

Browse files
csmoespastorino
authored andcommitted
convert old Place to NeoPlace
1 parent 96b641a commit 93c5ebb

File tree

2 files changed

+138
-3
lines changed

2 files changed

+138
-3
lines changed

src/librustc/mir/mod.rs

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,14 +1907,47 @@ pub enum Place<'tcx> {
19071907
}
19081908

19091909
/// A new Place repr
1910-
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1910+
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
19111911
pub struct NeoPlace<'tcx> {
19121912
base: PlaceBase<'tcx>,
19131913
elems: &'tcx List<PlaceElem<'tcx>>,
19141914
}
19151915

19161916
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx List<PlaceElem<'tcx>> {}
19171917

1918+
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1919+
pub fn as_new_place(self, place: &Place<'tcx>) -> NeoPlace<'tcx> {
1920+
let mut elems: Vec<PlaceElem<'tcx>> = Vec::new();
1921+
let mut p = place;
1922+
1923+
while let Place::Projection(proj) = p {
1924+
elems.push(proj.elem);
1925+
p = &proj.base;
1926+
}
1927+
1928+
elems.reverse();
1929+
1930+
match p.clone() {
1931+
Place::Projection(_) => unreachable!(),
1932+
1933+
Place::Local(local) => NeoPlace {
1934+
base: PlaceBase::Local(local),
1935+
elems: self.mk_place_elems(elems.iter()),
1936+
},
1937+
1938+
Place::Static(static_) => NeoPlace {
1939+
base: PlaceBase::Static(static_),
1940+
elems: self.mk_place_elems(elems.iter()),
1941+
},
1942+
1943+
Place::Promoted(promoted) => NeoPlace {
1944+
base: PlaceBase::Promoted(promoted),
1945+
elems: self.mk_place_elems(elems.iter()),
1946+
},
1947+
}
1948+
}
1949+
}
1950+
19181951
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
19191952
pub enum PlaceBase<'tcx> {
19201953
/// local variable
@@ -1950,7 +1983,7 @@ pub struct Projection<'tcx, B, V, T> {
19501983
pub elem: ProjectionElem<'tcx, V, T>,
19511984
}
19521985

1953-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
1986+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
19541987
pub enum ProjectionElem<'tcx, V, T> {
19551988
Deref,
19561989
Field(Field, T),
@@ -2103,6 +2136,23 @@ impl<'tcx> Debug for Place<'tcx> {
21032136
}
21042137
}
21052138

2139+
impl<'tcx> Debug for PlaceBase<'tcx> {
2140+
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
2141+
use self::PlaceBase::*;
2142+
2143+
match self {
2144+
Local(id) => write!(fmt, "{:?}", *id),
2145+
Static(box self::Static { def_id, ty }) => write!(
2146+
fmt,
2147+
"({}: {:?})",
2148+
ty::tls::with(|tcx| tcx.item_path_str(*def_id)),
2149+
ty
2150+
),
2151+
Promoted(promoted) => write!(fmt, "({:?}: {:?})", promoted.0, promoted.1),
2152+
}
2153+
}
2154+
}
2155+
21062156
///////////////////////////////////////////////////////////////////////////
21072157
// Scopes
21082158

@@ -3324,6 +3374,41 @@ impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
33243374
}
33253375
}
33263376

3377+
impl<'tcx> TypeFoldable<'tcx> for NeoPlace<'tcx> {
3378+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
3379+
NeoPlace {
3380+
base: self.base.fold_with(folder),
3381+
elems: self.elems.fold_with(folder),
3382+
}
3383+
}
3384+
3385+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3386+
self.base.visit_with(visitor) ||
3387+
self.elems.visit_with(visitor)
3388+
}
3389+
}
3390+
3391+
impl<'tcx> TypeFoldable<'tcx> for PlaceBase<'tcx> {
3392+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
3393+
self.clone()
3394+
}
3395+
3396+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
3397+
false
3398+
}
3399+
}
3400+
3401+
impl<'tcx> TypeFoldable<'tcx> for &'tcx List<PlaceElem<'tcx>> {
3402+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
3403+
let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
3404+
folder.tcx().intern_place_elems(&v)
3405+
}
3406+
3407+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
3408+
self.iter().any(|p| p.visit_with(visitor))
3409+
}
3410+
}
3411+
33273412
impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
33283413
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
33293414
use mir::Rvalue::*;
@@ -3444,6 +3529,31 @@ where
34443529
}
34453530
}
34463531

3532+
impl<'tcx, V, T> TypeFoldable<'tcx> for ProjectionElem<'tcx, V, T>
3533+
where
3534+
V: TypeFoldable<'tcx>,
3535+
T: TypeFoldable<'tcx>,
3536+
{
3537+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
3538+
use self::ProjectionElem::*;
3539+
match self {
3540+
Deref => Deref,
3541+
Field(f, ty) => Field(*f, ty.fold_with(folder)),
3542+
Index(v) => Index(v.fold_with(folder)),
3543+
elem => elem.clone(),
3544+
}
3545+
}
3546+
3547+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
3548+
use self::ProjectionElem::*;
3549+
match self {
3550+
Field(_, ty) => ty.visit_with(visitor),
3551+
Index(v) => v.visit_with(visitor),
3552+
_ => false,
3553+
}
3554+
}
3555+
}
3556+
34473557
impl<'tcx> TypeFoldable<'tcx> for Field {
34483558
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _: &mut F) -> Self {
34493559
*self

src/librustc/ty/context.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use middle::cstore::EncodedMetadata;
2121
use middle::lang_items;
2222
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
2323
use middle::stability;
24-
use mir::{self, Mir, interpret, ProjectionKind};
24+
use mir::{self, Mir, interpret, ProjectionKind, PlaceElem};
2525
use mir::interpret::Allocation;
2626
use ty::subst::{Kind, Substs, Subst};
2727
use ty::ReprOptions;
@@ -123,6 +123,7 @@ pub struct CtxtInterners<'tcx> {
123123
region: InternedSet<'tcx, RegionKind>,
124124
existential_predicates: InternedSet<'tcx, List<ExistentialPredicate<'tcx>>>,
125125
predicates: InternedSet<'tcx, List<Predicate<'tcx>>>,
126+
place_elems: InternedSet<'tcx, List<PlaceElem<'tcx>>>,
126127
clauses: InternedSet<'tcx, List<Clause<'tcx>>>,
127128
goal: InternedSet<'tcx, GoalKind<'tcx>>,
128129
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
@@ -140,6 +141,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
140141
existential_predicates: Default::default(),
141142
canonical_var_infos: Default::default(),
142143
predicates: Default::default(),
144+
place_elems: Default::default(),
143145
clauses: Default::default(),
144146
goal: Default::default(),
145147
goal_list: Default::default(),
@@ -1794,6 +1796,7 @@ nop_list_lift!{Clause<'a> => Clause<'tcx>}
17941796
nop_list_lift!{Ty<'a> => Ty<'tcx>}
17951797
nop_list_lift!{ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
17961798
nop_list_lift!{Predicate<'a> => Predicate<'tcx>}
1799+
nop_list_lift!{PlaceElem<'a> => PlaceElem<'tcx>}
17971800
nop_list_lift!{CanonicalVarInfo => CanonicalVarInfo}
17981801
nop_list_lift!{ProjectionKind<'a> => ProjectionKind<'tcx>}
17991802

@@ -2279,6 +2282,13 @@ impl<'tcx: 'lcx, 'lcx> Borrow<[Predicate<'lcx>]>
22792282
}
22802283
}
22812284

2285+
impl<'tcx: 'lcx, 'lcx> Borrow<[PlaceElem<'lcx>]>
2286+
for Interned<'tcx, List<PlaceElem<'tcx>>> {
2287+
fn borrow<'a>(&'a self) -> &'a [PlaceElem<'lcx>] {
2288+
&self.0[..]
2289+
}
2290+
}
2291+
22822292
impl<'tcx: 'lcx, 'lcx> Borrow<Const<'lcx>> for Interned<'tcx, Const<'tcx>> {
22832293
fn borrow<'a>(&'a self) -> &'a Const<'lcx> {
22842294
&self.0
@@ -2387,6 +2397,7 @@ macro_rules! slice_interners {
23872397
slice_interners!(
23882398
existential_predicates: _intern_existential_predicates(ExistentialPredicate),
23892399
predicates: _intern_predicates(Predicate),
2400+
place_elems: _intern_place_elems(PlaceElem),
23902401
type_list: _intern_type_list(Ty),
23912402
substs: _intern_substs(Kind),
23922403
clauses: _intern_clauses(Clause),
@@ -2711,6 +2722,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27112722
}
27122723
}
27132724

2725+
pub fn intern_place_elems(self, place_elems: &[PlaceElem<'tcx>])
2726+
-> &'tcx List<PlaceElem<'tcx>> {
2727+
if place_elems.is_empty() {
2728+
List::empty()
2729+
} else {
2730+
self._intern_place_elems(place_elems)
2731+
}
2732+
}
2733+
27142734
pub fn intern_type_list(self, ts: &[Ty<'tcx>]) -> &'tcx List<Ty<'tcx>> {
27152735
if ts.len() == 0 {
27162736
List::empty()
@@ -2787,6 +2807,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27872807
iter.intern_with(|xs| self.intern_predicates(xs))
27882808
}
27892809

2810+
pub fn mk_place_elems<I: InternAs<[PlaceElem<'tcx>],
2811+
&'tcx List<PlaceElem<'tcx>>>>(self, iter: I) -> I::Output {
2812+
iter.intern_with(|xs| self.intern_place_elems(xs))
2813+
}
2814+
27902815
pub fn mk_type_list<I: InternAs<[Ty<'tcx>],
27912816
&'tcx List<Ty<'tcx>>>>(self, iter: I) -> I::Output {
27922817
iter.intern_with(|xs| self.intern_type_list(xs))

0 commit comments

Comments
 (0)