Skip to content

Commit 8fc76db

Browse files
committed
add more sensible mut-ref-to-immutable test
1 parent 725ef53 commit 8fc76db

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
@@ -31,8 +31,19 @@ LL | const IMMUT_MUT_REF: &mut u16 = unsafe { mem::transmute(&13) };
3131
HEX_DUMP
3232
}
3333

34+
error[E0080]: constructing invalid value: encountered mutable reference or box pointing to read-only memory
35+
--> $DIR/mut_ref_in_final.rs:29:1
36+
|
37+
LL | static IMMUT_MUT_REF_STATIC: &mut u16 = unsafe { mem::transmute(&13) };
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
39+
|
40+
= 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.
41+
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
42+
HEX_DUMP
43+
}
44+
3445
error[E0716]: temporary value dropped while borrowed
35-
--> $DIR/mut_ref_in_final.rs:50:65
46+
--> $DIR/mut_ref_in_final.rs:52:65
3647
|
3748
LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
3849
| -------------------------------^^--
@@ -42,7 +53,7 @@ LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
4253
| using this value as a constant requires that borrow lasts for `'static`
4354

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

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

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

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

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

8293
error[E0764]: mutable references are not allowed in the final value of constants
83-
--> $DIR/mut_ref_in_final.rs:75:53
94+
--> $DIR/mut_ref_in_final.rs:77:53
8495
|
8596
LL | const RAW_MUT_COERCE_C: SyncPtr<i32> = SyncPtr { x: &mut 0 };
8697
| ^^^^^^
8798

88-
error: aborting due to 11 previous errors
99+
error: aborting due to 12 previous errors
89100

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

0 commit comments

Comments
 (0)