Skip to content

Commit f352550

Browse files
committed
Auto merge of #34012 - eddyb:tick-erased, r=nikomatsakis
rustc: add ReErased to be used by trait selection, MIR and trans. `ReErased` replaces `ReStatic` (i.e. `'static`) for erasing regions. Using a distinct lifetime helps prevent accidental mix-ups between the two. It also allows cleaner type printing (see test changes), including in symbol names: ```rust str..pattern..CharSearcher$LT$$u27$static$GT$::drop.30560::h840c2f2afc03bbea // before str..pattern..CharSearcher::drop.30561::h6bd31d2af614377a // after ``` Not that we should be producing symbols this way, but it's still better.
2 parents 763f923 + bcec7a5 commit f352550

File tree

28 files changed

+92
-71
lines changed

28 files changed

+92
-71
lines changed

src/librustc/infer/combine.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,10 @@ impl<'cx, 'gcx, 'tcx> ty::fold::TypeFolder<'gcx, 'tcx> for Generalizer<'cx, 'gcx
337337

338338
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
339339
match r {
340-
// Never make variables for regions bound within the type itself.
341-
ty::ReLateBound(..) => { return r; }
340+
// Never make variables for regions bound within the type itself,
341+
// nor for erased regions.
342+
ty::ReLateBound(..) |
343+
ty::ReErased => { return r; }
342344

343345
// Early-bound regions should really have been substituted away before
344346
// we get to this point.

src/librustc/infer/error_reporting.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
216216
//
217217
// We shouldn't really be having unification failures with ReVar
218218
// and ReLateBound though.
219-
ty::ReSkolemized(..) | ty::ReVar(_) | ty::ReLateBound(..) => {
219+
ty::ReSkolemized(..) |
220+
ty::ReVar(_) |
221+
ty::ReLateBound(..) |
222+
ty::ReErased => {
220223
(format!("lifetime {:?}", region), None)
221224
}
222225
};

src/librustc/infer/freshen.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! error messages or in any other form. Freshening is only really useful as an internal detail.
2424
//!
2525
//! __An important detail concerning regions.__ The freshener also replaces *all* regions with
26-
//! 'static. The reason behind this is that, in general, we do not take region relationships into
26+
//! 'erased. The reason behind this is that, in general, we do not take region relationships into
2727
//! account when making type-overloaded decisions. This is important because of the design of the
2828
//! region inferencer, which is not based on unification but rather on accumulating and then
2929
//! solving a set of constraints. In contrast, the type inferencer assigns a value to each type
@@ -96,9 +96,10 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
9696
ty::ReScope(_) |
9797
ty::ReVar(_) |
9898
ty::ReSkolemized(..) |
99-
ty::ReEmpty => {
100-
// replace all free regions with 'static
101-
ty::ReStatic
99+
ty::ReEmpty |
100+
ty::ReErased => {
101+
// replace all free regions with 'erased
102+
ty::ReErased
102103
}
103104
}
104105
}

src/librustc/infer/region_inference/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_data_structures::unify::{self, UnificationTable};
2525
use middle::free_region::FreeRegionMap;
2626
use ty::{self, Ty, TyCtxt};
2727
use ty::{BoundRegion, Region, RegionVid};
28-
use ty::{ReEmpty, ReStatic, ReFree, ReEarlyBound};
28+
use ty::{ReEmpty, ReStatic, ReFree, ReEarlyBound, ReErased};
2929
use ty::{ReLateBound, ReScope, ReVar, ReSkolemized, BrFresh};
3030

3131
use std::cell::{Cell, RefCell};
@@ -918,8 +918,10 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
918918
(ReLateBound(..), _) |
919919
(_, ReLateBound(..)) |
920920
(ReEarlyBound(..), _) |
921-
(_, ReEarlyBound(..)) => {
922-
bug!("cannot relate bound region: LUB({:?}, {:?})", a, b);
921+
(_, ReEarlyBound(..)) |
922+
(ReErased, _) |
923+
(_, ReErased) => {
924+
bug!("cannot relate region: LUB({:?}, {:?})", a, b);
923925
}
924926

925927
(ReStatic, _) | (_, ReStatic) => {

src/librustc/ty/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl FlagComputation {
187187
}
188188
ty::ReLateBound(debruijn, _) => { self.add_depth(debruijn.depth); }
189189
ty::ReEarlyBound(..) => { self.add_flags(TypeFlags::HAS_RE_EARLY_BOUND); }
190-
ty::ReStatic => {}
190+
ty::ReStatic | ty::ReErased => {}
191191
_ => { self.add_flags(TypeFlags::HAS_FREE_REGIONS); }
192192
}
193193

src/librustc/ty/fold.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
421421
collector.regions
422422
}
423423

424-
/// Replace any late-bound regions bound in `value` with `'static`. Useful in trans but also
424+
/// Replace any late-bound regions bound in `value` with `'erased`. Useful in trans but also
425425
/// method lookup and a few other places where precise region relationships are not required.
426426
pub fn erase_late_bound_regions<T>(self, value: &Binder<T>) -> T
427427
where T : TypeFoldable<'tcx>
428428
{
429-
self.replace_late_bound_regions(value, |_| ty::ReStatic).0
429+
self.replace_late_bound_regions(value, |_| ty::ReErased).0
430430
}
431431

432432
/// Rewrite any late-bound regions so that they are anonymous. Region numbers are
@@ -547,15 +547,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
547547
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
548548
// because late-bound regions affect subtyping, we can't
549549
// erase the bound/free distinction, but we can replace
550-
// all free regions with 'static.
550+
// all free regions with 'erased.
551551
//
552552
// Note that we *CAN* replace early-bound regions -- the
553553
// type system never "sees" those, they get substituted
554-
// away. In trans, they will always be erased to 'static
554+
// away. In trans, they will always be erased to 'erased
555555
// whenever a substitution occurs.
556556
match r {
557557
ty::ReLateBound(..) => r,
558-
_ => ty::ReStatic
558+
_ => ty::ReErased
559559
}
560560
}
561561
}
@@ -651,7 +651,7 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
651651
// does this represent a region that cannot be named
652652
// in a global way? used in fulfillment caching.
653653
match r {
654-
ty::ReStatic | ty::ReEmpty => {}
654+
ty::ReStatic | ty::ReEmpty | ty::ReErased => {}
655655
_ => return true,
656656
}
657657
}

src/librustc/ty/sty.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ pub enum Region {
705705
/// The only way to get an instance of ReEmpty is to have a region
706706
/// variable with no constraints.
707707
ReEmpty,
708+
709+
/// Erased region, used by trait selection, in MIR and during trans.
710+
ReErased,
708711
}
709712

710713
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]

src/librustc/ty/subst.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
8989

9090
pub fn erase_regions(self) -> Substs<'tcx> {
9191
let Substs { types, regions } = self;
92-
let regions = regions.map(|_| ty::ReStatic);
92+
let regions = regions.map(|_| ty::ReErased);
9393
Substs { types: types, regions: regions }
9494
}
9595

src/librustc/ty/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
348348

349349
let region = |state: &mut SipHasher, r: ty::Region| {
350350
match r {
351-
ty::ReStatic => {}
351+
ty::ReStatic | ty::ReErased => {}
352352
ty::ReLateBound(db, ty::BrAnon(i)) => {
353353
db.hash(state);
354354
i.hash(state);

src/librustc/util/ppaux.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -148,28 +148,36 @@ pub fn parameterized<GG>(f: &mut fmt::Formatter,
148148
write!(f, "{}", cont)
149149
}
150150
};
151-
let print_region = |f: &mut fmt::Formatter, region: &ty::Region| -> _ {
152-
if verbose {
153-
write!(f, "{:?}", region)
154-
} else {
155-
let s = region.to_string();
156-
if s.is_empty() {
157-
// This happens when the value of the region
158-
// parameter is not easily serialized. This may be
159-
// because the user omitted it in the first place,
160-
// or because it refers to some block in the code,
161-
// etc. I'm not sure how best to serialize this.
162-
write!(f, "'_")
151+
152+
let print_regions = |f: &mut fmt::Formatter, start: &str, regions: &[ty::Region]| {
153+
// Don't print any regions if they're all erased.
154+
if regions.iter().all(|r| *r == ty::ReErased) {
155+
return Ok(());
156+
}
157+
158+
for region in regions {
159+
start_or_continue(f, start, ", ")?;
160+
if verbose {
161+
write!(f, "{:?}", region)?;
163162
} else {
164-
write!(f, "{}", s)
163+
let s = region.to_string();
164+
if s.is_empty() {
165+
// This happens when the value of the region
166+
// parameter is not easily serialized. This may be
167+
// because the user omitted it in the first place,
168+
// or because it refers to some block in the code,
169+
// etc. I'm not sure how best to serialize this.
170+
write!(f, "'_")?;
171+
} else {
172+
write!(f, "{}", s)?;
173+
}
165174
}
166175
}
176+
177+
Ok(())
167178
};
168179

169-
for region in substs.regions.get_slice(subst::TypeSpace) {
170-
start_or_continue(f, "<", ", ")?;
171-
print_region(f, region)?;
172-
}
180+
print_regions(f, "<", substs.regions.get_slice(subst::TypeSpace))?;
173181

174182
let num_supplied_defaults = if verbose {
175183
0
@@ -211,10 +219,7 @@ pub fn parameterized<GG>(f: &mut fmt::Formatter,
211219
write!(f, "::{}", item_name)?;
212220
}
213221

214-
for region in substs.regions.get_slice(subst::FnSpace) {
215-
start_or_continue(f, "::<", ", ")?;
216-
print_region(f, region)?;
217-
}
222+
print_regions(f, "::<", substs.regions.get_slice(subst::FnSpace))?;
218223

219224
// FIXME: consider being smart with defaults here too
220225
for ty in substs.types.get_slice(subst::FnSpace) {
@@ -536,7 +541,9 @@ impl fmt::Debug for ty::Region {
536541
write!(f, "ReSkolemized({}, {:?})", id.index, bound_region)
537542
}
538543

539-
ty::ReEmpty => write!(f, "ReEmpty")
544+
ty::ReEmpty => write!(f, "ReEmpty"),
545+
546+
ty::ReErased => write!(f, "ReErased")
540547
}
541548
}
542549
}
@@ -600,7 +607,8 @@ impl fmt::Display for ty::Region {
600607
write!(f, "{}", br)
601608
}
602609
ty::ReScope(_) |
603-
ty::ReVar(_) => Ok(()),
610+
ty::ReVar(_) |
611+
ty::ReErased => Ok(()),
604612
ty::ReStatic => write!(f, "'static"),
605613
ty::ReEmpty => write!(f, "'<empty>"),
606614
}

0 commit comments

Comments
 (0)