rust-lang / rust-lang/rust

Unexpected type inference failure when comparing Vec::<T>::new() to an empty array []

Open
#141,329 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-discussion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When comparing a newly created empty Vec::<&i32> or Vec::<Vec<i32>> with an empty array literal [], the compiler fails to infer the type and gives error E0282, even though the types seem unambiguous from the context.

I tried this code:

fn test1() {
    assert_eq!(Vec::<i32>::new(), []);
}

fn test2() {
    assert_eq!(Vec::<&i32>::new(), []);
}

fn test3() {
    assert_eq!(Vec::<Vec<i32>>::new(), []);
}

Only test1 compiles. test2 and test3 fail with: error[E0282]: type annotations needed

I expected the compiler to infer the type of [] from the left-hand side (Vec::<&i32> or Vec::<Vec>), like it does in test1.

Instead, this happened: Type inference fails unless I add an explicit type to the right-hand side, or avoid [] altogether.

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or regression test is named. Reproduce the issue with the three Rust snippets using rustc 1.86.0, then trace the compiler's type-inference handling for empty array literals and equality comparisons. Done means test2 and test3 infer their right-hand-side types without explicit annotations, with regression coverage added.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.