Adding serde_json as an unused external crate causes a compiler error in unrelated code
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
I guess that kind of makes sense, though, doesn't it? Importing
serde_jsonadds some new impl ofPartialEqforu64, 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
(https://github.com/serde-rs/json/issues/357)
Shotaro Yamada at 2017-11-26 00:30:16
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
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
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 crateline 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
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
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
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 typecrumblingstatue at 2024-03-07 15:52:03