Skip to content

Commit 4c74056

Browse files
authored
Rollup merge of rust-lang#61554 - spastorino:change_visit_api, r=oli-obk
Change visit api r? @oli-obk In the [first commit](rust-lang@37386d3) of this PR, I'm changing `visit_place` to be the function that traverses the `Place` and have only that responsibility. Then there are two other functions `visit_place_base` and `visit_projection` which are the ones in charge of visiting the base and the projection. Visitor implementors can implement any of those. In the [second commit](rust-lang@e786f63) we can already see some things that confuses me, which I think this division will make more clear. The old code, first checked if the place was a base, did something with it and then called `super_place` [here](rust-lang@e786f63#diff-d583e4efe1a72516e274158e53223633L678). `super_place` checks again if it's a base [here](https://github.com/rust-lang/rust/blob/master/src/librustc/mir/visit.rs#L679-L684) and in case is a local, visits the local and stuff like that. That's not very obvious on the code, and if I'm not wrong it's not needed. In this PR or we have [this](rust-lang@e786f63#diff-d583e4efe1a72516e274158e53223633R673) as I did or we can just do `- => self.super_place_base(...)` and that will be obvious that I'm letting the default implementation process the base.
2 parents 654854f + 67197e2 commit 4c74056

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

src/librustc/mir/visit.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,17 @@ macro_rules! make_mir_visitor {
151151
self.super_place(place, context, location);
152152
}
153153

154-
fn visit_projection(&mut self,
155-
place: & $($mutability)? Projection<'tcx>,
154+
fn visit_place_base(&mut self,
155+
place_base: & $($mutability)? PlaceBase<'tcx>,
156156
context: PlaceContext,
157157
location: Location) {
158-
self.super_projection(place, context, location);
158+
self.super_place_base(place_base, context, location);
159159
}
160160

161-
fn visit_projection_elem(&mut self,
162-
place: & $($mutability)? PlaceElem<'tcx>,
163-
location: Location) {
164-
self.super_projection_elem(place, location);
161+
fn visit_projection(&mut self,
162+
place: & $($mutability)? Projection<'tcx>,
163+
location: Location) {
164+
self.super_projection(place, location);
165165
}
166166

167167
fn visit_constant(&mut self,
@@ -676,36 +676,40 @@ macro_rules! make_mir_visitor {
676676
context: PlaceContext,
677677
location: Location) {
678678
match place {
679-
Place::Base(PlaceBase::Local(local)) => {
680-
self.visit_local(local, context, location);
681-
}
682-
Place::Base(PlaceBase::Static(box Static { kind: _, ty })) => {
683-
self.visit_ty(& $($mutability)? *ty, TyContext::Location(location));
679+
Place::Base(place_base) => {
680+
self.visit_place_base(place_base, context, location);
684681
}
685682
Place::Projection(proj) => {
686-
self.visit_projection(proj, context, location);
683+
let context = if context.is_mutating_use() {
684+
PlaceContext::MutatingUse(MutatingUseContext::Projection)
685+
} else {
686+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
687+
};
688+
689+
self.visit_place(& $($mutability)? proj.base, context, location);
690+
self.visit_projection(proj, location);
687691
}
688692
}
689693
}
690694

691-
fn super_projection(&mut self,
692-
proj: & $($mutability)? Projection<'tcx>,
695+
fn super_place_base(&mut self,
696+
place_base: & $($mutability)? PlaceBase<'tcx>,
693697
context: PlaceContext,
694698
location: Location) {
695-
let Projection { base, elem } = proj;
696-
let context = if context.is_mutating_use() {
697-
PlaceContext::MutatingUse(MutatingUseContext::Projection)
698-
} else {
699-
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
700-
};
701-
self.visit_place(base, context, location);
702-
self.visit_projection_elem(elem, location);
699+
match place_base {
700+
PlaceBase::Local(local) => {
701+
self.visit_local(local, context, location);
702+
}
703+
PlaceBase::Static(box Static { kind: _, ty }) => {
704+
self.visit_ty(& $($mutability)? *ty, TyContext::Location(location));
705+
}
706+
}
703707
}
704708

705-
fn super_projection_elem(&mut self,
706-
proj: & $($mutability)? PlaceElem<'tcx>,
707-
location: Location) {
708-
match proj {
709+
fn super_projection(&mut self,
710+
proj: & $($mutability)? Projection<'tcx>,
711+
location: Location) {
712+
match & $($mutability)? proj.elem {
709713
ProjectionElem::Deref => {
710714
}
711715
ProjectionElem::Subslice { from: _, to: _ } => {

src/librustc_mir/monomorphize/collector.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind, Instance};
185185
use rustc::ty::print::obsolete::DefPathBasedNames;
186186
use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
187187
use rustc::session::config::EntryFnType;
188-
use rustc::mir::{self, Location, Place, PlaceBase, Promoted, Static, StaticKind};
188+
use rustc::mir::{self, Location, PlaceBase, Promoted, Static, StaticKind};
189189
use rustc::mir::visit::Visitor as MirVisitor;
190190
use rustc::mir::mono::{MonoItem, InstantiationMode};
191191
use rustc::mir::interpret::{Scalar, GlobalId, GlobalAlloc, ErrorHandled};
@@ -655,14 +655,12 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
655655
self.super_terminator_kind(kind, location);
656656
}
657657

658-
fn visit_place(&mut self,
659-
place: &mir::Place<'tcx>,
660-
context: mir::visit::PlaceContext,
661-
location: Location) {
662-
match place {
663-
Place::Base(
664-
PlaceBase::Static(box Static{ kind:StaticKind::Static(def_id), .. })
665-
) => {
658+
fn visit_place_base(&mut self,
659+
place_base: &mir::PlaceBase<'tcx>,
660+
_context: mir::visit::PlaceContext,
661+
location: Location) {
662+
match place_base {
663+
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. }) => {
666664
debug!("visiting static {:?} @ {:?}", def_id, location);
667665

668666
let tcx = self.tcx;
@@ -671,10 +669,13 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
671669
self.output.push(MonoItem::Static(*def_id));
672670
}
673671
}
674-
_ => {}
672+
PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. }) => {
673+
// FIXME: should we handle promoteds here instead of eagerly in collect_neighbours?
674+
}
675+
PlaceBase::Local(_) => {
676+
// Locals have no relevance for collector
677+
}
675678
}
676-
677-
self.super_place(place, context, location);
678679
}
679680
}
680681

0 commit comments

Comments
 (0)