Skip to content
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

Bounds on associated types prevent a trait from being dyn-compatible #65078

Open
RustyYato opened this issue Oct 4, 2019 · 0 comments
Open
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-objects Area: trait objects, vtable layout A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@RustyYato
Copy link
Contributor

RustyYato commented Oct 4, 2019

playground

trait Foo {
    type Bar: Other<Self::Bar>;
}

impl dyn Foo<Bar = ()> {}

trait Other<T> {}

impl<T> Other<T> for T {}

Doesn't compile with the error,

error[E0038]: the trait `Foo` cannot be made into an object
 --> src/lib.rs:5:6
  |
5 | impl dyn Foo<Bar = ()> {}
  |      ^^^^^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
  |
  = note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses

But this doesn't make sense, because Self (being the type erased thing in dyn Foo) is never referenced in any bound. Self::Bar is referenced, but this should be fine in a dyn-compatible1 trait because all associated types have to be specified to use the dyn-compatible trait (i.e. I can't do dyn Foo in this case). This can be seen in Iterator which is dyn-compatible.

I think this should work and allow these sorts of bounds.

Note that moving the bound to a function is perfectly fine as seen here, but not on associated types or the trait itself.

I think this may be related to #27675

Footnotes

  1. Formerly known as "object safe".

@RustyYato RustyYato changed the title Some bounds on associated types prevent object-safe Bounds on associated types prevent a trait from being object-safe Oct 4, 2019
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Oct 4, 2019
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 19, 2020
@fmease fmease changed the title Bounds on associated types prevent a trait from being object-safe Bounds on associated types prevent a trait from being dyn-compatible Oct 3, 2024
@fmease fmease added A-trait-objects Area: trait objects, vtable layout object-safe and removed object-safe labels Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-objects Area: trait objects, vtable layout A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants