Library: Rename "object safe" to "dyn compatible"

This commit is contained in:
León Orell Valerian Liehr 2024-09-25 13:17:58 +02:00
parent f5cd2c5888
commit e08dc0491a
No known key found for this signature in database
GPG Key ID: D17A07215F68E713
7 changed files with 26 additions and 20 deletions

View File

@ -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:
@ -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:
@ -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:

View File

@ -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) }
}

View File

@ -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.
///

View File

@ -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, ...>>`
@ -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)]

View File

@ -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).
@ -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
@ -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> {

View File

@ -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();

View File

@ -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].
@ -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")]