Skip to content

rustc fails to infer types, provides confusing error message #37528

Closed
@jimblandy

Description

@jimblandy

Rust is unable to infer the element type in the results of a call to Iterator::partition if the item type is a reference, but not if it's an integer. The error message says nothing about lifetimes, or any other characteristic specific to references.

In nightly Rust, compiling the following code:

fn main() {
    let one = Some(1i32);

    // Rust won't infer the element type here:
    let _a: (Vec<_>, Vec<_>) = one.iter().partition(|_v| true);
    
    // But it does here:
    let _a: (Vec<_>, Vec<_>) = one.iter().cloned().partition(|_v| true);
    
    // And it infers Vec<i32> here:
    let _a: (Vec<_>, Vec<_>) = (0i32..1).partition(|_v| true);    
}

produces the following error:

error[E0282]: unable to infer enough type information about `_`
 --> <anon>:3:13
  |
3 |     let _a: (Vec<_>, Vec<_>) = Some(1i32).iter().partition(|_v| true);
  |             ^^^^^^^^^^^^^^^^ cannot infer type for `_`
  |
  = note: type annotations or generic parameter binding required

It's not clear to me at all why making the iterator clone the items, or using an iterator that doesn't produce references in the first place, should make the type inference work; it seems like partition should be as happy to build a vector of references as a vector of integers.

This is using: rustc 1.14.0-nightly (3f4408347 2016-10-27)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions