Skip to content

Commit 42b121f

Browse files
committed
clarify and unify 'transient mutable borrow' errors
1 parent 5c945c9 commit 42b121f

28 files changed

+68
-65
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ const_eval_incompatible_types =
125125
calling a function with argument of type {$callee_ty} passing data of type {$caller_ty}
126126
127127
const_eval_interior_mutable_ref_escaping =
128-
{const_eval_const_context}s cannot refer to interior mutable data
129-
.label = this borrow of an interior mutable value may end up in the final value
128+
shared references to lifetime-extended temporaries with interior mutability are not allowed in {const_eval_const_context}s
129+
.label = this borrow of an interior mutable value refers to a lifetime-extended temporary
130130
.help = to fix this, the value can be extracted to a separate `static` item and then referenced
131131
.teach_note =
132-
References that escape into the final value of a constant or static must be immutable.
132+
This creates a raw pointer to a temporary that has its lifetime extended to last for the entire program.
133+
Lifetime-extended temporaries in constants and statics must be immutable.
133134
This is to avoid accidentally creating shared mutable state.
134135
135136
@@ -216,18 +217,20 @@ const_eval_modified_global =
216217
const_eval_mutable_ptr_in_final = encountered mutable pointer in final value of {const_eval_intern_kind}
217218
218219
const_eval_mutable_raw_escaping =
219-
raw mutable pointers are not allowed in the final value of {const_eval_const_context}s
220+
raw mutable pointers to lifetime-extended temporaries are not allowed in {const_eval_const_context}s
220221
.teach_note =
221-
Pointers that escape into the final value of a constant or static must be immutable.
222+
This creates a raw pointer to a temporary that has its lifetime extended to last for the entire program.
223+
Lifetime-extended temporaries in constants and statics must be immutable.
222224
This is to avoid accidentally creating shared mutable state.
223225
224226
225227
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
226228
227229
const_eval_mutable_ref_escaping =
228-
mutable references are not allowed in the final value of {const_eval_const_context}s
230+
mutable references to lifetime-extended temporaries are not allowed in {const_eval_const_context}s
229231
.teach_note =
230-
References that escape into the final value of a constant or static must be immutable.
232+
This creates a reference to a temporary that has its lifetime extended to last for the entire program.
233+
Lifetime-extended temporaries in constants and statics must be immutable.
231234
This is to avoid accidentally creating shared mutable state.
232235
233236

tests/ui/consts/const-mut-refs/issue-76510.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem::{transmute, ManuallyDrop};
22

33
const S: &'static mut str = &mut " hello ";
4-
//~^ ERROR: mutable references are not allowed in the final value of constants
4+
//~^ ERROR: mutable references to lifetime-extended temporaries
55

66
const fn trigger() -> [(); unsafe {
77
let s = transmute::<(*const u8, usize), &ManuallyDrop<str>>((S.as_ptr(), 3));

tests/ui/consts/const-mut-refs/issue-76510.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0764]: mutable references are not allowed in the final value of constants
1+
error[E0764]: mutable references to lifetime-extended temporaries are not allowed in constants
22
--> $DIR/issue-76510.rs:3:29
33
|
44
LL | const S: &'static mut str = &mut " hello ";

tests/ui/consts/const-mut-refs/mut_ref_in_final.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const A: *const i32 = &4;
1212
// It could be made sound to allow it to compile,
1313
// but we do not want to allow this to compile,
1414
// as that would be an enormous footgun in oli-obk's opinion.
15-
const B: *mut i32 = &mut 4; //~ ERROR mutable references are not allowed
15+
const B: *mut i32 = &mut 4; //~ ERROR mutable references to lifetime-extended temporaries
1616

1717
// Ok, no actual mutable allocation exists
1818
const B2: Option<&mut i32> = None;
@@ -69,13 +69,13 @@ unsafe impl<T> Sync for SyncPtr<T> {}
6969
// (This relies on `SyncPtr` being a curly brace struct.)
7070
// However, we intern the inner memory as read-only, so this must be rejected.
7171
static RAW_MUT_CAST_S: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
72-
//~^ ERROR mutable references are not allowed
72+
//~^ ERROR mutable references to lifetime-extended temporaries
7373
static RAW_MUT_COERCE_S: SyncPtr<i32> = SyncPtr { x: &mut 0 };
74-
//~^ ERROR mutable references are not allowed
74+
//~^ ERROR mutable references to lifetime-extended temporaries
7575
const RAW_MUT_CAST_C: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
76-
//~^ ERROR mutable references are not allowed
76+
//~^ ERROR mutable references to lifetime-extended temporaries
7777
const RAW_MUT_COERCE_C: SyncPtr<i32> = SyncPtr { x: &mut 0 };
78-
//~^ ERROR mutable references are not allowed
78+
//~^ ERROR mutable references to lifetime-extended temporaries
7979

8080
fn main() {
8181
println!("{}", unsafe { *A });

tests/ui/consts/const-mut-refs/mut_ref_in_final.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0764]: mutable references are not allowed in the final value of constants
1+
error[E0764]: mutable references to lifetime-extended temporaries are not allowed in constants
22
--> $DIR/mut_ref_in_final.rs:15:21
33
|
44
LL | const B: *mut i32 = &mut 4;
@@ -76,25 +76,25 @@ LL | static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
7676
| | creates a temporary value which is freed while still in use
7777
| using this value as a static requires that borrow lasts for `'static`
7878

79-
error[E0764]: mutable references are not allowed in the final value of statics
79+
error[E0764]: mutable references to lifetime-extended temporaries are not allowed in statics
8080
--> $DIR/mut_ref_in_final.rs:71:53
8181
|
8282
LL | static RAW_MUT_CAST_S: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
8383
| ^^^^^^^
8484

85-
error[E0764]: mutable references are not allowed in the final value of statics
85+
error[E0764]: mutable references to lifetime-extended temporaries are not allowed in statics
8686
--> $DIR/mut_ref_in_final.rs:73:54
8787
|
8888
LL | static RAW_MUT_COERCE_S: SyncPtr<i32> = SyncPtr { x: &mut 0 };
8989
| ^^^^^^
9090

91-
error[E0764]: mutable references are not allowed in the final value of constants
91+
error[E0764]: mutable references to lifetime-extended temporaries are not allowed in constants
9292
--> $DIR/mut_ref_in_final.rs:75:52
9393
|
9494
LL | const RAW_MUT_CAST_C: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
9595
| ^^^^^^^
9696

97-
error[E0764]: mutable references are not allowed in the final value of constants
97+
error[E0764]: mutable references to lifetime-extended temporaries are not allowed in constants
9898
--> $DIR/mut_ref_in_final.rs:77:53
9999
|
100100
LL | const RAW_MUT_COERCE_C: SyncPtr<i32> = SyncPtr { x: &mut 0 };

tests/ui/consts/const-promoted-opaque.atomic.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL |
77
LL | };
88
| - value is dropped here
99

10-
error[E0492]: constants cannot refer to interior mutable data
10+
error[E0492]: shared references to lifetime-extended temporaries with interior mutability are not allowed in constants
1111
--> $DIR/const-promoted-opaque.rs:36:19
1212
|
1313
LL | const BAZ: &Foo = &FOO;
14-
| ^^^^ this borrow of an interior mutable value may end up in the final value
14+
| ^^^^ this borrow of an interior mutable value refers to a lifetime-extended temporary
1515

1616
error[E0716]: temporary value dropped while borrowed
1717
--> $DIR/const-promoted-opaque.rs:40:26

tests/ui/consts/const-promoted-opaque.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const BAR: () = {
3434
};
3535

3636
const BAZ: &Foo = &FOO;
37-
//[atomic]~^ ERROR: constants cannot refer to interior mutable data
37+
//[atomic]~^ ERROR: shared references to lifetime-extended temporaries with interior mutability
3838

3939
fn main() {
4040
let _: &'static _ = &FOO;

tests/ui/consts/issue-17718-const-bad-values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(static_mut_refs)]
66

77
const C1: &'static mut [usize] = &mut [];
8-
//~^ ERROR: mutable references are not allowed
8+
//~^ ERROR: mutable references to lifetime-extended temporaries
99

1010
static mut S: i32 = 3;
1111
const C2: &'static mut i32 = unsafe { &mut S };

tests/ui/consts/issue-17718-const-bad-values.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0764]: mutable references are not allowed in the final value of constants
1+
error[E0764]: mutable references to lifetime-extended temporaries are not allowed in constants
22
--> $DIR/issue-17718-const-bad-values.rs:7:34
33
|
44
LL | const C1: &'static mut [usize] = &mut [];

tests/ui/consts/issue-17718-const-borrow.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::cell::UnsafeCell;
22

33
const A: UnsafeCell<usize> = UnsafeCell::new(1);
44
const B: &'static UnsafeCell<usize> = &A;
5-
//~^ ERROR: cannot refer to interior mutable
5+
//~^ ERROR: shared references to lifetime-extended temporaries with interior mutability
66

77
struct C { a: UnsafeCell<usize> }
88
const D: C = C { a: UnsafeCell::new(1) };
99
const E: &'static UnsafeCell<usize> = &D.a;
10-
//~^ ERROR: cannot refer to interior mutable
10+
//~^ ERROR: shared references to lifetime-extended temporaries with interior mutability
1111
const F: &'static C = &D;
12-
//~^ ERROR: cannot refer to interior mutable
12+
//~^ ERROR: shared references to lifetime-extended temporaries with interior mutability
1313

1414
fn main() {}

0 commit comments

Comments
 (0)