about summary refs log tree commit diff
diff options
context:
space:
mode:
authoroli <github35764891676564198441@oli-obk.de>2021-01-03 18:46:20 +0000
committeroli <github35764891676564198441@oli-obk.de>2021-01-23 11:33:45 +0000
commitd118021f8bfbfda2cd93277c15c607b2c66ff93c (patch)
tree3bb47effb0df11fc428390a0bbdd7aace551b012
parent1986b58c646a9523d0a8a0fa8a0bd20492e7795d (diff)
downloadrust-d118021f8bfbfda2cd93277c15c607b2c66ff93c.tar.gz
rust-d118021f8bfbfda2cd93277c15c607b2c66ff93c.zip
Permit mutable references in all const contexts
-rw-r--r--compiler/rustc_mir/src/transform/check_consts/ops.rs71
-rw-r--r--compiler/rustc_mir/src/transform/check_consts/validation.rs29
-rw-r--r--src/test/ui/check-static-immutable-mut-slices.rs2
-rw-r--r--src/test/ui/check-static-immutable-mut-slices.stderr4
-rw-r--r--src/test/ui/consts/const-address-of-mut.stderr24
-rw-r--r--src/test/ui/consts/const-eval/issue-65394.stderr9
-rw-r--r--src/test/ui/consts/const-multi-ref.stderr10
-rw-r--r--src/test/ui/consts/const-mut-refs/const_mut_address_of.rs3
-rw-r--r--src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr15
-rw-r--r--src/test/ui/consts/const-mut-refs/const_mut_refs.rs4
-rw-r--r--src/test/ui/consts/const-mut-refs/const_mut_refs.stderr21
-rw-r--r--src/test/ui/consts/const-mut-refs/mut_ref_in_final.rs24
-rw-r--r--src/test/ui/consts/const-mut-refs/mut_ref_in_final.stderr9
-rw-r--r--src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.rs22
-rw-r--r--src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.stderr13
-rw-r--r--src/test/ui/consts/const_let_assign3.stderr17
-rw-r--r--src/test/ui/consts/issue-17718-const-bad-values.rs3
-rw-r--r--src/test/ui/consts/issue-17718-const-bad-values.stderr12
-rw-r--r--src/test/ui/consts/projection_qualif.mut_refs.stderr11
-rw-r--r--src/test/ui/consts/projection_qualif.rs2
-rw-r--r--src/test/ui/consts/projection_qualif.stock.stderr10
-rw-r--r--src/test/ui/consts/read_from_static_mut_ref.rs5
-rw-r--r--src/test/ui/consts/read_from_static_mut_ref.stderr17
-rw-r--r--src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr8
-rw-r--r--src/test/ui/consts/static_mut_containing_mut_ref2.rs3
-rw-r--r--src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr9
-rw-r--r--src/test/ui/error-codes/E0017.rs9
-rw-r--r--src/test/ui/error-codes/E0017.stderr35
-rw-r--r--src/test/ui/error-codes/E0388.rs7
-rw-r--r--src/test/ui/error-codes/E0388.stderr29
-rw-r--r--src/test/ui/issues/issue-46604.rs2
-rw-r--r--src/test/ui/issues/issue-46604.stderr4
-rw-r--r--src/test/ui/never_type/issue-52443.stderr9
-rw-r--r--src/test/ui/unsafe/ranged_ints2_const.rs6
-rw-r--r--src/test/ui/unsafe/ranged_ints2_const.stderr11
35 files changed, 298 insertions, 171 deletions
diff --git a/compiler/rustc_mir/src/transform/check_consts/ops.rs b/compiler/rustc_mir/src/transform/check_consts/ops.rs
index 99ffb0edce9..23abd522cf0 100644
--- a/compiler/rustc_mir/src/transform/check_consts/ops.rs
+++ b/compiler/rustc_mir/src/transform/check_consts/ops.rs
@@ -268,43 +268,41 @@ impl NonConstOp for CellBorrow {
 }
 
 #[derive(Debug)]
+/// This op is for `&mut` borrows in the trailing expression of a constant
+/// which uses the "enclosing scopes rule" to leak its locals into anonymous
+/// static or const items.
 pub struct MutBorrow(pub hir::BorrowKind);
 
 impl NonConstOp for MutBorrow {
     fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
-        // Forbid everywhere except in const fn with a feature gate
-        if ccx.const_kind() == hir::ConstContext::ConstFn {
-            Status::Unstable(sym::const_mut_refs)
-        } else {
-            Status::Forbidden
+        match ccx.const_kind() {
+            // Mutable statics can handle mutable references in their final value
+            hir::ConstContext::Static(hir::Mutability::Mut) => Status::Allowed,
+            _ => Status::Forbidden,
         }
     }
 
+    fn importance(&self) -> DiagnosticImportance {
+        // If there were primary errors (like non-const function calls), do not emit further
+        // errors about mutable references.
+        DiagnosticImportance::Secondary
+    }
+
     fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
         let raw = match self.0 {
             hir::BorrowKind::Raw => "raw ",
             hir::BorrowKind::Ref => "",
         };
 
-        let mut err = if ccx.const_kind() == hir::ConstContext::ConstFn {
-            feature_err(
-                &ccx.tcx.sess.parse_sess,
-                sym::const_mut_refs,
-                span,
-                &format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
-            )
-        } else {
-            let mut err = struct_span_err!(
-                ccx.tcx.sess,
-                span,
-                E0764,
-                "{}mutable references are not allowed in {}s",
-                raw,
-                ccx.const_kind(),
-            );
-            err.span_label(span, format!("`&{}mut` is only allowed in `const fn`", raw));
-            err
-        };
+        let mut err = struct_span_err!(
+            ccx.tcx.sess,
+            span,
+            E0764,
+            "{}mutable references are not allowed in final value of {}s",
+            raw,
+            ccx.const_kind(),
+        );
+
         if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
             err.note(
                 "References in statics and constants may only refer \
@@ -322,6 +320,29 @@ impl NonConstOp for MutBorrow {
 }
 
 #[derive(Debug)]
+pub struct TransientMutBorrow(pub hir::BorrowKind);
+
+impl NonConstOp for TransientMutBorrow {
+    fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
+        Status::Unstable(sym::const_mut_refs)
+    }
+
+    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
+        let raw = match self.0 {
+            hir::BorrowKind::Raw => "raw ",
+            hir::BorrowKind::Ref => "",
+        };
+
+        feature_err(
+            &ccx.tcx.sess.parse_sess,
+            sym::const_mut_refs,
+            span,
+            &format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
+        )
+    }
+}
+
+#[derive(Debug)]
 pub struct MutDeref;
 impl NonConstOp for MutDeref {
     fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
@@ -329,7 +350,7 @@ impl NonConstOp for MutDeref {
     }
 
     fn importance(&self) -> DiagnosticImportance {
-        // Usually a side-effect of a `MutBorrow` somewhere.
+        // Usually a side-effect of a `TransientMutBorrow` somewhere.
         DiagnosticImportance::Secondary
     }
 
diff --git a/compiler/rustc_mir/src/transform/check_consts/validation.rs b/compiler/rustc_mir/src/transform/check_consts/validation.rs
index 08d969b27be..a92997dddac 100644
--- a/compiler/rustc_mir/src/transform/check_consts/validation.rs
+++ b/compiler/rustc_mir/src/transform/check_consts/validation.rs
@@ -466,6 +466,29 @@ impl Validator<'mir, 'tcx> {
             }
         }
     }
+
+    fn check_mut_borrow(&mut self, local: Local, kind: hir::BorrowKind) {
+        match self.const_kind() {
+            // In a const fn all borrows are transient or point to the places given via
+            // references in the arguments (so we already checked them with
+            // TransientMutBorrow/MutBorrow as appropriate).
+            // The borrow checker guarantees that no new non-transient borrows are created.
+            // NOTE: Once we have heap allocations during CTFE we need to figure out
+            // how to prevent `const fn` to create long-lived allocations that point
+            // to mutable memory.
+            hir::ConstContext::ConstFn => self.check_op(ops::TransientMutBorrow(kind)),
+            _ => {
+                // Locals with StorageDead do not live beyond the evaluation and can
+                // thus safely be borrowed without being able to be leaked to the final
+                // value of the constant.
+                if self.local_has_storage_dead(local) {
+                    self.check_op(ops::TransientMutBorrow(kind));
+                } else {
+                    self.check_op(ops::MutBorrow(kind));
+                }
+            }
+        }
+    }
 }
 
 impl Visitor<'tcx> for Validator<'mir, 'tcx> {
@@ -562,15 +585,15 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
 
                 if !is_allowed {
                     if let BorrowKind::Mut { .. } = kind {
-                        self.check_op(ops::MutBorrow(hir::BorrowKind::Ref));
+                        self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
                     } else {
                         self.check_op(ops::CellBorrow);
                     }
                 }
             }
 
-            Rvalue::AddressOf(Mutability::Mut, _) => {
-                self.check_op(ops::MutBorrow(hir::BorrowKind::Raw))
+            Rvalue::AddressOf(Mutability::Mut, ref place) => {
+                self.check_mut_borrow(place.local, hir::BorrowKind::Raw)
             }
 
             Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, ref place)
diff --git a/src/test/ui/check-static-immutable-mut-slices.rs b/src/test/ui/check-static-immutable-mut-slices.rs
index 3be02f6a0f6..8f9680778aa 100644
--- a/src/test/ui/check-static-immutable-mut-slices.rs
+++ b/src/test/ui/check-static-immutable-mut-slices.rs
@@ -1,6 +1,6 @@
 // Checks that immutable static items can't have mutable slices
 
 static TEST: &'static mut [isize] = &mut [];
-//~^ ERROR mutable references are not allowed in statics
+//~^ ERROR mutable references are not allowed
 
 pub fn main() { }
diff --git a/src/test/ui/check-static-immutable-mut-slices.stderr b/src/test/ui/check-static-immutable-mut-slices.stderr
index 9ffbb483d13..fcc18cc584c 100644
--- a/src/test/ui/check-static-immutable-mut-slices.stderr
+++ b/src/test/ui/check-static-immutable-mut-slices.stderr
@@ -1,8 +1,8 @@
-error[E0764]: mutable references are not allowed in statics
+error[E0764]: mutable references are not allowed in final value of statics
   --> $DIR/check-static-immutable-mut-slices.rs:3:37
    |
 LL | static TEST: &'static mut [isize] = &mut [];
