]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0644.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0644.md
1 A closure or generator was constructed that references its own type.
2
3 Erroneous code example:
4
5 ```compile_fail,E0644
6 fn fix<F>(f: &F)
7   where F: Fn(&F)
8 {
9     f(&f);
10 }
11
12 fn main() {
13     fix(&|y| {
14         // Here, when `x` is called, the parameter `y` is equal to `x`.
15     });
16 }
17 ```
18
19 Rust does not permit a closure to directly reference its own type,
20 either through an argument (as in the example above) or by capturing
21 itself through its environment. This restriction helps keep closure
22 inference tractable.
23
24 The easiest fix is to rewrite your closure into a top-level function,
25 or into a method. In some cases, you may also be able to have your
26 closure call itself by capturing a `&Fn()` object or `fn()` pointer
27 that refers to itself. That is permitting, since the closure would be
28 invoking itself via a virtual call, and hence does not directly
29 reference its own *type*.