Skip to content

Commit a53d674

Browse files
committed
Create a struct to represent early-bound regions
1 parent a691f1e commit a53d674

File tree

10 files changed

+79
-49
lines changed

10 files changed

+79
-49
lines changed

src/librustc/metadata/tydecode.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ fn parse_region_<F>(st: &mut PState, conv: &mut F) -> ty::Region where
341341
let index = parse_u32(st);
342342
assert_eq!(next(st), '|');
343343
let nm = token::str_to_ident(&parse_str(st, ']'));
344-
ty::ReEarlyBound(node_id, space, index, nm.name)
344+
ty::ReEarlyBound(ty::EarlyBoundRegion {
345+
param_id: node_id,
346+
space: space,
347+
index: index,
348+
name: nm.name
349+
})
345350
}
346351
'f' => {
347352
assert_eq!(next(st), '[');

src/librustc/metadata/tyencode.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ pub fn enc_region(w: &mut Encoder, cx: &ctxt, r: ty::Region) {
241241
enc_bound_region(w, cx, br);
242242
mywrite!(w, "]");
243243
}
244-
ty::ReEarlyBound(node_id, space, index, name) => {
244+
ty::ReEarlyBound(ref data) => {
245245
mywrite!(w, "B[{}|{}|{}|{}]",
246-
node_id,
247-
space.to_uint(),
248-
index,
249-
token::get_name(name));
246+
data.param_id,
247+
data.space.to_uint(),
248+
data.index,
249+
token::get_name(data.name));
250250
}
251251
ty::ReFree(ref fr) => {
252252
mywrite!(w, "f[");

src/librustc/middle/astencode.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,13 @@ impl tr for ty::Region {
496496
ty::ReLateBound(debruijn, br) => {
497497
ty::ReLateBound(debruijn, br.tr(dcx))
498498
}
499-
ty::ReEarlyBound(id, space, index, ident) => {
500-
ty::ReEarlyBound(dcx.tr_id(id), space, index, ident)
499+
ty::ReEarlyBound(data) => {
500+
ty::ReEarlyBound(ty::EarlyBoundRegion {
501+
param_id: dcx.tr_id(data.param_id),
502+
space: data.space,
503+
index: data.index,
504+
name: data.name,
505+
})
501506
}
502507
ty::ReScope(scope) => {
503508
ty::ReScope(scope.tr(dcx))

src/librustc/middle/region.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,11 @@ impl RegionMaps {
603603
self.sub_free_region(sub_fr, super_fr)
604604
}
605605

606-
(ty::ReEarlyBound(param_id_a, param_space_a, index_a, _),
607-
ty::ReEarlyBound(param_id_b, param_space_b, index_b, _)) => {
606+
(ty::ReEarlyBound(data_a), ty::ReEarlyBound(data_b)) => {
608607
// This case is used only to make sure that explicitly-
609608
// specified `Self` types match the real self type in
610-
// implementations.
611-
param_id_a == param_id_b &&
612-
param_space_a == param_space_b &&
613-
index_a == index_b
609+
// implementations. Yuck.
610+
data_a == data_b
614611
}
615612

616613
_ => {

src/librustc/middle/subst.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
622622
// regions that appear in a function signature is done using
623623
// the specialized routine `ty::replace_late_regions()`.
624624
match r {
625-
ty::ReEarlyBound(_, space, i, region_name) => {
625+
ty::ReEarlyBound(data) => {
626626
match self.substs.regions {
627627
ErasedRegions => ty::ReStatic,
628628
NonerasedRegions(ref regions) =>
629-
match regions.opt_get(space, i as usize) {
629+
match regions.opt_get(data.space, data.index as usize) {
630630
Some(&r) => {
631631
self.shift_region_through_binders(r)
632632
}
@@ -635,11 +635,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
635635
self.tcx().sess.span_bug(
636636
span,
637637
&format!("Type parameter out of range \
638-
when substituting in region {} (root type={}) \
639-
(space={:?}, index={})",
640-
region_name.as_str(),
641-
self.root_ty.repr(self.tcx()),
642-
space, i));
638+
when substituting in region {} (root type={}) \
639+
(space={:?}, index={})",
640+
data.name.as_str(),
641+
self.root_ty.repr(self.tcx()),
642+
data.space,
643+
data.index));
643644
}
644645
}
645646
}

src/librustc/middle/ty.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,7 @@ pub enum Region {
11341134
// Region bound in a type or fn declaration which will be
11351135
// substituted 'early' -- that is, at the same time when type
11361136
// parameters are substituted.
1137-
ReEarlyBound(/* param id */ ast::NodeId,
1138-
subst::ParamSpace,
1139-
/*index*/ u32,
1140-
ast::Name),
1137+
ReEarlyBound(EarlyBoundRegion),
11411138

11421139
// Region bound in a function scope, which will be substituted when the
11431140
// function is called.
@@ -1169,6 +1166,14 @@ pub enum Region {
11691166
ReEmpty,
11701167
}
11711168

1169+
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
1170+
pub struct EarlyBoundRegion {
1171+
pub param_id: ast::NodeId,
1172+
pub space: subst::ParamSpace,
1173+
pub index: u32,
1174+
pub name: ast::Name,
1175+
}
1176+
11721177
/// Upvars do not get their own node-id. Instead, we use the pair of
11731178
/// the original var id (that is, the root variable that is referenced
11741179
/// by the upvar) and the id of the closure expression.
@@ -1761,7 +1766,12 @@ pub struct RegionParameterDef {
17611766

17621767
impl RegionParameterDef {
17631768
pub fn to_early_bound_region(&self) -> ty::Region {
1764-
ty::ReEarlyBound(self.def_id.node, self.space, self.index, self.name)
1769+
ty::ReEarlyBound(ty::EarlyBoundRegion {
1770+
param_id: self.def_id.node,
1771+
space: self.space,
1772+
index: self.index,
1773+
name: self.name,
1774+
})
17651775
}
17661776
pub fn to_bound_region(&self) -> ty::BoundRegion {
17671777
ty::BoundRegion::BrNamed(self.def_id, self.name)
@@ -7071,8 +7081,7 @@ pub fn make_substs_for_receiver_types<'tcx>(tcx: &ty::ctxt<'tcx>,
70717081
let meth_regions: Vec<ty::Region> =
70727082
method.generics.regions.get_slice(subst::FnSpace)
70737083
.iter()
7074-
.map(|def| ty::ReEarlyBound(def.def_id.node, def.space,
7075-
def.index, def.name))
7084+
.map(|def| def.to_early_bound_region())
70767085
.collect();
70777086
trait_ref.substs.clone().with_method(meth_tps, meth_regions)
70787087
}

src/librustc/util/ppaux.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
163163

164164
ReEmpty => { ("the empty lifetime".to_string(), None) }
165165

166-
ReEarlyBound(_, _, _, name) => {
167-
(format!("{}", token::get_name(name)), None)
166+
ReEarlyBound(ref data) => {
167+
(format!("{}", token::get_name(data.name)), None)
168168
}
169169

170170
// I believe these cases should not occur (except when debugging,
@@ -223,8 +223,8 @@ pub fn region_to_string(cx: &ctxt, prefix: &str, space: bool, region: Region) ->
223223
// `explain_region()` or `note_and_explain_region()`.
224224
match region {
225225
ty::ReScope(_) => prefix.to_string(),
226-
ty::ReEarlyBound(_, _, _, name) => {
227-
token::get_name(name).to_string()
226+
ty::ReEarlyBound(ref data) => {
227+
token::get_name(data.name).to_string()
228228
}
229229
ty::ReLateBound(_, br) => bound_region_to_string(cx, prefix, space, br),
230230
ty::ReFree(ref fr) => bound_region_to_string(cx, prefix, space, fr.bound_region),
@@ -899,12 +899,12 @@ impl<'tcx> Repr<'tcx> for ty::BoundRegion {
899899
impl<'tcx> Repr<'tcx> for ty::Region {
900900
fn repr(&self, tcx: &ctxt) -> String {
901901
match *self {
902-
ty::ReEarlyBound(id, space, index, name) => {
902+
ty::ReEarlyBound(ref data) => {
903903
format!("ReEarlyBound({}, {:?}, {}, {})",
904-
id,
905-
space,
906-
index,
907-
token::get_name(name))
904+
data.param_id,
905+
data.space,
906+
data.index,
907+
token::get_name(data.name))
908908
}
909909

910910
ty::ReLateBound(binder_id, ref bound_region) => {

src/librustc_typeck/astconv.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ pub fn ast_region_to_region(tcx: &ty::ctxt, lifetime: &ast::Lifetime)
161161
}
162162

163163
Some(&rl::DefEarlyBoundRegion(space, index, id)) => {
164-
ty::ReEarlyBound(id, space, index, lifetime.name)
164+
ty::ReEarlyBound(ty::EarlyBoundRegion {
165+
param_id: id,
166+
space: space,
167+
index: index,
168+
name: lifetime.name
169+
})
165170
}
166171

167172
Some(&rl::DefFreeRegion(scope, id)) => {

src/librustc_typeck/collect.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -1217,10 +1217,12 @@ fn trait_def_of_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
12171217
generics.lifetimes
12181218
.iter()
12191219
.enumerate()
1220-
.map(|(i, def)| ty::ReEarlyBound(def.lifetime.id,
1221-
TypeSpace,
1222-
i as u32,
1223-
def.lifetime.name))
1220+
.map(|(i, def)| ty::ReEarlyBound(ty::EarlyBoundRegion {
1221+
param_id: def.lifetime.id,
1222+
space: TypeSpace,
1223+
index: i as u32,
1224+
name: def.lifetime.name
1225+
}))
12241226
.collect();
12251227

12261228
// Start with the generics in the type parameters...
@@ -1691,7 +1693,13 @@ fn ty_generic_predicates<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
16911693
let early_lifetimes = early_bound_lifetimes_from_generics(space, ast_generics);
16921694
for (index, param) in early_lifetimes.iter().enumerate() {
16931695
let index = index as u32;
1694-
let region = ty::ReEarlyBound(param.lifetime.id, space, index, param.lifetime.name);
1696+
let region =
1697+
ty::ReEarlyBound(ty::EarlyBoundRegion {
1698+
param_id: param.lifetime.id,
1699+
space: space,
1700+
index: index,
1701+
name: param.lifetime.name
1702+
});
16951703
for bound in &param.bounds {
16961704
let bound_region = ast_region_to_region(ccx.tcx, bound);
16971705
let outlives = ty::Binder(ty::OutlivesPredicate(region, bound_region));
@@ -2168,10 +2176,10 @@ fn check_method_self_type<'a, 'tcx, RS:RegionScope>(
21682176

21692177
ty_fold::fold_regions(tcx, value, |region, _| {
21702178
match region {
2171-
ty::ReEarlyBound(id, _, _, name) => {
2172-
let def_id = local_def(id);
2179+
ty::ReEarlyBound(data) => {
2180+
let def_id = local_def(data.param_id);
21732181
ty::ReFree(ty::FreeRegion { scope: scope,
2174-
bound_region: ty::BrNamed(def_id, name) })
2182+
bound_region: ty::BrNamed(def_id, data.name) })
21752183
}
21762184
_ => region
21772185
}

src/librustc_typeck/variance.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1046,9 +1046,9 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
10461046
region: ty::Region,
10471047
variance: VarianceTermPtr<'a>) {
10481048
match region {
1049-
ty::ReEarlyBound(param_id, _, _, _) => {
1050-
if self.is_to_be_inferred(param_id) {
1051-
let index = self.inferred_index(param_id);
1049+
ty::ReEarlyBound(ref data) => {
1050+
if self.is_to_be_inferred(data.param_id) {
1051+
let index = self.inferred_index(data.param_id);
10521052
self.add_constraint(index, variance);
10531053
}
10541054
}

0 commit comments

Comments
 (0)