Skip to content

where Self: Sized on trait method doesn't work as expected when implementing for unsized types. #57900

Closed

Description

@crlf0710

I've got this code

trait Foo {
    fn bar(&self) -> Self where Self: Sized;
}

impl Foo for str {
//    fn bar(&self) -> Self { unimplemented!() }
}

the error is:

error[E0046]: not all trait items implemented, missing: `bar`
 --> src/lib.rs:5:1

However, since str is not Sized, i don't think bar needs to be implemented. if i remove the "//", i'll got:

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:6:22

So although this trait fits object safety rules, i still can't implement it for unsized types.

Activity

  1. RustyYato commented on Jan 27, 2019

    @RustyYato
    Contributor

    As a work around, you can split off everything that needs Sized into another trait. like so

    trait ObjectSafe {
        // some functions
    }
    trait NotObjectSafe: ObjectSafe + Sized {
        // more functions
    }
  2. crlf0710 commented on Jan 29, 2019

    @crlf0710
    MemberAuthor

    Yes, i think this is a reasonable workaround. But it will be nice to get this limitation removed

  3. RustyYato commented on Jan 29, 2019

    @RustyYato
    Contributor

    Yes, I agree, this is an annoying limitation.

  4. crlf0710 commented on Dec 24, 2019

    @crlf0710
    MemberAuthor

    @rustbot modify labels to +A-traits +C-bug

  5. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    T-langRelevant to the language team
    on Mar 2, 2020
  6. QuineDot commented on May 7, 2022

    @QuineDot

    The original example can be worked around with the trivial bounds RFC (you still cannot call the implementation).

    That said, I still feel that the approximation of using Sized instead of some NotDyn trait should be fixed; said trait could be a supertrait of Sized for backwards compatibility. The reason (well, beyond semantics) being, sometimes you do want to call some generic method on a str implementation (imagine the method was -> () in the example).

  7. kupiakos commented on Jun 8, 2022

    @kupiakos
    Contributor

    For reference, you can also work around this on stable with an unused HRTB:

    trait Foo {
        fn bar(&self) -> Self where Self: Sized;
    }
    
    impl Foo for str {
        fn bar(&self) -> Self where for<'a> str: Sized { unimplemented!() }
    }
  8. added
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Jun 13, 2022
  9. grothesque commented on Nov 30, 2023

    @grothesque

    @QuineDot wrote:

    That said, I still feel that the approximation of using Sized instead of some NotDyn trait should be fixed; said trait could be a supertrait of Sized for backwards compatibility. The reason (well, beyond semantics) being, sometimes you do want to call some generic method on a str implementation (imagine the method was -> () in the example).

    Perhaps this approximation is not a huge problem in practice when using the language, but in my experience it can be a big problem when learning about trait objects in Rust. It was one of the big blockers for me.

    Although I read all the available official documentation (and many other texts) on trait objects and object safety, I kept being fundamentally confused about the relation of Self: Sized and trait objects. It's not like I did not understand why and how trait objects are unsized, but rather that I had the feeling that requiring Self: Sized is too strong when all one wants is excluding trait objects. However, since this point was never discussed, I thought that it is my understanding that must be flawed.

    It's only once I found @QuineDot's analysis that my doubts disappeared and I realized that my understanding was actually correct. (Thanks!)

    I assume that I am not the first or last to suffer from this difficulty, and suggest tackling it in some way. One could start by adding clarification to the definition of object safety.

  10. cyrgani commented on Jun 25, 2025

    @cyrgani
    Contributor

    This was fixed by #135480, meaning that

    trait Foo {
        fn bar(&self) -> Self where Self: Sized;
    }
    
    impl Foo for str {}

    compiles sucessfully now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamT-typesRelevant to the types 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