Skip to content

Self as generic trait parameter is not properly ignored by Self: Sized bound #137053

Closed
@izagawd

Description

@izagawd

This Compiles

struct Foo;
trait Bar{
    fn some_method(&self) -> impl SomeTrait where Self: Sized;
}
trait SomeTrait{}
fn main() {
    let obj : &dyn Bar;
}

This Also Compiles

struct Foo;
trait Bar{
    fn some_method(&self) -> &dyn SomeTrait<Self> where Self: Sized;
}
trait SomeTrait<T>{}
fn main() {
    let obj : &dyn Bar;
}

But This..

struct Foo;
trait Bar{
    fn some_method(&self) -> impl SomeTrait<Self> where Self: Sized;
}
trait SomeTrait<T>{}
fn main() {
    let obj : &dyn Bar;
}

Show This Error output

error[E0038]: the trait `Bar` is not dyn compatible
 --> src\main.rs:7:15
  |
7 |     let obj : &dyn Bar;
  |               ^^^^^^^^ `Bar` is not dyn compatible
  |
note: for a trait to be dyn compatible it needs to allow building a vtable
      for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
 --> src\main.rs:3:35
  |
2 | trait Bar{
  |       --- this trait is not dyn compatible...
3 |     fn some_method(&self) -> impl SomeTrait<Self> where Self: Sized;
  |                                   ^^^^^^^^^^^^^^^ ...because it uses `Self` as a type parameter

Even though Self : Sized. This should be valid in rust.

version:
rustc 1.86.0-nightly (a567209 2025-02-13)

Metadata

Metadata

Labels

A-dyn-compatibilityArea: Dyn compatibility (formerly: object safety)A-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.T-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