Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"reached the recursion limit during monomorphization" error occured #21460

Closed
hebiyan opened this issue Jan 21, 2015 · 2 comments
Closed

"reached the recursion limit during monomorphization" error occured #21460

hebiyan opened this issue Jan 21, 2015 · 2 comments

Comments

@hebiyan
Copy link

hebiyan commented Jan 21, 2015

I tried writing tarai function with Rust, version info is:

$ rustc --version
rustc 1.0.0-dev (3d5fbae33 2015-01-13 01:08:03 +0000)

written code is:

#![feature(unboxed_closures)]

fn tarai<F:Fn() -> i32>(x:i32, y:i32, z:F) -> i32 {
    if x <= y {
        y
    }
    else {
        let fz = z();
        let nx = tarai(x - 1, y, z);
        let ny = tarai(y - 1, fz, |&:| { x });
        let nz = |&:|  { tarai(fz - 1, x, |&:| { y }) };

        tarai(nx, ny, nz)
    }
}

fn main() {
    println!("{}", tarai(12, 6, |&:| { 0 }));
}

but compiling this code, occurs an error like this:

$ rustc ./21-161505.rs
21-161505.rs:3:1: 15:2 error: reached the recursion limit during monomorphization
21-161505.rs:3 fn tarai<F:Fn() -> i32>(x:i32, y:i32, z:F) -> i32 {
21-161505.rs:4     if x <= y {
21-161505.rs:5         y
21-161505.rs:6     }
21-161505.rs:7     else {
21-161505.rs:8         let fz = z();
               ...

Why my code is wrong? And what should I do for pass compiling without errors?
Please show me these. Sorry for poor my english. Thank you.

@hebiyan hebiyan changed the title "reached the recursion limit dureing monomorphization" error occured "reached the recursion limit during monomorphization" error occured Jan 21, 2015
@Aatch
Copy link
Contributor

Aatch commented Jan 21, 2015

This is a duplicate of #19596.

The solution is to take a boxed closure and avoid the recursive instantiation: fn tarai(x: i32, y: i32, z: &Fn() -> i32) -> i32

@Aatch Aatch closed this as completed Jan 21, 2015
@hebiyan
Copy link
Author

hebiyan commented Jan 21, 2015

@Aatch Thank you for showing the solution and the link to existing issue.
Sorry for duplicating issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants