Skip to content

Update object safety to match impl around self as receiver #1248

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
21 changes: 15 additions & 6 deletions src/items/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,25 @@ Object safe traits can be the base trait of a [trait object]. A trait is
* Not have any type parameters (although lifetime parameters are allowed),
* Be a [method] that does not use `Self` except in the type of the receiver.
* Have a receiver with one of the following types:
* `Self` (i.e. `self`)
* But unstable feature `unsized_fn_params` is required to dispatch it on a trait object.
* `&Self` (i.e. `&self`)
* `&mut Self` (i.e `&mut self`)
* [`Box<Self>`]
* [`Rc<Self>`]
* [`Arc<Self>`]
* [`Pin<P>`] where `P` is one of the types above
* Does not have a `where Self: Sized` bound (receiver type of `Self` (i.e. `self`) implies this).
* Does not have a `where Self: Sized` bound
* Explicitly non-dispatchable functions require:
* Have a `where Self: Sized` bound (receiver type of `Self` (i.e. `self`) implies this).
* Have a `where Self: Sized` bound

```rust
# use std::rc::Rc;
# use std::sync::Arc;
# use std::pin::Pin;
// Examples of object safe methods.
trait TraitMethods {
fn by_value(self: Self);
fn by_ref(self: &Self) {}
fn by_ref_mut(self: &mut Self) {}
fn by_box(self: Box<Self>) {}
Expand All @@ -101,7 +104,9 @@ trait TraitMethods {
fn nested_pin(self: Pin<Arc<Self>>) {}
}
# struct S;
# impl TraitMethods for S {}
# impl TraitMethods for S {
# fn by_value(self) {}
# }
# let t: Box<dyn TraitMethods> = Box::new(S);
```

Expand All @@ -110,6 +115,8 @@ trait TraitMethods {
trait NonDispatchable {
// Non-methods cannot be dispatched.
fn foo() where Self: Sized {}
// Requires unstable `unsized_fn_params` feature.
Copy link
Contributor

Choose a reason for hiding this comment

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

The other reference to the unstable feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, fixed in bc9a036

fn by_value(self);
// Self type isn't known until runtime.
fn returns(&self) -> Self where Self: Sized;
// `other` may be a different concrete type of the receiver.
Expand All @@ -120,12 +127,14 @@ trait NonDispatchable {

struct S;
impl NonDispatchable for S {
fn by_value(self) {}
fn returns(&self) -> Self where Self: Sized { S }
}
let obj: Box<dyn NonDispatchable> = Box::new(S);
obj.returns(); // ERROR: cannot call with Self return
obj.param(S); // ERROR: cannot call with Self parameter
obj.typed(1); // ERROR: cannot call with generic type
obj.by_value(); // ERROR: cannot call with Self as receiver
obj.returns(); // ERROR: cannot call with Self return
obj.param(S); // ERROR: cannot call with Self parameter
obj.typed(1); // ERROR: cannot call with generic type
```

```rust,compile_fail
Expand Down