Skip to content

Commit f3ac509

Browse files
committed
remove ErasedRegions from substitutions
This hack has long since outlived its usefulness; the transition to trans passing around full substitutions is basically done. Instead of `ErasedRegions`, just supply substitutions with a suitable number of `'static` entries, and invoke `erase_regions` when needed (the latter of which we already do).
1 parent c743390 commit f3ac509

32 files changed

+168
-299
lines changed

src/librustc/middle/subst.rs

+28-98
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// Type substitutions.
1212

1313
pub use self::ParamSpace::*;
14-
pub use self::RegionSubsts::*;
1514

1615
use middle::cstore;
1716
use middle::def_id::DefId;
@@ -34,24 +33,15 @@ use syntax::codemap::{Span, DUMMY_SP};
3433
#[derive(Clone, PartialEq, Eq, Hash)]
3534
pub struct Substs<'tcx> {
3635
pub types: VecPerParamSpace<Ty<'tcx>>,
37-
pub regions: RegionSubsts,
38-
}
39-
40-
/// Represents the values to use when substituting lifetime parameters.
41-
/// If the value is `ErasedRegions`, then this subst is occurring during
42-
/// trans, and all region parameters will be replaced with `ty::ReStatic`.
43-
#[derive(Clone, PartialEq, Eq, Hash)]
44-
pub enum RegionSubsts {
45-
ErasedRegions,
46-
NonerasedRegions(VecPerParamSpace<ty::Region>)
36+
pub regions: VecPerParamSpace<ty::Region>,
4737
}
4838

4939
impl<'tcx> Substs<'tcx> {
5040
pub fn new(t: VecPerParamSpace<Ty<'tcx>>,
5141
r: VecPerParamSpace<ty::Region>)
5242
-> Substs<'tcx>
5343
{
54-
Substs { types: t, regions: NonerasedRegions(r) }
44+
Substs { types: t, regions: r }
5545
}
5646

5747
pub fn new_type(t: Vec<Ty<'tcx>>,
@@ -71,32 +61,15 @@ impl<'tcx> Substs<'tcx> {
7161
VecPerParamSpace::new(r, Vec::new(), Vec::new()))
7262
}
7363

74-
pub fn erased(t: VecPerParamSpace<Ty<'tcx>>) -> Substs<'tcx>
75-
{
76-
Substs { types: t, regions: ErasedRegions }
77-
}
78-
7964
pub fn empty() -> Substs<'tcx> {
8065
Substs {
8166
types: VecPerParamSpace::empty(),
82-
regions: NonerasedRegions(VecPerParamSpace::empty()),
83-
}
84-
}
85-
86-
pub fn trans_empty() -> Substs<'tcx> {
87-
Substs {
88-
types: VecPerParamSpace::empty(),
89-
regions: ErasedRegions
67+
regions: VecPerParamSpace::empty(),
9068
}
9169
}
9270

9371
pub fn is_noop(&self) -> bool {
94-
let regions_is_noop = match self.regions {
95-
ErasedRegions => false, // may be used to canonicalize
96-
NonerasedRegions(ref regions) => regions.is_empty(),
97-
};
98-
99-
regions_is_noop && self.types.is_empty()
72+
self.regions.is_empty() && self.types.is_empty()
10073
}
10174

10275
pub fn type_for_def(&self, ty_param_def: &ty::TypeParameterDef) -> Ty<'tcx> {
@@ -115,26 +88,9 @@ impl<'tcx> Substs<'tcx> {
11588
}
11689

11790
pub fn erase_regions(self) -> Substs<'tcx> {
118-
let Substs { types, regions: _ } = self;
119-
Substs { types: types, regions: ErasedRegions }
120-
}
121-
122-
/// Since ErasedRegions are only to be used in trans, most of the compiler can use this method
123-
/// to easily access the set of region substitutions.
124-
pub fn regions<'a>(&'a self) -> &'a VecPerParamSpace<ty::Region> {
125-
match self.regions {
126-
ErasedRegions => panic!("Erased regions only expected in trans"),
127-
NonerasedRegions(ref r) => r
128-
}
129-
}
130-
131-
/// Since ErasedRegions are only to be used in trans, most of the compiler can use this method
132-
/// to easily access the set of region substitutions.
133-
pub fn mut_regions<'a>(&'a mut self) -> &'a mut VecPerParamSpace<ty::Region> {
134-
match self.regions {
135-
ErasedRegions => panic!("Erased regions only expected in trans"),
136-
NonerasedRegions(ref mut r) => r
137-
}
91+
let Substs { types, regions } = self;
92+
let regions = regions.map(|_| ty::ReStatic);
93+
Substs { types: types, regions: regions }
13894
}
13995

14096
pub fn with_method(self,
@@ -144,7 +100,7 @@ impl<'tcx> Substs<'tcx> {
144100
{
145101
let Substs { types, regions } = self;
146102
let types = types.with_slice(FnSpace, &m_types);
147-
let regions = regions.map(|r| r.with_slice(FnSpace, &m_regions));
103+
let regions = regions.with_slice(FnSpace, &m_regions);
148104
Substs { types: types, regions: regions }
149105
}
150106

@@ -154,27 +110,23 @@ impl<'tcx> Substs<'tcx> {
154110
{
155111
let Substs { types, regions } = self.clone();
156112
let types = types.with_slice(FnSpace, meth_substs.types.get_slice(FnSpace));
157-
let regions = regions.map(|r| {
158-
r.with_slice(FnSpace, meth_substs.regions().get_slice(FnSpace))
159-
});
113+
let regions = regions.with_slice(FnSpace, meth_substs.regions.get_slice(FnSpace));
160114
Substs { types: types, regions: regions }
161115
}
162116

163117
pub fn with_method_from_subst(self, other: &Substs<'tcx>) -> Substs<'tcx> {
164118
let Substs { types, regions } = self;
165119
let types = types.with_slice(FnSpace, other.types.get_slice(FnSpace));
166-
let regions = regions.map(|r| {
167-
r.with_slice(FnSpace, other.regions().get_slice(FnSpace))
168-
});
120+
let regions = regions.with_slice(FnSpace, other.regions.get_slice(FnSpace));
169121
Substs { types: types, regions: regions }
170122
}
171123

172124
/// Creates a trait-ref out of this substs, ignoring the FnSpace substs
173125
pub fn to_trait_ref(&self, tcx: &TyCtxt<'tcx>, trait_id: DefId)
174126
-> ty::TraitRef<'tcx> {
175-
let Substs { mut types, regions } = self.clone();
127+
let Substs { mut types, mut regions } = self.clone();
176128
types.truncate(FnSpace, 0);
177-
let regions = regions.map(|mut r| { r.truncate(FnSpace, 0); r });
129+
regions.truncate(FnSpace, 0);
178130

179131
ty::TraitRef {
180132
def_id: trait_id,
@@ -212,24 +164,6 @@ impl<'tcx> Decodable for &'tcx Substs<'tcx> {
212164
}
213165
}
214166

215-
impl RegionSubsts {
216-
pub fn map<F>(self, op: F) -> RegionSubsts where
217-
F: FnOnce(VecPerParamSpace<ty::Region>) -> VecPerParamSpace<ty::Region>,
218-
{
219-
match self {
220-
ErasedRegions => ErasedRegions,
221-
NonerasedRegions(r) => NonerasedRegions(op(r))
222-
}
223-
}
224-
225-
pub fn is_erased(&self) -> bool {
226-
match *self {
227-
ErasedRegions => true,
228-
NonerasedRegions(_) => false,
229-
}
230-
}
231-
}
232-
233167
///////////////////////////////////////////////////////////////////////////
234168
// ParamSpace
235169

@@ -664,26 +598,22 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
664598
// the specialized routine `ty::replace_late_regions()`.
665599
match r {
666600
ty::ReEarlyBound(data) => {
667-
match self.substs.regions {
668-
ErasedRegions => ty::ReStatic,
669-
NonerasedRegions(ref regions) =>
670-
match regions.opt_get(data.space, data.index as usize) {
671-
Some(&r) => {
672-
self.shift_region_through_binders(r)
673-
}
674-
None => {
675-
let span = self.span.unwrap_or(DUMMY_SP);
676-
self.tcx().sess.span_bug(
677-
span,
678-
&format!("Type parameter out of range \
679-
when substituting in region {} (root type={:?}) \
680-
(space={:?}, index={})",
681-
data.name,
682-
self.root_ty,
683-
data.space,
684-
data.index));
685-
}
686-
}
601+
match self.substs.regions.opt_get(data.space, data.index as usize) {
602+
Some(&r) => {
603+
self.shift_region_through_binders(r)
604+
}
605+
None => {
606+
let span = self.span.unwrap_or(DUMMY_SP);
607+
self.tcx().sess.span_bug(
608+
span,
609+
&format!("Region parameter out of range \
610+
when substituting in region {} (root type={:?}) \
611+
(space={:?}, index={})",
612+
data.name,
613+
self.root_ty,
614+
data.space,
615+
data.index));
616+
}
687617
}
688618
}
689619
_ => r

src/librustc/middle/traits/specialize/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,6 @@ pub fn translate_substs<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
102102
specialization_graph::Node::Trait(..) => source_trait_ref.substs.clone(),
103103
};
104104

105-
// retain erasure mode
106-
// NB: this must happen before inheriting method generics below
107-
let target_substs = if source_substs.regions.is_erased() {
108-
target_substs.erase_regions()
109-
} else {
110-
target_substs
111-
};
112-
113105
// directly inherent the method generics, since those do not vary across impls
114106
infcx.tcx.mk_substs(target_substs.with_method_from_subst(source_substs))
115107
}

src/librustc/middle/ty/flags.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,8 @@ impl FlagComputation {
194194

195195
fn add_substs(&mut self, substs: &subst::Substs) {
196196
self.add_tys(substs.types.as_slice());
197-
match substs.regions {
198-
subst::ErasedRegions => {}
199-
subst::NonerasedRegions(ref regions) => {
200-
for &r in regions {
201-
self.add_region(r);
202-
}
203-
}
197+
for &r in &substs.regions {
198+
self.add_region(r);
204199
}
205200
}
206201

src/librustc/middle/ty/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ impl<'tcx> TyCtxt<'tcx> {
532532
fn fold_substs(&mut self,
533533
substs: &subst::Substs<'tcx>)
534534
-> subst::Substs<'tcx> {
535-
subst::Substs { regions: subst::ErasedRegions,
535+
subst::Substs { regions: substs.regions.fold_with(self),
536536
types: substs.types.fold_with(self) }
537537
}
538538
}

src/librustc/middle/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,7 @@ impl<'tcx> TyCtxt<'tcx> {
26052605

26062606
Substs {
26072607
types: types,
2608-
regions: subst::NonerasedRegions(regions)
2608+
regions: regions,
26092609
}
26102610
}
26112611

src/librustc/middle/ty/relate.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! type equality, etc.
1515
1616
use middle::def_id::DefId;
17-
use middle::subst::{ErasedRegions, NonerasedRegions, ParamSpace, Substs};
17+
use middle::subst::{ParamSpace, Substs};
1818
use middle::ty::{self, Ty, TyCtxt, TypeFoldable};
1919
use middle::ty::error::{ExpectedFound, TypeError};
2020
use std::rc::Rc;
@@ -156,23 +156,15 @@ pub fn relate_substs<'a,'tcx:'a,R>(relation: &mut R,
156156
substs.types.replace(space, tps);
157157
}
158158

159-
match (&a_subst.regions, &b_subst.regions) {
160-
(&ErasedRegions, _) | (_, &ErasedRegions) => {
161-
substs.regions = ErasedRegions;
162-
}
163-
164-
(&NonerasedRegions(ref a), &NonerasedRegions(ref b)) => {
165-
for &space in &ParamSpace::all() {
166-
let a_regions = a.get_slice(space);
167-
let b_regions = b.get_slice(space);
168-
let r_variances = variances.map(|v| v.regions.get_slice(space));
169-
let regions = relate_region_params(relation,
170-
r_variances,
171-
a_regions,
172-
b_regions)?;
173-
substs.mut_regions().replace(space, regions);
174-
}
175-
}
159+
for &space in &ParamSpace::all() {
160+
let a_regions = a_subst.regions.get_slice(space);
161+
let b_regions = b_subst.regions.get_slice(space);
162+
let r_variances = variances.map(|v| v.regions.get_slice(space));
163+
let regions = relate_region_params(relation,
164+
r_variances,
165+
a_regions,
166+
b_regions)?;
167+
substs.regions.replace(space, regions);
176168
}
177169

178170
Ok(substs)

src/librustc/middle/ty/structural_impls.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::Region {
487487

488488
impl<'tcx> TypeFoldable<'tcx> for subst::Substs<'tcx> {
489489
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
490-
let regions = match self.regions {
491-
subst::ErasedRegions => subst::ErasedRegions,
492-
subst::NonerasedRegions(ref regions) => {
493-
subst::NonerasedRegions(regions.fold_with(folder))
494-
}
495-
};
496-
497-
subst::Substs { regions: regions,
490+
subst::Substs { regions: self.regions.fold_with(folder),
498491
types: self.types.fold_with(folder) }
499492
}
500493

@@ -503,10 +496,7 @@ impl<'tcx> TypeFoldable<'tcx> for subst::Substs<'tcx> {
503496
}
504497

505498
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
506-
self.types.visit_with(visitor) || match self.regions {
507-
subst::ErasedRegions => false,
508-
subst::NonerasedRegions(ref regions) => regions.visit_with(visitor),
509-
}
499+
self.types.visit_with(visitor) || self.regions.visit_with(visitor)
510500
}
511501
}
512502

src/librustc/middle/ty/sty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1204,18 +1204,18 @@ impl<'tcx> TyS<'tcx> {
12041204
TyTrait(ref obj) => {
12051205
let mut v = vec![obj.bounds.region_bound];
12061206
v.extend_from_slice(obj.principal.skip_binder()
1207-
.substs.regions().as_slice());
1207+
.substs.regions.as_slice());
12081208
v
12091209
}
12101210
TyEnum(_, substs) |
12111211
TyStruct(_, substs) => {
1212-
substs.regions().as_slice().to_vec()
1212+
substs.regions.as_slice().to_vec()
12131213
}
12141214
TyClosure(_, ref substs) => {
1215-
substs.func_substs.regions().as_slice().to_vec()
1215+
substs.func_substs.regions.as_slice().to_vec()
12161216
}
12171217
TyProjection(ref data) => {
1218-
data.trait_ref.substs.regions().as_slice().to_vec()
1218+
data.trait_ref.substs.regions.as_slice().to_vec()
12191219
}
12201220
TyFnDef(..) |
12211221
TyFnPtr(_) |

0 commit comments

Comments
 (0)