Skip to content

Commit 999ff5e

Browse files
authored
Rollup merge of rust-lang#122835 - compiler-errors:deref-pure, r=Nadrieril
Require `DerefMut` and `DerefPure` on `deref!()` patterns when appropriate Waiting on the deref pattern syntax pr to merge r? nadrieril
2 parents 2642ef3 + 55f44f0 commit 999ff5e

File tree

8 files changed

+41
-3
lines changed

8 files changed

+41
-3
lines changed

alloc/src/boxed.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ use core::marker::Unsize;
161161
use core::mem::{self, SizedTypeProperties};
162162
use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce};
163163
use core::ops::{
164-
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DispatchFromDyn, Receiver,
164+
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver,
165165
};
166166
use core::pin::Pin;
167167
use core::ptr::{self, addr_of_mut, NonNull, Unique};
@@ -1939,6 +1939,9 @@ impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A> {
19391939
}
19401940
}
19411941

1942+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
1943+
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Box<T, A> {}
1944+
19421945
#[unstable(feature = "receiver_trait", issue = "none")]
19431946
impl<T: ?Sized, A: Allocator> Receiver for Box<T, A> {}
19441947

alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#![feature(const_waker)]
123123
#![feature(core_intrinsics)]
124124
#![feature(deprecated_suggestion)]
125+
#![feature(deref_pure_trait)]
125126
#![feature(dispatch_from_dyn)]
126127
#![feature(error_generic_member_access)]
127128
#![feature(error_in_core)]

alloc/src/rc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ use core::marker::{PhantomData, Unsize};
260260
#[cfg(not(no_global_oom_handling))]
261261
use core::mem::size_of_val;
262262
use core::mem::{self, align_of_val_raw, forget, ManuallyDrop};
263-
use core::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Receiver};
263+
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver};
264264
use core::panic::{RefUnwindSafe, UnwindSafe};
265265
#[cfg(not(no_global_oom_handling))]
266266
use core::pin::Pin;
@@ -2126,6 +2126,9 @@ impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
21262126
}
21272127
}
21282128

2129+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
2130+
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
2131+
21292132
#[unstable(feature = "receiver_trait", issue = "none")]
21302133
impl<T: ?Sized> Receiver for Rc<T> {}
21312134

alloc/src/string.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,9 @@ impl ops::Deref for String {
24792479
}
24802480
}
24812481

2482+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
2483+
unsafe impl ops::DerefPure for String {}
2484+
24822485
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
24832486
impl ops::DerefMut for String {
24842487
#[inline]

alloc/src/sync.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use core::marker::{PhantomData, Unsize};
2121
#[cfg(not(no_global_oom_handling))]
2222
use core::mem::size_of_val;
2323
use core::mem::{self, align_of_val_raw};
24-
use core::ops::{CoerceUnsized, Deref, DispatchFromDyn, Receiver};
24+
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
2525
use core::panic::{RefUnwindSafe, UnwindSafe};
2626
use core::pin::Pin;
2727
use core::ptr::{self, NonNull};
@@ -2107,6 +2107,9 @@ impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
21072107
}
21082108
}
21092109

2110+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
2111+
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
2112+
21102113
#[unstable(feature = "receiver_trait", issue = "none")]
21112114
impl<T: ?Sized> Receiver for Arc<T> {}
21122115

alloc/src/vec/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,9 @@ impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
27722772
}
27732773
}
27742774

2775+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
2776+
unsafe impl<T, A: Allocator> ops::DerefPure for Vec<T, A> {}
2777+
27752778
#[cfg(not(no_global_oom_handling))]
27762779
#[stable(feature = "rust1", since = "1.0.0")]
27772780
impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> {

core/src/ops/deref.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,25 @@ impl<T: ?Sized> DerefMut for &mut T {
275275
}
276276
}
277277

278+
/// Perma-unstable marker trait. Indicates that the type has a well-behaved [`Deref`]
279+
/// (and, if applicable, [`DerefMut`]) implementation. This is relied on for soundness
280+
/// of deref patterns.
281+
///
282+
/// FIXME(deref_patterns): The precise semantics are undecided; the rough idea is that
283+
/// successive calls to `deref`/`deref_mut` without intermediate mutation should be
284+
/// idempotent, in the sense that they return the same value as far as pattern-matching
285+
/// is concerned. Calls to `deref`/`deref_mut`` must leave the pointer itself likewise
286+
/// unchanged.
287+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
288+
#[cfg_attr(not(bootstrap), lang = "deref_pure")]
289+
pub unsafe trait DerefPure {}
290+
291+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
292+
unsafe impl<T: ?Sized> DerefPure for &T {}
293+
294+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
295+
unsafe impl<T: ?Sized> DerefPure for &mut T {}
296+
278297
/// Indicates that a struct can be used as a method receiver, without the
279298
/// `arbitrary_self_types` feature. This is implemented by stdlib pointer types like `Box<T>`,
280299
/// `Rc<T>`, `&T`, and `Pin<P>`.

core/src/ops/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ pub use self::bit::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssig
165165
#[stable(feature = "rust1", since = "1.0.0")]
166166
pub use self::deref::{Deref, DerefMut};
167167

168+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
169+
pub use self::deref::DerefPure;
170+
168171
#[unstable(feature = "receiver_trait", issue = "none")]
169172
pub use self::deref::Receiver;
170173

0 commit comments

Comments
 (0)