30

This code compiles:

#[derive(Debug, Default)]
struct Example;

impl Example {
    fn some_method(&self) {}
}

fn reproduction() -> Example {
    let example = Default::default();
    // example.some_method();
    example
}

If the commented line is added back, it will cause an error:

error[E0282]: type annotations needed
  --> src/lib.rs:10:5
   |
9  |     let example = Default::default();
   |         ------- consider giving `example` a type
10 |     example.some_method();
   |     ^^^^^^^ cannot infer type
   |
   = note: type must be known at this point

Why does adding this method call cause type inference to fail?

I've seen these two questions:

From them, I know that Rust uses a (modified) version of Hindley-Milner. The latter question has an answer that describes Rust's type inference as a system of equations. Another answer explicitly states that "Type information in Rust can flow backwards".

Using this knowledge applied to this situation, we have:

  1. example is type ?E
  2. ?E must have a method called some_method
  3. ?E is returned
  4. The return type is Example

Working backward, it's easy for a human to see that ?E must be Example. Where is the gap between what I can see and what the compiler can see?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 3
    Note that this question is inspired from [another question](https://stackoverflow.com/q/55209837/155423). The author of that question did not appear to want to provide a MCVE, so I've created this question as a cleaner example. – Shepmaster Mar 19 '19 at 14:45
  • 1
    modified? - yes, see [Type Inference](https://github.com/rust-lang/rustc-guide/blob/master/src/type-inference.md#type-inference): _The type inference is based on the standard Hindley-Milner (HM) type inference algorithm, but extended in various way to accommodate subtyping, region inference, and higher-ranked types._ – zrzka Mar 19 '19 at 15:22
  • I wonder if this is something that [chalk](https://github.com/rust-lang/chalk) can help with when it lands… – Jmb Mar 19 '19 at 15:30
  • Tried with `-Z chalk` (nightly) and same result. – zrzka Mar 19 '19 at 20:11

2 Answers2

21

Based on known facts (see below), it fails to compile because:

  • the type checker goes through the function in the order it was written,
  • in let example = Default::default();, example can be anything which implements Default,
  • field accesses & method calls require a known type,
  • "anything implementing Default" is not a known type.

I replaced some_method() with a field access and it produces same error.


From Type inference depends on ordering (#42333):

use std::path::PathBuf;

pub struct Thing {
    pub f1: PathBuf,
}

fn junk() -> Vec<Thing> {
    let mut things = Vec::new();
    for x in vec![1, 2, 3] {
        if x == 2 {
            for thing in things.drain(..) {
                thing.f1.clone();
            }
            return vec![]
        }
        things.push(Thing{f1: PathBuf::from(format!("/{}", x))});
    }   
    things  
}               

fn main() { 
    junk();
}

This produces a compiler error with Rust 1.33.0:

error[E0282]: type annotations needed
  --> src/main.rs:13:17
   |
9  |     let mut things = Vec::new();
   |         ---------- consider giving `things` a type
...
13 |                 thing.f1.clone();
   |                 ^^^^^ cannot infer type
   |
   = note: type must be known at this point

You should focus on the following comments from eddyb (a well-known member of the the Rust language design team since May, 2016).

Comment #1:

This is a known limitation of the in-order type-checker. While inference flows freely, thing.f1.clone() is checked before things.push(Thing {...}) so it isn't known that thing: Thing when you try to access the f1 field. We may in the future move away from this, but there are no immediate plans.

What's more important is comment #2:

What I mean is that the type-checker goes through the function in the order it was written. [...] Fields accesses and methods calls are simply not supported unless the type is already known.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
zrzka
  • 20,249
  • 5
  • 47
  • 73
  • 2
    I don't think we need proof that eddyb is a prominent member; just a note next his name when you give the first comments would be enough. I think this answer would be improved by removing the Proof and Temporary sections; you can comment on your own answers for work in progress stuff, etc... no need to scare people with a longer answer when 1/3 doesn't actually answer. // // // On another note: awesome digging! – Matthieu M. Mar 20 '19 at 16:26
  • 1
    1) No official documentation for the _in-order_ type checking (incl. Rust compiler docs) 2) Lot of other issues / PRs mention _in-order_ with a note that it's a known limitation 3) It's covered by tests like [this one](https://github.com/rust-lang/rust/blob/1.33.0/src/test/ui/span/method-and-field-eager-resolution.rs) 4) Recompiled rustc with debug assertions and matched the output with the source code. I can confirm that there's _in-order_ type checking (but not the rustc expert, still may be wrong). Impossible to summarize my findings in a short answer (too long, complicated, so many paths). – zrzka Mar 26 '19 at 10:19
  • 1
    Based on summary from my previous comment, I'm affraid that there's not much else to add. I'm happy with what I've found, learned some internals, but I also agree that this isn't satisfying / very good answer. It's probably all we can do now unless some rust compiler team member steps in. If you want to play with it, [here're](https://github.com/rust-lang/rustc-guide/issues/8) some notes from Niko describing the fastest way to get a working compiler (if you'd like to rebuild with debug assertions). – zrzka Mar 26 '19 at 10:29
  • 2
    I personally find your answer very satisfying; sure it's not a "proof", but I don't think the Rust language is willing to enshrine type inference in the specification (as it leads to stagnation), so I suspect that studying the code and referencing known limitations is the best we can get. – Matthieu M. Mar 26 '19 at 12:38
  • 1
    Happy 10K reputation! – Shepmaster Mar 31 '19 at 14:13
8

I don't know the full answer and I have next to no knowledge of the internal workings of the Rust compiler, but here are some deductions I've come to from my experience with Rust.

Information about types in Rust can "flow backwards", but there are certain times when Rust needs to know (for absolute certain) the type of an expression. In these situations, it must "already" know the type, i.e. it will not continue to look forward.

From what I've seen, this situation is limited to method calls. I suspect it has something to do with the fact that methods can be implemented on traits, which substantially complicates things. I doubt that there are any traits in scope with a method named some_method, but I think that whenever the Rust compiler encounters a method call it requires the type to already be known for certain.

You can see this happen a lot with method calls on types which implement traits, the most common being the collect method on a type that implements the Iter trait. You will be able to call collect, but won't be able to call any methods on the result unless you specify the type.

So this works:

fn create_numbers(last_num: i32) -> Vec<i32> {
    let x = (0..10).collect();
    x
}

But this does not:

fn create_numbers(last_num: i32) -> Vec<i32> {
    let x = (0..10).collect();
    // In order to call `push`, we need to *already* know the type
    // of x for "absolute certain", and the Rust compiler doesn't 
    // keep looking forward
    x.push(42);
    x
}
  • _"I suspect it has something to do with the fact that methods can be implemented on traits"_ — I don't think it's specifically about the possibility of there being an ambiguous trait. I think it's just that the type must be known at the point of calling a method, in order to know which function is intended. It could be a trait but there could also be multiple types with this method in their impl. – Peter Hall Mar 19 '19 at 16:29
  • I appreciate the response, but isn't a very satisfying answer. The second half of the post (starting at "You can see...") is simply a restatement of the problem in the question and doesn't provide an answer. The first half correlates with my experiences, but, as you point out, isn't based on any particular authoritative source. – Shepmaster Mar 19 '19 at 16:41