Skip to content

Add rule to object safety? #756

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
ehuss opened this issue Feb 10, 2020 · 4 comments
Open

Add rule to object safety? #756

ehuss opened this issue Feb 10, 2020 · 4 comments
Labels
A-types Area: Types

Comments

@ehuss
Copy link
Contributor

ehuss commented Feb 10, 2020

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?

@ehuss ehuss added the A-types Area: Types label Feb 10, 2020
@Centril
Copy link
Contributor

Centril commented Feb 10, 2020

cc @nikomatsakis

@jmaargh
Copy link

jmaargh commented Dec 13, 2023

This apparently doesn't only apply for supertraits that use Self as a type parameter, but also if the trait itself does (for example PartialEq appears to be non-object-safe as a result) because

trait PartialEq<Rhs = Self>

I suggest adding

  • A generic type parameter of this trait, or any of its supertraits, is not specified to be Self

together with an expanded example to show that this applies even when Self: ?Sized.

For reference, this is the compiler error describing the same rule:

5  | trait MyTrait: PartialEq {}
   |       -------  ^^^^^^^^^ ...because it uses `Self` as a type parameter
   |       |
   |       this trait cannot be made into an object...

Happy to make a PR where exact language can be agreed if this is desirable.

@ehuss
Copy link
Contributor Author

ehuss commented Feb 4, 2024

I asked over at https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/object.20safety.20of.20Self.20in.20supertraits for some suggestions. Someone noted rust-lang/rust#88904 as a strange edge case, but I am uncertain if that is just a bug.

What you have suggested above sounds good to me, and would be happy to take a PR.

@jmaargh
Copy link

jmaargh commented Feb 4, 2024

Happy to. Will be delayed until I get permission from my new job but I don't see that being a problem.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-types Area: Types
Projects
None yet
Development

No branches or pull requests

3 participants