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

Cannot infer type for [], but only when serde_json is used (?) #89254

Open
m-ou-se opened this issue Sep 25, 2021 · 4 comments
Open

Cannot infer type for [], but only when serde_json is used (?) #89254

m-ou-se opened this issue Sep 25, 2021 · 4 comments
Labels
A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@m-ou-se
Copy link
Member

m-ou-se commented Sep 25, 2021

extern crate serde_json; // Code works fine if you remove this line.

fn main() {
    Vec::<u8>::new() == [];
}

That produces:

error[E0282]: type annotations needed
 --> src/main.rs:4:25
  |
4 |     Vec::<u8>::new() == [];
  |                         ^^ cannot infer type

Somehow, referencing serde_json makes rustc unable to infer some type here. This happens on both the current stable and nightly versions of Rust.

@m-ou-se m-ou-se added A-inference Area: Type inference C-bug Category: This is a bug. labels Sep 25, 2021
@m-ou-se m-ou-se changed the title Cannot infer type for Vec, but only when serde_json is used (?) Cannot infer type for [], but only when serde_json is used (?) Sep 25, 2021
@m-ou-se
Copy link
Member Author

m-ou-se commented Sep 25, 2021

Looks like the problem is impl PartialEq<Value> for u8 in serde_json. Without that, impl PartialEq<u8> for u8 is the only PartialEq impl for u8.

So I suppose this is expected, although very confusing.

Here's a reproduction without a dependency:

struct Shark;

impl PartialEq<Shark> for u8 {
    fn eq(&self, other: &Shark) -> bool {
        false
    }
}

fn main() {
    Vec::<u8>::new() == [];
}

Labeling this as a diagnostics issue.

@m-ou-se m-ou-se added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. and removed C-bug Category: This is a bug. labels Sep 25, 2021
@jhpratt
Copy link
Member

jhpratt commented Sep 27, 2021

Of course having better diagnostics here is best for the immediate future, but has it ever been considered to have a "preferred" implementation where Self is the generic type? I can't recall the exact situation off-hand, but I know I've run into inference issues where that would have solved it in the past.

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 29, 2021
@estebank
Copy link
Contributor

For the general diagnostic improvement, we should be pointing at all the impls that apply (and also that info when suggesting the turbofish, while we are at it). Having a way to nudge inference towards a certain type is something I'd like, but it feels like it'd have similar problems as specialization.

bors added a commit to rust-lang-ci/rust that referenced this issue Oct 24, 2021
…=jackh726

Point at overlapping impls when type annotations are needed

Address rust-lang#89254.
@estebank
Copy link
Contributor

estebank commented Aug 14, 2023

Current output:

error[E0282]: type annotations needed
  --> f71.rs:10:25
   |
10 |     Vec::<u8>::new() == [];
   |                         ^^ cannot infer type

error[E0283]: type annotations needed
  --> f71.rs:10:22
   |
10 |     Vec::<u8>::new() == [];
   |                      ^^ cannot infer type for type parameter `U`
   |
note: multiple `impl`s satisfying `u8: PartialEq<_>` found
  --> f71.rs:3:1
   |
3  | impl PartialEq<Shark> for u8 {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: and another `impl` found in the `core` crate: `impl PartialEq for u8;`
   = note: required for `Vec<u8>` to implement `PartialEq<[_; 0]>`

I think the diagnostics part of this is handled relatively ok.

Tagging t-lang for the "priority impl" feature suggestion.

@estebank estebank added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants