Skip to content

Commit 2b2e35b

Browse files
committed
Prepare def_use MutVisitor to have projections interned
1 parent 39c9ed3 commit 2b2e35b

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/librustc_mir/util/def_use.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Def-use analysis.
22
3-
use rustc::mir::{Local, Location, Body};
3+
use rustc::mir::{Body, Local, Location, Place, PlaceElem};
44
use rustc::mir::visit::{PlaceContext, MutVisitor, Visitor};
55
use rustc_index::vec::IndexVec;
66
use std::mem;
@@ -47,13 +47,13 @@ impl DefUseAnalysis {
4747
&self.info[local]
4848
}
4949

50-
fn mutate_defs_and_uses<F>(&self, local: Local, body: &mut Body<'_>, mut callback: F)
51-
where F: for<'a> FnMut(&'a mut Local,
50+
fn mutate_defs_and_uses<F>(&self, local: Local, body: &mut Body<'_>, callback: F)
51+
where F: for<'a> Fn(&'a Local,
5252
PlaceContext,
53-
Location) {
53+
Location) -> Local {
5454
for place_use in &self.info[local].defs_and_uses {
5555
MutateUseVisitor::new(local,
56-
&mut callback,
56+
&callback,
5757
body).visit_location(body, place_use.location)
5858
}
5959
}
@@ -63,7 +63,7 @@ impl DefUseAnalysis {
6363
local: Local,
6464
body: &mut Body<'_>,
6565
new_local: Local) {
66-
self.mutate_defs_and_uses(local, body, |local, _, _| *local = new_local)
66+
self.mutate_defs_and_uses(local, body, |_, _, _| new_local)
6767
}
6868
}
6969

@@ -125,7 +125,7 @@ struct MutateUseVisitor<F> {
125125
impl<F> MutateUseVisitor<F> {
126126
fn new(query: Local, callback: F, _: &Body<'_>)
127127
-> MutateUseVisitor<F>
128-
where F: for<'a> FnMut(&'a mut Local, PlaceContext, Location) {
128+
where F: for<'a> Fn(&'a Local, PlaceContext, Location) -> Local {
129129
MutateUseVisitor {
130130
query,
131131
callback,
@@ -134,13 +134,31 @@ impl<F> MutateUseVisitor<F> {
134134
}
135135

136136
impl<F> MutVisitor<'_> for MutateUseVisitor<F>
137-
where F: for<'a> FnMut(&'a mut Local, PlaceContext, Location) {
137+
where F: for<'a> Fn(&'a Local, PlaceContext, Location) -> Local {
138138
fn visit_local(&mut self,
139139
local: &mut Local,
140140
context: PlaceContext,
141141
location: Location) {
142142
if *local == self.query {
143-
(self.callback)(local, context, location)
143+
*local = (self.callback)(local, context, location)
144144
}
145145
}
146+
147+
fn visit_place(&mut self,
148+
place: &mut Place<'tcx>,
149+
context: PlaceContext,
150+
location: Location) {
151+
self.visit_place_base(&mut place.base, context, location);
152+
153+
let new_projection: Vec<_> = place.projection.iter().map(|elem|
154+
match elem {
155+
PlaceElem::Index(local) if *local == self.query => {
156+
PlaceElem::Index((self.callback)(&local, context, location))
157+
}
158+
_ => elem.clone(),
159+
}
160+
).collect();
161+
162+
place.projection = new_projection.into_boxed_slice();
163+
}
146164
}

0 commit comments

Comments
 (0)