Skip to content

Presence of std::simd sometimes breaks type inference #90904

Closed

Description

I tried this code:

use std::mem;

pub fn minimum_total(triangle: Vec<Vec<i32>>) -> i32 {
    let mut temp_cache = Vec::with_capacity(triangle.len());
    let mut cache = Vec::with_capacity(triangle.len());

    for row in triangle {
        temp_cache.resize(row.len(), 0);

        for (i, (target, source)) in temp_cache.iter_mut().zip(row).enumerate() {
            *target = match (cache.get(i.wrapping_sub(1)), cache.get(i)) {
                (None, None) => source,
                (None, Some(val)) | (Some(val), None) => source + val,
                (Some(left), Some(right)) => source + left.min(right),
            };
        }

        mem::swap(&mut cache, &mut temp_cache);
    }

    cache.into_iter().min().unwrap()
}

I expected to see this happen: The code compiles successfully.

Instead, this happened: The code failed to compile:

  --> src/lib.rs:11:36
   |
5  |     let mut cache = Vec::with_capacity(triangle.len());
   |         --------- consider giving `cache` the explicit type `Vec<T>`, where the type parameter `T` is specified
...
11 |             *target = match (cache.get(i.wrapping_sub(1)), cache.get(i)) {
   |                                    ^^^ cannot infer type for type parameter `T`
   |
   = note: type must be known at this point

For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground` due to previous error

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=446e975abb9c2a303485f483494a6512.

The code works fine with stable toolchain.

Meta

Rustc version: 1.58.0-nightly (2021-11-13 b416e38)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.P-highHigh priorityT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions