Skip to content

Uncaught recursive type definition with cargo check #15383

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

Closed
asdfish opened this issue Apr 3, 2025 · 1 comment
Closed

Uncaught recursive type definition with cargo check #15383

asdfish opened this issue Apr 3, 2025 · 1 comment

Comments

@asdfish
Copy link

asdfish commented Apr 3, 2025

Problem

cargo check does not catch a case of recursive type definitions

Steps

  1. Put the following in main.rs
#[derive(Clone, Debug)]
enum Expr {
    Literal(i32),
    Add([Box<Expr>; 2]),
}

fn eval_literal(expr: &i32, cont: impl FnOnce(&i32)) {
    cont(expr);
}
fn eval_add([left, right]: &[Box<Expr>; 2], cont: impl FnOnce(&i32)) {
    eval_expr(left, move |left| {
        eval_expr(right, move |right| cont(&(left + right)))
    })
}

fn eval_expr(expr: &Expr, cont: impl FnOnce(&i32)) {
    match expr {
        Expr::Literal(expr) => eval_literal(expr, cont),
        Expr::Add(expr) => eval_add(expr, cont),
    }
}

fn main() {
    eval_expr(
        &Expr::Add([
            Box::new(Expr::Literal(1)),
            Box::new(Expr::Add([
                Box::new(Expr::Literal(1)),
                Box::new(Expr::Literal(2)),
            ])),
        ]),
        |result| {
            println!("{}", result);
        },
    );
}

Output

cargo build

error: reached the recursion limit while instantiating `eval_expr::<{closure@src\main.rs:11:21: 11:32}>`
  --> src\main.rs:11:5
   |
11 | /     eval_expr(left, move |left| {
12 | |         eval_expr(right, move |right| cont(&(left + right)))
13 | |     })
   | |______^
   |
note: `eval_expr` defined here
  --> src\main.rs:16:1
   |
16 | fn eval_expr(expr: &Expr, cont: impl FnOnce(&i32)) {

cargo check

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s

Possible Solution(s)

No response

Version

@ehuss
Copy link
Contributor

ehuss commented Apr 3, 2025

Thanks for the report! This is a known issue called "post-monomorphization error". cargo check does not do monomoprhization, and so any errors that show up then are not reported.

Closing as a duplicate of rust-lang/rust#99682
Also note that https://rust-lang.github.io/rfcs/3477-cargo-check-lang-policy.html sets a policy that we acknowledge not everything will not catch everything.

@ehuss ehuss closed this as completed Apr 3, 2025
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