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 "trait-object safety" #1512

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kornelski
Copy link

"Object safe" is not a self-explanatory term.

The the word "object" doesn't have a clear definition in Rust's context, and to users it may not be obvious that the term refers to trait objects specifically.

The "safe" part is not good either, because it could be misunderstood as relating to "safe Rust" or not-unsafe.

However, the term "object safe" is used in a lot of places, so I propose a minimal backwards-compatible fix by calling this concept "trait-object safety". Addition of the word "trait" adds context, and clearly connects it to trait objects.

Users may only know this concept from Box<dyn Trait> types, so I'm also emphasising dyn Trait compatibility.

More in the forum thread:
https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025

@kornelski
Copy link
Author

It's odd that the link checker doesn't get the right anchor, even tough when I run mdbook build locally, it writes the correct one. It seems like {#anchor} syntax isn't always supported?

@ehuss
Copy link
Contributor

ehuss commented Jun 18, 2024

To support heading attributes, the version of mdbook in

curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.14/mdbook-v0.4.14-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin
needs to be updated to the latest version (0.4.40).

@traviscross
Copy link
Contributor

We discussed this in a rustdocs call. We're going to let the discussion on IRLO play out a bit further, then we may lang nominate this.

@joshtriplett
Copy link
Member

However, the term "object safe" is used in a lot of places, so I propose a minimal backwards-compatible fix by calling this concept "trait-object safety".

We've definitely been using the term "object safe" or "object safety" for a long time in many places, so we don't want to leave people unable to look up that term. But I don't think that means we need to leave the term substantially unmodified; I think that means we need to leave plenty of pointers to what we used to call it.

I would suggest, instead, that we fully rebrand this as "dyn Trait compatible" (or, when appropriate, "compatible with dyn Trait" or "dyn-compatible"), and then include notes saying things like "This is sometimes also called 'trait-object safety', 'object safety', or 'object safe'." With that, plus keeping the old link anchors working (though we should add new ones as well to avoid making the old ones look like the canonical names), I think that'll be sufficient to not abandon people who already know the existing term.

@traviscross traviscross added S-waiting-on-team Status: This is waiting for action from some team. T-lang Team: Lang labels Sep 3, 2024
@traviscross
Copy link
Contributor

@rustbot labels -S-waiting-on-team

When this ongoing FCP completes, we will have decided to call traits that are compatible with dyn "dyn compatible" rather than "object safe".

@rustbot rustbot removed the S-waiting-on-team Status: This is waiting for action from some team. label Sep 25, 2024
@@ -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

Comment on lines +180 to +184
[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*.
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".

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

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

Comment on lines +67 to +68
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]):
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.

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

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

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Team: Lang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants