Skip to content

Commit 0f0f254

Browse files
committed
Use erased regions in MIR
1 parent 6724d58 commit 0f0f254

File tree

11 files changed

+63
-110
lines changed

11 files changed

+63
-110
lines changed

src/librustc_mir/interpret/intrinsics/caller_location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3939
let loc_ty = self
4040
.tcx
4141
.type_of(self.tcx.require_lang_item(PanicLocationLangItem, None))
42-
.subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_static.into()].iter()));
42+
.subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_erased.into()].iter()));
4343
let loc_layout = self.layout_of(loc_ty).unwrap();
4444
let location = self.allocate(loc_layout, MemoryKind::CallerLocation);
4545

src/librustc_mir/transform/cleanup_post_borrowck.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements {
3232
fn run_pass(&self, tcx: TyCtxt<'tcx>, _source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
3333
let mut delete = DeleteNonCodegenStatements { tcx };
3434
delete.visit_body(body);
35+
body.user_type_annotations.raw.clear();
3536
}
3637
}
3738

src/librustc_mir/transform/erase_regions.rs

-63
This file was deleted.

src/librustc_mir/transform/generator.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,11 @@ impl MutVisitor<'tcx> for TransformVisitor<'tcx> {
357357
}
358358
}
359359

360-
fn make_generator_state_argument_indirect<'tcx>(
361-
tcx: TyCtxt<'tcx>,
362-
def_id: DefId,
363-
body: &mut BodyAndCache<'tcx>,
364-
) {
360+
fn make_generator_state_argument_indirect<'tcx>(tcx: TyCtxt<'tcx>, body: &mut BodyAndCache<'tcx>) {
365361
let gen_ty = body.local_decls.raw[1].ty;
366362

367-
let region = ty::ReFree(ty::FreeRegion { scope: def_id, bound_region: ty::BoundRegion::BrEnv });
368-
369-
let region = tcx.mk_region(region);
370-
371-
let ref_gen_ty = tcx.mk_ref(region, ty::TypeAndMut { ty: gen_ty, mutbl: hir::Mutability::Mut });
363+
let ref_gen_ty =
364+
tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut { ty: gen_ty, mutbl: Mutability::Mut });
372365

373366
// Replace the by value generator argument
374367
body.local_decls.raw[1].ty = ref_gen_ty;
@@ -874,7 +867,6 @@ fn elaborate_generator_drops<'tcx>(
874867
fn create_generator_drop_shim<'tcx>(
875868
tcx: TyCtxt<'tcx>,
876869
transform: &TransformVisitor<'tcx>,
877-
def_id: DefId,
878870
source: MirSource<'tcx>,
879871
gen_ty: Ty<'tcx>,
880872
body: &mut BodyAndCache<'tcx>,
@@ -912,7 +904,7 @@ fn create_generator_drop_shim<'tcx>(
912904
local_info: LocalInfo::Other,
913905
};
914906

915-
make_generator_state_argument_indirect(tcx, def_id, &mut body);
907+
make_generator_state_argument_indirect(tcx, &mut body);
916908

917909
// Change the generator argument from &mut to *mut
918910
body.local_decls[SELF_ARG] = LocalDecl {
@@ -1047,7 +1039,6 @@ fn can_unwind<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool {
10471039
fn create_generator_resume_function<'tcx>(
10481040
tcx: TyCtxt<'tcx>,
10491041
transform: TransformVisitor<'tcx>,
1050-
def_id: DefId,
10511042
source: MirSource<'tcx>,
10521043
body: &mut BodyAndCache<'tcx>,
10531044
can_return: bool,
@@ -1112,7 +1103,7 @@ fn create_generator_resume_function<'tcx>(
11121103

11131104
insert_switch(body, cases, &transform, TerminatorKind::Unreachable);
11141105

1115-
make_generator_state_argument_indirect(tcx, def_id, body);
1106+
make_generator_state_argument_indirect(tcx, body);
11161107
make_generator_state_argument_pinned(tcx, body);
11171108

11181109
no_landing_pads(tcx, body);
@@ -1332,11 +1323,11 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
13321323

13331324
// Create a copy of our MIR and use it to create the drop shim for the generator
13341325
let drop_shim =
1335-
create_generator_drop_shim(tcx, &transform, def_id, source, gen_ty, body, drop_clean);
1326+
create_generator_drop_shim(tcx, &transform, source, gen_ty, body, drop_clean);
13361327

13371328
body.generator_drop = Some(box drop_shim);
13381329

13391330
// Create the Generator::resume function
1340-
create_generator_resume_function(tcx, transform, def_id, source, body, can_return);
1331+
create_generator_resume_function(tcx, transform, source, body, can_return);
13411332
}
13421333
}

src/librustc_mir/transform/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub mod copy_prop;
2222
pub mod deaggregator;
2323
pub mod dump_mir;
2424
pub mod elaborate_drops;
25-
pub mod erase_regions;
2625
pub mod generator;
2726
pub mod inline;
2827
pub mod instcombine;
@@ -296,8 +295,6 @@ fn run_optimization_passes<'tcx>(
296295
&simplify::SimplifyCfg::new("elaborate-drops"),
297296
// No lifetime analysis based on borrowing can be done from here on out.
298297

