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

assert_eq(x, y) is not the same as assert_eq(y, x) because of type inferrence #70500

Open
jyn514 opened this issue Mar 28, 2020 · 3 comments
Open
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Mar 28, 2020

I tried this code (playground):

fn main() {
    assert_eq!(0_i64, S::zero());
    assert_eq!(S::zero(), 0_i64);
}

struct S;

trait Zeroed<T> {
    fn zero() -> T;
}

impl Zeroed<i16> for S {
    fn zero() -> i16 {
        0
    }
}

impl Zeroed<i64> for S {
    fn zero() -> i64 {
        0
    }
}

I expected to see this happen: The program compiles and runs successfully.

Instead, this happened:

error[E0282]: type annotations needed
 --> src/main.rs:3:16
  |
3 |     assert_eq!(S::zero(), 0_i64);
  |                ^^^^^^^ cannot infer type for type parameter `T` declared on the trait `Zeroed`

Note that the assert_eq!(0_i64, S::zero()); works as expected.

Meta

rustc --version --verbose:

rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-unknown-linux-gnu
release: 1.42.0
LLVM version: 9.0

The same error is present on nightly:

rustc 1.44.0-nightly (2fbb07525 2020-03-26)
binary: rustc
commit-hash: 2fbb07525e2f07a815e780a4268b11916248b5a9
commit-date: 2020-03-26
host: x86_64-unknown-linux-gnu
release: 1.44.0-nightly
LLVM version: 9.0
@jyn514 jyn514 added the C-bug Category: This is a bug. label Mar 28, 2020
@jonas-schievink jonas-schievink added A-inference Area: Type inference C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Mar 28, 2020
@jyn514
Copy link
Member Author

jyn514 commented Mar 28, 2020

Not sure the T-lang label is right here, I think this could be fixed in the standard library.

@jonas-schievink
Copy link
Contributor

How so? There's already a PartialEq impl for i64

@contrun
Copy link
Contributor

contrun commented Apr 8, 2020

Here is a related problem

fn main() {
    let xs: Vec<u64> = vec![1, 2, 3];
    assert_eq!(6u64, xs.iter().sum()); // This works
    // assert_eq!(xs.iter().sum(), 6u64); // This does not work.
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4f26089c7d9d2fc303bcbf38facdf8e3

@Dylan-DPC Dylan-DPC added C-bug Category: This is a bug. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Dec 19, 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 C-bug Category: This is a bug. 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

4 participants