Skip to content

Cannot infer type when indexing an array inside closure with an argument #76112

Description

@YaLTeR

I tried this code (playground):

fn main() {
    let a: [i64; 1] = [0];
    let f = |p| {
        a[p].pow(1)
    };
    let c: i64 = f(0);
}

I expected to see this happen: no errors

Instead, this happened:

error[E0282]: type annotations needed
 --> src/main.rs:4:9
  |
4 |         a[p].pow(1)
  |         ^^^^ cannot infer type
  |
  = note: type must be known at this point

Replacing it with a[0] compiles, removing .pow(1) also compiles. Extracting a[p] into a variable as so:

let x = a[p];
x.pow(1)

also results in an error suggesting giving x an explicit type. Giving x an explicit i64 type also compiles.

Meta

rustc --version --verbose:

rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-unknown-linux-gnu
release: 1.46.0
LLVM version: 10.0

Same issue on beta and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions