Skip to content

E0308 Fallback to blanket impl due to lack of ?Sized creates a puzzling type error #146208

@kornelski

Description

@kornelski

Code

trait MyToOwned {
    type Owned;
    fn my_to_owned(&self) -> Self::Owned;
}

impl<T: ToOwned> MyToOwned for T {
    type Owned = <T as ToOwned>::Owned;
    fn my_to_owned(&self) -> Self::Owned {
        self.to_owned()
    }
}

fn main() {
    let a = "asd";
    let b: String = a.to_owned();
    let c: String = a.my_to_owned();
}

Current output

error[E0308]: mismatched types
  --> src/main.rs:16:21
   |
16 |     let c: String = a.my_to_owned();
   |            ------   ^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
   |            |        |
   |            |        expected `String`, found `&str`
   |            expected due to this

Desired output

The ultimate solution is to add `?Sized`:

`impl<T: ToOwned + ?Sized> MyToOwned for T`

Rationale and extra context

Type error is technically correct, but it's very surprising in this context, and it's hard to see why the generic impl doesn't seem to work.

Perhaps the error could hint that a blanket impl has been used to satisfy T: ToOwned for &str instead of using impl for str:

https://doc.rust-lang.org/stable/std/borrow/trait.ToOwned.html#impl-ToOwned-for-T

Other cases

Rust Version

rustc 1.91.0-nightly (07d246fc6 2025-08-31)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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