title: Why does calling a method on a variable prevent Rust from inferring the type of the variable?
body: <p>This code compiles:</p>
  
  <pre><code>#[derive(Debug, Default)]
  struct Example;
  
  impl Example {
      fn some_method(&amp;self) {}
  }
  
  fn reproduction() -&gt; Example {
      let example = Default::default();
      // example.some_method();
      example
  }
  </code></pre>
  
  <p>If the commented line is added back, it will cause an error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0282]: type annotations needed
    --&gt; 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
  </code></pre>
  
  <p>Why does adding this method call cause type inference to fail?</p>
  
  <p>I've seen these two questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/37542971/155423">How does Rust&#39;s type inference work across multiple statements?</a></li>
  <li><a href="https://stackoverflow.com/q/44737154/155423">How does Rust infer resultant types from From::&lt;&gt;::from()?</a></li>
  </ul>
  
  <p>From them, I know that Rust uses a (<a href="https://rust-lang.github.io/rustc-guide/type-inference.html" rel="noreferrer">modified</a>) version of Hindley-Milner. The latter question has <a href="https://stackoverflow.com/a/44739469/155423">an answer</a> that describes Rust's type inference as a system of equations. <a href="https://stackoverflow.com/a/44737275/155423">Another answer</a> explicitly states that "Type information in Rust can flow backwards".</p>
  
  <p>Using this knowledge applied to this situation, we have:</p>
  
  <ol>
  <li><code>example</code> is type <code>?E</code></li>
  <li><code>?E</code> must have a method called <code>some_method</code></li>
  <li><code>?E</code> is returned</li>
  <li>The return type is <code>Example</code></li>
  </ol>
  
  <p>Working backward, it's easy for a human to see that <code>?E</code> must be <code>Example</code>. Where is the gap between what I can see and what the compiler can see?</p>