Skip to content

Type inference error when accessing field #50692

Closed
@df5602

Description

@df5602

I was advised to open an issue here, so here goes:

I stumbled upon a strange compile error today, that I have minimized to the following example (Playground):

#[derive(Copy, Clone, Debug)]
struct Element {
    next: Option<usize>,
}

fn main() {
    let array = [Element { next: None }; 16];
    let number: usize = 42;
    let mut option = None;

    match option {
        Some(n) => {
            // This alone compiles
            let bar = array[n];
            println!("Number is {:?}", bar);

            // The following doesn't compile:

            // error[E0282]: type annotations needed
            //  --> src/main.rs:19:23
            //    |
            // 19 |             let foo = array[n].next;
            //    |                       ^^^^^^^^^^^^^ cannot infer type for `_`

            let foo = array[n].next;
            println!("Number is {:?}", foo);
        }
        None => option = Some(number),
    }

    println!("{:?}", option);
}

What's strange is that when I add a purposefully wrong type annotation like this:

let bar: bool = array[n];

the compiler correctly recognizes that bool is the wrong type and even tells me the correct type:

error[E0271]: type mismatch resolving `<usize as std::slice::SliceIndex<[Element]>>::Output == bool`
  --> src/main.rs:14:29
   |
14 |             let bar: bool = array[n];
   |                             ^^^^^^^^ expected struct `Element`, found bool
   |
   = note: expected type `Element`
              found type `bool`

However, as soon as I try to access a field, type inference fails. (Basically it knows that array[n] is of type Element, but later cannot infer the type of array[n].next.)

Is this a known limitation of type inference?

(EDIT: I'm aware that I can get rid of the compile error by adding a type annotation to the declaration of option. I'm just surprised by the behaviour of type inference in this example..)

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