json icon indicating copy to clipboard operation
json copied to clipboard

Found multiple `impl`s satisfying `u8: PartialEq<_>`

Open ireina7 opened this issue 2 years ago • 1 comments

I'm currently writing a framework with some sorting algorithms, I wrote the following interface:

pub fn merge_sort_<T: Ord + Copy>(arr: &mut [T]) {
    let len = arr.len();
    if len > 1 {
        _merge_sort_(arr, 0, len - 1);
    }
}

#[test]
fn empty() {
    let mut res = Vec::<u8>::new();
    merge_sort_(&mut res);
    assert_eq!(res, Vec::new());
}

However, when adding the serde_json crate dependency:

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

The compiler complains with my test code:

error[E0283]: type annotations needed
   --> src/algos/mutable/sort.rs:145:29
    |
145 |             assert_eq!(res, Vec::new());
    |             ----------------^^^^^^^^---
    |             |               |
    |             |               cannot infer type of the type parameter `T` declared on the struct `Vec`
    |             type must be known at this point
    |
    = note: multiple `impl`s satisfying `u8: PartialEq<_>` found in the following crates: `core`, `serde_json`:
            - impl PartialEq for u8;
            - impl PartialEq<Value> for u8;
    = note: required for `Vec<u8>` to implement `PartialEq<Vec<_>>`
help: consider specifying the generic argument
    |
145 |             assert_eq!(res, Vec::<T>::new());
    |                                +++++

Some errors have detailed explanations: E0282, E0283.
For more information about an error, try `rustc --explain E0282`.

What should I do to avoid this? Is adding type annotation the only way to solve this?

ireina7 avatar Nov 29 '22 03:11 ireina7

I think a type annotation is the best way to solve this, yes.

This seems more like Rust's fault than serde-json's. A PartialEq in some dependency crate shouldn't be able to break type inference like this.

kangalio avatar May 23 '23 17:05 kangalio