Skip to content

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

@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)

Activity

added
C-bugCategory: This is a bug.
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Feb 15, 2025
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Feb 15, 2025
changed the title [-]Cannot return impl Trait<Self> despite adding constraint "where Self : Sized"[/-] [+]Self as generic trait parameter is not properly ignored by Self: Sized bound[/+] on Feb 15, 2025
konnorandrews

konnorandrews commented on Feb 15, 2025

@konnorandrews

@rustbot modify labels: -I-ICE

removed
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
on Feb 15, 2025
konnorandrews

konnorandrews commented on Feb 15, 2025

@konnorandrews

Here is a smaller example without RPITIT.

struct X(&'static dyn A);

trait A {
    type T: B<Self> where Self: Sized;
}

trait B<T> {}
...
3 | trait A {
  |       - this trait cannot be made into an object...
4 |     type T: B<Self> where Self: Sized;
  |             ^^^^^^^ ...because it uses `Self` as a type parameter
added
A-dyn-compatibilityArea: Dyn compatibility (formerly: object safety)
A-dyn-traitArea: trait objects, vtable layout
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Feb 15, 2025
added a commit that references this issue on Feb 16, 2025
7884e17
added a commit that references this issue on Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @compiler-errors@fmease@konnorandrews@izagawd@rustbot

    Issue actions

      Self as generic trait parameter is not properly ignored by Self: Sized bound · Issue #137053 · rust-lang/rust