Make OnceCell<T> transparent to dropck
See the failed build in https://github.com/rust-lang/rust/pull/75555#issuecomment-675016718 for an example where we need this in real life
This commit is contained in:
@@ -122,3 +122,12 @@ fn reentrant_init() {
|
|||||||
});
|
});
|
||||||
eprintln!("use after free: {:?}", dangling_ref.get().unwrap());
|
eprintln!("use after free: {:?}", dangling_ref.get().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dropck() {
|
||||||
|
let cell = OnceCell::new();
|
||||||
|
{
|
||||||
|
let s = String::new();
|
||||||
|
cell.set(&s).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -386,9 +386,10 @@ impl<T> SyncOnceCell<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Drop for SyncOnceCell<T> {
|
unsafe impl<#[may_dangle] T> Drop for SyncOnceCell<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Safety: The cell is being dropped, so it can't be accessed again
|
// Safety: The cell is being dropped, so it can't be accessed again.
|
||||||
|
// We also don't touch the `T`, which validates our usage of #[may_dangle].
|
||||||
unsafe { self.take_inner() };
|
unsafe { self.take_inner() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -845,4 +846,13 @@ mod tests {
|
|||||||
assert_eq!(msg, MSG);
|
assert_eq!(msg, MSG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dropck() {
|
||||||
|
let cell = SyncOnceCell::new();
|
||||||
|
{
|
||||||
|
let s = String::new();
|
||||||
|
cell.set(&s).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user