Skip to content

Commit bd837e8

Browse files
committed
make sure we do not promote things with interior mutability
1 parent a68864b commit bd837e8

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/test/ui/consts/promote-not.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#![allow(unconditional_panic, const_err)]
44
#![feature(const_fn, const_fn_union)]
55

6+
use std::cell::Cell;
7+
68
// We do not promote mutable references.
79
static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]); //~ ERROR temporary value dropped while borrowed
810

@@ -32,4 +34,7 @@ const TEST_UNION: () = {
3234
let _x: &'static i32 = &unsafe { U { x: 0 }.x }; //~ ERROR temporary value dropped while borrowed
3335
};
3436

35-
fn main() {}
37+
fn main() {
38+
// We must not promote things with interior mutability.
39+
let _val: &'static _ = &(Cell::new(1), 2).0; //~ ERROR temporary value dropped while borrowed
40+
}

src/test/ui/consts/promote-not.stderr

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0716]: temporary value dropped while borrowed
2-
--> $DIR/promote-not.rs:7:50
2+
--> $DIR/promote-not.rs:9:50
33
|
44
LL | static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]);
55
| ----------^^^^^^^^^-
@@ -9,7 +9,7 @@ LL | static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]);
99
| using this value as a static requires that borrow lasts for `'static`
1010

1111
error[E0716]: temporary value dropped while borrowed
12-
--> $DIR/promote-not.rs:10:18
12+
--> $DIR/promote-not.rs:12:18
1313
|
1414
LL | let x = &mut [1,2,3];
1515
| ^^^^^^^ creates a temporary which is freed while still in use
@@ -19,7 +19,7 @@ LL | };
1919
| - temporary value is freed at the end of this statement
2020

2121
error[E0716]: temporary value dropped while borrowed
22-
--> $DIR/promote-not.rs:19:32
22+
--> $DIR/promote-not.rs:21:32
2323
|
2424
LL | let _x: &'static () = &foo();
2525
| ----------- ^^^^^ creates a temporary which is freed while still in use
@@ -29,7 +29,7 @@ LL | }
2929
| - temporary value is freed at the end of this statement
3030

3131
error[E0716]: temporary value dropped while borrowed
32-
--> $DIR/promote-not.rs:27:29
32+
--> $DIR/promote-not.rs:29:29
3333
|
3434
LL | let _x: &'static i32 = &unsafe { U { x: 0 }.x };
3535
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
@@ -39,7 +39,7 @@ LL | }
3939
| - temporary value is freed at the end of this statement
4040

4141
error[E0716]: temporary value dropped while borrowed
42-
--> $DIR/promote-not.rs:32:29
42+
--> $DIR/promote-not.rs:34:29
4343
|
4444
LL | let _x: &'static i32 = &unsafe { U { x: 0 }.x };
4545
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
@@ -48,6 +48,16 @@ LL | let _x: &'static i32 = &unsafe { U { x: 0 }.x };
4848
LL | };
4949
| - temporary value is freed at the end of this statement
5050

51-
error: aborting due to 5 previous errors
51+
error[E0716]: temporary value dropped while borrowed
52+
--> $DIR/promote-not.rs:39:29
53+
|
54+
LL | let _val: &'static _ = &(Cell::new(1), 2).0;
55+
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
56+
| |
57+
| type annotation requires that borrow lasts for `'static`
58+
LL | }
59+
| - temporary value is freed at the end of this statement
60+
61+
error: aborting due to 6 previous errors
5262

5363
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)