Skip to content

Commit 9ad5728

Browse files
committed
clean up const checking of mutable references
1 parent 3175cc2 commit 9ad5728

File tree

1 file changed

+2
-77
lines changed
  • compiler/rustc_const_eval/src/check_consts

1 file changed

+2
-77
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

+2-77
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use rustc_hir::{self as hir, LangItem};
1111
use rustc_index::bit_set::BitSet;
1212
use rustc_infer::infer::TyCtxtInferExt;
1313
use rustc_infer::traits::ObligationCause;
14-
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
14+
use rustc_middle::mir::visit::Visitor;
1515
use rustc_middle::mir::*;
1616
use rustc_middle::span_bug;
1717
use rustc_middle::ty::adjustment::PointerCoercion;
18-
use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt, TypeVisitableExt};
18+
use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TypeVisitableExt};
1919
use rustc_mir_dataflow::impls::MaybeStorageLive;
2020
use rustc_mir_dataflow::storage::always_storage_live_locals;
2121
use rustc_mir_dataflow::Analysis;
@@ -373,47 +373,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
373373
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
374374
trace!("visit_rvalue: rvalue={:?} location={:?}", rvalue, location);
375375

376-
// Special-case reborrows to be more like a copy of a reference.
377-
// FIXME: this does not actually handle all reborrows. It only detects cases where `*` is the outermost
378-
// projection of the borrowed place, it skips deref'ing raw pointers and it skips `static`.
379-
// All those cases are handled below with shared/mutable borrows.
380-
// Once `const_mut_refs` is stable, we should be able to entirely remove this special case.
381-
// (`const_refs_to_cell` is not needed, we already allow all borrows of indirect places anyway.)
382-
match *rvalue {
383-
Rvalue::Ref(_, kind, place) => {
384-
if let Some(reborrowed_place_ref) = place_as_reborrow(self.tcx, self.body, place) {
385-
let ctx = match kind {
386-
BorrowKind::Shared => {
387-
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
388-
}
389-
BorrowKind::Fake(_) => {
390-
PlaceContext::NonMutatingUse(NonMutatingUseContext::FakeBorrow)
391-
}
392-
BorrowKind::Mut { .. } => {
393-
PlaceContext::MutatingUse(MutatingUseContext::Borrow)
394-
}
395-
};
396-
self.visit_local(reborrowed_place_ref.local, ctx, location);
397-
self.visit_projection(reborrowed_place_ref, ctx, location);
398-
return;
399-
}
400-
}
401-
Rvalue::RawPtr(mutbl, place) => {
402-
if let Some(reborrowed_place_ref) = place_as_reborrow(self.tcx, self.body, place) {
403-
let ctx = match mutbl {
404-
Mutability::Not => {
405-
PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow)
406-
}
407-
Mutability::Mut => PlaceContext::MutatingUse(MutatingUseContext::RawBorrow),
408-
};
409-
self.visit_local(reborrowed_place_ref.local, ctx, location);
410-
self.visit_projection(reborrowed_place_ref, ctx, location);
411-
return;
412-
}
413-
}
414-
_ => {}
415-
}
416-
417376
self.super_rvalue(rvalue, location);
418377

419378
match rvalue {
@@ -885,40 +844,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
885844
}
886845
}
887846

888-
fn place_as_reborrow<'tcx>(
889-
tcx: TyCtxt<'tcx>,
890-
body: &Body<'tcx>,
891-
place: Place<'tcx>,
892-
) -> Option<PlaceRef<'tcx>> {
893-
match place.as_ref().last_projection() {
894-
Some((place_base, ProjectionElem::Deref)) => {
895-
// FIXME: why do statics and raw pointers get excluded here? This makes
896-
// some code involving mutable pointers unstable, but it is unclear
897-
// why that code is treated differently from mutable references.
898-
// Once TransientMutBorrow and TransientCellBorrow are stable,
899-
// this can probably be cleaned up without any behavioral changes.
900-
901-
// A borrow of a `static` also looks like `&(*_1)` in the MIR, but `_1` is a `const`
902-
// that points to the allocation for the static. Don't treat these as reborrows.
903-
if body.local_decls[place_base.local].is_ref_to_static() {
904-
None
905-
} else {
906-
// Ensure the type being derefed is a reference and not a raw pointer.
907-
// This is sufficient to prevent an access to a `static mut` from being marked as a
908-
// reborrow, even if the check above were to disappear.
909-
let inner_ty = place_base.ty(body, tcx).ty;
910-
911-
if let ty::Ref(..) = inner_ty.kind() {
912-
return Some(place_base);
913-
} else {
914-
return None;
915-
}
916-
}
917-
}
918-
_ => None,
919-
}
920-
}
921-
922847
fn is_int_bool_float_or_char(ty: Ty<'_>) -> bool {
923848
ty.is_bool() || ty.is_integral() || ty.is_char() || ty.is_floating_point()
924849
}

0 commit comments

Comments
 (0)