clarify and unify 'transient mutable borrow' errors

This commit is contained in:
Ralf Jung
2025-06-18 10:04:43 +09:00
parent ff17a225e6
commit bade3fd058
31 changed files with 88 additions and 117 deletions

View File

@@ -124,12 +124,13 @@ const_eval_incompatible_return_types =
const_eval_incompatible_types =
calling a function with argument of type {$callee_ty} passing data of type {$caller_ty}
const_eval_interior_mutable_ref_escaping =
{const_eval_const_context}s cannot refer to interior mutable data
.label = this borrow of an interior mutable value may end up in the final value
const_eval_interior_mutable_borrow_escaping =
interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a {const_eval_const_context} are not allowed
.label = this borrow of an interior mutable value refers to a lifetime-extended temporary
.help = to fix this, the value can be extracted to a separate `static` item and then referenced
.teach_note =
References that escape into the final value of a constant or static must be immutable.
This creates a raw pointer to a temporary that has its lifetime extended to last for the entire program.
Lifetime-extended temporaries in constants and statics must be immutable.
This is to avoid accidentally creating shared mutable state.
@@ -213,26 +214,18 @@ const_eval_memory_exhausted =
const_eval_modified_global =
modifying a static's initial value from another static's initializer
const_eval_mutable_borrow_escaping =
mutable borrows of lifetime-extended temporaries in the top-level scope of a {const_eval_const_context} are not allowed
.teach_note =
This creates a reference to a temporary that has its lifetime extended to last for the entire program.
Lifetime-extended temporaries in constants and statics must be immutable.
This is to avoid accidentally creating shared mutable state.
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
const_eval_mutable_ptr_in_final = encountered mutable pointer in final value of {const_eval_intern_kind}
const_eval_mutable_raw_escaping =
raw mutable pointers are not allowed in the final value of {const_eval_const_context}s
.teach_note =
Pointers that escape into the final value of a constant or static must be immutable.
This is to avoid accidentally creating shared mutable state.
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
const_eval_mutable_ref_escaping =
mutable references are not allowed in the final value of {const_eval_const_context}s
.teach_note =
References that escape into the final value of a constant or static must be immutable.
This is to avoid accidentally creating shared mutable state.
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
const_eval_nested_static_in_thread_local = #[thread_local] does not support implicit nested statics, please create explicit static items and refer to them instead
const_eval_non_const_await =

View File

@@ -595,11 +595,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
self.const_kind() == hir::ConstContext::Static(hir::Mutability::Mut);
if !is_allowed && self.place_may_escape(place) {
self.check_op(ops::EscapingMutBorrow(if matches!(rvalue, Rvalue::Ref(..)) {
hir::BorrowKind::Ref
} else {
hir::BorrowKind::Raw
}));
self.check_op(ops::EscapingMutBorrow);
}
}

View File

@@ -567,7 +567,7 @@ impl<'tcx> NonConstOp<'tcx> for EscapingCellBorrow {
DiagImportance::Secondary
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
ccx.dcx().create_err(errors::InteriorMutableRefEscaping {
ccx.dcx().create_err(errors::InteriorMutableBorrowEscaping {
span,
opt_help: matches!(ccx.const_kind(), hir::ConstContext::Static(_)),
kind: ccx.const_kind(),
@@ -580,7 +580,7 @@ impl<'tcx> NonConstOp<'tcx> for EscapingCellBorrow {
/// 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(crate) struct EscapingMutBorrow(pub hir::BorrowKind);
pub(crate) struct EscapingMutBorrow;
impl<'tcx> NonConstOp<'tcx> for EscapingMutBorrow {
fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
@@ -594,21 +594,12 @@ impl<'tcx> NonConstOp<'tcx> for EscapingMutBorrow {
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
match self.0 {
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::MutableRawEscaping {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0764),
}),
hir::BorrowKind::Ref | hir::BorrowKind::Pin => {
ccx.dcx().create_err(errors::MutableRefEscaping {
ccx.dcx().create_err(errors::MutableBorrowEscaping {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0764),
})
}
}
}
}
/// A call to a `panic()` lang item where the first argument is _not_ a `&str`.

View File

@@ -150,8 +150,8 @@ pub(crate) struct UnmarkedIntrinsicExposed {
}
#[derive(Diagnostic)]
#[diag(const_eval_mutable_ref_escaping, code = E0764)]
pub(crate) struct MutableRefEscaping {
#[diag(const_eval_mutable_borrow_escaping, code = E0764)]
pub(crate) struct MutableBorrowEscaping {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
@@ -159,15 +159,6 @@ pub(crate) struct MutableRefEscaping {
pub teach: bool,
}
#[derive(Diagnostic)]
#[diag(const_eval_mutable_raw_escaping, code = E0764)]
pub(crate) struct MutableRawEscaping {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
#[note(const_eval_teach_note)]
pub teach: bool,
}
#[derive(Diagnostic)]
#[diag(const_eval_non_const_fmt_macro_call, code = E0015)]
pub(crate) struct NonConstFmtMacroCall {
@@ -225,8 +216,8 @@ pub(crate) struct UnallowedInlineAsm {
}
#[derive(Diagnostic)]
#[diag(const_eval_interior_mutable_ref_escaping, code = E0492)]
pub(crate) struct InteriorMutableRefEscaping {
#[diag(const_eval_interior_mutable_borrow_escaping, code = E0492)]
pub(crate) struct InteriorMutableBorrowEscaping {
#[primary_span]
#[label]
pub span: Span,

View File

@@ -1,7 +1,7 @@
use std::mem::{transmute, ManuallyDrop};
const S: &'static mut str = &mut " hello ";
//~^ ERROR: mutable references are not allowed in the final value of constants
//~^ ERROR: mutable borrows of lifetime-extended temporaries
const fn trigger() -> [(); unsafe {
let s = transmute::<(*const u8, usize), &ManuallyDrop<str>>((S.as_ptr(), 3));

View File

@@ -1,4 +1,4 @@
error[E0764]: mutable references are not allowed in the final value of constants
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/issue-76510.rs:3:29
|
LL | const S: &'static mut str = &mut " hello ";

View File

@@ -12,13 +12,13 @@ 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
const B: *mut i32 = &mut 4; //~ ERROR mutable borrows of lifetime-extended temporaries
// Ok, no actual mutable allocation exists
const B2: Option<&mut i32> = None;
// Not ok, can't prove that no mutable allocation ends up in final value
const B3: Option<&mut i32> = Some(&mut 42); //~ ERROR mutable references are not allowed
const B3: Option<&mut i32> = Some(&mut 42); //~ ERROR mutable borrows of lifetime-extended temporaries
const fn helper(x: &mut i32) -> Option<&mut i32> { Some(x) }
const B4: Option<&mut i32> = helper(&mut 42); //~ ERROR temporary value dropped while borrowed
@@ -29,7 +29,7 @@ const IMMUT_MUT_REF: &mut u16 = unsafe { mem::transmute(&13) };
static IMMUT_MUT_REF_STATIC: &mut u16 = unsafe { mem::transmute(&13) };
//~^ ERROR pointing to read-only memory
// Ok, because no references to mutable data exist here, since the `{}` moves
// Ok, because no borrows of mutable data exist here, since the `{}` moves
// its value and then takes a reference to that.
const C: *const i32 = &{
let mut x = 42;
@@ -69,13 +69,13 @@ unsafe impl<T> Sync for SyncPtr<T> {}
// (This relies on `SyncPtr` being a curly brace struct.)
// However, we intern the inner memory as read-only, so this must be rejected.
static RAW_MUT_CAST_S: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
//~^ ERROR mutable references are not allowed
//~^ ERROR mutable borrows of lifetime-extended temporaries
static RAW_MUT_COERCE_S: SyncPtr<i32> = SyncPtr { x: &mut 0 };
//~^ ERROR mutable references are not allowed
//~^ ERROR mutable borrows of lifetime-extended temporaries
const RAW_MUT_CAST_C: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
//~^ ERROR mutable references are not allowed
//~^ ERROR mutable borrows of lifetime-extended temporaries
const RAW_MUT_COERCE_C: SyncPtr<i32> = SyncPtr { x: &mut 0 };
//~^ ERROR mutable references are not allowed
//~^ ERROR mutable borrows of lifetime-extended temporaries
fn main() {
println!("{}", unsafe { *A });

View File

@@ -1,10 +1,10 @@
error[E0764]: mutable references are not allowed in the final value of constants
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/mut_ref_in_final.rs:15:21
|
LL | const B: *mut i32 = &mut 4;
| ^^^^^^
error[E0764]: mutable references are not allowed in the final value of constants
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/mut_ref_in_final.rs:21:35
|
LL | const B3: Option<&mut i32> = Some(&mut 42);
@@ -72,25 +72,25 @@ LL | static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
| | creates a temporary value which is freed while still in use
| using this value as a static requires that borrow lasts for `'static`
error[E0764]: mutable references are not allowed in the final value of statics
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/mut_ref_in_final.rs:71:53
|
LL | static RAW_MUT_CAST_S: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
| ^^^^^^^
error[E0764]: mutable references are not allowed in the final value of statics
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/mut_ref_in_final.rs:73:54
|
LL | static RAW_MUT_COERCE_S: SyncPtr<i32> = SyncPtr { x: &mut 0 };
| ^^^^^^
error[E0764]: mutable references are not allowed in the final value of constants
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/mut_ref_in_final.rs:75:52
|
LL | const RAW_MUT_CAST_C: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
| ^^^^^^^
error[E0764]: mutable references are not allowed in the final value of constants
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/mut_ref_in_final.rs:77:53
|
LL | const RAW_MUT_COERCE_C: SyncPtr<i32> = SyncPtr { x: &mut 0 };

View File

@@ -7,11 +7,11 @@ LL |
LL | };
| - value is dropped here
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/const-promoted-opaque.rs:36:19
|
LL | const BAZ: &Foo = &FOO;
| ^^^^ this borrow of an interior mutable value may end up in the final value
| ^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error[E0716]: temporary value dropped while borrowed
--> $DIR/const-promoted-opaque.rs:40:26

View File

@@ -34,7 +34,7 @@ const BAR: () = {
};
const BAZ: &Foo = &FOO;
//[atomic]~^ ERROR: constants cannot refer to interior mutable data
//[atomic]~^ ERROR: interior mutable shared borrows of lifetime-extended temporaries
fn main() {
let _: &'static _ = &FOO;

View File

@@ -5,7 +5,7 @@
#![allow(static_mut_refs)]
const C1: &'static mut [usize] = &mut [];
//~^ ERROR: mutable references are not allowed
//~^ ERROR: mutable borrows of lifetime-extended temporaries
static mut S: i32 = 3;
const C2: &'static mut i32 = unsafe { &mut S };

View File

@@ -1,4 +1,4 @@
error[E0764]: mutable references are not allowed in the final value of constants
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/issue-17718-const-bad-values.rs:7:34
|
LL | const C1: &'static mut [usize] = &mut [];

View File

@@ -2,13 +2,13 @@ use std::cell::UnsafeCell;
const A: UnsafeCell<usize> = UnsafeCell::new(1);
const B: &'static UnsafeCell<usize> = &A;
//~^ ERROR: cannot refer to interior mutable
//~^ ERROR: interior mutable shared borrows of lifetime-extended temporaries
struct C { a: UnsafeCell<usize> }
const D: C = C { a: UnsafeCell::new(1) };
const E: &'static UnsafeCell<usize> = &D.a;
//~^ ERROR: cannot refer to interior mutable
//~^ ERROR: interior mutable shared borrows of lifetime-extended temporaries
const F: &'static C = &D;
//~^ ERROR: cannot refer to interior mutable
//~^ ERROR: interior mutable shared borrows of lifetime-extended temporaries
fn main() {}

View File

@@ -1,20 +1,20 @@
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/issue-17718-const-borrow.rs:4:39
|
LL | const B: &'static UnsafeCell<usize> = &A;
| ^^ this borrow of an interior mutable value may end up in the final value
| ^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/issue-17718-const-borrow.rs:9:39
|
LL | const E: &'static UnsafeCell<usize> = &D.a;
| ^^^^ this borrow of an interior mutable value may end up in the final value
| ^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/issue-17718-const-borrow.rs:11:23
|
LL | const F: &'static C = &D;
| ^^ this borrow of an interior mutable value may end up in the final value
| ^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error: aborting due to 3 previous errors

View File

@@ -3,7 +3,7 @@ use std::cell::Cell;
const FOO: &(Cell<usize>, bool) = {
let mut a = (Cell::new(0), false);
a.1 = true; // sets `qualif(a)` to `qualif(a) | qualif(true)`
&{a} //~ ERROR cannot refer to interior mutable
&{a} //~ ERROR interior mutable shared borrows of lifetime-extended temporaries
};
fn main() {}

View File

@@ -1,8 +1,8 @@
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/partial_qualif.rs:6:5
|
LL | &{a}
| ^^^^ this borrow of an interior mutable value may end up in the final value
| ^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error: aborting due to 1 previous error

View File

@@ -7,7 +7,7 @@ use std::cell::Cell;
const FOO: &Option<Cell<usize>> = {
let mut a = Some(Cell::new(0));
a = None; // sets `qualif(a)` to `qualif(a) | qualif(None)`
&{a} //~ ERROR cannot refer to interior mutable
&{a} //~ ERROR interior mutable shared borrows of lifetime-extended temporaries
};
fn main() {}

View File

@@ -1,8 +1,8 @@
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/qualif_overwrite.rs:10:5
|
LL | &{a}
| ^^^^ this borrow of an interior mutable value may end up in the final value
| ^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error: aborting due to 1 previous error

View File

@@ -5,7 +5,7 @@ use std::cell::Cell;
const FOO: &Option<Cell<usize>> = {
let mut a = (Some(Cell::new(0)),);
a.0 = None; // sets `qualif(a)` to `qualif(a) | qualif(None)`
&{a.0} //~ ERROR cannot refer to interior mutable
&{a.0} //~ ERROR interior mutable shared borrows of lifetime-extended temporaries
};
fn main() {}

View File

@@ -1,8 +1,8 @@
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/qualif_overwrite_2.rs:8:5
|
LL | &{a.0}
| ^^^^^^ this borrow of an interior mutable value may end up in the final value
| ^^^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error: aborting due to 1 previous error

View File

@@ -11,9 +11,9 @@ unsafe impl<T> Sync for SyncPtr<T> {}
// The resulting constant would pass all validation checks, so it is crucial that this gets rejected
// by static const checks!
static RAW_SYNC_S: SyncPtr<Cell<i32>> = SyncPtr { x: &Cell::new(42) };
//~^ ERROR: cannot refer to interior mutable data
//~^ ERROR: interior mutable shared borrows of lifetime-extended temporaries
const RAW_SYNC_C: SyncPtr<Cell<i32>> = SyncPtr { x: &Cell::new(42) };
//~^ ERROR: cannot refer to interior mutable data
//~^ ERROR: interior mutable shared borrows of lifetime-extended temporaries
// This one does not get promoted because of `Drop`, and then enters interesting codepaths because
// as a value it has no interior mutability, but as a type it does. See
@@ -39,7 +39,7 @@ const NONE_EXPLICIT_PROMOTED: &'static Option<Cell<i32>> = {
// Not okay, since we are borrowing something with interior mutability.
const INTERIOR_MUT_VARIANT: &Option<UnsafeCell<bool>> = &{
//~^ERROR: cannot refer to interior mutable data
//~^ERROR: interior mutable shared borrows of lifetime-extended temporaries
let mut x = None;
assert!(x.is_none());
x = Some(UnsafeCell::new(false));

View File

@@ -1,18 +1,18 @@
error[E0492]: statics cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/refs-to-cell-in-final.rs:13:54
|
LL | static RAW_SYNC_S: SyncPtr<Cell<i32>> = SyncPtr { x: &Cell::new(42) };
| ^^^^^^^^^^^^^^ this borrow of an interior mutable value may end up in the final value
| ^^^^^^^^^^^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
|
= help: to fix this, the value can be extracted to a separate `static` item and then referenced
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/refs-to-cell-in-final.rs:15:53
|
LL | const RAW_SYNC_C: SyncPtr<Cell<i32>> = SyncPtr { x: &Cell::new(42) };
| ^^^^^^^^^^^^^^ this borrow of an interior mutable value may end up in the final value
| ^^^^^^^^^^^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/refs-to-cell-in-final.rs:41:57
|
LL | const INTERIOR_MUT_VARIANT: &Option<UnsafeCell<bool>> = &{
@@ -23,7 +23,7 @@ LL | | assert!(x.is_none());
LL | | x = Some(UnsafeCell::new(false));
LL | | x
LL | | };
| |_^ this borrow of an interior mutable value may end up in the final value
| |_^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error: aborting due to 3 previous errors

View File

@@ -1,4 +1,4 @@
static OH_NO: &mut i32 = &mut 42; //~ ERROR mutable references are not allowed
static OH_NO: &mut i32 = &mut 42; //~ ERROR mutable borrows of lifetime-extended temporaries
fn main() {
assert_eq!(*OH_NO, 42);
*OH_NO = 43; //~ ERROR cannot assign to `*OH_NO`, as `OH_NO` is an immutable static

View File

@@ -1,4 +1,4 @@
error[E0764]: mutable references are not allowed in the final value of statics
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/write_to_static_via_mut_ref.rs:1:26
|
LL | static OH_NO: &mut i32 = &mut 42;

View File

@@ -5,12 +5,12 @@ static X: i32 = 1;
const C: i32 = 2;
static mut M: i32 = 3;
const CR: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
const CR: &'static mut i32 = &mut C; //~ ERROR mutable borrows of lifetime-extended temporaries
//~| WARN taking a mutable
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR cannot borrow immutable static item `X` as mutable
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable borrows of lifetime-extended temporaries
//~| WARN taking a mutable
fn main() {}

View File

@@ -13,7 +13,7 @@ LL | const C: i32 = 2;
| ^^^^^^^^^^^^
= note: `#[warn(const_item_mutation)]` on by default
error[E0764]: mutable references are not allowed in the final value of constants
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/E0017.rs:8:30
|
LL | const CR: &'static mut i32 = &mut C;
@@ -39,7 +39,7 @@ note: `const` item defined here
LL | const C: i32 = 2;
| ^^^^^^^^^^^^
error[E0764]: mutable references are not allowed in the final value of statics
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/E0017.rs:13:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;

View File

@@ -1,14 +1,14 @@
error[E0492]: constants cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a constant are not allowed
--> $DIR/E0492.rs:4:33
|
LL | const B: &'static AtomicUsize = &A;
| ^^ this borrow of an interior mutable value may end up in the final value
| ^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
error[E0492]: statics cannot refer to interior mutable data
error[E0492]: interior mutable shared borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/E0492.rs:5:34
|
LL | static C: &'static AtomicUsize = &A;
| ^^ this borrow of an interior mutable value may end up in the final value
| ^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
|
= help: to fix this, the value can be extracted to a separate `static` item and then referenced

View File

@@ -1,4 +1,4 @@
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR mutable references are not allowed
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR mutable borrows of lifetime-extended temporaries
fn write<T: AsRef<[u8]>>(buffer: T) { }
fn main() {

View File

@@ -1,4 +1,4 @@
error[E0764]: mutable references are not allowed in the final value of statics
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/issue-46604.rs:1:25
|
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];

View File

@@ -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
//~^ ERROR mutable borrows of lifetime-extended temporaries
pub fn main() { }

View File

@@ -1,4 +1,4 @@
error[E0764]: mutable references are not allowed in the final value of statics
error[E0764]: mutable borrows of lifetime-extended temporaries in the top-level scope of a static are not allowed
--> $DIR/check-immutable-mut-slices.rs:3:37
|
LL | static TEST: &'static mut [isize] = &mut [];