Add Weak may_dangle tests

This commit is contained in:
David Tolnay 2021-05-20 19:40:48 -07:00
parent 40d2302047
commit c441675edf
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 30 additions and 0 deletions

View File

@ -195,3 +195,18 @@ fn shared_from_iter_trustedlen_no_fuse() {
assert_trusted_len(&iter);
assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
}
#[test]
fn weak_may_dangle() {
fn hmm<'a>(val: &'a mut Weak<&'a str>) -> Weak<&'a str> {
val.clone()
}
// Without #[may_dangle] we get:
let mut val = Weak::new();
hmm(&mut val);
// ~~~~~~~~ borrowed value does not live long enough
//
// `val` dropped here while still borrowed
// borrow might be used here, when `val` is dropped and runs the `Drop` code for type `std::sync::Weak`
}

View File

@ -191,3 +191,18 @@ fn shared_from_iter_trustedlen_no_fuse() {
assert_trusted_len(&iter);
assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
}
#[test]
fn weak_may_dangle() {
fn hmm<'a>(val: &'a mut Weak<&'a str>) -> Weak<&'a str> {
val.clone()
}
// Without #[may_dangle] we get:
let mut val = Weak::new();
hmm(&mut val);
// ~~~~~~~~ borrowed value does not live long enough
//
// `val` dropped here while still borrowed
// borrow might be used here, when `val` is dropped and runs the `Drop` code for type `std::rc::Weak`
}