A few weeks back, I wanted to add a char
to a String
. After finding out that that is not currently possible, I tried to find out why and found an issue about it¹. It was closed with the comment
Seems like something we shouldn’t track, but would probably accept a PR for.
So I forked the rust repo, read the contributing guide and made sure building it without any modifications worked fine, then added two impl
s: impl Add<char> for String
and impl AddAssign<char> for String
. After that though, as you can probably guess from the title, rustc
did not build anymore. Some code in an external crate resulted in a compiler error when trying to add a Cow<str>
to a String
. After some debugging, I was able to figure out what had gone wrong: Previously, there only was a single impl Add<_> for String
(the one for &str
), and the compiler allowed deref coercion to happen from Cow<str>
to &str
. However, once there was a second impl Add<_> for String
, the compiler didn’t infer a deref coercion anymore, and the code trying to add a Cow<str>
to a String
failed to compile.
I have created a small repository² that showcases this bug, and while creating it, came up with another case where adding a trait implementation would result in a compiler error in code using the trait, this time without deref.
1: https://github.com/rust-lang/rust/issues/38784
2: https://github.com/jplatte/rust-trait-impl-compatiblity