Skip to content

Commit 5c945c9

Browse files
committed
add more sensible mut-ref-to-immutable test
1 parent 4af530e commit 5c945c9

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const B4: Option<&mut i32> = helper(&mut 42); //~ ERROR temporary value dropped
2626
// Not ok, since it points to read-only memory.
2727
const IMMUT_MUT_REF: &mut u16 = unsafe { mem::transmute(&13) };
2828
//~^ ERROR pointing to read-only memory
29+
static IMMUT_MUT_REF_STATIC: &mut u16 = unsafe { mem::transmute(&13) };
30+
//~^ ERROR pointing to read-only memory
2931

3032
// Ok, because no references to mutable data exist here, since the `{}` moves
3133
// its value and then takes a reference to that.

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,19 @@ LL | const IMMUT_MUT_REF: &mut u16 = unsafe { mem::transmute(&13) };
3535
HEX_DUMP
3636
}
3737

38+
error[E0080]: constructing invalid value: encountered mutable reference or box pointing to read-only memory
39+
--> $DIR/mut_ref_in_final.rs:29:1
40+
|
41+
LL | static IMMUT_MUT_REF_STATIC: &mut u16 = unsafe { mem::transmute(&13) };
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
43+
|
44+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
45+
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
46+
HEX_DUMP
47+
}
48+
3849
error[E0716]: temporary value dropped while borrowed
39-
--> $DIR/mut_ref_in_final.rs:50:65
50+
--> $DIR/mut_ref_in_final.rs:52:65
4051
|
4152
LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
4253
| -------------------------------^^--
@@ -46,7 +57,7 @@ LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
4657
| using this value as a constant requires that borrow lasts for `'static`
4758

4859
error[E0716]: temporary value dropped while borrowed
49-
--> $DIR/mut_ref_in_final.rs:53:67
60+
--> $DIR/mut_ref_in_final.rs:55:67
5061
|
5162
LL | static FOO2: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
5263
| -------------------------------^^--
@@ -56,7 +67,7 @@ LL | static FOO2: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
5667
| using this value as a static requires that borrow lasts for `'static`
5768

5869
error[E0716]: temporary value dropped while borrowed
59-
--> $DIR/mut_ref_in_final.rs:56:71
70+
--> $DIR/mut_ref_in_final.rs:58:71
6071
|
6172
LL | static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
6273
| -------------------------------^^--
@@ -66,30 +77,30 @@ LL | static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
6677
| using this value as a static requires that borrow lasts for `'static`
6778

6879
error[E0764]: mutable references are not allowed in the final value of statics
69-
--> $DIR/mut_ref_in_final.rs:69:53
80+
--> $DIR/mut_ref_in_final.rs:71:53
7081
|
7182
LL | static RAW_MUT_CAST_S: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
7283
| ^^^^^^^
7384

7485
error[E0764]: mutable references are not allowed in the final value of statics
75-
--> $DIR/mut_ref_in_final.rs:71:54
86+
--> $DIR/mut_ref_in_final.rs:73:54
7687
|
7788
LL | static RAW_MUT_COERCE_S: SyncPtr<i32> = SyncPtr { x: &mut 0 };
7889
| ^^^^^^
7990

8091
error[E0764]: mutable references are not allowed in the final value of constants
81-
--> $DIR/mut_ref_in_final.rs:73:52
92+
--> $DIR/mut_ref_in_final.rs:75:52
8293
|
8394
LL | const RAW_MUT_CAST_C: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
8495
| ^^^^^^^
8596

8697
error[E0764]: mutable references are not allowed in the final value of constants
87-
--> $DIR/mut_ref_in_final.rs:75:53
98+
--> $DIR/mut_ref_in_final.rs:77:53
8899
|
89100
LL | const RAW_MUT_COERCE_C: SyncPtr<i32> = SyncPtr { x: &mut 0 };
90101
| ^^^^^^
91102

92-
error: aborting due to 11 previous errors
103+
error: aborting due to 12 previous errors
93104

94105
Some errors have detailed explanations: E0080, E0716, E0764.
95106
For more information about an error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)