rust-lang / rust-lang/rust

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

Open
#146,208 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-confusing D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example from the issue in src/main.rs with rustc 1.91.0-nightly and confirm the current E0308 diagnostic. Trace the diagnostic's handling of the blanket ToOwned implementation and determine whether it can identify the missing ?Sized bound. Done means the compiler gives a useful hint explaining the selected blanket impl and suggesting ?Sized.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.