Skip to content

Commit 711777b

Browse files
StableDeref trait into core
1 parent dde8cfa commit 711777b

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ language_item_table! {
220220
DerefPure, sym::deref_pure, deref_pure_trait, Target::Trait, GenericRequirement::Exact(0);
221221
DerefTarget, sym::deref_target, deref_target, Target::AssocTy, GenericRequirement::None;
222222
Receiver, sym::receiver, receiver_trait, Target::Trait, GenericRequirement::None;
223+
StableDeref, sym::stable_deref, stable_deref_trait, Target::Trait, GenericRequirement::Exact(0);
223224

224225
Fn, kw::Fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
225226
FnMut, sym::fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ symbols! {
302302
SliceIter,
303303
Some,
304304
SpanCtxt,
305+
StableDeref,
305306
String,
306307
StructuralPartialEq,
307308
SubdiagMessage,
@@ -1767,6 +1768,7 @@ symbols! {
17671768
sse,
17681769
sse4a_target_feature,
17691770
stable,
1771+
stable_deref,
17701772
staged_api,
17711773
start,
17721774
state,

library/core/src/ops/deref.rs

+22
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,25 @@ impl<T: ?Sized> Receiver for &T {}
309309

310310
#[unstable(feature = "receiver_trait", issue = "none")]
311311
impl<T: ?Sized> Receiver for &mut T {}
312+
313+
#[lang = "stable_deref"]
314+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
315+
/// # Safety
316+
///
317+
/// Any two calls to `deref` must return the same value at the same address unless
318+
/// `self` has been modified in the meantime. Moves and unsizing coercions of `self`
319+
/// are not considered modifications.
320+
///
321+
/// Here, "same value" means that if `deref` returns a trait object, then the actual
322+
/// type behind that trait object must not change. Additionally, when you unsize
323+
/// coerce from `Self` to `Unsized`, then if you call `deref` on `Unsized` and get a
324+
/// trait object, then the underlying type of that trait object must be `<Self as
325+
/// Deref>::Target`.
326+
///
327+
/// Analogous requirements apply to other unsized types. E.g., if `deref` returns
328+
/// `[T]`, then the length must not change. In other words, the underlying type
329+
/// must not change from `[T; N]` to `[T; M]`.
330+
///
331+
/// If this type implements `DerefMut`, then the same restrictions apply to calls
332+
/// to `deref_mut`.
333+
pub unsafe trait StableDeref: Deref {}

0 commit comments

Comments
 (0)