-   |                                     ^^^^^^^ `&mut` is only allowed in `const fn`
+   |                                     ^^^^^^^
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/consts/const-address-of-mut.stderr b/src/test/ui/consts/const-address-of-mut.stderr
index ec2dac5a7d1..60cdcc7df74 100644
--- a/src/test/ui/consts/const-address-of-mut.stderr
+++ b/src/test/ui/consts/const-address-of-mut.stderr
@@ -1,20 +1,29 @@
-error[E0764]: raw mutable references are not allowed in constants
+error[E0658]: raw mutable references are not allowed in constants
   --> $DIR/const-address-of-mut.rs:3:32
    |
 LL | const A: () = { let mut x = 2; &raw mut x; };
-   |                                ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
+   |                                ^^^^^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
-error[E0764]: raw mutable references are not allowed in statics
+error[E0658]: raw mutable references are not allowed in statics
   --> $DIR/const-address-of-mut.rs:5:33
    |
 LL | static B: () = { let mut x = 2; &raw mut x; };
-   |                                 ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
+   |                                 ^^^^^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
-error[E0764]: raw mutable references are not allowed in statics
+error[E0658]: raw mutable references are not allowed in statics
   --> $DIR/const-address-of-mut.rs:7:37
    |
 LL | static mut C: () = { let mut x = 2; &raw mut x; };
-   |                                     ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
+   |                                     ^^^^^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error[E0658]: raw mutable references are not allowed in constant functions
   --> $DIR/const-address-of-mut.rs:11:13
@@ -27,5 +36,4 @@ LL |     let y = &raw mut x;
 
 error: aborting due to 4 previous errors
 
-Some errors have detailed explanations: E0658, E0764.
-For more information about an error, try `rustc --explain E0658`.
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/consts/const-eval/issue-65394.stderr b/src/test/ui/consts/const-eval/issue-65394.stderr
index 771d368d783..ec229d7f53a 100644
--- a/src/test/ui/consts/const-eval/issue-65394.stderr
+++ b/src/test/ui/consts/const-eval/issue-65394.stderr
@@ -1,8 +1,11 @@
-error[E0764]: mutable references are not allowed in constants
+error[E0658]: mutable references are not allowed in constants
   --> $DIR/issue-65394.rs:8:13
    |
 LL |     let r = &mut x;
-   |             ^^^^^^ `&mut` is only allowed in `const fn`
+   |             ^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error[E0493]: destructors cannot be evaluated at compile-time
   --> $DIR/issue-65394.rs:7:9
@@ -15,5 +18,5 @@ LL | };
 
 error: aborting due to 2 previous errors
 
-Some errors have detailed explanations: E0493, E0764.
+Some errors have detailed explanations: E0493, E0658.
 For more information about an error, try `rustc --explain E0493`.
diff --git a/src/test/ui/consts/const-multi-ref.stderr b/src/test/ui/consts/const-multi-ref.stderr
index c0a320d46cb..dd5cadfe295 100644
--- a/src/test/ui/consts/const-multi-ref.stderr
+++ b/src/test/ui/consts/const-multi-ref.stderr
@@ -1,8 +1,11 @@
-error[E0764]: mutable references are not allowed in constants
+error[E0658]: mutable references are not allowed in constants
   --> $DIR/const-multi-ref.rs:6:13
    |
 LL |     let p = &mut a;
-   |             ^^^^^^ `&mut` is only allowed in `const fn`
+   |             ^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
   --> $DIR/const-multi-ref.rs:16:13
@@ -15,5 +18,4 @@ LL |     let p = &a;
 
 error: aborting due to 2 previous errors
 
-Some errors have detailed explanations: E0658, E0764.
-For more information about an error, try `rustc --explain E0658`.
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/consts/const-mut-refs/const_mut_address_of.rs b/src/test/ui/consts/const-mut-refs/const_mut_address_of.rs
index 5819daa817a..24df647f05b 100644
--- a/src/test/ui/consts/const-mut-refs/const_mut_address_of.rs
+++ b/src/test/ui/consts/const-mut-refs/const_mut_address_of.rs
@@ -1,3 +1,4 @@
+// check-pass
 #![feature(const_mut_refs)]
 #![feature(const_fn)]
 #![feature(raw_ref_op)]
@@ -22,9 +23,7 @@ const fn baz(foo: &mut Foo)-> *mut usize {
 
 const _: () = {
     foo().bar();
-    //~^ ERROR mutable references are not allowed in constants
     baz(&mut foo());
-    //~^ ERROR mutable references are not allowed in constants
 };
 
 fn main() {}
diff --git a/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr b/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr
deleted file mode 100644
index 2214ce6ee1c..00000000000
--- a/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr
+++ /dev/null
@@ -1,15 +0,0 @@
-error[E0764]: mutable references are not allowed in constants
-  --> $DIR/const_mut_address_of.rs:24:5
-   |
-LL |     foo().bar();
-   |     ^^^^^ `&mut` is only allowed in `const fn`
-
-error[E0764]: mutable references are not allowed in constants
-  --> $DIR/const_mut_address_of.rs:26:9
-   |
-LL |     baz(&mut foo());
-   |         ^^^^^^^^^^ `&mut` is only allowed in `const fn`
-
-error: aborting due to 2 previous errors
-
-For more information about this error, try `rustc --explain E0764`.
diff --git a/src/test/ui/consts/const-mut-refs/const_mut_refs.rs b/src/test/ui/consts/const-mut-refs/const_mut_refs.rs
index 9099d5a1b8e..544458dfcd8 100644
--- a/src/test/ui/consts/const-mut-refs/const_mut_refs.rs
+++ b/src/test/ui/consts/const-mut-refs/const_mut_refs.rs
@@ -1,3 +1,4 @@
+// check-pass
 #![feature(const_mut_refs)]
 
 struct Foo {
@@ -29,9 +30,6 @@ const fn bazz(foo: &mut Foo) -> usize {
 
 fn main() {
     let _: [(); foo().bar()] = [(); 1];
-    //~^ ERROR mutable references are not allowed in constants
     let _: [(); baz(&mut foo())] = [(); 2];
-    //~^ ERROR mutable references are not allowed in constants
     let _: [(); bazz(&mut foo())] = [(); 3];
-    //~^ ERROR mutable references are not allowed in constants
 }
diff --git a/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr b/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr
deleted file mode 100644
index 4ca7b128b7c..00000000000
--- a/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr
+++ /dev/null
@@ -1,21 +0,0 @@
-error[E0764]: mutable references are not allowed in constants
-  --> $DIR/const_mut_refs.rs:31:17
-   |
-LL |     let _: [(); foo().bar()] = [(); 1];
-   |                 ^^^^^ `&mut` is only allowed in `const fn`
-
-error[E0764]: mutable references are not allowed in constants
-  --> $DIR/const_mut_refs.rs:33:21
-   |
-LL |     let _: [(); baz(&mut foo())] = [(); 2];
-   |                     ^^^^^^^^^^ `&mut` is only allowed in `const fn`
-
-error[E0764]: mutable references are not allowed in constants
-  --> $DIR/const_mut_refs.rs:35:22
-   |
-LL |     let _: [(); bazz(&mut foo())] = [(); 3];
-   |                      ^^^^^^^^^^ `&mut` is only allowed in `const fn`
-
-error: aborting due to 3 previous errors
-
-For more information about this error, try `rustc --explain E0764`.
diff --git a/src/test/ui/consts/const-mut-refs/mut_ref_in_final.rs b/src/test/ui/consts/const-mut-refs/mut_ref_in_final.rs
new file mode 100644
index 00000000000..c85acd3b849
--- /dev/null
+++ b/src/test/ui/consts/const-mut-refs/mut_ref_in_final.rs
@@ -0,0 +1,24 @@
+#![feature(const_mut_refs)]
+#![feature(const_fn)]
+#![feature(raw_ref_op)]
+const NULL: *mut i32 = std::ptr::null_mut();
+const A: *const i32 = &4;
+
+// It could be made sound to allow it to compile,
+// but we do not want to allow this to compile,
+// as that would be an enormous footgun in oli-obk's opinion.
+const B: *mut i32 = &mut 4; //~ ERROR mutable references are not allowed
+
+// Could be ok, but the same analysis that prevents the mutable one above will also bail out here
+// Using a block with some complex content, because just `&45` would get promoted,
+// which is not what we want to test here.
+const C: *const i32 = &{
+    let mut x = 42;
+    x += 3;
+    x
+};
+
+fn main() {
+    println!("{}", unsafe { *A });
+    unsafe { *B = 4 } // Bad news
+}
diff --git a/src/test/ui/consts/const-mut-refs/mut_ref_in_final.stderr b/src/test/ui/consts/const-mut-refs/mut_ref_in_final.stderr
new file mode 100644
index 00000000000..6d570052aeb
--- /dev/null
+++ b/src/test/ui/consts/const-mut-refs/mut_ref_in_final.stderr
@@ -0,0 +1,9 @@
+error[E0764]: mutable references are not allowed in final value of constants
+  --> $DIR/mut_ref_in_final.rs:10:21
+   |
+LL | const B: *mut i32 = &mut 4;
+   |                     ^^^^^^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0764`.
diff --git a/src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.rs b/src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.rs
new file mode 100644
index 00000000000..3f2995df2d7
--- /dev/null
+++ b/src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.rs
@@ -0,0 +1,22 @@
+#![feature(const_mut_refs)]
+#![feature(const_fn)]
+#![feature(raw_ref_op)]
+
+use std::cell::UnsafeCell;
+struct NotAMutex<T>(UnsafeCell<T>);
+
+unsafe impl<T> Sync for NotAMutex<T> {}
+
+const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
+//~^ ERROR temporary value dropped while borrowed
+
+// `BAR` works, because `&42` promotes immediately instead of relying on
+// "final value lifetime extension".
+const BAR: NotAMutex<&i32> = NotAMutex(UnsafeCell::new(&42));
+
+fn main() {
+    unsafe {
+        **FOO.0.get() = 99;
+        assert_eq!(**FOO.0.get(), 99);
+    }
+}
diff --git a/src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.stderr b/src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.stderr
new file mode 100644
index 00000000000..8b51e44e169
--- /dev/null
+++ b/src/test/ui/consts/const-mut-refs/mut_ref_in_final_ok.stderr
@@ -0,0 +1,13 @@
+error[E0716]: temporary value dropped while borrowed
+  --> $DIR/mut_ref_in_final_ok.rs:10:65
+   |
+LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
+   |                                  -------------------------------^^--
+   |                                  |                              |  |
+   |                                  |                              |  temporary value is freed at the end of this statement
+   |                                  |                              creates a temporary which is freed while still in use
+   |                                  using this value as a constant requires that borrow lasts for `'static`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0716`.
diff --git a/src/test/ui/consts/const_let_assign3.stderr b/src/test/ui/consts/const_let_assign3.stderr
index dc86e178a42..3eac61c0ce6 100644
--- a/src/test/ui/consts/const_let_assign3.stderr
+++ b/src/test/ui/consts/const_let_assign3.stderr
@@ -7,19 +7,24 @@ LL |     const fn foo(&mut self, x: u32) {
    = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
    = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
-error[E0764]: mutable references are not allowed in constants
+error[E0658]: mutable references are not allowed in constants
   --> $DIR/const_let_assign3.rs:16:5
    |
 LL |     s.foo(3);
-   |     ^ `&mut` is only allowed in `const fn`
+   |     ^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
-error[E0764]: mutable references are not allowed in constants
+error[E0658]: mutable references are not allowed in constants
   --> $DIR/const_let_assign3.rs:22:13
    |
 LL |     let y = &mut x;
-   |             ^^^^^^ `&mut` is only allowed in `const fn`
+   |             ^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error: aborting due to 3 previous errors
 
-Some errors have detailed explanations: E0658, E0764.
-For more information about an error, try `rustc --explain E0658`.
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/consts/issue-17718-const-bad-values.rs b/src/test/ui/consts/issue-17718-const-bad-values.rs
index 49023f18ddb..62bbb3b569c 100644
--- a/src/test/ui/consts/issue-17718-const-bad-values.rs
+++ b/src/test/ui/consts/issue-17718-const-bad-values.rs
@@ -1,10 +1,9 @@
 const C1: &'static mut [usize] = &mut [];
-//~^ ERROR: mutable references are not allowed in constants
+//~^ ERROR: mutable references are not allowed
 
 static mut S: usize = 3;
 const C2: &'static mut usize = unsafe { &mut S };
 //~^ ERROR: constants cannot refer to statics
 //~| ERROR: constants cannot refer to statics
-//~| ERROR: mutable references are not allowed in constants
 
 fn main() {}
diff --git a/src/test/ui/consts/issue-17718-const-bad-values.stderr b/src/test/ui/consts/issue-17718-const-bad-values.stderr
index 7c50978d4eb..7e02fa4686f 100644
--- a/src/test/ui/consts/issue-17718-const-bad-values.stderr
+++ b/src/test/ui/consts/issue-17718-const-bad-values.stderr
@@ -1,8 +1,8 @@
-error[E0764]: mutable references are not allowed in constants
+error[E0764]: mutable references are not allowed in final value of constants
   --> $DIR/issue-17718-const-bad-values.rs:1:34
    |
 LL | const C1: &'static mut [usize] = &mut [];
-   |                                  ^^^^^^^ `&mut` is only allowed in `const fn`
+   |                                  ^^^^^^^
 
 error[E0013]: constants cannot refer to statics
   --> $DIR/issue-17718-const-bad-values.rs:5:46
@@ -20,13 +20,7 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
    |
    = help: consider extracting the value of the `static` to a `const`, and referring to that
 
-error[E0764]: mutable references are not allowed in constants
-  --> $DIR/issue-17718-const-bad-values.rs:5:41
-   |
-LL | const C2: &'static mut usize = unsafe { &mut S };
-   |                                         ^^^^^^ `&mut` is only allowed in `const fn`
-
-error: aborting due to 4 previous errors
+error: aborting due to 3 previous errors
 
 Some errors have detailed explanations: E0013, E0764.
 For more information about an error, try `rustc --explain E0013`.
diff --git a/src/test/ui/consts/projection_qualif.mut_refs.stderr b/src/test/ui/consts/projection_qualif.mut_refs.stderr
index fad8f011f75..0945a23f3b1 100644
--- a/src/test/ui/consts/projection_qualif.mut_refs.stderr
+++ b/src/test/ui/consts/projection_qualif.mut_refs.stderr
@@ -1,9 +1,3 @@
-error[E0764]: mutable references are not allowed in constants
-  --> $DIR/projection_qualif.rs:10:27
-   |
-LL |         let b: *mut u32 = &mut a;
-   |                           ^^^^^^ `&mut` is only allowed in `const fn`
-
 error[E0658]: dereferencing raw pointers in constants is unstable
   --> $DIR/projection_qualif.rs:11:18
    |
@@ -13,7 +7,6 @@ LL |         unsafe { *b = 5; }
    = note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information
    = help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable
 
-error: aborting due to 2 previous errors
+error: aborting due to previous error
 
-Some errors have detailed explanations: E0658, E0764.
-For more information about an error, try `rustc --explain E0658`.
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/consts/projection_qualif.rs b/src/test/ui/consts/projection_qualif.rs
index 5e2584a6e95..d35df330bb8 100644
--- a/src/test/ui/consts/projection_qualif.rs
+++ b/src/test/ui/consts/projection_qualif.rs
@@ -7,7 +7,7 @@ use std::cell::Cell;
 const FOO: &u32 = {
     let mut a = 42;
     {
-        let b: *mut u32 = &mut a; //~ ERROR mutable references are not allowed in constants
+        let b: *mut u32 = &mut a; //[stock]~ ERROR mutable references are not allowed in constants
         unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants
     }
     &{a}
diff --git a/src/test/ui/consts/projection_qualif.stock.stderr b/src/test/ui/consts/projection_qualif.stock.stderr
index fad8f011f75..e451898caba 100644
--- a/src/test/ui/consts/projection_qualif.stock.stderr
+++ b/src/test/ui/consts/projection_qualif.stock.stderr
@@ -1,8 +1,11 @@
-error[E0764]: mutable references are not allowed in constants
+error[E0658]: mutable references are not allowed in constants
   --> $DIR/projection_qualif.rs:10:27
    |
 LL |         let b: *mut u32 = &mut a;
-   |                           ^^^^^^ `&mut` is only allowed in `const fn`
+   |                           ^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error[E0658]: dereferencing raw pointers in constants is unstable
   --> $DIR/projection_qualif.rs:11:18
@@ -15,5 +18,4 @@ LL |         unsafe { *b = 5; }
 
 error: aborting due to 2 previous errors
 
-Some errors have detailed explanations: E0658, E0764.
-For more information about an error, try `rustc --explain E0658`.
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/consts/read_from_static_mut_ref.rs b/src/test/ui/consts/read_from_static_mut_ref.rs
index 5faa983ab09..665c305e961 100644
--- a/src/test/ui/consts/read_from_static_mut_ref.rs
+++ b/src/test/ui/consts/read_from_static_mut_ref.rs
@@ -1,9 +1,8 @@
-// We are keeping this test in case we decide to allow mutable references in statics again
 #![feature(const_mut_refs)]
 #![allow(const_err)]
 
-static OH_NO: &mut i32 = &mut 42;
-//~^ ERROR mutable references are not allowed in statics
+static OH_NO: &mut i32 = &mut 42; //~ ERROR mutable references are not allowed
 fn main() {
     assert_eq!(*OH_NO, 42);
+    *OH_NO = 43; //~ ERROR cannot assign to `*OH_NO`, as `OH_NO` is an immutable static
 }
diff --git a/src/test/ui/consts/read_from_static_mut_ref.stderr b/src/test/ui/consts/read_from_static_mut_ref.stderr
index c936ac0b7d5..373220878ec 100644
--- a/src/test/ui/consts/read_from_static_mut_ref.stderr
+++ b/src/test/ui/consts/read_from_static_mut_ref.stderr
@@ -1,9 +1,16 @@
-error[E0764]: mutable references are not allowed in statics
-  --> $DIR/read_from_static_mut_ref.rs:5:26
+error[E0764]: mutable references are not allowed in final value of statics
+  --> $DIR/read_from_static_mut_ref.rs:4:26
    |
 LL | static OH_NO: &mut i32 = &mut 42;
-   |                          ^^^^^^^ `&mut` is only allowed in `const fn`
+   |                          ^^^^^^^
 
-error: aborting due to previous error
+error[E0594]: cannot assign to `*OH_NO`, as `OH_NO` is an immutable static item
+  --> $DIR/read_from_static_mut_ref.rs:7:5
+   |
+LL |     *OH_NO = 43;
+   |     ^^^^^^^^^^^ cannot assign
+
+error: aborting due to 2 previous errors
 
-For more information about this error, try `rustc --explain E0764`.
+Some errors have detailed explanations: E0594, E0764.
+For more information about an error, try `rustc --explain E0594`.
diff --git a/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr b/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr
index 36c280ca5c6..8db75dd63cf 100644
--- a/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr
+++ b/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr
@@ -1,9 +1,9 @@
-error[E0764]: mutable references are not allowed in statics
-  --> $DIR/static_mut_containing_mut_ref2.rs:7:46
+error[E0080]: could not evaluate static initializer
+  --> $DIR/static_mut_containing_mut_ref2.rs:7:45
    |
 LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
-   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
+   |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer
 
 error: aborting due to previous error
 
-For more information about this error, try `rustc --explain E0764`.
+For more information about this error, try `rustc --explain E0080`.
diff --git a/src/test/ui/consts/static_mut_containing_mut_ref2.rs b/src/test/ui/consts/static_mut_containing_mut_ref2.rs
index 2821d1a0154..61368546083 100644
--- a/src/test/ui/consts/static_mut_containing_mut_ref2.rs
+++ b/src/test/ui/consts/static_mut_containing_mut_ref2.rs
@@ -5,6 +5,7 @@
 static mut STDERR_BUFFER_SPACE: u8 = 0;
 
 pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
-//~^ ERROR  mutable references are not allowed in statics
+//[mut_refs]~^ ERROR could not evaluate static initializer
+//[stock]~^^ ERROR mutable references are not allowed in statics
 
 fn main() {}
diff --git a/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr b/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr
index 36c280ca5c6..5cdcea23231 100644
--- a/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr
+++ b/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr
@@ -1,9 +1,12 @@
-error[E0764]: mutable references are not allowed in statics
+error[E0658]: mutable references are not allowed in statics
   --> $DIR/static_mut_containing_mut_ref2.rs:7:46
    |
 LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
-   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
+   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error: aborting due to previous error
 
-For more information about this error, try `rustc --explain E0764`.
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/error-codes/E0017.rs b/src/test/ui/error-codes/E0017.rs
index 262f7bc72c7..c211ad1a2f8 100644
--- a/src/test/ui/error-codes/E0017.rs
+++ b/src/test/ui/error-codes/E0017.rs
@@ -2,12 +2,13 @@ static X: i32 = 1;
 const C: i32 = 2;
 static mut M: i32 = 3;
 
-const CR: &'static mut i32 = &mut C; //~ ERROR E0764
+const CR: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
                                      //~| WARN taking a mutable
-static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0764
+static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658
                                               //~| ERROR cannot borrow
+                                              //~| ERROR mutable references are not allowed
 
-static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
+static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
                                               //~| WARN taking a mutable
-static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0764
+static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR mutable references are not
 fn main() {}
diff --git a/src/test/ui/error-codes/E0017.stderr b/src/test/ui/error-codes/E0017.stderr
index ea591587e6d..9b094b19757 100644
--- a/src/test/ui/error-codes/E0017.stderr
+++ b/src/test/ui/error-codes/E0017.stderr
@@ -13,17 +13,26 @@ note: `const` item defined here
 LL | const C: i32 = 2;
    | ^^^^^^^^^^^^^^^^^
 
-error[E0764]: mutable references are not allowed in constants
+error[E0764]: mutable references are not allowed in final value of constants
   --> $DIR/E0017.rs:5:30
    |
 LL | const CR: &'static mut i32 = &mut C;
-   |                              ^^^^^^ `&mut` is only allowed in `const fn`
+   |                              ^^^^^^
 
-error[E0764]: mutable references are not allowed in statics
+error[E0658]: mutation through a reference is not allowed in statics
   --> $DIR/E0017.rs:7:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
-   |                                       ^^^^^^ `&mut` is only allowed in `const fn`
+   |                                       ^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
+
+error[E0764]: mutable references are not allowed in final value of statics
+  --> $DIR/E0017.rs:7:39
+   |
+LL | static STATIC_REF: &'static mut i32 = &mut X;
+   |                                       ^^^^^^
 
 error[E0596]: cannot borrow immutable static item `X` as mutable
   --> $DIR/E0017.rs:7:39
@@ -32,7 +41,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^ cannot borrow as mutable
 
 warning: taking a mutable reference to a `const` item
-  --> $DIR/E0017.rs:10:38
+  --> $DIR/E0017.rs:11:38
    |
 LL | static CONST_REF: &'static mut i32 = &mut C;
    |                                      ^^^^^^
@@ -45,19 +54,19 @@ note: `const` item defined here
 LL | const C: i32 = 2;
    | ^^^^^^^^^^^^^^^^^
 
-error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0017.rs:10:38
+error[E0764]: mutable references are not allowed in final value of statics
+  --> $DIR/E0017.rs:11:38
    |
 LL | static CONST_REF: &'static mut i32 = &mut C;
-   |                                      ^^^^^^ `&mut` is only allowed in `const fn`
+   |                                      ^^^^^^
 
-error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0017.rs:12:52
+error[E0764]: mutable references are not allowed in final value of statics
+  --> $DIR/E0017.rs:13:52
    |
 LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
-   |                                                    ^^^^^^ `&mut` is only allowed in `const fn`
+   |                                                    ^^^^^^
 
-error: aborting due to 5 previous errors; 2 warnings emitted
+error: aborting due to 6 previous errors; 2 warnings emitted
 
-Some errors have detailed explanations: E0596, E0764.
+Some errors have detailed explanations: E0596, E0658, E0764.
 For more information about an error, try `rustc --explain E0596`.
diff --git a/src/test/ui/error-codes/E0388.rs b/src/test/ui/error-codes/E0388.rs
index bb0c4979b9a..6049d95f0d2 100644
--- a/src/test/ui/error-codes/E0388.rs
+++ b/src/test/ui/error-codes/E0388.rs
@@ -1,12 +1,13 @@
 static X: i32 = 1;
 const C: i32 = 2;
 
-const CR: &'static mut i32 = &mut C; //~ ERROR E0764
+const CR: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
                                      //~| WARN taking a mutable
 static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR cannot borrow
-                                              //~| ERROR E0764
+                                              //~| ERROR E0658
+                                              //~| ERROR mutable references are not allowed
 
-static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
+static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
                                              //~| WARN taking a mutable
 
 fn main() {}
diff --git a/src/test/ui/error-codes/E0388.stderr b/src/test/ui/error-codes/E0388.stderr
index 73e0b139cd0..74d6a92e170 100644
--- a/src/test/ui/error-codes/E0388.stderr
+++ b/src/test/ui/error-codes/E0388.stderr
@@ -13,17 +13,26 @@ note: `const` item defined here
 LL | const C: i32 = 2;
    | ^^^^^^^^^^^^^^^^^
 
-error[E0764]: mutable references are not allowed in constants
+error[E0764]: mutable references are not allowed in final value of constants
   --> $DIR/E0388.rs:4:30
    |
 LL | const CR: &'static mut i32 = &mut C;
-   |                              ^^^^^^ `&mut` is only allowed in `const fn`
+   |                              ^^^^^^
 
-error[E0764]: mutable references are not allowed in statics
+error[E0658]: mutation through a reference is not allowed in statics
   --> $DIR/E0388.rs:6:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
-   |                                       ^^^^^^ `&mut` is only allowed in `const fn`
+   |                                       ^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
+
+error[E0764]: mutable references are not allowed in final value of statics
+  --> $DIR/E0388.rs:6:39
+   |
+LL | static STATIC_REF: &'static mut i32 = &mut X;
+   |                                       ^^^^^^
 
 error[E0596]: cannot borrow immutable static item `X` as mutable
   --> $DIR/E0388.rs:6:39
@@ -32,7 +41,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^ cannot borrow as mutable
 
 warning: taking a mutable reference to a `const` item
-  --> $DIR/E0388.rs:9:38
+  --> $DIR/E0388.rs:10:38
    |
 LL | static CONST_REF: &'static mut i32 = &mut C;
    |                                      ^^^^^^
@@ -45,13 +54,13 @@ note: `const` item defined here
 LL | const C: i32 = 2;
    | ^^^^^^^^^^^^^^^^^
 
-error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0388.rs:9:38
+error[E0764]: mutable references are not allowed in final value of statics
+  --> $DIR/E0388.rs:10:38
    |
 LL | static CONST_REF: &'static mut i32 = &mut C;
-   |                                      ^^^^^^ `&mut` is only allowed in `const fn`
+   |                                      ^^^^^^
 
-error: aborting due to 4 previous errors; 2 warnings emitted
+error: aborting due to 5 previous errors; 2 warnings emitted
 
-Some errors have detailed explanations: E0596, E0764.
+Some errors have detailed explanations: E0596, E0658, E0764.
 For more information about an error, try `rustc --explain E0596`.
diff --git a/src/test/ui/issues/issue-46604.rs b/src/test/ui/issues/issue-46604.rs
index 273187a5a13..6ec6e7bdcb8 100644
--- a/src/test/ui/issues/issue-46604.rs
+++ b/src/test/ui/issues/issue-46604.rs
@@ -1,4 +1,4 @@
-static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];   //~ ERROR E0764
+static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];   //~ ERROR mutable references are not allowed
 fn write<T: AsRef<[u8]>>(buffer: T) { }
 
 fn main() {
diff --git a/src/test/ui/issues/issue-46604.stderr b/src/test/ui/issues/issue-46604.stderr
index 5421721dec2..488be0e7731 100644
--- a/src/test/ui/issues/issue-46604.stderr
+++ b/src/test/ui/issues/issue-46604.stderr
@@ -1,8 +1,8 @@
-error[E0764]: mutable references are not allowed in statics
+error[E0764]: mutable references are not allowed in final value of statics
   --> $DIR/issue-46604.rs:1:25
    |
 LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
-   |                         ^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
+   |                         ^^^^^^^^^^^^^^^^^^^^
 
 error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item
   --> $DIR/issue-46604.rs:6:5
diff --git a/src/test/ui/never_type/issue-52443.stderr b/src/test/ui/never_type/issue-52443.stderr
index 051896cb89c..1683841e9d7 100644
--- a/src/test/ui/never_type/issue-52443.stderr
+++ b/src/test/ui/never_type/issue-52443.stderr
@@ -39,11 +39,14 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
 LL |     [(); { for _ in 0usize.. {}; 0}];
    |                     ^^^^^^^^
 
-error[E0764]: mutable references are not allowed in constants
+error[E0658]: mutable references are not allowed in constants
   --> $DIR/issue-52443.rs:9:21
    |
 LL |     [(); { for _ in 0usize.. {}; 0}];
-   |                     ^^^^^^^^ `&mut` is only allowed in `const fn`
+   |                     ^^^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
   --> $DIR/issue-52443.rs:9:21
@@ -53,5 +56,5 @@ LL |     [(); { for _ in 0usize.. {}; 0}];
 
 error: aborting due to 6 previous errors; 1 warning emitted
 
-Some errors have detailed explanations: E0015, E0308, E0744, E0764.
+Some errors have detailed explanations: E0015, E0308, E0658, E0744.
 For more information about an error, try `rustc --explain E0015`.
diff --git a/src/test/ui/unsafe/ranged_ints2_const.rs b/src/test/ui/unsafe/ranged_ints2_const.rs
index 65e0d79308c..b7178c2b52b 100644
--- a/src/test/ui/unsafe/ranged_ints2_const.rs
+++ b/src/test/ui/unsafe/ranged_ints2_const.rs
@@ -18,3 +18,9 @@ const fn bar() -> NonZero<u32> {
     let y = unsafe { &mut x.0 }; //~ ERROR mutable references
     unsafe { NonZero(1) }
 }
+
+const fn boo() -> NonZero<u32> {
+    let mut x = unsafe { NonZero(1) };
+    unsafe { let y = &mut x.0; } //~ ERROR mutable references
+    unsafe { NonZero(1) }
+}
diff --git a/src/test/ui/unsafe/ranged_ints2_const.stderr b/src/test/ui/unsafe/ranged_ints2_const.stderr
index 5ce4296458e..a0dc950e76d 100644
--- a/src/test/ui/unsafe/ranged_ints2_const.stderr
+++ b/src/test/ui/unsafe/ranged_ints2_const.stderr
@@ -16,6 +16,15 @@ LL |     let y = unsafe { &mut x.0 };
    = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
    = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
+error[E0658]: mutable references are not allowed in constant functions
+  --> $DIR/ranged_ints2_const.rs:24:22
+   |
+LL |     unsafe { let y = &mut x.0; }
+   |                      ^^^^^^^^
+   |
+   = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
+   = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
+
 error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
   --> $DIR/ranged_ints2_const.rs:11:13
    |
@@ -24,7 +33,7 @@ LL |     let y = &mut x.0;
    |
    = note: mutating layout constrained fields cannot statically be checked for valid values
 
-error: aborting due to 3 previous errors
+error: aborting due to 4 previous errors
 
 Some errors have detailed explanations: E0133, E0658.
 For more information about an error, try `rustc --explain E0133`.