Skip to content

Commit 22d937d

Browse files
Sanity check for move from an uninit variable whose address is taken
1 parent ece0e6a commit 22d937d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Ensure that taking a mutable raw ptr to an uninitialized variable does not change its
2+
// initializedness.
3+
4+
struct S;
5+
6+
fn main() {
7+
let mut x: S;
8+
std::ptr::addr_of_mut!(x); //~ borrow of
9+
10+
let y = x; // Should error here if `addr_of_mut` is ever allowed on uninitialized variables
11+
drop(y);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0381]: borrow of possibly-uninitialized variable: `x`
2+
--> $DIR/move-of-addr-of-mut.rs:8:5
3+
|
4+
LL | std::ptr::addr_of_mut!(x);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ use of possibly-uninitialized `x`
6+
|
7+
= note: this error originates in the macro `std::ptr::addr_of_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0381`.

0 commit comments

Comments
 (0)