Skip to content

Commit a9244f7

Browse files
authored
Rollup merge of rust-lang#65056 - spastorino:place-mut-visitor-adjusts, r=oli-obk
Make visit projection iterative r? @oli-obk /cc @nikomatsakis
2 parents c086b9f + b9ed642 commit a9244f7

File tree

3 files changed

+110
-87
lines changed

3 files changed

+110
-87
lines changed

src/librustc/mir/visit.rs

+38-20
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ macro_rules! make_mir_visitor {
166166
self.super_projection(base, projection, context, location);
167167
}
168168

169+
fn visit_projection_elem(&mut self,
170+
base: & $($mutability)? PlaceBase<'tcx>,
171+
proj_base: & $($mutability)? [PlaceElem<'tcx>],
172+
elem: & $($mutability)? PlaceElem<'tcx>,
173+
context: PlaceContext,
174+
location: Location) {
175+
self.super_projection_elem(base, proj_base, elem, context, location);
176+
}
177+
169178
fn visit_constant(&mut self,
170179
constant: & $($mutability)? Constant<'tcx>,
171180
location: Location) {
@@ -725,27 +734,36 @@ macro_rules! make_mir_visitor {
725734
projection: & $($mutability)? [PlaceElem<'tcx>],
726735
context: PlaceContext,
727736
location: Location) {
728-
if let [proj_base @ .., elem] = projection {
729-
self.visit_projection(base, proj_base, context, location);
737+
let mut cursor = projection;
738+
while let [proj_base @ .., elem] = cursor {
739+
cursor = proj_base;
740+
self.visit_projection_elem(base, cursor, elem, context, location);
741+
}
742+
}
730743

731-
match elem {
732-
ProjectionElem::Field(_field, ty) => {
733-
self.visit_ty(ty, TyContext::Location(location));
734-
}
735-
ProjectionElem::Index(local) => {
736-
self.visit_local(
737-
local,
738-
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
739-
location
740-
);
741-
}
742-
ProjectionElem::Deref |
743-
ProjectionElem::Subslice { from: _, to: _ } |
744-
ProjectionElem::ConstantIndex { offset: _,
745-
min_length: _,
746-
from_end: _ } |
747-
ProjectionElem::Downcast(_, _) => {
748-
}
744+
fn super_projection_elem(&mut self,
745+
_base: & $($mutability)? PlaceBase<'tcx>,
746+
_proj_base: & $($mutability)? [PlaceElem<'tcx>],
747+
elem: & $($mutability)? PlaceElem<'tcx>,
748+
_context: PlaceContext,
749+
location: Location) {
750+
match elem {
751+
ProjectionElem::Field(_field, ty) => {
752+
self.visit_ty(ty, TyContext::Location(location));
753+
}
754+
ProjectionElem::Index(local) => {
755+
self.visit_local(
756+
local,
757+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
758+
location
759+
);
760+
}
761+
ProjectionElem::Deref |
762+
ProjectionElem::Subslice { from: _, to: _ } |
763+
ProjectionElem::ConstantIndex { offset: _,
764+
min_length: _,
765+
from_end: _ } |
766+
ProjectionElem::Downcast(_, _) => {
749767
}
750768
}
751769
}

src/librustc_mir/transform/check_consts/validation.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -404,25 +404,25 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
404404
self.super_assign(dest, rvalue, location);
405405
}
406406

407-
fn visit_projection(
407+
fn visit_projection_elem(
408408
&mut self,
409409
place_base: &PlaceBase<'tcx>,
410-
proj: &[PlaceElem<'tcx>],
410+
proj_base: &[PlaceElem<'tcx>],
411+
elem: &PlaceElem<'tcx>,
411412
context: PlaceContext,
412413
location: Location,
413414
) {
414415
trace!(
415-
"visit_place_projection: proj={:?} context={:?} location={:?}",
416-
proj,
416+
"visit_projection_elem: place_base={:?} proj_base={:?} elem={:?} \
417+
context={:?} location={:?}",
418+
place_base,
419+
proj_base,
420+
elem,
417421
context,
418422
location,
419423
);
420-
self.super_projection(place_base, proj, context, location);
421424

422-
let (elem, proj_base) = match proj.split_last() {
423-
Some(x) => x,
424-
None => return,
425-
};
425+
self.super_projection_elem(place_base, proj_base, elem, context, location);
426426

427427
match elem {
428428
ProjectionElem::Deref => {

src/librustc_mir/transform/qualify_consts.rs

+63-58
Original file line numberDiff line numberDiff line change
@@ -1156,82 +1156,87 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
11561156
}
11571157
}
11581158

1159-
fn visit_projection(
1159+
fn visit_projection_elem(
11601160
&mut self,
11611161
place_base: &PlaceBase<'tcx>,
1162-
proj: &[PlaceElem<'tcx>],
1162+
proj_base: &[PlaceElem<'tcx>],
1163+
elem: &PlaceElem<'tcx>,
11631164
context: PlaceContext,
11641165
location: Location,
11651166
) {
11661167
debug!(
1167-
"visit_place_projection: proj={:?} context={:?} location={:?}",
1168-
proj, context, location,
1168+
"visit_projection_elem: place_base={:?} proj_base={:?} elem={:?} \
1169+
context={:?} location={:?}",
1170+
place_base,
1171+
proj_base,
1172+
elem,
1173+
context,
1174+
location,
11691175
);
1170-
self.super_projection(place_base, proj, context, location);
11711176

1172-
if let [proj_base @ .., elem] = proj {
1173-
match elem {
1174-
ProjectionElem::Deref => {
1175-
if context.is_mutating_use() {
1176-
// `not_const` errors out in const contexts
1177-
self.not_const(ops::MutDeref)
1178-
}
1179-
let base_ty = Place::ty_from(place_base, proj_base, self.body, self.tcx).ty;
1180-
match self.mode {
1181-
Mode::NonConstFn => {}
1182-
_ if self.suppress_errors => {}
1183-
_ => {
1184-
if let ty::RawPtr(_) = base_ty.kind {
1185-
if !self.tcx.features().const_raw_ptr_deref {
1186-
self.record_error(ops::RawPtrDeref);
1187-
emit_feature_err(
1188-
&self.tcx.sess.parse_sess, sym::const_raw_ptr_deref,
1189-
self.span, GateIssue::Language,
1190-
&format!(
1191-
"dereferencing raw pointers in {}s is unstable",
1192-
self.mode,
1193-
),
1194-
);
1195-
}
1177+
self.super_projection_elem(place_base, proj_base, elem, context, location);
1178+
1179+
match elem {
1180+
ProjectionElem::Deref => {
1181+
if context.is_mutating_use() {
1182+
// `not_const` errors out in const contexts
1183+
self.not_const(ops::MutDeref)
1184+
}
1185+
let base_ty = Place::ty_from(place_base, proj_base, self.body, self.tcx).ty;
1186+
match self.mode {
1187+
Mode::NonConstFn => {}
1188+
_ if self.suppress_errors => {}
1189+
_ => {
1190+
if let ty::RawPtr(_) = base_ty.kind {
1191+
if !self.tcx.features().const_raw_ptr_deref {
1192+
self.record_error(ops::RawPtrDeref);
1193+
emit_feature_err(
1194+
&self.tcx.sess.parse_sess, sym::const_raw_ptr_deref,
1195+
self.span, GateIssue::Language,
1196+
&format!(
1197+
"dereferencing raw pointers in {}s is unstable",
1198+
self.mode,
1199+
),
1200+
);
11961201
}
11971202
}
11981203
}
11991204
}
1205+
}
12001206

1201-
ProjectionElem::ConstantIndex {..} |
1202-
ProjectionElem::Subslice {..} |
1203-
ProjectionElem::Field(..) |
1204-
ProjectionElem::Index(_) => {
1205-
let base_ty = Place::ty_from(place_base, proj_base, self.body, self.tcx).ty;
1206-
if let Some(def) = base_ty.ty_adt_def() {
1207-
if def.is_union() {
1208-
match self.mode {
1209-
Mode::ConstFn => {
1210-
if !self.tcx.features().const_fn_union
1211-
&& !self.suppress_errors
1212-
{
1213-
self.record_error(ops::UnionAccess);
1214-
emit_feature_err(
1215-
&self.tcx.sess.parse_sess, sym::const_fn_union,
1216-
self.span, GateIssue::Language,
1217-
"unions in const fn are unstable",
1218-
);
1219-
}
1220-
},
1207+
ProjectionElem::ConstantIndex {..} |
1208+
ProjectionElem::Subslice {..} |
1209+
ProjectionElem::Field(..) |
1210+
ProjectionElem::Index(_) => {
1211+
let base_ty = Place::ty_from(place_base, proj_base, self.body, self.tcx).ty;
1212+
if let Some(def) = base_ty.ty_adt_def() {
1213+
if def.is_union() {
1214+
match self.mode {
1215+
Mode::ConstFn => {
1216+
if !self.tcx.features().const_fn_union
1217+
&& !self.suppress_errors
1218+
{
1219+
self.record_error(ops::UnionAccess);
1220+
emit_feature_err(
1221+
&self.tcx.sess.parse_sess, sym::const_fn_union,
1222+
self.span, GateIssue::Language,
1223+
"unions in const fn are unstable",
1224+
);
1225+
}
1226+
},
12211227

1222-
| Mode::NonConstFn
1223-
| Mode::Static
1224-
| Mode::StaticMut
1225-
| Mode::Const
1226-
=> {},
1227-
}
1228+
| Mode::NonConstFn
1229+
| Mode::Static
1230+
| Mode::StaticMut
1231+
| Mode::Const
1232+
=> {},
12281233
}
12291234
}
12301235
}
1236+
}
12311237

1232-
ProjectionElem::Downcast(..) => {
1233-
self.not_const(ops::Downcast)
1234-
}
1238+
ProjectionElem::Downcast(..) => {
1239+
self.not_const(ops::Downcast)
12351240
}
12361241
}
12371242
}

0 commit comments

Comments
 (0)