New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive impl-trait type causes compiler stack overflow #43263

Closed
Arnavion opened this Issue Jul 16, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@Arnavion
Copy link

Arnavion commented Jul 16, 2017

rustc 1.20.0-nightly (086eaa78e 2017-07-15)
binary: rustc
commit-hash: 086eaa78ea70075abe4e6b7fb9dc76259867b4be
commit-date: 2017-07-15
host: x86_64-pc-windows-msvc
release: 1.20.0-nightly
LLVM version: 4.0
#![feature(conservative_impl_trait)]

trait Future {
    type Item;

    fn and_then<M>(self, m: M) -> AndThen<Self, M> where Self: Sized {
        AndThen(self, m)
    }
}

struct AndThen<F, M>(F, M);

impl<F, M, F2> Future for AndThen<F, M> where F: Future, M: FnOnce(F::Item) -> F2, F2: Future {
    type Item = F2::Item;
}

struct FutureOk<T>(T);

impl<T> Future for FutureOk<T> {
    type Item = T;
}

fn foo(x: i32) -> impl Future<Item = i32> {
    FutureOk(x)
    .and_then(foo)
}

fn main() {
    let _ = foo(5);
}

This crashes the compiler with:

thread 'rustc' has overflowed its stack

because of the recursive return type of foo (X = AndThen<FutureOk<i32>, fn(i32) -> X>). Of course without impl trait I could not have written the type in the first place.

Replace .and_then(foo) with .and_then(|x| foo(x)) fixes it, possibly because the type then becomes AndThen<FutureOk<i32>, [closure]> so the recursive part is separated into the closure type.

@Mark-Simulacrum

This comment has been minimized.

Copy link
Member

Mark-Simulacrum commented Jul 19, 2017

Closing in favor of #39639.

@Arnavion

This comment has been minimized.

Copy link
Author

Arnavion commented Jul 19, 2017

Ah, I didn't find it because it wasn't tagged with the A-impl-trait label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment