Skip to content

Commit

Permalink
Setup a different visit place set of methods for mutable and immutabl…
Browse files Browse the repository at this point in the history
…e visitors

In particular, use a blank visit_place for mutable visitor to be sure,
non modified visitors are not trying to mutating place.
  • Loading branch information
spastorino committed Oct 18, 2019
1 parent 4f2a110 commit 7fa3425
Showing 1 changed file with 103 additions and 77 deletions.
180 changes: 103 additions & 77 deletions src/librustc/mir/visit.rs
Expand Up @@ -158,22 +158,7 @@ macro_rules! make_mir_visitor {
self.super_place_base(base, context, location);
}

fn visit_projection(&mut self,
base: & $($mutability)? PlaceBase<'tcx>,
projection: & $($mutability)? [PlaceElem<'tcx>],
context: PlaceContext,
location: Location) {
self.super_projection(base, projection, context, location);
}

fn visit_projection_elem(&mut self,
base: & $($mutability)? PlaceBase<'tcx>,
proj_base: & $($mutability)? [PlaceElem<'tcx>],
elem: & $($mutability)? PlaceElem<'tcx>,
context: PlaceContext,
location: Location) {
self.super_projection_elem(base, proj_base, elem, context, location);
}
visit_place_fns!($($mutability)?);

fn visit_constant(&mut self,
constant: & $($mutability)? Constant<'tcx>,
Expand Down Expand Up @@ -681,28 +666,6 @@ macro_rules! make_mir_visitor {
);
}

fn super_place(&mut self,
place: & $($mutability)? Place<'tcx>,
context: PlaceContext,
location: Location) {
let mut context = context;

if !place.projection.is_empty() {
context = if context.is_mutating_use() {
PlaceContext::MutatingUse(MutatingUseContext::Projection)
} else {
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
};
}

self.visit_place_base(& $($mutability)? place.base, context, location);

self.visit_projection(& $($mutability)? place.base,
& $($mutability)? place.projection,
context,
location);
}

fn super_place_base(&mut self,
place_base: & $($mutability)? PlaceBase<'tcx>,
context: PlaceContext,
Expand All @@ -717,45 +680,6 @@ macro_rules! make_mir_visitor {
}
}

fn super_projection(&mut self,
base: & $($mutability)? PlaceBase<'tcx>,
projection: & $($mutability)? [PlaceElem<'tcx>],
context: PlaceContext,
location: Location) {
let mut cursor = projection;
while let [proj_base @ .., elem] = cursor {
cursor = proj_base;
self.visit_projection_elem(base, cursor, elem, context, location);
}
}

fn super_projection_elem(&mut self,
_base: & $($mutability)? PlaceBase<'tcx>,
_proj_base: & $($mutability)? [PlaceElem<'tcx>],
elem: & $($mutability)? PlaceElem<'tcx>,
_context: PlaceContext,
location: Location) {
match elem {
ProjectionElem::Field(_field, ty) => {
self.visit_ty(ty, TyContext::Location(location));
}
ProjectionElem::Index(local) => {
self.visit_local(
local,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
location
);
}
ProjectionElem::Deref |
ProjectionElem::Subslice { from: _, to: _ } |
ProjectionElem::ConstantIndex { offset: _,
min_length: _,
from_end: _ } |
ProjectionElem::Downcast(_, _) => {
}
}
}

fn super_local_decl(&mut self,
local: Local,
local_decl: & $($mutability)? LocalDecl<'tcx>) {
Expand Down Expand Up @@ -858,6 +782,108 @@ macro_rules! make_mir_visitor {
}
}

macro_rules! visit_place_fns {
(mut) => (
fn super_place(
&mut self,
_place: &mut Place<'tcx>,
_context: PlaceContext,
_location: Location,
) {
}
);

() => (
fn visit_projection(
&mut self,
base: &PlaceBase<'tcx>,
projection: &[PlaceElem<'tcx>],
context: PlaceContext,
location: Location,
) {
self.super_projection(base, projection, context, location);
}

fn visit_projection_elem(
&mut self,
base: &PlaceBase<'tcx>,
proj_base: &[PlaceElem<'tcx>],
elem: &PlaceElem<'tcx>,
context: PlaceContext,
location: Location,
) {
self.super_projection_elem(base, proj_base, elem, context, location);
}

fn super_place(
&mut self,
place: &Place<'tcx>,
context: PlaceContext,
location: Location,
) {
let mut context = context;

if !place.projection.is_empty() {
context = if context.is_mutating_use() {
PlaceContext::MutatingUse(MutatingUseContext::Projection)
} else {
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
};
}

self.visit_place_base(&place.base, context, location);

self.visit_projection(&place.base,
&place.projection,
context,
location);
}

fn super_projection(
&mut self,
base: &PlaceBase<'tcx>,
projection: &[PlaceElem<'tcx>],
context: PlaceContext,
location: Location,
) {
let mut cursor = projection;
while let [proj_base @ .., elem] = cursor {
cursor = proj_base;
self.visit_projection_elem(base, cursor, elem, context, location);
}
}

fn super_projection_elem(
&mut self,
_base: &PlaceBase<'tcx>,
_proj_base: &[PlaceElem<'tcx>],
elem: &PlaceElem<'tcx>,
_context: PlaceContext,
location: Location,
) {
match elem {
ProjectionElem::Field(_field, ty) => {
self.visit_ty(ty, TyContext::Location(location));
}
ProjectionElem::Index(local) => {
self.visit_local(
local,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
location
);
}
ProjectionElem::Deref |
ProjectionElem::Subslice { from: _, to: _ } |
ProjectionElem::ConstantIndex { offset: _,
min_length: _,
from_end: _ } |
ProjectionElem::Downcast(_, _) => {
}
}
}
);
}

make_mir_visitor!(Visitor,);
make_mir_visitor!(MutVisitor,mut);

Expand Down

0 comments on commit 7fa3425

Please sign in to comment.