299-
// From here on out, regions are gone.
300-
&erase_regions::EraseRegions,
301298
// Optimizations begin.
302299
&unreachable_prop::UnreachablePropagation,
303300
&uninhabited_enum_branching::UninhabitedEnumBranching,
@@ -341,6 +338,9 @@ fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &BodyAndCache<'_> {
341338
let mut body = body.steal();
342339
run_optimization_passes(tcx, &mut body, def_id, None);
343340
body.ensure_predecessors();
341+
342+
debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR");
343+
344344
tcx.arena.alloc(body)
345345
}
346346

@@ -358,5 +358,7 @@ fn promoted_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &IndexVec<Promoted, BodyAndCa
358358
body.ensure_predecessors();
359359
}
360360

361+
debug_assert!(!promoted.has_free_regions(), "Free regions in promoted MIR");
362+
361363
tcx.intern_promoted(promoted)
362364
}

src/librustc_mir/transform/promote_consts.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,13 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
913913
ty,
914914
val: ty::ConstKind::Unevaluated(
915915
def_id,
916-
InternalSubsts::identity_for_item(tcx, def_id),
916+
InternalSubsts::for_item(tcx, def_id, |param, _| {
917+
if let ty::GenericParamDefKind::Lifetime = param.kind {
918+
tcx.lifetimes.re_erased.into()
919+
} else {
920+
tcx.mk_param_from_def(param)
921+
}
922+
}),
917923
Some(promoted_id),
918924
),
919925
}),

src/librustc_mir_build/build/mod.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::middle::lang_items;
66
use rustc::middle::region;
77
use rustc::mir::*;
88
use rustc::ty::subst::Subst;
9-
use rustc::ty::{self, Ty, TyCtxt};
9+
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
1010
use rustc_attr::{self as attr, UnwindAttr};
1111
use rustc_hir as hir;
1212
use rustc_hir::def_id::DefId;
@@ -43,8 +43,7 @@ fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> BodyAndCache<'_> {
4343
..
4444
})
4545
| Node::TraitItem(hir::TraitItem {
46-
kind:
47-
hir::TraitItemKind::Fn(hir::FnSig { decl, .. }, hir::TraitFn::Provided(body_id)),
46+
kind: hir::TraitItemKind::Fn(hir::FnSig { decl, .. }, hir::TraitFn::Provided(body_id)),
4847
..
4948
}) => (*body_id, decl.output.span()),
5049
Node::Item(hir::Item { kind: hir::ItemKind::Static(ty, _, body_id), .. })
@@ -128,12 +127,8 @@ fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> BodyAndCache<'_> {
128127
let ty = if fn_sig.c_variadic && index == fn_sig.inputs().len() {
129128
let va_list_did =
130129
tcx.require_lang_item(lang_items::VaListTypeLangItem, Some(arg.span));
131-
let region = tcx.mk_region(ty::ReScope(region::Scope {
132-
id: body.value.hir_id.local_id,
133-
data: region::ScopeData::CallSite,
134-
}));
135130

136-
tcx.type_of(va_list_did).subst(tcx, &[region.into()])
131+
tcx.type_of(va_list_did).subst(tcx, &[tcx.lifetimes.re_erased.into()])
137132
} else {
138133
fn_sig.inputs()[index]
139134
};
@@ -189,6 +184,20 @@ fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> BodyAndCache<'_> {
189184

190185
let mut body = BodyAndCache::new(body);
191186
body.ensure_predecessors();
187+
188+
// The borrow checker will replace all the regions here with its own
189+
// inference variables. There's no point having non-erased regions here.
190+
// The exception is `body.user_type_annotations`, which is used unmodified
191+
// by borrow checking.
192+
debug_assert!(
193+
!(body.local_decls.has_free_regions()
194+
|| body.basic_blocks().has_free_regions()
195+
|| body.var_debug_info.has_free_regions()
196+
|| body.yield_ty.has_free_regions()),
197+
"Unexpected free regions in MIR: {:?}",
198+
body,
199+
);
200+
192201
body
193202
})
194203
}
@@ -209,7 +218,7 @@ fn liberated_closure_env_ty(
209218
};
210219

211220
let closure_env_ty = tcx.closure_env_ty(closure_def_id, closure_substs).unwrap();
212-
tcx.liberate_late_bound_regions(closure_def_id, &closure_env_ty)
221+
tcx.erase_late_bound_regions(&closure_env_ty)
213222
}
214223

215224
#[derive(Debug, PartialEq, Eq)]

src/test/mir-opt/array-index-is-temporary.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
}
1616

1717
// END RUST SOURCE
18-
// START rustc.main.EraseRegions.after.mir
18+
// START rustc.main.SimplifyCfg-elaborate-drops.after.mir
1919
// bb0: {
2020
// ...
2121
// _4 = &mut _2;
@@ -38,4 +38,4 @@ fn main() {
3838
// ...
3939
// return;
4040
// }
41-
// END rustc.main.EraseRegions.after.mir
41+
// END rustc.main.SimplifyCfg-elaborate-drops.after.mir

src/test/mir-opt/byte_slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ fn main() {
66
}
77

88
// END RUST SOURCE
9-
// START rustc.main.EraseRegions.after.mir
9+
// START rustc.main.SimplifyCfg-elaborate-drops.after.mir
1010
// ...
1111
// _1 = const b"foo";
1212
// ...
1313
// _2 = [const 5u8, const 120u8];
1414
// ...
15-
// END rustc.main.EraseRegions.after.mir
15+
// END rustc.main.SimplifyCfg-elaborate-drops.after.mir

src/test/mir-opt/packed-struct-drop-aligned.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Drop for Droppy {
1515
}
1616

1717
// END RUST SOURCE
18-
// START rustc.main.EraseRegions.before.mir
18+
// START rustc.main.SimplifyCfg-elaborate-drops.after.mir
1919
// fn main() -> () {
2020
// let mut _0: ();
2121
// let mut _1: Packed;
@@ -56,4 +56,4 @@ impl Drop for Droppy {
5656
// drop(_1) -> [return: bb2, unwind: bb1];
5757
// }
5858
// }
59-
// END rustc.main.EraseRegions.before.mir
59+
// END rustc.main.SimplifyCfg-elaborate-drops.after.mir

src/test/mir-opt/retag.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ struct Test(i32);
88

99
impl Test {
1010
// Make sure we run the pass on a method, not just on bare functions.
11-
fn foo<'x>(&self, x: &'x mut i32) -> &'x mut i32 { x }
12-
fn foo_shr<'x>(&self, x: &'x i32) -> &'x i32 { x }
11+
fn foo<'x>(&self, x: &'x mut i32) -> &'x mut i32 {
12+
x
13+
}
14+
fn foo_shr<'x>(&self, x: &'x i32) -> &'x i32 {
15+
x
16+
}
1317
}
1418

1519
impl Drop for Test {
@@ -27,7 +31,10 @@ fn main() {
2731
}
2832

2933
// Also test closures
30-
let c: fn(&i32) -> &i32 = |x: &i32| -> &i32 { let _y = x; x };
34+
let c: fn(&i32) -> &i32 = |x: &i32| -> &i32 {
35+
let _y = x;
36+
x
37+
};
3138
let _w = c(&x);
3239

3340
// need to call `foo_shr` or it doesn't even get generated
@@ -38,7 +45,7 @@ fn main() {
3845
}
3946

4047
// END RUST SOURCE
41-
// START rustc.{{impl}}-foo.EraseRegions.after.mir
48+
// START rustc.{{impl}}-foo.SimplifyCfg-elaborate-drops.after.mir
4249
// bb0: {
4350
// Retag([fn entry] _1);
4451
// Retag([fn entry] _2);
@@ -48,8 +55,8 @@ fn main() {
4855
// ...
4956
// return;
5057
// }
51-
// END rustc.{{impl}}-foo.EraseRegions.after.mir
52-
// START rustc.{{impl}}-foo_shr.EraseRegions.after.mir
58+
// END rustc.{{impl}}-foo.SimplifyCfg-elaborate-drops.after.mir
59+
// START rustc.{{impl}}-foo_shr.SimplifyCfg-elaborate-drops.after.mir
5360
// bb0: {
5461
// Retag([fn entry] _1);
5562
// Retag([fn entry] _2);
@@ -59,8 +66,8 @@ fn main() {
5966
// ...
6067
// return;
6168
// }
62-
// END rustc.{{impl}}-foo_shr.EraseRegions.after.mir
63-
// START rustc.main.EraseRegions.after.mir
69+
// END rustc.{{impl}}-foo_shr.SimplifyCfg-elaborate-drops.after.mir
70+
// START rustc.main.SimplifyCfg-elaborate-drops.after.mir
6471
// fn main() -> () {
6572
// ...
6673
// bb0: {
@@ -96,8 +103,8 @@ fn main() {
96103
//
97104
// ...
98105
// }
99-
// END rustc.main.EraseRegions.after.mir
100-
// START rustc.main-{{closure}}.EraseRegions.after.mir
106+
// END rustc.main.SimplifyCfg-elaborate-drops.after.mir
107+
// START rustc.main-{{closure}}.SimplifyCfg-elaborate-drops.after.mir
101108
// fn main::{{closure}}#0(_1: &[closure@main::{{closure}}#0], _2: &i32) -> &i32 {
102109
// ...
103110
// bb0: {
@@ -112,7 +119,7 @@ fn main() {
112119
// return;
113120
// }
114121
// }
115-
// END rustc.main-{{closure}}.EraseRegions.after.mir
122+
// END rustc.main-{{closure}}.SimplifyCfg-elaborate-drops.after.mir
116123
// START rustc.ptr-drop_in_place.Test.SimplifyCfg-make_shim.after.mir
117124
// fn std::intrinsics::drop_in_place(_1: *mut Test) -> () {
118125
// ...

0 commit comments

Comments
 (0)