Skip to content

assert_eq!(x, y) doesn't coerce as well as assert!(x == y) does #120262

@ilyvion

Description

@ilyvion

(I don't know that this is a bug, exactly, but. Was still the best-fit template.)

I tried this code:

fn main() {
    let x: Option<&[u8]> = Some(&[1, 2]);
    assert_eq!(x, Some(&[3, 4]));
}

I expected to see this happen: This should compile, and then fail the assertion.

Instead, this happened: This fails to compile with the following error:

error[E0308]: mismatched types
 --> src/main.rs:3:19
  |
3 |     assert_eq!(x, Some(&[3, 4]));
  |                   ^^^^^^^^^^^^^ expected `Option<&[u8]>`, found `Option<&[{integer}; 2]>`
  |
  = note: expected enum `Option<&[u8]>`
             found enum `Option<&[{integer}; 2]>`

Re-writing the code as

fn main() {
    let x: Option<&[u8]> = Some(&[1, 2]);
    assert!(x == Some(&[3, 4]));
}

works as expected.

Also, yes, I know that it's possible to manually coerce this by writing assert_eq!(x, Some(&[3, 4][..]));, but it's disappointing that I should have to.

Meta

rustc --version --verbose:

Used the playground; it reports 1.77.0-nightly (2024-01-22 d5fd0997291ca0135401)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions