Skip to content

Commit fba5ed3

Browse files
authored
Rollup merge of rust-lang#61023 - spastorino:use-iterate-qualify-consts, r=oli-obk
Migrate from recursion to iterate on qualify consts visitor impl r? @oli-obk
2 parents 6212310 + f47b872 commit fba5ed3

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

src/librustc_mir/transform/qualify_consts.rs

+48-46
Original file line numberDiff line numberDiff line change
@@ -930,58 +930,60 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
930930
context: PlaceContext,
931931
location: Location) {
932932
debug!("visit_place: place={:?} context={:?} location={:?}", place, context, location);
933-
self.super_place(place, context, location);
934-
match *place {
935-
Place::Base(PlaceBase::Local(_)) => {}
936-
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => {
937-
unreachable!()
938-
}
939-
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
940-
if self.tcx
941-
.get_attrs(def_id)
942-
.iter()
943-
.any(|attr| attr.check_name(sym::thread_local)) {
944-
if self.mode != Mode::Fn {
945-
span_err!(self.tcx.sess, self.span, E0625,
946-
"thread-local statics cannot be \
947-
accessed at compile-time");
948-
}
949-
return;
933+
place.iterate(|place_base, place_projections| {
934+
match place_base {
935+
PlaceBase::Local(_) => {}
936+
PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. }) => {
937+
unreachable!()
950938
}
939+
PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. }) => {
940+
if self.tcx
941+
.get_attrs(*def_id)
942+
.iter()
943+
.any(|attr| attr.check_name(sym::thread_local)) {
944+
if self.mode != Mode::Fn {
945+
span_err!(self.tcx.sess, self.span, E0625,
946+
"thread-local statics cannot be \
947+
accessed at compile-time");
948+
}
949+
return;
950+
}
951951

952-
// Only allow statics (not consts) to refer to other statics.
953-
if self.mode == Mode::Static || self.mode == Mode::StaticMut {
954-
if self.mode == Mode::Static && context.is_mutating_use() {
955-
// this is not strictly necessary as miri will also bail out
956-
// For interior mutability we can't really catch this statically as that
957-
// goes through raw pointers and intermediate temporaries, so miri has
958-
// to catch this anyway
959-
self.tcx.sess.span_err(
960-
self.span,
961-
"cannot mutate statics in the initializer of another static",
962-
);
952+
// Only allow statics (not consts) to refer to other statics.
953+
if self.mode == Mode::Static || self.mode == Mode::StaticMut {
954+
if self.mode == Mode::Static && context.is_mutating_use() {
955+
// this is not strictly necessary as miri will also bail out
956+
// For interior mutability we can't really catch this statically as that
957+
// goes through raw pointers and intermediate temporaries, so miri has
958+
// to catch this anyway
959+
self.tcx.sess.span_err(
960+
self.span,
961+
"cannot mutate statics in the initializer of another static",
962+
);
963+
}
964+
return;
963965
}
964-
return;
965-
}
966-
unleash_miri!(self);
966+
unleash_miri!(self);
967967

968-
if self.mode != Mode::Fn {
969-
let mut err = struct_span_err!(self.tcx.sess, self.span, E0013,
970-
"{}s cannot refer to statics, use \
971-
a constant instead", self.mode);
972-
if self.tcx.sess.teach(&err.get_code().unwrap()) {
973-
err.note(
974-
"Static and const variables can refer to other const variables. But a \
975-
const variable cannot refer to a static variable."
976-
);
977-
err.help(
978-
"To fix this, the value can be extracted as a const and then used."
979-
);
968+
if self.mode != Mode::Fn {
969+
let mut err = struct_span_err!(self.tcx.sess, self.span, E0013,
970+
"{}s cannot refer to statics, use \
971+
a constant instead", self.mode);
972+
if self.tcx.sess.teach(&err.get_code().unwrap()) {
973+
err.note(
974+
"Static and const variables can refer to other const variables. \
975+
But a const variable cannot refer to a static variable."
976+
);
977+
err.help(
978+
"To fix this, the value can be extracted as a const and then used."
979+
);
980+
}
981+
err.emit()
980982
}
981-
err.emit()
982983
}
983984
}
984-
Place::Projection(ref proj) => {
985+
986+
for proj in place_projections {
985987
match proj.elem {
986988
ProjectionElem::Deref => {
987989
if context.is_mutating_use() {
@@ -1041,7 +1043,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
10411043
}
10421044
}
10431045
}
1044-
}
1046+
});
10451047
}
10461048

10471049
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {

0 commit comments

Comments
 (0)