about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-10 12:28:39 +0000
committerbors <bors@rust-lang.org>2024-02-10 12:28:39 +0000
commit5f40394baa07b6fb50bc70dedd8b780524b20934 (patch)
tree5c3cfc67941b0bf40c4ad3dec50f529e473ff23e /compiler/rustc_const_eval/src
parent232919c33a5ba8ce8b4171b03cc898c77da136cc (diff)
parent55913368c53b2846bd96fd017abc34a0af07ccc8 (diff)
downloadrust-5f40394baa07b6fb50bc70dedd8b780524b20934.tar.gz
rust-5f40394baa07b6fb50bc70dedd8b780524b20934.zip
Auto merge of #120877 - matthiaskrgr:rollup-j1b8mv6, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - #117614 (static mut: allow mutable reference to arbitrary types, not just slices and arrays)
 - #120719 (Remove support for `associated_type_bound` nested in `dyn` types)
 - #120764 (Add documentation on `str::starts_with`)
 - #120823 (Clarify that atomic and regular integers can differ in alignment)
 - #120859 (Loosen an assertion to account for stashed errors.)
 - #120865 (Turn the "no saved object file in work product" ICE into a translatable fatal error)
 - #120866 (Remove unnecessary `#![feature(min_specialization)]`)
 - #120870 (Allow restricted trait impls under `#[allow_internal_unstable(min_specialization)]`)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_const_eval/src')
-rw-r--r--compiler/rustc_const_eval/src/transform/check_consts/check.rs40
1 files changed, 16 insertions, 24 deletions
diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs
index ee3f349c6b8..28dc69859fd 100644
--- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs
+++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs
@@ -449,35 +449,27 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
                 }
             }
 
-            Rvalue::Ref(_, BorrowKind::Mut { .. }, 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() == hir::ConstContext::Static(hir::Mutability::Mut) =>
-                    {
-                        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_target_usize(cx.tcx, cx.param_env) == Some(0)
-                        => true,
-                    */
-                    _ => false,
-                };
+            Rvalue::Ref(_, BorrowKind::Mut { .. }, place)
+            | Rvalue::AddressOf(Mutability::Mut, place) => {
+                // Inside mutable statics, we allow arbitrary mutable references.
+                // We've allowed `static mut FOO = &mut [elements];` for a long time (the exact
+                // reasons why are lost to history), and there is no reason to restrict that to
+                // arrays and slices.
+                let is_allowed =
+                    self.const_kind() == hir::ConstContext::Static(hir::Mutability::Mut);
 
                 if !is_allowed {
-                    self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
+                    self.check_mut_borrow(
+                        place.local,
+                        if matches!(rvalue, Rvalue::Ref(..)) {
+                            hir::BorrowKind::Ref
+                        } else {
+                            hir::BorrowKind::Raw
+                        },
+                    );
                 }
             }
 
-            Rvalue::AddressOf(Mutability::Mut, place) => {
-                self.check_mut_borrow(place.local, hir::BorrowKind::Raw)
-            }
-
             Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Fake, place)
             | Rvalue::AddressOf(Mutability::Not, place) => {
                 let borrowed_place_has_mut_interior = qualifs::in_place::<HasMutInterior, _>(