From 22d937ddfc64bdf1f8724a27a782f2da2ae72be0 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Thu, 11 Nov 2021 14:55:58 -0800 Subject: [PATCH] Sanity check for move from an uninit variable whose address is taken --- src/test/ui/moves/move-of-addr-of-mut.rs | 12 ++++++++++++ src/test/ui/moves/move-of-addr-of-mut.stderr | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/test/ui/moves/move-of-addr-of-mut.rs create mode 100644 src/test/ui/moves/move-of-addr-of-mut.stderr diff --git a/src/test/ui/moves/move-of-addr-of-mut.rs b/src/test/ui/moves/move-of-addr-of-mut.rs new file mode 100644 index 000000000000..f2f64e43cd2d --- /dev/null +++ b/src/test/ui/moves/move-of-addr-of-mut.rs @@ -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); +} diff --git a/src/test/ui/moves/move-of-addr-of-mut.stderr b/src/test/ui/moves/move-of-addr-of-mut.stderr new file mode 100644 index 000000000000..ce8fb0283165 --- /dev/null +++ b/src/test/ui/moves/move-of-addr-of-mut.stderr @@ -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`.