Recursive instantiation for Iterator trait with closure-taking adaptors hangs rustc #78403
Labels
A-impl-trait
Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
C-bug
Category: This is a bug.
I-hang
Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.
I-monomorphization
Issue: An error at monomorphization time.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Hello all,
This is probably a revival of #21403 and might be linked to #77173 and #76351, but I am not quite sure.
When defining a function that accepts a type implementing
Iterator<Item = ...>
and making a recursive call with the iterator adapted, the compiler reports a recursive instantiation error, which is to be expected. However, when considering some corner cases, the compiler can take quite a bit of time to reach this conclusion and in some others, it hangs "indefinitely".Code
Consider the following simple example, which does not fail:
It simply traverses the given iterator - in a really weird way yes - and displays each value, yielding:
So no infinite recursion call here. When applying an iterator adaptor to the recursive call, such as by replacing
traverse(iter)
withtraverse(iter.skip(1))
, then as expected it yields the error:However, when using adaptors that require a closure to be passed as an argument, such as
skip_while
, it becomes much slower. On my machine:Duration can vary between 10 and 15 seconds from my tests. Now, when applying the same adaptor but taking the iterator by reference, it completely hangs the compiler:
This does not reach the expected error, at least from my testing where rustc did not exit even after more than two hours. The previous example also hangs when using other closure-taking adaptors such as
take_while
,map
andfilter
, but not with ones likeskip
,take
norstep_by
.Obviously, a quick fix in a comparable situation is to use
dyn
trait objects in order to skip the iterator type monomorphization entirely, but I think the compiler shouldn't get stuck and still be able to report the instantiation recursion limit error successfully.Meta
Tested on stable, beta and nightly:
rustc +stable --version --verbose
:rustc +beta --version --verbose
:rustc +nightly --version --verbose
:Hope this helps.
Cheers,
Paul.
The text was updated successfully, but these errors were encountered: