Skip to content

Commit 26e73da

Browse files
committed
Auto merge of #52711 - eddyb:unsized-manuallydrop, r=nikomatsakis
Change ManuallyDrop<T> to a lang item. This PR implements the approach @RalfJung proposes in https://internals.rust-lang.org/t/pre-rfc-unions-drop-types-and-manuallydrop/8025 (lang item `struct` instead of `union`). A followup PR can easily solve #47034 as well, by just adding a few `?Sized` to `libcore/mem.rs`. r? @nikomatsakis
2 parents 5b465e3 + 4e6aea1 commit 26e73da

File tree

9 files changed

+251
-96
lines changed

9 files changed

+251
-96
lines changed

src/doc/reference

src/libcore/manually_drop_stage0.rs

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/// A wrapper to inhibit compiler from automatically calling `T`’s destructor.
12+
///
13+
/// This wrapper is 0-cost.
14+
///
15+
/// # Examples
16+
///
17+
/// This wrapper helps with explicitly documenting the drop order dependencies between fields of
18+
/// the type:
19+
///
20+
/// ```rust
21+
/// use std::mem::ManuallyDrop;
22+
/// struct Peach;
23+
/// struct Banana;
24+
/// struct Melon;
25+
/// struct FruitBox {
26+
/// // Immediately clear there’s something non-trivial going on with these fields.
27+
/// peach: ManuallyDrop<Peach>,
28+
/// melon: Melon, // Field that’s independent of the other two.
29+
/// banana: ManuallyDrop<Banana>,
30+
/// }
31+
///
32+
/// impl Drop for FruitBox {
33+
/// fn drop(&mut self) {
34+
/// unsafe {
35+
/// // Explicit ordering in which field destructors are run specified in the intuitive
36+
/// // location – the destructor of the structure containing the fields.
37+
/// // Moreover, one can now reorder fields within the struct however much they want.
38+
/// ManuallyDrop::drop(&mut self.peach);
39+
/// ManuallyDrop::drop(&mut self.banana);
40+
/// }
41+
/// // After destructor for `FruitBox` runs (this function), the destructor for Melon gets
42+
/// // invoked in the usual manner, as it is not wrapped in `ManuallyDrop`.
43+
/// }
44+
/// }
45+
/// ```
46+
#[stable(feature = "manually_drop", since = "1.20.0")]
47+
#[allow(unions_with_drop_fields)]
48+
#[derive(Copy)]
49+
pub union ManuallyDrop<T>{ value: T }
50+
51+
impl<T> ManuallyDrop<T> {
52+
/// Wrap a value to be manually dropped.
53+
///
54+
/// # Examples
55+
///
56+
/// ```rust
57+
/// use std::mem::ManuallyDrop;
58+
/// ManuallyDrop::new(Box::new(()));
59+
/// ```
60+
#[stable(feature = "manually_drop", since = "1.20.0")]
61+
#[rustc_const_unstable(feature = "const_manually_drop_new")]
62+
#[inline]
63+
pub const fn new(value: T) -> ManuallyDrop<T> {
64+
ManuallyDrop { value: value }
65+
}
66+
67+
/// Extract the value from the ManuallyDrop container.
68+
///
69+
/// # Examples
70+
///
71+
/// ```rust
72+
/// use std::mem::ManuallyDrop;
73+
/// let x = ManuallyDrop::new(Box::new(()));
74+
/// let _: Box<()> = ManuallyDrop::into_inner(x);
75+
/// ```
76+
#[stable(feature = "manually_drop", since = "1.20.0")]
77+
#[inline]
78+
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
79+
unsafe {
80+
slot.value
81+
}
82+
}
83+
84+
/// Manually drops the contained value.
85+
///
86+
/// # Safety
87+
///
88+
/// This function runs the destructor of the contained value and thus the wrapped value
89+
/// now represents uninitialized data. It is up to the user of this method to ensure the
90+
/// uninitialized data is not actually used.
91+
#[stable(feature = "manually_drop", since = "1.20.0")]
92+
#[inline]
93+
pub unsafe fn drop(slot: &mut ManuallyDrop<T>) {
94+
ptr::drop_in_place(&mut slot.value)
95+
}
96+
}
97+
98+
#[stable(feature = "manually_drop", since = "1.20.0")]
99+
impl<T> Deref for ManuallyDrop<T> {
100+
type Target = T;
101+
#[inline]
102+
fn deref(&self) -> &Self::Target {
103+
unsafe {
104+
&self.value
105+
}
106+
}
107+
}
108+
109+
#[stable(feature = "manually_drop", since = "1.20.0")]
110+
impl<T> DerefMut for ManuallyDrop<T> {
111+
#[inline]
112+
fn deref_mut(&mut self) -> &mut Self::Target {
113+
unsafe {
114+
&mut self.value
115+
}
116+
}
117+
}
118+
119+
#[stable(feature = "manually_drop", since = "1.20.0")]
120+
impl<T: ::fmt::Debug> ::fmt::Debug for ManuallyDrop<T> {
121+
fn fmt(&self, fmt: &mut ::fmt::Formatter) -> ::fmt::Result {
122+
unsafe {
123+
fmt.debug_tuple("ManuallyDrop").field(&self.value).finish()
124+
}
125+
}
126+
}
127+
128+
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
129+
impl<T: Clone> Clone for ManuallyDrop<T> {
130+
fn clone(&self) -> Self {
131+
ManuallyDrop::new(self.deref().clone())
132+
}
133+
134+
fn clone_from(&mut self, source: &Self) {
135+
self.deref_mut().clone_from(source);
136+
}
137+
}
138+
139+
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
140+
impl<T: Default> Default for ManuallyDrop<T> {
141+
fn default() -> Self {
142+
ManuallyDrop::new(Default::default())
143+
}
144+
}
145+
146+
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
147+
impl<T: PartialEq> PartialEq for ManuallyDrop<T> {
148+
fn eq(&self, other: &Self) -> bool {
149+
self.deref().eq(other)
150+
}
151+
152+
fn ne(&self, other: &Self) -> bool {
153+
self.deref().ne(other)
154+
}
155+
}
156+
157+
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
158+
impl<T: Eq> Eq for ManuallyDrop<T> {}
159+
160+
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
161+
impl<T: PartialOrd> PartialOrd for ManuallyDrop<T> {
162+
fn partial_cmp(&self, other: &Self) -> Option<::cmp::Ordering> {
163+
self.deref().partial_cmp(other)
164+
}
165+
166+
fn lt(&self, other: &Self) -> bool {
167+
self.deref().lt(other)
168+
}
169+
170+
fn le(&self, other: &Self) -> bool {
171+
self.deref().le(other)
172+
}
173+
174+
fn gt(&self, other: &Self) -> bool {
175+
self.deref().gt(other)
176+
}
177+
178+
fn ge(&self, other: &Self) -> bool {
179+
self.deref().ge(other)
180+
}
181+
}
182+
183+
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
184+
impl<T: Ord> Ord for ManuallyDrop<T> {
185+
fn cmp(&self, other: &Self) -> ::cmp::Ordering {
186+
self.deref().cmp(other)
187+
}
188+
}
189+
190+
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
191+
impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> {
192+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
193+
self.deref().hash(state);
194+
}
195+
}

src/libcore/mem.rs

+16-92
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,6 @@ pub fn discriminant<T>(v: &T) -> Discriminant<T> {
918918
}
919919
}
920920

921-
922921
/// A wrapper to inhibit compiler from automatically calling `T`’s destructor.
923922
///
924923
/// This wrapper is 0-cost.
@@ -954,11 +953,18 @@ pub fn discriminant<T>(v: &T) -> Discriminant<T> {
954953
/// }
955954
/// }
956955
/// ```
956+
#[cfg(not(stage0))]
957957
#[stable(feature = "manually_drop", since = "1.20.0")]
958-
#[allow(unions_with_drop_fields)]
959-
#[derive(Copy)]
960-
pub union ManuallyDrop<T>{ value: T }
958+
#[lang = "manually_drop"]
959+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
960+
pub struct ManuallyDrop<T> {
961+
value: T,
962+
}
963+
964+
#[cfg(stage0)]
965+
include!("manually_drop_stage0.rs");
961966

967+
#[cfg(not(stage0))]
962968
impl<T> ManuallyDrop<T> {
963969
/// Wrap a value to be manually dropped.
964970
///
@@ -972,7 +978,7 @@ impl<T> ManuallyDrop<T> {
972978
#[rustc_const_unstable(feature = "const_manually_drop_new")]
973979
#[inline]
974980
pub const fn new(value: T) -> ManuallyDrop<T> {
975-
ManuallyDrop { value: value }
981+
ManuallyDrop { value }
976982
}
977983

978984
/// Extract the value from the ManuallyDrop container.
@@ -987,9 +993,7 @@ impl<T> ManuallyDrop<T> {
987993
#[stable(feature = "manually_drop", since = "1.20.0")]
988994
#[inline]
989995
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
990-
unsafe {
991-
slot.value
992-
}
996+
slot.value
993997
}
994998

995999
/// Manually drops the contained value.
@@ -1006,102 +1010,22 @@ impl<T> ManuallyDrop<T> {
10061010
}
10071011
}
10081012

1013+
#[cfg(not(stage0))]
10091014
#[stable(feature = "manually_drop", since = "1.20.0")]
10101015
impl<T> Deref for ManuallyDrop<T> {
10111016
type Target = T;
10121017
#[inline]
10131018
fn deref(&self) -> &Self::Target {
1014-
unsafe {
1015-
&self.value
1016-
}
1019+
&self.value
10171020
}
10181021
}
10191022

1023+
#[cfg(not(stage0))]
10201024
#[stable(feature = "manually_drop", since = "1.20.0")]
10211025
impl<T> DerefMut for ManuallyDrop<T> {
10221026
#[inline]
10231027
fn deref_mut(&mut self) -> &mut Self::Target {
1024-
unsafe {
1025-
&mut self.value
1026-
}
1027-
}
1028-
}
1029-
1030-
#[stable(feature = "manually_drop", since = "1.20.0")]
1031-
impl<T: ::fmt::Debug> ::fmt::Debug for ManuallyDrop<T> {
1032-
fn fmt(&self, fmt: &mut ::fmt::Formatter) -> ::fmt::Result {
1033-
unsafe {
1034-
fmt.debug_tuple("ManuallyDrop").field(&self.value).finish()
1035-
}
1036-
}
1037-
}
1038-
1039-
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
1040-
impl<T: Clone> Clone for ManuallyDrop<T> {
1041-
fn clone(&self) -> Self {
1042-
ManuallyDrop::new(self.deref().clone())
1043-
}
1044-
1045-
fn clone_from(&mut self, source: &Self) {
1046-
self.deref_mut().clone_from(source);
1047-
}
1048-
}
1049-
1050-
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
1051-
impl<T: Default> Default for ManuallyDrop<T> {
1052-
fn default() -> Self {
1053-
ManuallyDrop::new(Default::default())
1054-
}
1055-
}
1056-
1057-
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
1058-
impl<T: PartialEq> PartialEq for ManuallyDrop<T> {
1059-
fn eq(&self, other: &Self) -> bool {
1060-
self.deref().eq(other)
1061-
}
1062-
1063-
fn ne(&self, other: &Self) -> bool {
1064-
self.deref().ne(other)
1065-
}
1066-
}
1067-
1068-
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
1069-
impl<T: Eq> Eq for ManuallyDrop<T> {}
1070-
1071-
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
1072-
impl<T: PartialOrd> PartialOrd for ManuallyDrop<T> {
1073-
fn partial_cmp(&self, other: &Self) -> Option<::cmp::Ordering> {
1074-
self.deref().partial_cmp(other)
1075-
}
1076-
1077-
fn lt(&self, other: &Self) -> bool {
1078-
self.deref().lt(other)
1079-
}
1080-
1081-
fn le(&self, other: &Self) -> bool {
1082-
self.deref().le(other)
1083-
}
1084-
1085-
fn gt(&self, other: &Self) -> bool {
1086-
self.deref().gt(other)
1087-
}
1088-
1089-
fn ge(&self, other: &Self) -> bool {
1090-
self.deref().ge(other)
1091-
}
1092-
}
1093-
1094-
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
1095-
impl<T: Ord> Ord for ManuallyDrop<T> {
1096-
fn cmp(&self, other: &Self) -> ::cmp::Ordering {
1097-
self.deref().cmp(other)
1098-
}
1099-
}
1100-
1101-
#[stable(feature = "manually_drop_impls", since = "1.22.0")]
1102-
impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> {
1103-
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
1104-
self.deref().hash(state);
1028+
&mut self.value
11051029
}
11061030
}
11071031

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ mod fmt;
6262
mod hash;
6363
mod intrinsics;
6464
mod iter;
65+
mod manually_drop;
6566
mod mem;
6667
mod nonzero;
6768
mod num;

src/libcore/tests/manually_drop.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use core::mem::ManuallyDrop;
12+
13+
#[test]
14+
fn smoke() {
15+
struct TypeWithDrop;
16+
impl Drop for TypeWithDrop {
17+
fn drop(&mut self) {
18+
unreachable!("Should not get dropped");
19+
}
20+
}
21+
22+
let x = ManuallyDrop::new(TypeWithDrop);
23+
drop(x);
24+
}

src/librustc/middle/lang_items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ language_item_table! {
324324

325325
NonZeroItem, "non_zero", non_zero;
326326

327+
ManuallyDropItem, "manually_drop", manually_drop;
328+
327329
DebugTraitLangItem, "debug_trait", debug_trait;
328330

329331
// A lang item for each of the 128-bit operators we can optionally lower.

0 commit comments

Comments
 (0)