File tree 2 files changed +21
-2
lines changed
2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -122,3 +122,12 @@ fn reentrant_init() {
122
122
} ) ;
123
123
eprintln ! ( "use after free: {:?}" , dangling_ref. get( ) . unwrap( ) ) ;
124
124
}
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> {
386
386
}
387
387
}
388
388
389
- impl < T > Drop for SyncOnceCell < T > {
389
+ unsafe impl < # [ may_dangle ] T > Drop for SyncOnceCell < T > {
390
390
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].
392
393
unsafe { self . take_inner ( ) } ;
393
394
}
394
395
}
@@ -845,4 +846,13 @@ mod tests {
845
846
assert_eq ! ( msg, MSG ) ;
846
847
}
847
848
}
849
+
850
+ #[ test]
851
+ fn dropck ( ) {
852
+ let cell = SyncOnceCell :: new ( ) ;
853
+ {
854
+ let s = String :: new ( ) ;
855
+ cell. set ( & s) . unwrap ( ) ;
856
+ }
857
+ }
848
858
}
You can’t perform that action at this time.
0 commit comments