Closed
Description
I made a MRE of a bug I encountered (in someone else's code actually):
struct S<T> {
_t: std::marker::PhantomData<T>
}
impl<T> S<T> {
fn f<U: ?Sized>(k: &U) where
T: std::borrow::Borrow<U> {}
}
fn main() {
let _ = |_: String| {};
S::<String>::f("".as_ref());
}
I expected to see this happen: A compile error mentioning something about not being able to infer the type that as_ref
should return, I suppose.
Instead, this happened:
error[E0283]: type annotations needed
--> src/main.rs:12:5
|
6 | / fn f<U: ?Sized>(k: &U) where
7 | | T: std::borrow::Borrow<U> {}
| |________________________________- required by `S::<T>::f`
...
12 | S::<String>::f("".as_ref());
| ^^^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
|
= note: cannot satisfy `std::string::String: std::borrow::Borrow<_>`
error: aborting due to previous error
Meta
Tested on playground, using stable, beta, and nightly (same error for all):
1.45.2
1.46.0-beta.4
1.47.0-nightly (2020-08-10 770bd3d1d03f0de2e27b)
Interesting variation:
struct S<T> {
_t: std::marker::PhantomData<T>
}
impl<T> S<T> {
fn f<U: ?Sized>(k: &U) where
T: std::borrow::Borrow<U> {}
}
fn main() {
S::<String>::f("".as_ref());
}
Which instead yields:
error[E0283]: type annotations needed
--> src/main.rs:11:5
|
6 | / fn f<U: ?Sized>(k: &U) where
7 | | T: std::borrow::Borrow<U> {}
| |________________________________- required by `S::<T>::f`
...
11 | S::<String>::f("".as_ref());
| ^^^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
|
= note: cannot satisfy `std::string::String: std::borrow::Borrow<_>`
error: aborting due to previous error
(which is just a poor error, I think)