-
Notifications
You must be signed in to change notification settings - Fork 487
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||||
|
||||||||||||||||||
[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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
+ updating the second part of the reference-style MarkDown link. TC, let me know if you agree with my suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||||
|
||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -62,12 +62,12 @@ trait Seq<T> { | |||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
## Object Safety | ||||||||||||||||||||||||||||||||
## Trait-Object Safety <a id="object-safety"></a> | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
NB: I'd love the URL fragment (named anchor) to get changed to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When renaming sections, you need to use a reference/src/expressions/operator-expr.md Lines 687 to 699 in c64e52a
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
* All [supertraits] must also be object safe. | ||||||||||||||||||||||||||||||||
* All [supertraits] must also be trait-object safe. | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
* `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. | ||||||||||||||||||||||||||||||||
|
@@ -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. | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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) {} | ||||||||||||||||||||||||||||||||
|
@@ -110,7 +110,7 @@ trait TraitMethods { | |||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
```rust,compile_fail | ||||||||||||||||||||||||||||||||
// This trait is object-safe, but these methods cannot be dispatched on a trait object. | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should double-check if there are any other mentions of |
||||||||||||||||||||||||||||||||
// 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 {} | ||||||||||||||||||||||||||||||||
|
@@ -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. | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
+ all other occurrences of this phrasing in this repo. |
||||||||||||||||||||||||||||||||
trait NotObjectSafe { | ||||||||||||||||||||||||||||||||
trait NotDynCompatible { | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
+ usage sites |
||||||||||||||||||||||||||||||||
const CONST: i32 = 1; // ERROR: cannot have associated const | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
fn foo() {} // ERROR: associated function without Sized | ||||||||||||||||||||||||||||||||
|
@@ -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; | ||||||||||||||||||||||||||||||||
|
@@ -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 {} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update analogously |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,8 @@ | |
> _TraitObjectTypeOneBound_ :\ | ||
> `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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.