Skip to content

object-safety removed with type AssocType: PartialEq #122798

Closed
@RustyYato

Description

@RustyYato

I tried this code:

pub trait Foo {
    type X: PartialEq;
}

pub trait Bar {
    type Y: Eq;
}

// this doesn't work for some reason!!!
// fn get_foo<T: Foo>(t: &T) -> &dyn Foo<X = T::X> {
//     t
// }

fn get_bar<T: Bar>(t: &T) -> &dyn Bar<Y = T::Y> {
    t
}

If I uncomment out get_foo, I get an error, but get_bar doesn't error!
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f20eef39a18a6fdc11cbeea9778ee294

To me this makes no sense since Bar is more strict than Foo, so either: both should not compile or Foo should compile (Bar may not since it has more requirements on it's associated type).

What's more fun is that this used to compile way back in v1.15.1 according to godbolt 😄 https://godbolt.org/z/TvPsjPq5o
But no version after that

Meta

playground is on v1.76.0 when I ran this

rustc --version --verbose:

rustc 1.79.0-nightly (3c85e5624 2024-03-18)
binary: rustc
commit-hash: 3c85e56249b0b1942339a6a989a971bf6f1c9e0f
commit-date: 2024-03-18
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2
Compile Error

error[E0038]: the trait `Foo` cannot be made into an object
  --> src/lib.rs:10:31
   |
10 | fn get_foo<T: Foo>(t: &T) -> &dyn Foo<X = T::X> {
   |                               ^^^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
   |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> src/lib.rs:2:13
   |
1  | pub trait Foo {
   |           --- this trait cannot be made into an object...
2  |     type X: PartialEq;
   |             ^^^^^^^^^ ...because it uses `Self` as a type parameter

For more information about this error, try `rustc --explain E0038`.
error: could not compile `playground` (lib) due to 1 previous error

Metadata

Metadata

Labels

A-type-systemArea: Type systemC-bugCategory: This is a bug.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions