Skip to content

Self receiver does *not* imply Self: Sized #1445

Closed as not planned
Closed as not planned
@QuineDot

Description

@QuineDot

The object safety page says

  • All associated functions must either be dispatchable from a trait object or be explicitly non-dispatchable:
    • Dispatchable functions must:
      • [...]
      • Not have a where Self: Sized bound (receiver type of Self (i.e. self) implies this).
    • Explicitly non-dispatchable functions require:
      • Have a where Self: Sized bound (receiver type of Self (i.e. self) implies this).

However, it is not true that a receiver type of Self implies Self: Sized.

// Minimal example; it also can be demonstrated with bounds that
// require `Sized`, like `<G: From<Self>>` say, even without a
// function body
trait Trait {
    // Fails without an explicit `where Self: Sized` bound
    fn method(self) {}
}

Accepting self-receiving methods as object-safe methods was a separate consideration from a Self: Sized bound in the RFC.

This is why all methods that require Self:Sized are exempt from the typical rules. This is also why by-value self methods are permitted, since currently one cannot invoke pass an unsized type by-value (though we consider that a useful future extension).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions