Skip to content

Commit

Permalink
Library: Rename "object safe" to "dyn compatible"
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Oct 9, 2024
1 parent f5cd2c5 commit e08dc04
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
6 changes: 3 additions & 3 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ impl<T: Default> Cell<T> {
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}

// Allow types that wrap `Cell` to also implement `DispatchFromDyn`
// and become object safe method receivers.
// and become dyn-compatible method receivers.
// Note that currently `Cell` itself cannot be a method receiver
// because it does not implement Deref.
// In other words:
Expand Down Expand Up @@ -2236,7 +2236,7 @@ impl<T> From<T> for UnsafeCell<T> {
impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}

// Allow types that wrap `UnsafeCell` to also implement `DispatchFromDyn`
// and become object safe method receivers.
// and become dyn-compatible method receivers.
// Note that currently `UnsafeCell` itself cannot be a method receiver
// because it does not implement Deref.
// In other words:
Expand Down Expand Up @@ -2379,7 +2379,7 @@ impl<T> From<T> for SyncUnsafeCell<T> {
impl<T: CoerceUnsized<U>, U> CoerceUnsized<SyncUnsafeCell<U>> for SyncUnsafeCell<T> {}

// Allow types that wrap `SyncUnsafeCell` to also implement `DispatchFromDyn`
// and become object safe method receivers.
// and become dyn-compatible method receivers.
// Note that currently `SyncUnsafeCell` itself cannot be a method receiver
// because it does not implement Deref.
// In other words:
Expand Down
21 changes: 11 additions & 10 deletions library/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,17 @@ impl dyn Error {
#[unstable(feature = "error_iter", issue = "58520")]
#[inline]
pub fn sources(&self) -> Source<'_> {
// You may think this method would be better in the Error trait, and you'd be right.
// Unfortunately that doesn't work, not because of the object safety rules but because we
// save a reference to self in Sources below as a trait object. If this method was
// declared in Error, then self would have the type &T where T is some concrete type which
// implements Error. We would need to coerce self to have type &dyn Error, but that requires
// that Self has a known size (i.e., Self: Sized). We can't put that bound on Error
// since that would forbid Error trait objects, and we can't put that bound on the method
// because that means the method can't be called on trait objects (we'd also need the
// 'static bound, but that isn't allowed because methods with bounds on Self other than
// Sized are not object-safe). Requiring an Unsize bound is not backwards compatible.
// You may think this method would be better in the `Error` trait, and you'd be right.
// Unfortunately that doesn't work, not because of the dyn-incompatibility rules but
// because we save a reference to `self` in `Source`s below as a trait object.
// If this method was declared in `Error`, then `self` would have the type `&T` where
// `T` is some concrete type which implements `Error`. We would need to coerce `self`
// to have type `&dyn Error`, but that requires that `Self` has a known size
// (i.e., `Self: Sized`). We can't put that bound on `Error` since that would forbid
// `Error` trait objects, and we can't put that bound on the method because that means
// the method can't be called on trait objects (we'd also need the `'static` bound,
// but that isn't allowed because methods with bounds on `Self` other than `Sized` are
// dyn-incompatible). Requiring an `Unsize` bound is not backwards compatible.

Source { current: Some(self) }
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::cmp::{self, Ordering};
use crate::num::NonZero;
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};

fn _assert_is_object_safe(_: &dyn Iterator<Item = ()>) {}
fn _assert_is_dyn_compatible(_: &dyn Iterator<Item = ()>) {}

/// A trait for dealing with iterators.
///
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub trait Sized {
/// - Arrays `[T; N]` implement `Unsize<[T]>`.
/// - A type implements `Unsize<dyn Trait + 'a>` if all of these conditions are met:
/// - The type implements `Trait`.
/// - `Trait` is object safe.
/// - `Trait` is dyn-compatible[^1].
/// - The type is sized.
/// - The type outlives `'a`.
/// - Structs `Foo<..., T1, ..., Tn, ...>` implement `Unsize<Foo<..., U1, ..., Un, ...>>`
Expand All @@ -178,6 +178,7 @@ pub trait Sized {
/// [`Rc`]: ../../std/rc/struct.Rc.html
/// [RFC982]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
/// [nomicon-coerce]: ../../nomicon/coercions.html
/// [^1]: Formerly known as *object safe*.
#[unstable(feature = "unsize", issue = "18598")]
#[lang = "unsize"]
#[rustc_deny_explicit_impl(implement_via_object = false)]
Expand Down
8 changes: 5 additions & 3 deletions library/core/src/ops/unsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
#[unstable(feature = "coerce_unsized", issue = "18598")]
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}

/// `DispatchFromDyn` is used in the implementation of object safety checks (specifically allowing
/// arbitrary self types), to guarantee that a method's receiver type can be dispatched on.
/// `DispatchFromDyn` is used in the implementation of dyn-compatibility[^1] checks (specifically
/// allowing arbitrary self types), to guarantee that a method's receiver type can be dispatched on.
///
/// Note: `DispatchFromDyn` was briefly named `CoerceSized` (and had a slightly different
/// interpretation).
Expand All @@ -80,7 +80,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
/// type). The compiler must generate an implicit conversion from the trait object/wide pointer to
/// the concrete reference/narrow pointer. Implementing `DispatchFromDyn` indicates that that
/// conversion is allowed and thus that the type implementing `DispatchFromDyn` is safe to use as
/// the self type in an object-safe method. (in the above example, the compiler will require
/// the self type in an dyn-compatible method. (in the above example, the compiler will require
/// `DispatchFromDyn` is implemented for `&'a U`).
///
/// `DispatchFromDyn` does not specify the conversion from wide pointer to narrow pointer; the
Expand Down Expand Up @@ -112,6 +112,8 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
/// T: Unsize<U>,
/// {}
/// ```
///
/// [^1]: Formerly known as *object safety*.
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
#[lang = "dispatch_from_dyn"]
pub trait DispatchFromDyn<T> {
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn test_indirect_hasher() {
}

#[test]
fn test_build_hasher_object_safe() {
fn test_build_hasher_dyn_compatible() {
use std::hash::{DefaultHasher, RandomState};

let _: &dyn BuildHasher<Hasher = DefaultHasher> = &RandomState::new();
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2349,12 +2349,13 @@ mod async_keyword {}
/// [`async`]: ../std/keyword.async.html
mod await_keyword {}

// FIXME(dyn_compat_renaming): Update URL and link text.
#[doc(keyword = "dyn")]
//
/// `dyn` is a prefix of a [trait object]'s type.
///
/// The `dyn` keyword is used to highlight that calls to methods on the associated `Trait`
/// are [dynamically dispatched]. To use the trait this way, it must be 'object safe'.
/// are [dynamically dispatched]. To use the trait this way, it must be 'dyn-compatible'[^1].
///
/// Unlike generic parameters or `impl Trait`, the compiler does not know the concrete type that
/// is being passed. That is, the type has been [erased].
Expand Down Expand Up @@ -2382,6 +2383,7 @@ mod await_keyword {}
/// [ref-trait-obj]: ../reference/types/trait-object.html
/// [ref-obj-safety]: ../reference/items/traits.html#object-safety
/// [erased]: https://en.wikipedia.org/wiki/Type_erasure
/// [^1]: Formerly known as 'object safe'.
mod dyn_keyword {}

#[doc(keyword = "union")]
Expand Down

0 comments on commit e08dc04

Please sign in to comment.