-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Mark places as initialized when mutably borrowed #90788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bea1bde
d846fe0
ece0e6a
22d937d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// run-pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe instead of checking that we are getting the wrong result we should just have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone (including me) were to change the behavior of this test, they should be made aware. |
||
|
||
use std::cell::RefCell; | ||
|
||
struct S<'a>(i32, &'a RefCell<Vec<i32>>); | ||
|
||
impl<'a> Drop for S<'a> { | ||
fn drop(&mut self) { | ||
self.1.borrow_mut().push(self.0); | ||
} | ||
} | ||
|
||
fn test(drops: &RefCell<Vec<i32>>) { | ||
let mut foo = None; | ||
let pfoo: *mut _ = &mut foo; | ||
|
||
match foo { | ||
None => (), | ||
_ => return, | ||
} | ||
|
||
// Both S(0) and S(1) should be dropped, but aren't. | ||
unsafe { *pfoo = Some((S(0, drops), S(1, drops))); } | ||
|
||
match foo { | ||
Some((_x, _)) => {} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn main() { | ||
let drops = RefCell::new(Vec::new()); | ||
test(&drops); | ||
|
||
// Ideally, we want this... | ||
//assert_eq!(*drops.borrow(), &[0, 1]); | ||
|
||
// But the delayed access through the raw pointer confuses drop elaboration, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unfortunate. Do you have an idea about how this could be fixed in the future? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We typically handle this is by assuming that any variable which has been mutably borrowed at some point in the CFG could change at any subsequent point. The correctness of that approach doesn't depend on stacked borrows. We can't implement it directly on top of |
||
// causing S(1) to be leaked. | ||
assert_eq!(*drops.borrow(), &[0]); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// run-pass | ||
|
||
use std::cell::RefCell; | ||
|
||
struct S<'a>(i32, &'a RefCell<Vec<i32>>); | ||
|
||
impl<'a> Drop for S<'a> { | ||
fn drop(&mut self) { | ||
self.1.borrow_mut().push(self.0); | ||
} | ||
} | ||
|
||
fn test(drops: &RefCell<Vec<i32>>) { | ||
let mut foo = None; | ||
match foo { | ||
None => (), | ||
_ => return, | ||
} | ||
|
||
*(&mut foo) = Some((S(0, drops), S(1, drops))); // Both S(0) and S(1) should be dropped | ||
|
||
match foo { | ||
Some((_x, _)) => {} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn main() { | ||
let drops = RefCell::new(Vec::new()); | ||
test(&drops); | ||
assert_eq!(*drops.borrow(), &[0, 1]); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Can you go ahead and copy the comment from line 322 here as well? Seems like we should try to keep these as similar as possible.