+ | Rvalue::Ref(_, kind @ BorrowKind::Mut { .. }, ref place)
+ | Rvalue::Ref(_, kind @ BorrowKind::Unique, ref place)
+ => {
+ let ty = place.ty(self.body, self.tcx).ty;
+ let is_allowed = match ty.kind {
+ // Inside a `static mut`, `&mut [...]` is allowed.
+ ty::Array(..) | ty::Slice(_) if self.const_kind() == ConstKind::StaticMut
+ => true,
+
+ // FIXME(ecstaticmorse): We could allow `&mut []` inside a const context given
+ // that this is merely a ZST and it is already eligible for promotion.
+ // This may require an RFC?
+ /*
+ ty::Array(_, len) if len.try_eval_usize(cx.tcx, cx.param_env) == Some(0)
+ => true,
+ */
+
+ _ => false,
+ };
+
+ if !is_allowed {
+ self.check_op(ops::MutBorrow(kind));
+ }
+ }
+
+ // Taking a shared borrow of a `static` is always legal, even if that `static` has
+ // interior mutability.
+ | Rvalue::Ref(_, BorrowKind::Shared, ref place)
+ | Rvalue::Ref(_, BorrowKind::Shallow, ref place)
+ if matches!(place.base, PlaceBase::Static(_))
+ => {}
+
+ | Rvalue::Ref(_, kind @ BorrowKind::Shared, ref place)
+ | Rvalue::Ref(_, kind @ BorrowKind::Shallow, ref place)
+ => {
+ // FIXME: Change the `in_*` methods to take a `FnMut` so we don't have to manually
+ // seek the cursors beforehand.
+ self.qualifs.has_mut_interior.cursor.seek_before(location);
+ self.qualifs.indirectly_mutable.seek(location);
+
+ let borrowed_place_has_mut_interior = HasMutInterior::in_place(
+ &self.item,
+ &|local| self.qualifs.has_mut_interior_eager_seek(local),
+ place.as_ref(),
+ );
+
+ if borrowed_place_has_mut_interior {
+ let src_derived_from_illegal_borrow = borrowed_place
+ .as_local()
+ .map_or(false, |local| self.derived_from_illegal_borrow.contains(local));
+
+ // Don't emit errors for borrows of values that are *themselves* the result of
+ // an illegal borrow (e.g., the outermost `&` in `&&Cell::new(42)`). We want to
+ // point the user to the place where the original illegal borrow occurred, not
+ // to subsequent borrows of the resulting value.
+ let dest_derived_from_illegal_borrow = if !src_derived_from_illegal_borrow {
+ self.check_op(ops::MutBorrow(kind)) == CheckOpResult::Forbidden
+ } else {
+ true
+ };
+
+ // When the target of the assignment is a local with no projections, it will be
+ // marked as derived from an illegal borrow if necessary.
+ //
+ // FIXME: should we also clear `derived_from_illegal_borrow` when a local is
+ // assigned a new value?
+
+ if dest_derived_from_illegal_borrow {
+ let block = &self.body[location.block];
+ let statement = &block.statements[location.statement_index];
+ if let StatementKind::Assign(box (dest, _)) = &statement.kind {
+ if let Some(dest) = dest.as_local() {
+ self.derived_from_illegal_borrow.insert(dest);
+ }
+ }
+ }
+ }
+ }
+