Skip to content

Add rule to object safety? #756

Open
@ehuss

Description

@ehuss

There seems to be a rule missing from the object safety list: https://github.com/rust-lang/reference/blob/master/src/items/traits.md#object-safety

I'm not sure how to properly word the rule, but it something along that lines that "Supertraits cannot reference Self as a type parameter".

This restriction was introduced in rust-lang/rust#22452.

Some examples:

trait Super<T: ?Sized> {}
trait WithSelf: Super<Self> {}
struct S;
impl<A> Super<A> for S {}
impl WithSelf for S {}
let obj: Box<dyn WithSelf> = Box::new(S);  // ERROR: `Self` in type parameter
// Slight variation using default type.
trait Super<T: ?Sized = Self> {}
trait WithSelf: Super {}
struct S;
impl Super for S {}
impl WithSelf for S {}
let obj: Box<dyn WithSelf> = Box::new(S);  // ERROR: `Self` in type parameter

Is it correct that this rule should be added to the list? If so, can someone write down the proper wording?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions