Skip to content

Infinite type-level recursion in ForEachKey impl in Policy #541

Closed
@apoelstra

Description

@apoelstra

In this code we have a method for_each which takes a F: FnMut and calls itself with an &mut F. This causes the compiler to try to instantiate the method with F, &mut F, &mut &mut F, etc., forever. Throughout our codebase, we've avoided this infinite recursion by having an internal real_for_each method which directly takes a &mut F, so that it can call itself with the same type that it was called with. Then our public-facing for_each simply sticks a &mut on its argument and calls real_for_each.

In the code in question, though, we didn't do this, and for some reason the Rust compiler has accepted it, until tonight. The latest nightly, 2023-04-21, does not compile the code.

Unfortunately this code has been present for a long time (I think since 1.0 of this crate) so to fix it, we will need to backport fixes to ever released version. Fortunately, there are a couple potential fixes, both of which are pretty simple:

  • Add a real_for_each internal funciton like we do elsewhere
  • Change &mut pred to &mut |key| pred(key) in two places (thanks GPT4 for this one)

See rust-lang/rust#110656

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions