Sanity check for move from an uninit variable whose address is taken

This commit is contained in:
Dylan MacKenzie 2021-11-11 14:55:58 -08:00
parent ece0e6ae6d
commit 22d937ddfc
2 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,12 @@
// Ensure that taking a mutable raw ptr to an uninitialized variable does not change its
// initializedness.
struct S;
fn main() {
let mut x: S;
std::ptr::addr_of_mut!(x); //~ borrow of
let y = x; // Should error here if `addr_of_mut` is ever allowed on uninitialized variables
drop(y);
}

View File

@ -0,0 +1,11 @@
error[E0381]: borrow of possibly-uninitialized variable: `x`
--> $DIR/move-of-addr-of-mut.rs:8:5
|
LL | std::ptr::addr_of_mut!(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ use of possibly-uninitialized `x`
|
= note: this error originates in the macro `std::ptr::addr_of_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0381`.