rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.79k stars 177 forks source link

Compiling a function with "return ;" results in infinite compilation time. #421

Closed a2aaron closed 3 years ago

a2aaron commented 3 years ago

When I compile a Rhai script that has a function containing return;, the engine will seemingly compile the script forever.

// main.rs
fn main() {
    let engine = rhai::Engine::new();

    println!("Compiling script...");
    let result = engine.compile(
        r#"
        fn foo() {
            print("Hello, World!");
            return;
        }
        "#,
    );
    println!("Compile result: {:?}", result);
}

When this program is run, only Compiling script... appears. No further progress is made. Note that this doesn't always happen for some reason. For example, the following Rhai script compiles successfully.

// rhai script
fn foo() {
    let x = 1;
    return;
}

But this locks up:

// rhai script
fn foo() {
    let x = 1;
    if x == 2 {

    }
    return;
}

For some reason, I can't replicate this behavior on https://rhai.rs/playground/stable/, all of the scripts shown above compile successfully.

Note that I am using version 0.20.3 of the crate.

schungx commented 3 years ago

Thanks for bringing this up. Seems like empty blocks trip up the optimizer.

The Playground is based on 0.20.1. I believe the bug was introduced after that version. In the meantime you can try pulling 0.20.1...

schungx commented 3 years ago

This is now fixed: https://github.com/rhaiscript/rhai/pull/422

You can try pulling it and test.

a2aaron commented 3 years ago

Thank you so much! That PR seemed to fix it!