Skip to content

Commit ae5a922

Browse files
committed
Remove _dropck flags
The nomicon has been outdated about this since 2015, see rust-lang/nomicon#363 for the update. We don't need to tell dropck that we are owning a T as it can already infer that from the generic parameter in the drop impl.
1 parent 3b27d35 commit ae5a922

2 files changed

Lines changed: 2 additions & 49 deletions

File tree

src/errors.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{dealloc, Channel};
22
use core::fmt;
3-
use core::marker::PhantomData;
43
use core::mem;
54
use core::ptr::NonNull;
65

@@ -10,41 +9,6 @@ use core::ptr::NonNull;
109
/// The message that could not be sent can be retreived again with [`SendError::into_inner`].
1110
pub struct SendError<T> {
1211
channel_ptr: NonNull<Channel<T>>,
13-
/// Required due to the reasons outlined in
14-
/// [this section](https://doc.rust-lang.org/nomicon/dropck.html) of the nomicon, as well as
15-
/// the next section.
16-
///
17-
/// Without the phantom data, the following code would incorrectly compile. This code is
18-
/// invalid because `error` gets dropped first (struct fields are dropped in declaration order)
19-
/// but `oof` then accesses the data deallocated by `error`, causing a use-after-free.
20-
///
21-
/// ```compile_fail
22-
/// let (tx, rx) = oneshot::channel::<Box<u8>>();
23-
/// drop(rx);
24-
/// let error = tx.send(Box::new(0)).unwrap_err();
25-
///
26-
/// struct Oof<'a>(&'a u8);
27-
///
28-
/// impl<'a> Drop for Oof<'a> {
29-
/// fn drop(&mut self) {
30-
/// println!("{}", self.0);
31-
/// }
32-
/// }
33-
///
34-
/// struct Foo<'a> {
35-
/// error: SendError<Box<u8>>,
36-
/// oof: Option<Oof<'a>>,
37-
/// }
38-
///
39-
/// let mut foo = Foo {
40-
/// error,
41-
/// oof: None
42-
/// };
43-
///
44-
/// foo.oof = Some(Oof(&**foo.error.as_inner()));
45-
/// drop(foo);
46-
/// ```
47-
_dropck: PhantomData<T>,
4812
}
4913

5014
unsafe impl<T: Send> Send for SendError<T> {}
@@ -58,10 +22,7 @@ impl<T> SendError<T> {
5822
/// pointer is not used in a way which would violate this ownership transfer. Moreover,
5923
/// the caller must assert that the channel contains a valid, initialized message.
6024
pub(crate) const unsafe fn new(channel_ptr: NonNull<Channel<T>>) -> Self {
61-
Self {
62-
channel_ptr,
63-
_dropck: PhantomData,
64-
}
25+
Self { channel_ptr }
6526
}
6627

6728
/// Consumes the error and returns the message that failed to be sent.

src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,8 @@ pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
206206
Sender {
207207
channel_ptr,
208208
_invariant: PhantomData,
209-
_dropck: PhantomData,
210-
},
211-
Receiver {
212-
channel_ptr,
213-
_dropck: PhantomData,
214209
},
210+
Receiver { channel_ptr },
215211
)
216212
}
217213

@@ -235,17 +231,13 @@ pub struct Sender<T> {
235231
// If this type were covariant then we could safely extend lifetimes, which is not okay.
236232
// Hence, we enforce invariance.
237233
_invariant: PhantomData<fn(T) -> T>,
238-
// See SendError for details
239-
_dropck: PhantomData<T>,
240234
}
241235

242236
#[derive(Debug)]
243237
pub struct Receiver<T> {
244238
// Covariance is the right choice here. Consider the example presented in Sender, and you'll
245239
// see that if we replaced `rx` instead then we would get the expected behavior
246240
channel_ptr: NonNull<Channel<T>>,
247-
// See SendError for details
248-
_dropck: PhantomData<T>,
249241
}
250242

251243
unsafe impl<T: Send> Send for Sender<T> {}

0 commit comments

Comments
 (0)