-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: 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.Diagnostics: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
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
futile
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: 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.Diagnostics: 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.Relevant to the compiler team, which will review and decide on the PR/issue.