Skip to content

Commit 6ec12a0

Browse files
committed
Add process_* place hooks to improve code reutilization
1 parent 040ef45 commit 6ec12a0

File tree

7 files changed

+82
-97
lines changed

7 files changed

+82
-97
lines changed

src/librustc/mir/visit.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -786,10 +786,32 @@ macro_rules! visit_place_fns {
786786
(mut) => (
787787
fn super_place(
788788
&mut self,
789-
_place: &mut Place<'tcx>,
790-
_context: PlaceContext,
791-
_location: Location,
789+
place: &mut Place<'tcx>,
790+
context: PlaceContext,
791+
location: Location,
792792
) {
793+
self.visit_place_base(&mut place.base, context, location);
794+
795+
place.projection = self.process_projection(&place.projection);
796+
}
797+
798+
fn process_projection(
799+
&mut self,
800+
projection: &Box<[PlaceElem<'tcx>]>,
801+
) -> Box<[PlaceElem<'tcx>]> {
802+
let new_projection: Vec<_> = projection.iter().map(|elem|
803+
self.process_projection_elem(elem)
804+
).collect();
805+
806+
new_projection.into_boxed_slice()
807+
}
808+
809+
fn process_projection_elem(
810+
&mut self,
811+
elem: &PlaceElem<'tcx>,
812+
) -> PlaceElem<'tcx> {
813+
// FIXME: avoid cloning here
814+
elem.clone()
793815
}
794816
);
795817

src/librustc_mir/borrow_check/nll/renumber.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::ty::subst::SubstsRef;
22
use rustc::ty::{self, Ty, TypeFoldable};
3-
use rustc::mir::{Body, Location, Place, PlaceElem, Promoted};
4-
use rustc::mir::visit::{MutVisitor, PlaceContext, TyContext};
3+
use rustc::mir::{Body, Location, PlaceElem, Promoted};
4+
use rustc::mir::visit::{MutVisitor, TyContext};
55
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
66
use rustc_index::vec::IndexVec;
77

@@ -62,23 +62,15 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'tcx> {
6262
debug!("visit_ty: ty={:?}", ty);
6363
}
6464

65-
fn visit_place(
65+
fn process_projection_elem(
6666
&mut self,
67-
place: &mut Place<'tcx>,
68-
context: PlaceContext,
69-
location: Location,
70-
) {
71-
self.visit_place_base(&mut place.base, context, location);
72-
73-
let new_projection: Vec<_> = place.projection.iter().map(|elem|
74-
if let PlaceElem::Field(field, ty) = elem {
75-
PlaceElem::Field(*field, self.renumber_regions(ty))
76-
} else {
77-
elem.clone()
78-
}
79-
).collect();
80-
81-
place.projection = new_projection.into_boxed_slice();
67+
elem: &PlaceElem<'tcx>,
68+
) -> PlaceElem<'tcx> {
69+
if let PlaceElem::Field(field, ty) = elem {
70+
PlaceElem::Field(*field, self.renumber_regions(ty))
71+
} else {
72+
elem.clone()
73+
}
8274
}
8375

8476
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {

src/librustc_mir/transform/erase_regions.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use rustc::ty::subst::SubstsRef;
88
use rustc::ty::{self, Ty, TyCtxt};
99
use rustc::mir::*;
10-
use rustc::mir::visit::{MutVisitor, PlaceContext, TyContext};
10+
use rustc::mir::visit::{MutVisitor, TyContext};
1111
use crate::transform::{MirPass, MirSource};
1212

1313
struct EraseRegionsVisitor<'tcx> {
@@ -39,23 +39,15 @@ impl MutVisitor<'tcx> for EraseRegionsVisitor<'tcx> {
3939
*substs = self.tcx.erase_regions(substs);
4040
}
4141

42-
fn visit_place(
42+
fn process_projection_elem(
4343
&mut self,
44-
place: &mut Place<'tcx>,
45-
context: PlaceContext,
46-
location: Location,
47-
) {
48-
self.visit_place_base(&mut place.base, context, location);
49-
50-
let new_projection: Vec<_> = place.projection.iter().map(|elem|
51-
if let PlaceElem::Field(field, ty) = elem {
52-
PlaceElem::Field(*field, self.tcx.erase_regions(ty))
53-
} else {
54-
elem.clone()
55-
}
56-
).collect();
57-
58-
place.projection = new_projection.into_boxed_slice();
44+
elem: &PlaceElem<'tcx>,
45+
) -> PlaceElem<'tcx> {
46+
if let PlaceElem::Field(field, ty) = elem {
47+
PlaceElem::Field(*field, self.tcx.erase_regions(ty))
48+
} else {
49+
elem.clone()
50+
}
5951
}
6052
}
6153

src/librustc_mir/transform/inline.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -695,21 +695,22 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
695695
*place = self.destination.clone();
696696
},
697697
_ => {
698-
self.visit_place_base(&mut place.base, context, location);
699-
700-
let new_projection: Vec<_> = place.projection.iter().map(|elem|
701-
if let PlaceElem::Index(local) = elem {
702-
PlaceElem::Index(self.make_integrate_local(local))
703-
} else {
704-
elem.clone()
705-
}
706-
).collect();
707-
708-
place.projection = new_projection.into_boxed_slice();
698+
self.super_place(place, context, location);
709699
}
710700
}
711701
}
712702

703+
fn process_projection_elem(
704+
&mut self,
705+
elem: &PlaceElem<'tcx>,
706+
) -> PlaceElem<'tcx> {
707+
if let PlaceElem::Index(local) = elem {
708+
PlaceElem::Index(self.make_integrate_local(local))
709+
} else {
710+
elem.clone()
711+
}
712+
}
713+
713714
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
714715
self.in_cleanup_block = data.is_cleanup;
715716
self.super_basic_block_data(block, data);

src/librustc_mir/transform/promote_consts.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -405,24 +405,16 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
405405
}
406406
}
407407

408-
fn visit_place(
408+
fn process_projection_elem(
409409
&mut self,
410-
place: &mut Place<'tcx>,
411-
context: PlaceContext,
412-
location: Location,
413-
) {
414-
self.visit_place_base(&mut place.base, context, location);
415-
416-
let new_projection: Vec<_> = place.projection.iter().map(|elem|
417-
match elem {
418-
PlaceElem::Index(local) if self.is_temp_kind(*local) => {
419-
PlaceElem::Index(self.promote_temp(*local))
420-
}
421-
_ => elem.clone(),
410+
elem: &PlaceElem<'tcx>,
411+
) -> PlaceElem<'tcx> {
412+
match elem {
413+
PlaceElem::Index(local) if self.is_temp_kind(*local) => {
414+
PlaceElem::Index(self.promote_temp(*local))
422415
}
423-
).collect();
424-
425-
place.projection = new_projection.into_boxed_slice();
416+
_ => elem.clone(),
417+
}
426418
}
427419
}
428420

src/librustc_mir/transform/simplify.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,14 @@ impl<'tcx> MutVisitor<'tcx> for LocalUpdater {
371371
*l = self.map[*l].unwrap();
372372
}
373373

374-
fn visit_place(
374+
fn process_projection_elem(
375375
&mut self,
376-
place: &mut Place<'tcx>,
377-
context: PlaceContext,
378-
location: Location,
379-
) {
380-
self.visit_place_base(&mut place.base, context, location);
381-
382-
let new_projection: Vec<_> = place.projection.iter().map(|elem|
383-
if let PlaceElem::Index(local) = elem {
384-
PlaceElem::Index(self.map[*local].unwrap())
385-
} else {
386-
elem.clone()
387-
}
388-
).collect();
389-
390-
place.projection = new_projection.into_boxed_slice();
376+
elem: &PlaceElem<'tcx>,
377+
) -> PlaceElem<'tcx> {
378+
if let PlaceElem::Index(local) = elem {
379+
PlaceElem::Index(self.map[*local].unwrap())
380+
} else {
381+
elem.clone()
382+
}
391383
}
392384
}

src/librustc_mir/util/def_use.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Def-use analysis.
22
3-
use rustc::mir::{Body, Local, Location, Place, PlaceElem};
3+
use rustc::mir::{Body, Local, Location, PlaceElem};
44
use rustc::mir::visit::{PlaceContext, MutVisitor, Visitor};
55
use rustc_index::vec::IndexVec;
66
use std::mem;
@@ -138,21 +138,15 @@ impl MutVisitor<'_> for MutateUseVisitor {
138138
}
139139
}
140140

141-
fn visit_place(&mut self,
142-
place: &mut Place<'tcx>,
143-
context: PlaceContext,
144-
location: Location) {
145-
self.visit_place_base(&mut place.base, context, location);
146-
147-
let new_projection: Vec<_> = place.projection.iter().map(|elem|
148-
match elem {
149-
PlaceElem::Index(local) if *local == self.query => {
150-
PlaceElem::Index(self.new_local)
151-
}
152-
_ => elem.clone(),
141+
fn process_projection_elem(
142+
&mut self,
143+
elem: &PlaceElem<'tcx>,
144+
) -> PlaceElem<'tcx> {
145+
match elem {
146+
PlaceElem::Index(local) if *local == self.query => {
147+
PlaceElem::Index(self.new_local)
153148
}
154-
).collect();
155-
156-
place.projection = new_projection.into_boxed_slice();
149+
_ => elem.clone(),
150+
}
157151
}
158152
}

0 commit comments

Comments
 (0)