Skip to content

Commit 4123237

Browse files
authored
Rollup merge of #75648 - matklad:lazy-dropck, r=KodrAus
Make OnceCell<T> transparent to dropck See the failed build in #75555 (comment) for an example where we need this in real life r? @ghost
2 parents 0e7e939 + 695d86f commit 4123237

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

library/core/tests/lazy.rs

+9
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,12 @@ fn reentrant_init() {
122122
});
123123
eprintln!("use after free: {:?}", dangling_ref.get().unwrap());
124124
}
125+
126+
#[test]
127+
fn dropck() {
128+
let cell = OnceCell::new();
129+
{
130+
let s = String::new();
131+
cell.set(&s).unwrap();
132+
}
133+
}

library/std/src/lazy.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,10 @@ impl<T> SyncOnceCell<T> {
386386
}
387387
}
388388

389-
impl<T> Drop for SyncOnceCell<T> {
389+
unsafe impl<#[may_dangle] T> Drop for SyncOnceCell<T> {
390390
fn drop(&mut self) {
391-
// Safety: The cell is being dropped, so it can't be accessed again
391+
// Safety: The cell is being dropped, so it can't be accessed again.
392+
// We also don't touch the `T`, which validates our usage of #[may_dangle].
392393
unsafe { self.take_inner() };
393394
}
394395
}
@@ -845,4 +846,13 @@ mod tests {
845846
assert_eq!(msg, MSG);
846847
}
847848
}
849+
850+
#[test]
851+
fn dropck() {
852+
let cell = SyncOnceCell::new();
853+
{
854+
let s = String::new();
855+
cell.set(&s).unwrap();
856+
}
857+
}
848858
}

0 commit comments

Comments
 (0)