@@ -6,6 +6,7 @@ mod tests;
6
6
use crate :: {
7
7
cell:: { Cell , UnsafeCell } ,
8
8
fmt,
9
+ marker:: PhantomData ,
9
10
mem:: { self , MaybeUninit } ,
10
11
ops:: { Deref , Drop } ,
11
12
panic:: { RefUnwindSafe , UnwindSafe } ,
@@ -46,6 +47,26 @@ pub struct SyncOnceCell<T> {
46
47
once : Once ,
47
48
// Whether or not the value is initialized is tracked by `state_and_queue`.
48
49
value : UnsafeCell < MaybeUninit < T > > ,
50
+ /// `PhantomData` to make sure dropck understands we're dropping T in our Drop impl.
51
+ ///
52
+ /// ```compile_fail,E0597
53
+ /// #![feature(once_cell)]
54
+ ///
55
+ /// use std::lazy::SyncOnceCell;
56
+ ///
57
+ /// struct A<'a>(&'a str);
58
+ ///
59
+ /// impl<'a> Drop for A<'a> {
60
+ /// fn drop(&mut self) {}
61
+ /// }
62
+ ///
63
+ /// let cell = SyncOnceCell::new();
64
+ /// {
65
+ /// let s = String::new();
66
+ /// let _ = cell.set(A(&s));
67
+ /// }
68
+ /// ```
69
+ _marker : PhantomData < T > ,
49
70
}
50
71
51
72
// Why do we need `T: Send`?
@@ -119,7 +140,11 @@ impl<T> SyncOnceCell<T> {
119
140
/// Creates a new empty cell.
120
141
#[ unstable( feature = "once_cell" , issue = "74465" ) ]
121
142
pub const fn new ( ) -> SyncOnceCell < T > {
122
- SyncOnceCell { once : Once :: new ( ) , value : UnsafeCell :: new ( MaybeUninit :: uninit ( ) ) }
143
+ SyncOnceCell {
144
+ once : Once :: new ( ) ,
145
+ value : UnsafeCell :: new ( MaybeUninit :: uninit ( ) ) ,
146
+ _marker : PhantomData ,
147
+ }
123
148
}
124
149
125
150
/// Gets the reference to the underlying value.
0 commit comments