What does “Overflow evaluating the requirement” mean and how can I fix it?

后端 未结 2 386
天涯浪人
天涯浪人 2020-12-11 17:31

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

2条回答
  •  青春惊慌失措
    2020-12-11 18:10

    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)
        );
    }
    

提交回复
热议问题