rhai: Unhelpful error when trying to call a non-existing function or with wrong signature

The returned error when trying to call a function implemented in Rust with either incorrect name or incorrect signature is not helpful. E.g.

Function not found: add_param_setting (rusp::usp_builder::add::CreateObjectBuilder, &str | ImmutableString | String, &str | ImmutableString | String, i64) (line 5, position 10)

It’s basically throwing back at you what you’re trying to do without any hints about what might be wrong with it.

I guess in the interest of space we don’t want to do some guesswork to find the correct fix and make a suggestion. But it would be nice to at least detect whether the function doesn’t exist at all (i.e. wrong function name) or the function does exist but has a non-matching signature.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 21

Most upvoted comments

Yes you’re correct. It should yield the namespace of the function.

I can add it.

@schungx This minimal example:

use rhai::{Engine, FuncRegistration, Module};

fn main() {
    let mut engine = Engine::new();
    let mut math_module = Module::new();
    FuncRegistration::new("add").set_into_module(&mut math_module, |a: i64, b: i64| a + b);
    engine.register_static_module("math", math_module.into());
    engine.collect_fn_metadata(
        None,
        |info| {
            dbg!(&info.metadata, &info.namespace);
            Some(())
        },
        false,
    );
}

yields the following output:

[src/main.rs:11:13] &info.metadata = FuncMetadata {
    hash: 13645348010736347151,
    namespace: Internal,
    access: Public,
    name: "add",
    num_params: 2,
    param_types: [
        TypeId(0xae6c4318bb07632e00428affbea41961),
        TypeId(0xae6c4318bb07632e00428affbea41961),
    ],
    params_info: [],
    return_type: "",
    comments: [],
}
[src/main.rs:11:13] &info.namespace = ""

my expectation would be that info.namespace should contain "math". Am I misunderstanding the API here?

@schungx I’m finally getting around to working on this, and I’m already quite close to having something neatly usable:

  × No function found matching signature: cos (i64)
   ╭─[<inline>:1:2]
 1 │ cos(1)
   ·  ┬
   ·  ╰── here
   ╰────
  help: Candidates:
        cos(x: f64) -> f64

However, it seems that the info that I get access to in collect_fn_metadata doesn’t ever contain the name of the containing namespace of the function – this means that I can neither somehow filter by namespace, nor properly show the namespace of a given function in the suggestion output (which would be useful for cases where I want to display “did you mean this similarly named function”-style errors) Specifically, I don’t see the namespace name for any namespaces that are defined in my code, through the register_static_module-api.

Additionally, it’d of course be very useful to have some more structured information in the EvalAltResult directly, as currently I effectively need to parse the signature that I get – which works, but feels a bit ugly and somewhat fragile. The main blocker right now is the namespace name availability though.

Yes, it contains everything to know about a function including all the parameter types and names.