Skip to content

Commit c076da0

Browse files
authored
Rollup merge of #71013 - jonas-schievink:visit-projection, r=eddyb
Pass the `PlaceElem::Index` local to `visit_local` Fixes #71008 cc @rust-lang/wg-mir-opt r? @spastorino
2 parents 6947dec + 72ae73a commit c076da0

File tree

7 files changed

+39
-74
lines changed

7 files changed

+39
-74
lines changed

src/librustc_middle/mir/visit.rs

+33-26
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ macro_rules! make_mir_visitor {
838838
}
839839

840840
macro_rules! visit_place_fns {
841-
(mut) => (
841+
(mut) => {
842842
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
843843

844844
fn super_place(
@@ -849,20 +849,21 @@ macro_rules! visit_place_fns {
849849
) {
850850
self.visit_place_base(&mut place.local, context, location);
851851

852-
if let Some(new_projection) = self.process_projection(&place.projection) {
852+
if let Some(new_projection) = self.process_projection(&place.projection, location) {
853853
place.projection = self.tcx().intern_place_elems(&new_projection);
854854
}
855855
}
856856

857857
fn process_projection(
858858
&mut self,
859859
projection: &'a [PlaceElem<'tcx>],
860+
location: Location,
860861
) -> Option<Vec<PlaceElem<'tcx>>> {
861862
let mut projection = Cow::Borrowed(projection);
862863

863864
for i in 0..projection.len() {
864865
if let Some(elem) = projection.get(i) {
865-
if let Some(elem) = self.process_projection_elem(elem) {
866+
if let Some(elem) = self.process_projection_elem(elem, location) {
866867
// This converts the borrowed projection into `Cow::Owned(_)` and returns a
867868
// clone of the projection so we can mutate and reintern later.
868869
let vec = projection.to_mut();
@@ -879,13 +880,30 @@ macro_rules! visit_place_fns {
879880

880881
fn process_projection_elem(
881882
&mut self,
882-
_elem: &PlaceElem<'tcx>,
883+
elem: &PlaceElem<'tcx>,
884+
location: Location,
883885
) -> Option<PlaceElem<'tcx>> {
884-
None
886+
match elem {
887+
PlaceElem::Index(local) => {
888+
let mut new_local = *local;
889+
self.visit_local(
890+
&mut new_local,
891+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
892+
location,
893+
);
894+
895+
if new_local == *local { None } else { Some(PlaceElem::Index(new_local)) }
896+
}
897+
PlaceElem::Deref
898+
| PlaceElem::Field(..)
899+
| PlaceElem::ConstantIndex { .. }
900+
| PlaceElem::Subslice { .. }
901+
| PlaceElem::Downcast(..) => None,
902+
}
885903
}
886-
);
904+
};
887905

888-
() => (
906+
() => {
889907
fn visit_projection(
890908
&mut self,
891909
local: Local,
@@ -907,12 +925,7 @@ macro_rules! visit_place_fns {
907925
self.super_projection_elem(local, proj_base, elem, context, location);
908926
}
909927

910-
fn super_place(
911-
&mut self,
912-
place: &Place<'tcx>,
913-
context: PlaceContext,
914-
location: Location,
915-
) {
928+
fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
916929
let mut context = context;
917930

918931
if !place.projection.is_empty() {
@@ -925,10 +938,7 @@ macro_rules! visit_place_fns {
925938

926939
self.visit_place_base(&place.local, context, location);
927940

928-
self.visit_projection(place.local,
929-
&place.projection,
930-
context,
931-
location);
941+
self.visit_projection(place.local, &place.projection, context, location);
932942
}
933943

934944
fn super_projection(
@@ -961,19 +971,16 @@ macro_rules! visit_place_fns {
961971
self.visit_local(
962972
local,
963973
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
964-
location
974+
location,
965975
);
966976
}
967-
ProjectionElem::Deref |
968-
ProjectionElem::Subslice { from: _, to: _, from_end: _ } |
969-
ProjectionElem::ConstantIndex { offset: _,
970-
min_length: _,
971-
from_end: _ } |
972-
ProjectionElem::Downcast(_, _) => {
973-
}
977+
ProjectionElem::Deref
978+
| ProjectionElem::Subslice { from: _, to: _, from_end: _ }
979+
| ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
980+
| ProjectionElem::Downcast(_, _) => {}
974981
}
975982
}
976-
);
983+
};
977984
}
978985

979986
make_mir_visitor!(Visitor,);

src/librustc_mir/borrow_check/renumber.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'tcx> {
6464
debug!("visit_ty: ty={:?}", ty);
6565
}
6666

67-
fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
67+
fn process_projection_elem(
68+
&mut self,
69+
elem: &PlaceElem<'tcx>,
70+
_: Location,
71+
) -> Option<PlaceElem<'tcx>> {
6872
if let PlaceElem::Field(field, ty) = elem {
6973
let new_ty = self.renumber_regions(ty);
7074

src/librustc_mir/transform/generator.rs

-7
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor<'tcx> {
8989
*local = self.to;
9090
}
9191
}
92-
93-
fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
94-
match elem {
95-
PlaceElem::Index(local) if *local == self.from => Some(PlaceElem::Index(self.to)),
96-
_ => None,
97-
}
98-
}
9992
}
10093

10194
struct DerefArgVisitor<'tcx> {

src/librustc_mir/transform/inline.rs

-12
Original file line numberDiff line numberDiff line change
@@ -706,18 +706,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
706706
self.super_place(place, context, location)
707707
}
708708

709-
fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
710-
if let PlaceElem::Index(local) = elem {
711-
let new_local = self.make_integrate_local(*local);
712-
713-
if new_local != *local {
714-
return Some(PlaceElem::Index(new_local));
715-
}
716-
}
717-
718-
None
719-
}
720-
721709
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
722710
self.in_cleanup_block = data.is_cleanup;
723711
self.super_basic_block_data(block, data);

src/librustc_mir/transform/promote_consts.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1036,15 +1036,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
10361036
*local = self.promote_temp(*local);
10371037
}
10381038
}
1039-
1040-
fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
1041-
match elem {
1042-
PlaceElem::Index(local) if self.is_temp_kind(*local) => {
1043-
Some(PlaceElem::Index(self.promote_temp(*local)))
1044-
}
1045-
_ => None,
1046-
}
1047-
}
10481039
}
10491040

10501041
pub fn promote_candidates<'tcx>(

src/librustc_mir/transform/simplify.rs

-7
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,4 @@ impl<'tcx> MutVisitor<'tcx> for LocalUpdater<'tcx> {
417417
fn visit_local(&mut self, l: &mut Local, _: PlaceContext, _: Location) {
418418
*l = self.map[*l].unwrap();
419419
}
420-
421-
fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
422-
match elem {
423-
PlaceElem::Index(local) => Some(PlaceElem::Index(self.map[*local].unwrap())),
424-
_ => None,
425-
}
426-
}
427420
}

src/librustc_mir/util/def_use.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
33
use rustc_index::vec::IndexVec;
44
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
5-
use rustc_middle::mir::{
6-
Body, BodyAndCache, Local, Location, PlaceElem, ReadOnlyBodyAndCache, VarDebugInfo,
7-
};
5+
use rustc_middle::mir::{Body, BodyAndCache, Local, Location, ReadOnlyBodyAndCache, VarDebugInfo};
86
use rustc_middle::ty::TyCtxt;
97
use std::mem;
108

@@ -157,13 +155,4 @@ impl MutVisitor<'tcx> for MutateUseVisitor<'tcx> {
157155
*local = self.new_local;
158156
}
159157
}
160-
161-
fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
162-
match elem {
163-
PlaceElem::Index(local) if *local == self.query => {
164-
Some(PlaceElem::Index(self.new_local))
165-
}
166-
_ => None,
167-
}
168-
}
169158
}

0 commit comments

Comments
 (0)