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

serde_json breaks type inference of user code in some cases #651

Closed
acanthite1855 opened this issue Apr 6, 2020 · 2 comments
Closed

serde_json breaks type inference of user code in some cases #651

acanthite1855 opened this issue Apr 6, 2020 · 2 comments

Comments

@acanthite1855
Copy link

acanthite1855 commented Apr 6, 2020

I'm not sure whether this is serde_json's issue or rustc's. If you think this is not related tho this repo, but rather a bug in rustc, file it to them or tell me and I'll do this.

OK, now the issue. This code does not compile unless the first line is removed:

use serde_json;

#[cfg(test)]
mod test {
    #[test]
    fn into_iter() {
        assert_eq!(10, vec![1, 2, 3, 4].iter().sum());
    }
}

The problem is that compiler cannot infer types for sum(). However, removing or putting the use serde_json; into a comment makes it compile.
The problem is even more strange as it brakes similar code in other modules of the same package.

I made a demo repo that shows the problem.
It brakes only with_serde json. At least importing serde and failure does not brake it.

@acanthite1855 acanthite1855 changed the title use serde_json breaks type inference in some cases serde_json breaks type inference of user code in some cases Apr 6, 2020
@acanthite1855
Copy link
Author

acanthite1855 commented Apr 6, 2020

I found the issue in serde_json. This macro expansion causes it: src/value/partial_eq.rs#L90.
Without this macro expansion inference works.
If I understand correctly, rustc cannot figure what I want to compare in the assert_eq, because the partialeq_numeric makes primitives equatable to Value.

I can see that the macro is used only in tests, because the main library compiles fine without this macro.
But it cannot be moved into tests because of the rust's orphan rules for trait implementations. Neither #[cfg(test)] will work. Because the tests module implemented as a separate crate (very strange as for me, why not in the same carte?), and uses serde_json as a regular dependency. Which builds as regular depndency and throws all of the #[cfg(test)] code, if understand it correctly.

@dtolnay
Copy link
Member

dtolnay commented Jul 5, 2020

Thanks for the report -- I'll close this in favor of rust-lang/rust#46257 / #380.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants