diff --git a/codegen/tests/test_modules.rs b/codegen/tests/test_modules.rs index f873e42b..014edbeb 100644 --- a/codegen/tests/test_modules.rs +++ b/codegen/tests/test_modules.rs @@ -222,6 +222,73 @@ fn duplicate_fn_rename_test() -> Result<(), Box> { Ok(()) } +mod multiple_fn_rename { + use rhai::plugin::*; + #[export_module] + pub mod my_adds { + use rhai::{FLOAT, INT}; + + pub fn get_mystic_number() -> FLOAT { + 42.0 + } + #[rhai_fn(name = "add", name = "+", name = "add_together")] + pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT { + f1 + f2 * 2.0 + } + + #[rhai_fn(name = "add", name = "+", name = "add_together")] + pub fn add_int(i1: INT, i2: INT) -> INT { + i1 + i2 * 2 + } + + #[rhai_fn(name = "prop", get = "prop")] + pub fn get_prop(x: FLOAT) -> FLOAT { + x * 2.0 + } + + #[rhai_fn(name = "idx", index_get)] + pub fn index(x: FLOAT, i: INT) -> FLOAT { + x + (i as FLOAT) + } + } +} + +#[test] +fn multiple_fn_rename_test() -> Result<(), Box> { + let mut engine = Engine::new(); + let m = rhai::exported_module!(crate::multiple_fn_rename::my_adds); + engine.load_package(m); + + let output_array = engine.eval::( + r#" + let fx = get_mystic_number(); + let fy1 = add(fx, 1.0); + let fy2 = add_together(fx, 1.0); + let fy3 = fx + 1.0; + let p1 = fx.prop; + let p2 = prop(fx); + let idx1 = fx[1]; + let idx2 = idx(fx, 1); + let ix = 42; + let iy1 = add(ix, 1); + let iy2 = add_together(ix, 1); + let iy3 = ix + 1; + [fy1, fy2, fy3, iy1, iy2, iy3, p1, p2, idx1, idx2] + "#, + )?; + assert_eq!(&output_array[0].as_float().unwrap(), &44.0); + assert_eq!(&output_array[1].as_float().unwrap(), &44.0); + assert_eq!(&output_array[2].as_float().unwrap(), &44.0); + assert_eq!(&output_array[3].as_int().unwrap(), &44); + assert_eq!(&output_array[4].as_int().unwrap(), &44); + assert_eq!(&output_array[5].as_int().unwrap(), &44); + assert_eq!(&output_array[6].as_float().unwrap(), &84.0); + assert_eq!(&output_array[7].as_float().unwrap(), &84.0); + assert_eq!(&output_array[8].as_float().unwrap(), &43.0); + assert_eq!(&output_array[9].as_float().unwrap(), &43.0); + Ok(()) +} + mod export_by_prefix { use rhai::plugin::*; #[export_module(export_prefix = "foo_")] diff --git a/codegen/ui_tests/rhai_fn_rename_collision_oneattr_multiple.rs b/codegen/ui_tests/rhai_fn_rename_collision_oneattr_multiple.rs new file mode 100644 index 00000000..112d9cc0 --- /dev/null +++ b/codegen/ui_tests/rhai_fn_rename_collision_oneattr_multiple.rs @@ -0,0 +1,30 @@ +use rhai::plugin::*; + +#[derive(Clone)] +pub struct Point { + x: f32, + y: f32, +} + +#[export_module] +pub mod test_module { + pub use super::Point; + #[rhai_fn(name = "foo", get = "bar")] + pub fn test_fn(input: Point) -> bool { + input.x > input.y + } + + #[rhai_fn(get = "bar")] + pub fn foo(input: Point) -> bool { + input.x < input.y + } +} + +fn main() { + let n = Point { x: 0.0, y: 10.0 }; + if test_module::test_fn(n) { + println!("yes"); + } else { + println!("no"); + } +} diff --git a/codegen/ui_tests/rhai_fn_rename_collision_oneattr_multiple.stderr b/codegen/ui_tests/rhai_fn_rename_collision_oneattr_multiple.stderr new file mode 100644 index 00000000..2b1da81c --- /dev/null +++ b/codegen/ui_tests/rhai_fn_rename_collision_oneattr_multiple.stderr @@ -0,0 +1,17 @@ +error: duplicate Rhai signature for 'get$bar' + --> $DIR/rhai_fn_rename_collision_oneattr_multiple.rs:17:15 + | +17 | #[rhai_fn(get = "bar")] + | ^^^ + +error: duplicated function renamed 'get$bar' + --> $DIR/rhai_fn_rename_collision_oneattr_multiple.rs:12:15 + | +12 | #[rhai_fn(name = "foo", get = "bar")] + | ^^^^ + +error[E0433]: failed to resolve: use of undeclared type or module `test_module` + --> $DIR/rhai_fn_rename_collision_oneattr_multiple.rs:25:8 + | +25 | if test_module::test_fn(n) { + | ^^^^^^^^^^^ use of undeclared type or module `test_module` diff --git a/codegen/ui_tests/rhai_fn_rename_collision_with_itself.rs b/codegen/ui_tests/rhai_fn_rename_collision_with_itself.rs new file mode 100644 index 00000000..0ba996eb --- /dev/null +++ b/codegen/ui_tests/rhai_fn_rename_collision_with_itself.rs @@ -0,0 +1,25 @@ +use rhai::plugin::*; + +#[derive(Clone)] +pub struct Point { + x: f32, + y: f32, +} + +#[export_module] +pub mod test_module { + pub use super::Point; + #[rhai_fn(name = "foo", name = "bar", name = "foo")] + pub fn test_fn(input: Point) -> bool { + input.x > input.y + } +} + +fn main() { + let n = Point { x: 0.0, y: 10.0 }; + if test_module::test_fn(n) { + println!("yes"); + } else { + println!("no"); + } +} diff --git a/codegen/ui_tests/rhai_fn_rename_collision_with_itself.stderr b/codegen/ui_tests/rhai_fn_rename_collision_with_itself.stderr new file mode 100644 index 00000000..40f3b830 --- /dev/null +++ b/codegen/ui_tests/rhai_fn_rename_collision_with_itself.stderr @@ -0,0 +1,17 @@ +error: duplicate Rhai signature for 'foo' + --> $DIR/rhai_fn_rename_collision_with_itself.rs:12:15 + | +12 | #[rhai_fn(name = "foo", name = "bar", name = "foo")] + | ^^^^ + +error: duplicated function renamed 'foo' + --> $DIR/rhai_fn_rename_collision_with_itself.rs:12:15 + | +12 | #[rhai_fn(name = "foo", name = "bar", name = "foo")] + | ^^^^ + +error[E0433]: failed to resolve: use of undeclared type or module `test_module` + --> $DIR/rhai_fn_rename_collision_with_itself.rs:20:8 + | +20 | if test_module::test_fn(n) { + | ^^^^^^^^^^^ use of undeclared type or module `test_module`