Skip to content

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

Open
@623637646

Description

@623637646

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-discussionCategory: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler 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