Skip to content

Commit b9ed642

Browse files
committed
Make visit_projection iterative
1 parent a8d70d1 commit b9ed642

File tree

3 files changed

+76
-70
lines changed

3 files changed

+76
-70
lines changed

src/librustc/mir/visit.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,10 @@ macro_rules! make_mir_visitor {
734734
projection: & $($mutability)? [PlaceElem<'tcx>],
735735
context: PlaceContext,
736736
location: Location) {
737-
if let [proj_base @ .., elem] = projection {
738-
self.visit_projection(base, proj_base, context, location);
739-
self.visit_projection_elem(base, proj_base, elem, 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);
740741
}
741742
}
742743

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)