Skip to content

Commit 2611fac

Browse files
committed
add back some more attempts at having &mut in the final value of a const/static
1 parent f68e79d commit 2611fac

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,25 @@ static mut BUFFER: i32 = 42;
1414
const fn helper() -> Option<&'static mut i32> { unsafe {
1515
Some(&mut *std::ptr::addr_of_mut!(BUFFER))
1616
} }
17-
const A: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value
17+
const MUT: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value
1818
//~^ encountered mutable reference
19-
static A_STATIC: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value
19+
static MUT_STATIC: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value
2020
//~^ encountered mutable reference
2121

22+
const fn helper_int2ptr() -> Option<&'static mut i32> { unsafe {
23+
// Undefined behaviour (integer as pointer), who doesn't love tests like this.
24+
Some(&mut *(42 as *mut i32))
25+
} }
26+
const INT2PTR: Option<&mut i32> = helper_int2ptr(); //~ ERROR it is undefined behavior to use this value
27+
//~^ encountered a dangling reference
28+
static INT2PTR_STATIC: Option<&mut i32> = helper_int2ptr(); //~ ERROR it is undefined behavior to use this value
29+
//~^ encountered a dangling reference
30+
31+
const fn helper_dangling() -> Option<&'static mut i32> { unsafe {
32+
// Undefined behaviour (dangling pointer), who doesn't love tests like this.
33+
Some(&mut *(&mut 42 as *mut i32))
34+
} }
35+
const DANGLING: Option<&mut i32> = helper_dangling(); //~ ERROR encountered dangling pointer
36+
static DANGLING_STATIC: Option<&mut i32> = helper_dangling(); //~ ERROR encountered dangling pointer
37+
2238
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0080]: it is undefined behavior to use this value
22
--> $DIR/mut_ref_in_final_dynamic_check.rs:17:1
33
|
4-
LL | const A: Option<&mut i32> = helper();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered mutable reference in a `const` or `static`
4+
LL | const MUT: Option<&mut i32> = helper();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered mutable reference in a `const` or `static`
66
|
77
= 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.
88
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
@@ -12,14 +12,48 @@ LL | const A: Option<&mut i32> = helper();
1212
error[E0080]: it is undefined behavior to use this value
1313
--> $DIR/mut_ref_in_final_dynamic_check.rs:19:1
1414
|
15-
LL | static A_STATIC: Option<&mut i32> = helper();
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered mutable reference in a `const` or `static`
15+
LL | static MUT_STATIC: Option<&mut i32> = helper();
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered mutable reference in a `const` or `static`
1717
|
1818
= 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.
1919
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
2020
HEX_DUMP
2121
}
2222

23-
error: aborting due to 2 previous errors
23+
error[E0080]: it is undefined behavior to use this value
24+
--> $DIR/mut_ref_in_final_dynamic_check.rs:26:1
25+
|
26+
LL | const INT2PTR: Option<&mut i32> = helper_int2ptr();
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (0x2a[noalloc] has no provenance)
28+
|
29+
= 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.
30+
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
31+
HEX_DUMP
32+
}
33+
34+
error[E0080]: it is undefined behavior to use this value
35+
--> $DIR/mut_ref_in_final_dynamic_check.rs:28:1
36+
|
37+
LL | static INT2PTR_STATIC: Option<&mut i32> = helper_int2ptr();
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (0x2a[noalloc] has no provenance)
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+
45+
error: encountered dangling pointer in final value of constant
46+
--> $DIR/mut_ref_in_final_dynamic_check.rs:35:1
47+
|
48+
LL | const DANGLING: Option<&mut i32> = helper_dangling();
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
error: encountered dangling pointer in final value of static
52+
--> $DIR/mut_ref_in_final_dynamic_check.rs:36:1
53+
|
54+
LL | static DANGLING_STATIC: Option<&mut i32> = helper_dangling();
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
error: aborting due to 6 previous errors
2458

2559
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)