Skip to content

Weird integer inference failure #33846

@nagisa

Description

@nagisa
const K: u64 = 8;
const P0: [u64; 6] = [2, 3, 7, 9, 10, 14];

fn main() {
    let mut ps = Vec::from(&P0 as &[_]);
    // Fixup P0 if necessary.
    for p in &mut ps {
        if *p == K {
            if *p == 2 || *p == 9 {
                *p -= 1;
            } else {
                *p += 1;
            }
        }
    }
    println!("{:?}", ps);
}

fails to compile, despite compiler figuring out that ps: Vec<u64>, but explicit annotation of the vector type is necessary for integers to be inferred correctly:

…
    // either works
    let mut ps: Vec<u64> = Vec::from(&P0 as &[_]);
    let mut ps = Vec::from(&P0 as &[u64]);
…

I would expect all the code snippets to be equivalent and compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions