E0308 Fallback to blanket impl due to lack of ?Sized creates a puzzling type error
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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