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

+4-2
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

+4-1
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

+5-4
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

+5-3
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

+1-1
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

+6-6
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

+3
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

+1-1
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

+1-1
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

+31-23
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
}

src/librustc_borrowck/borrowck/gather_loans/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
369369
ty::ReLateBound(..) |
370370
ty::ReEarlyBound(..) |
371371
ty::ReVar(..) |
372-
ty::ReSkolemized(..) => {
372+
ty::ReSkolemized(..) |
373+
ty::ReErased => {
373374
span_bug!(
374375
cmt.span,
375376
"invalid borrow lifetime: {:?}",

src/librustc_metadata/tydecode.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,9 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
221221
assert_eq!(self.next(), '|');
222222
ty::ReScope(scope)
223223
}
224-
't' => {
225-
ty::ReStatic
226-
}
227-
'e' => {
228-
ty::ReStatic
229-
}
224+
't' => ty::ReStatic,
225+
'e' => ty::ReEmpty,
226+
'E' => ty::ReErased,
230227
_ => bug!("parse_region: bad input")
231228
}
232229
}

src/librustc_metadata/tyencode.rs

+3
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ pub fn enc_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, r: ty::Region) {
283283
ty::ReEmpty => {
284284
write!(w, "e");
285285
}
286+
ty::ReErased => {
287+
write!(w, "E");
288+
}
286289
ty::ReVar(_) | ty::ReSkolemized(..) => {
287290
// these should not crop up after typeck
288291
bug!("cannot encode region variables");

src/librustc_trans/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ fn bind_subslice_pat(bcx: Block,
731731
let slice_begin = InBoundsGEP(bcx, base, &[C_uint(bcx.ccx(), offset_left)]);
732732
let slice_len_offset = C_uint(bcx.ccx(), offset_left + offset_right);
733733
let slice_len = Sub(bcx, len, slice_len_offset, DebugLoc::None);
734-
let slice_ty = bcx.tcx().mk_imm_ref(bcx.tcx().mk_region(ty::ReStatic),
734+
let slice_ty = bcx.tcx().mk_imm_ref(bcx.tcx().mk_region(ty::ReErased),
735735
bcx.tcx().mk_slice(unit_ty));
736736
let scratch = rvalue_scratch_datum(bcx, slice_ty, "");
737737
Store(bcx, slice_begin, expr::get_dataptr(bcx, scratch.val));

src/librustc_trans/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
328328
};
329329

330330
let bare_fn_ty_maybe_ref = if is_by_ref {
331-
tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), bare_fn_ty)
331+
tcx.mk_imm_ref(tcx.mk_region(ty::ReErased), bare_fn_ty)
332332
} else {
333333
bare_fn_ty
334334
};

src/librustc_trans/closure.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ fn get_self_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
123123
-> Ty<'tcx> {
124124
match tcx.closure_kind(closure_id) {
125125
ty::ClosureKind::Fn => {
126-
tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), fn_ty)
126+
tcx.mk_imm_ref(tcx.mk_region(ty::ReErased), fn_ty)
127127
}
128128
ty::ClosureKind::FnMut => {
129-
tcx.mk_mut_ref(tcx.mk_region(ty::ReStatic), fn_ty)
129+
tcx.mk_mut_ref(tcx.mk_region(ty::ReErased), fn_ty)
130130
}
131131
ty::ClosureKind::FnOnce => fn_ty,
132132
}
@@ -344,7 +344,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
344344
// Find a version of the closure type. Substitute static for the
345345
// region since it doesn't really matter.
346346
let closure_ty = tcx.mk_closure_from_closure_substs(closure_def_id, substs);
347-
let ref_closure_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), closure_ty);
347+
let ref_closure_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReErased), closure_ty);
348348

349349
// Make a version with the type of by-ref closure.
350350
let ty::ClosureTy { unsafety, abi, mut sig } =

src/librustc_trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
382382
// Don't copy data to do a deref+ref
383383
// (i.e., skip the last auto-deref).
384384
llconst = addr_of(cx, llconst, type_of::align_of(cx, ty), "autoref");
385-
ty = cx.tcx().mk_imm_ref(cx.tcx().mk_region(ty::ReStatic), ty);
385+
ty = cx.tcx().mk_imm_ref(cx.tcx().mk_region(ty::ReErased), ty);
386386
}
387387
} else if adj.autoderefs > 0 {
388388
let (dv, dt) = const_deref(cx, llconst, ty);

src/librustc_trans/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
495495
assert!(scheme.generics.types.is_empty());
496496
self.tcx().mk_substs(
497497
Substs::new(VecPerParamSpace::empty(),
498-
scheme.generics.regions.map(|_| ty::ReStatic)))
498+
scheme.generics.regions.map(|_| ty::ReErased)))
499499
}
500500

501501
pub fn symbol_hasher(&self) -> &RefCell<Sha256> {

src/librustc_trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ fn auto_ref<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
19781978
// Compute final type. Note that we are loose with the region and
19791979
// mutability, since those things don't matter in trans.
19801980
let referent_ty = lv_datum.ty;
1981-
let ptr_ty = bcx.tcx().mk_imm_ref(bcx.tcx().mk_region(ty::ReStatic), referent_ty);
1981+
let ptr_ty = bcx.tcx().mk_imm_ref(bcx.tcx().mk_region(ty::ReErased), referent_ty);
19821982

19831983
// Construct the resulting datum. The right datum to return here would be an Lvalue datum,
19841984
// because there is cleanup scheduled and the datum doesn't own the data, but for thin pointers

src/librustc_trans/meth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ pub fn get_vtable_methods<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
271271
// the method may have some early-bound lifetimes, add
272272
// regions for those
273273
let num_dummy_regions = trait_method_type.generics.regions.len(FnSpace);
274-
let dummy_regions = vec![ty::ReStatic; num_dummy_regions];
274+
let dummy_regions = vec![ty::ReErased; num_dummy_regions];
275275
let method_substs = substs.clone()
276276
.with_method(vec![], dummy_regions);
277277
let method_substs = tcx.mk_substs(method_substs);

src/librustc_trans/mir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
715715
let tr_lvalue = self.const_lvalue(lvalue, span)?;
716716

717717
let ty = tr_lvalue.ty;
718-
let ref_ty = tcx.mk_ref(tcx.mk_region(ty::ReStatic),
718+
let ref_ty = tcx.mk_ref(tcx.mk_region(ty::ReErased),
719719
ty::TypeAndMut { ty: ty, mutbl: bk.to_mutbl_lossy() });
720720

721721
let base = match tr_lvalue.base {

src/librustc_trans/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
373373

374374
let ty = tr_lvalue.ty.to_ty(bcx.tcx());
375375
let ref_ty = bcx.tcx().mk_ref(
376-
bcx.tcx().mk_region(ty::ReStatic),
376+
bcx.tcx().mk_region(ty::ReErased),
377377
ty::TypeAndMut { ty: ty, mutbl: bk.to_mutbl_lossy() }
378378
);
379379

src/librustc_trans/monomorphize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
180180
impl<'tcx> Instance<'tcx> {
181181
pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
182182
-> Instance<'tcx> {
183-
assert!(substs.regions.iter().all(|&r| r == ty::ReStatic));
183+
assert!(substs.regions.iter().all(|&r| r == ty::ReErased));
184184
Instance { def: def_id, substs: substs }
185185
}
186186
pub fn mono<'a>(scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId) -> Instance<'tcx> {

0 commit comments

Comments
 (0)