Adding serde_json as an unused external crate causes a compiler error in unrelated code

c4c359e
Opened by Josh Matthews at 2024-03-07 15:52:03

This code compiles:

use std::cell::Cell;
use std::default::Default;

fn main() {
   let cell = Cell::new(0u64);
    cell.get() == Default::default();
}

This code does not:

extern crate serde_json;

use std::cell::Cell;
use std::default::Default;

fn main() {
   let cell = Cell::new(0u64);
    cell.get() == Default::default();
}
error[E0283]: type annotations required: cannot resolve `u64: std::cmp::PartialEq<_>`
 --> src/lib.rs:7:16
  |
7 |     cell.get() == Default::default();
  |                ^^

error: aborting due to previous error
  1. I guess that kind of makes sense, though, doesn't it? Importing serde_json adds some new impl of PartialEq for u64, and the compiler can no longer tell which one you want to use because RHS of == does not give it any info on which to make a choice.

    mark at 2017-11-25 22:31:19

  2. (https://github.com/serde-rs/json/issues/357)

    Shotaro Yamada at 2017-11-26 00:30:16

  3. It was a bad idea to have those impls (https://github.com/serde-rs/json/issues/380) but while they exist, it would be nice if the compiler could resolve this case better. https://github.com/rust-lang/rust/pull/46206 may fix this.

    David Tolnay at 2017-11-26 02:11:43

  4. Wow, just stumbled upon this. Had to search quite a bit until I figured out that serde / its usage is the cause. The compiler message isn't helpful at all

    playground

    extern crate serde_json; // 1.0.41
    
    pub enum MyEnum {
        Variant,
    }
    
    impl Into<u16> for MyEnum {
        fn into(self) -> u16 {
            0
        }
    }
    
    fn blubb() {
        // for as long as serde_json is mentioned in this file:
        // type annotations required: cannot resolve `MyEnum: std::convert::Into<_>`
        assert_eq!(0u16, MyEnum::Variant.into());
        
        // fine
        // assert_eq!(0u16, <MyEnum as Into<u16>>::into(MyEnum::Variant));
        
        // in case of no "extern crate"
        // serde_json::to_string_pretty(&"");
    }
    

    Remove the extern crate line and it compiles, otherwise it will throw an error:

       Compiling playground v0.0.1 (/playground)
    error[E0283]: type annotations required: cannot resolve `MyEnum: std::convert::Into<_>`
      --> src/lib.rs:16:38
       |
    16 |     assert_eq!(0u16, MyEnum::Variant.into());
       |                                      ^^^^
    
    error: aborting due to previous error
    

    @dtolnay Will there be any kind of quick fix available / possible? (except of using <... as Into<u16>>)

    Michael Watzko at 2019-11-04 18:40:49

  5. It is slightly different from the above question. If A depends on B, B depends on serde_json. Then serde_json would cause type inference in A to fail.

    Here is a example repo: https://github.com/cathaysia/rust_46257

    loongtao.zhang at 2023-08-26 10:06:25

  6. The problem is easy to spot on the latest version of rust's compiler. But it would be better if there is no problem

    loongtao.zhang at 2023-08-26 10:08:00

  7. Someone on the Rust community discord stumbled into this, and I made a super minimal repro, I figured I'd post it just in case.

    #![no_implicit_prelude]
    use ::core::todo;
    
    enum Foo {}
    
    impl ::core::cmp::PartialEq<Foo> for u32 {
        fn eq(&self, other: &Foo) -> bool {
            todo!()
        }
    }
    
    
    fn main() {
        0u32 == 0u64 as _;
    }
    
    error[E0282]: type annotations needed
      --> src/main.rs:14:21
       |
    14 |     0u32 == 0u64 as _;
       |                     ^ cannot infer type
    

    crumblingstatue at 2024-03-07 15:52:03