Open
Description
Code
I tried this code:
#[test]
fn infer_as_ref() {
let left = std::borrow::Cow::Borrowed("lhs");
let right = String::from("rhs");
assert!(left != right.as_ref());
}
I expected to see this happen: Compiles OK and test passes.
Instead, this happened: Fails to compile,
Compiling asref v0.1.0 (/tmp/asref)
error[E0283]: type annotations needed
--> src/lib.rs:5:18
|
5 | assert!(left != right.as_ref());
| ^^ cannot infer type for reference `&_`
|
= note: multiple `impl`s satisfying `Cow<'_, str>: PartialEq<&_>` found in the `alloc` crate:
- impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str>;
- impl<'a> PartialEq<&'a ByteStr> for Cow<'a, str>;
error[E0283]: type annotations needed
--> src/lib.rs:5:27
|
5 | assert!(left != right.as_ref());
| ^^^^^^
|
= note: multiple `impl`s satisfying `String: AsRef<_>` found in the following crates: `alloc`, `std`:
- impl AsRef<OsStr> for String;
- impl AsRef<Path> for String;
- impl AsRef<[u8]> for String;
- impl AsRef<str> for String;
help: try using a fully qualified path to specify the expected types
|
5 - assert!(left != right.as_ref());
5 + assert!(left != <String as AsRef<T>>::as_ref(&right));
|
For more information about this error, try `rustc --explain E0283`.
error: could not compile `asref` (lib test) due to 2 previous errors
Note: impl<'a> PartialEq<&'a ByteStr> for Cow<'a, str>
should not be implemented as this should have a #![feature(bstr)]
gate.
Version it worked on
It most recently worked on: Rust 1.85.1
Version with regression
rustc --version --verbose
:
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Backtrace
Backtrace
<backtrace>