Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions library/core/tests/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,12 @@ fn reentrant_init() {
});
eprintln!("use after free: {:?}", dangling_ref.get().unwrap());
}

#[test]
fn dropck() {
let cell = OnceCell::new();
{
let s = String::new();
cell.set(&s).unwrap();
}
}
14 changes: 12 additions & 2 deletions library/std/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
// 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].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this isn't safe after all :( See #76367

unsafe { self.take_inner() };
}
}
Expand Down Expand Up @@ -845,4 +846,13 @@ mod tests {
assert_eq!(msg, MSG);
}
}

#[test]
fn dropck() {
let cell = SyncOnceCell::new();
{
let s = String::new();
cell.set(&s).unwrap();
}
}
}