Repository: rust
Issue: Recursive call passes `cargo check` but fails with `cargo build`
Labels: A-closures,T-lang,A-specialization,A-impl-trait

Hi Rust team, 
Thank you so much for the great language.

## Problem

Today I encountered a strange behavior of Cargo, 2018 edition.
This code passes `cargo check`, but it fails with `cargo build`.

```rust
fn repeat(n: i64, f: impl Fn()) {
    if n > 0 {
        f();
        repeat(n - 1, &f);
    }
}

fn main() {
    repeat(3, || {});
}
```

`cargo check` output:

```
Finished dev [unoptimized + debuginfo] target(s) in 0.16s
```

`cargo build --verbose` output:

<details>
<summary> (collapsed)</summary>

```
   Compiling rust-err v0.1.0 (/Users/ryu/work/rust-err)
     Running `rustc --edition=2018 --crate-name rust_err src/main.rs --color never --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=330d5c8df6305891 -C extra-filename=-330d5c8df6305891 --out-dir /Users/ryu/work/rust-err/target/debug/deps -C incremental=/Users/ryu/work/rust-err/target/debug/incremental -L dependency=/Users/ryu/work/rust-err/target/debug/deps`
error[E0275]: overflow evaluating the requirement `[closure@src/main.rs:9:15: 9:20]: std::ops::Fn<()>`
  |
  = help: consider adding a `#![recursion_limit="128"]` attribute to your crate
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`
  = note: required because of the requirements on the impl of `std::ops::Fn<()>` for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[closure@src/main.rs:9:15: 9:20]`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0275`.
error: Could not compile `rust-err`.

Caused by:
  process didn't exit successfully: `rustc --edition=2018 --crate-name rust_err src/main.rs --color never --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=330d5c8df6305891 -C extra-filename=-330d5c8df6305891 --out-dir /Users/ryu/work/rust-err/target/debug/deps -C incremental=/Users/ryu/work/rust-err/target/debug/incremental -L dependency=/Users/ryu/work/rust-err/target/debug/deps` (exit code: 1)
```

It seems that there is a recursive trait requirement, but I have no idea why this simple code causes it.

</details>

## Meta

`rustc --version --verbose`:

```
rustc 1.31.0 (abe02cefd 2018-12-04)
binary: rustc
commit-hash: abe02cefd6cd1916df62ad7dc80161bea50b72e8
commit-date: 2018-12-04
host: x86_64-apple-darwin
release: 1.31.0
LLVM version: 8.0
```