Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jdm opened this issue Nov 25, 2017 · 7 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jdm
Copy link
Contributor

jdm commented Nov 25, 2017

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
@mark-i-m
Copy link
Member

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.

@sinkuu
Copy link
Contributor

sinkuu commented Nov 26, 2017

(serde-rs/json#357)

@dtolnay
Copy link
Member

dtolnay commented Nov 26, 2017

It was a bad idea to have those impls (serde-rs/json#380) but while they exist, it would be nice if the compiler could resolve this case better. #46206 may fix this.

@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 28, 2017
@kellerkindt
Copy link
Contributor

kellerkindt commented Nov 4, 2019

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>)

agrover pushed a commit to agrover/neqo that referenced this issue Apr 6, 2020
See rust-lang/rust#46257

This just requires us to be a little more explicit about types in a couple
places
agrover pushed a commit to agrover/neqo that referenced this issue Apr 9, 2020
See rust-lang/rust#46257

This just requires us to be a little more explicit about types in a couple
places
agrover pushed a commit to agrover/neqo that referenced this issue Apr 9, 2020
See rust-lang/rust#46257

This just requires us to be a little more explicit about types in a couple
places
agrover pushed a commit to agrover/neqo that referenced this issue Apr 17, 2020
See rust-lang/rust#46257

This just requires us to be a little more explicit about types in a couple
places
agrover pushed a commit to mozilla/neqo that referenced this issue Apr 17, 2020
See rust-lang/rust#46257

This just requires us to be a little more explicit about types in a couple
places
@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
jpcsmith pushed a commit to jpcsmith/neqo-qcsd that referenced this issue Jun 22, 2022
See rust-lang/rust#46257

This just requires us to be a little more explicit about types in a couple
places
@cathaysia
Copy link
Contributor

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

@cathaysia
Copy link
Contributor

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

@crumblingstatue
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants