I\'m running into what is potentially a compiler bug. However, I don\'t understand the issue well enough to port the proposed solution to my own code. Here\'s a stripped-dow
I don't claim to fully understand the problem, but it looks like an issue with resolving a type parameter. For example, what does F correspond to? At the first level, it is the closure. At the next level, it's a reference to that closure. At the next level, it's a reference to the reference of the closure.
My guess is that this is happening because of inlining, and basically it has hit an infinite recursion.
You can fix it by passing a reference to your closure instead:
struct Node {
pub children: Vec,
}
fn map_nodes(f: &F, n: &Node) -> Vec
where
F: Fn(&Node) -> R,
{
let mut v = Vec::new();
let z: R = f(n);
v.push(z);
v.extend(n.children.iter().flat_map(|child| map_nodes(f, &child)));
v
}
fn main() {
let node = Node {
children: vec![Node { children: vec![] }, Node { children: vec![] }],
};
println!(
"Node lengths: {:?}",
map_nodes(&|n| n.children.len(), &node)
);
}