File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments