Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reword "object safety" as "dyn compatibility" #1512

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,13 @@ the hierarchy has its own collection of named entities.
Types that can be referred to by a path directly. Specifically [enums],
[structs], [unions], and [trait objects].

### Object safe traits
### Trait-object-safe traits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Trait-object-safe traits
### Dyn-compatible traits


[Traits] that can be used as [trait objects]. Only traits that follow specific
[rules][object safety] are object safe.
[Traits] that can be used in `dyn Trait` types, and allow creation of
[trait objects]. Only traits that follow specific [rules][object safety] are
trait-object safe.

This is also called *object safety*.
Comment on lines +180 to +184
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Traits] that can be used in `dyn Trait` types, and allow creation of
[trait objects]. Only traits that follow specific [rules][object safety] are
trait-object safe.
This is also called *object safety*.
[Traits] that can be used in [trait object types][trait objects] (`dyn Trait`). Only traits that follow specific [rules][dyn-compatibility] are dyn compatible.
Formerly known as *object safety*.

+ updating the second part of the reference-style MarkDown link.

TC, let me know if you agree with my suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks about right. Probably worth making a full sentence out of the last line, e.g. "These were formerly known as object safe traits".


### Path

Expand Down
22 changes: 11 additions & 11 deletions src/items/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ trait Seq<T> {
}
```

## Object Safety
## Trait-Object Safety <a id="object-safety"></a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Trait-Object Safety <a id="object-safety"></a>
## Dyn Compatibility

NB: I'd love the URL fragment (named anchor) to get changed to dyn-compatibility and have an HTTP redirect from object-safety. Although I haven't checked if the Reference is set up to support redirects. I know that the rustc-dev-guide is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a paragraph or footnote pointing out that this concept is "formerly known as object safety".

Copy link
Member

@fmease fmease Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: I'd love the URL fragment (named anchor) to get changed to dyn-compatibility and have an HTTP redirect from object-safety

This would be quite nice because we show this URL literally in rustc's diagnostics. As a result of that we currently have a mismatch of terminology on nightly (diagnostic text vs. URL). Of course, that's expected but I'd like this to be temporary only (namely until this PR hits stable (rustc's diagnostics usually link to the stable channel of the Reference)) as it's a bit awkward imho.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When renaming sections, you need to use a <script> tag to redirect the fragment, see

<script>
(function() {
var fragments = {
"#slice-dst-pointer-to-pointer-cast": "operator-expr.html#pointer-to-pointer-cast",
};
var target = fragments[window.location.hash];
if (target) {
var url = window.location.toString();
var base = url.substring(0, url.lastIndexOf('/'));
window.location.replace(base + "/" + target);
}
})();
</script>
as an example.


Object safe traits can be the base trait of a [trait object]. A trait is
*object safe* if it has the following qualities (defined in [RFC 255]):
An object-safe trait can be used in a `dyn Trait` type, and be the base trait of a [trait object].
A trait is *object safe* if it has the following qualities (defined in [RFC 255]):
Comment on lines +67 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An object-safe trait can be used in a `dyn Trait` type, and be the base trait of a [trait object].
A trait is *object safe* if it has the following qualities (defined in [RFC 255]):
A dyn-compatible trait can be used in a trait object type (`dyn Trait`), and be the base trait of a [trait object].
A trait is *dyn compatible* if it has the following qualities (defined in [RFC 255]):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the renaming: The dyn-compatibility rules have been updated over time (for example, "dyn-incompatible" associated types marked with where Self: Sized no longer render the trait dyn-incompatible). So stating that the rules were defined in RFC 255 is misleading or at the very least outdated. That should probably be changed as part of a separate PR though.


* All [supertraits] must also be object safe.
* All [supertraits] must also be trait-object safe.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* All [supertraits] must also be trait-object safe.
* All [supertraits] must also be dyn compatible.

* `Sized` must not be a [supertrait][supertraits]. In other words, it must not require `Self: Sized`.
* It must not have any associated constants.
* It must not have any associated types with generics.
Expand All @@ -93,7 +93,7 @@ Object safe traits can be the base trait of a [trait object]. A trait is
# use std::rc::Rc;
# use std::sync::Arc;
# use std::pin::Pin;
// Examples of object safe methods.
// Examples of trait-object safe methods.
Copy link
Member

@fmease fmease Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Examples of trait-object safe methods.
// Examples of dyn-compatible methods.

Though strictly speaking we haven't defined "dyn compatible" for methods, only for traits. We could be more explicit but not sure if worthwhile.

trait TraitMethods {
fn by_ref(self: &Self) {}
fn by_ref_mut(self: &mut Self) {}
Expand All @@ -110,7 +110,7 @@ trait TraitMethods {
```

```rust,compile_fail
// This trait is object-safe, but these methods cannot be dispatched on a trait object.
Copy link
Member

@fmease fmease Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should double-check if there are any other mentions of /(non[\- _]?)?obj(ect)?[\- _](un)?safe(ty)?/i and update them accordingly.

// This trait is object-safe, but these methods cannot be dispatched on a trait object (such as `&dyn NonDispatchable`).
trait NonDispatchable {
// Non-methods cannot be dispatched.
fn foo() where Self: Sized {}
Expand All @@ -135,7 +135,7 @@ obj.typed(1); // ERROR: cannot call with generic type
```rust,compile_fail
# use std::rc::Rc;
// Examples of non-object safe traits.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Examples of non-object safe traits.
// Examples of dyn-incompatible traits.

+ all other occurrences of this phrasing in this repo.

trait NotObjectSafe {
trait NotDynCompatible {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
trait NotDynCompatible {
trait DynIncompatible {

+ usage sites

const CONST: i32 = 1; // ERROR: cannot have associated const

fn foo() {} // ERROR: associated function without Sized
Expand All @@ -145,14 +145,14 @@ trait NotObjectSafe {
}

struct S;
impl NotObjectSafe for S {
impl NotDynCompatible for S {
fn returns(&self) -> Self { S }
}
let obj: Box<dyn NotObjectSafe> = Box::new(S); // ERROR
let obj: Box<dyn NotDynCompatible> = Box::new(S); // ERROR
```

```rust,compile_fail
// Self: Sized traits are not object-safe.
// Self: Sized traits are not trait-object-safe.
trait TraitWithSize where Self: Sized {}

struct S;
Expand All @@ -161,7 +161,7 @@ let obj: Box<dyn TraitWithSize> = Box::new(S); // ERROR
```

```rust,compile_fail
// Not object safe if `Self` is a type argument.
// Not trait-object safe if `Self` is a type argument.
trait Super<A> {}
trait WithSelf: Super<Self> where Self: Sized {}

Expand Down
4 changes: 2 additions & 2 deletions src/type-coercions.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ an implementation of `Unsize<U>` for `T` will be provided:

* `[T; n]` to `[T]`.

* `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [object safe].
* `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [trait-object safe].

* `Foo<..., T, ...>` to `Foo<..., U, ...>`, when:
* `Foo` is a struct.
Expand Down Expand Up @@ -269,7 +269,7 @@ precisely.
[RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
[RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md
[subtype]: subtyping.md
[object safe]: items/traits.md#object-safety
[trait-object safe]: items/traits.md#object-safety
[type cast operator]: expressions/operator-expr.md#type-cast-expressions
[`Unsize`]: ../std/marker/trait.Unsize.html
[`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html
Expand Down
6 changes: 3 additions & 3 deletions src/types/trait-object.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update analogously

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
> _TraitObjectTypeOneBound_ :\
> &nbsp;&nbsp; `dyn`<sup>?</sup> [_TraitBound_]

A *trait object* is an opaque value of another type that implements a set of
traits. The set of traits is made up of an [object safe] *base trait* plus any
A *trait object* is an opaque value of a `dyn`-prefixed type that implements a set of
Copy link
Member

@fmease fmease Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick, can ignore: Describing types (semantic layer) via a 'syntactic characteristic' doesn't sit quite right with me. Esp. since this specific characteristic isn't the whole truth (cc existence of bare trait object types in earlier editions).

traits. The set of traits is made up of a [trait-object safe] *base trait* plus any
number of [auto traits].

Trait objects implement the base trait, its auto traits, and any [supertraits]
Expand Down Expand Up @@ -103,5 +103,5 @@ inferred with a sensible choice.
[auto traits]: ../special-types-and-traits.md#auto-traits
[defaults]: ../lifetime-elision.md#default-trait-object-lifetimes
[dynamically sized types]: ../dynamically-sized-types.md
[object safe]: ../items/traits.md#object-safety
[trait-object safe]: ../items/traits.md#object-safety
[supertraits]: ../items/traits.md#supertraits