Enforce signatures on user get/set/index functions

This commit is contained in:
J Henry Waugh 2020-09-12 22:28:41 -05:00
parent 0a2f93439e
commit b1f23ac3ed
26 changed files with 553 additions and 21 deletions

View File

@ -377,9 +377,11 @@ impl ExportedFn {
}
pub fn set_params(&mut self, mut params: ExportedFnParams) -> syn::Result<()> {
// Do not allow non-returning raw functions.
// Several issues are checked here to avoid issues with diagnostics caused by raising them
// later.
//
// 1. Do not allow non-returning raw functions.
//
// This is caught now to avoid issues with diagnostics later.
if params.return_raw
&& mem::discriminant(&self.signature.output)
== mem::discriminant(&syn::ReturnType::Default)
@ -390,6 +392,58 @@ impl ExportedFn {
));
}
match params.special {
// 2a. Property getters must take only the subject as an argument.
FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 =>
return Err(syn::Error::new(
self.signature.span(),
"property getter requires exactly 1 argument",
)),
// 2b. Property getters must return a value.
FnSpecialAccess::Property(Property::Get(_)) if self.return_type().is_none() =>
return Err(syn::Error::new(
self.signature.span(),
"property getter must return a value"
)),
// 3a. Property setters must take the subject and a new value as arguments.
FnSpecialAccess::Property(Property::Set(_)) if self.arg_count() != 2 =>
return Err(syn::Error::new(
self.signature.span(),
"property setter requires exactly 2 arguments",
)),
// 3b. Property setters must return nothing.
FnSpecialAccess::Property(Property::Set(_)) if self.return_type().is_some() =>
return Err(syn::Error::new(
self.signature.span(),
"property setter must return no value"
)),
// 4a. Index getters must take the subject and the accessed "index" as arguments.
FnSpecialAccess::Index(Index::Get) if self.arg_count() != 2 =>
return Err(syn::Error::new(
self.signature.span(),
"index getter requires exactly 2 arguments",
)),
// 4b. Index getters must return a value.
FnSpecialAccess::Index(Index::Get) if self.return_type().is_none() =>
return Err(syn::Error::new(
self.signature.span(),
"index getter must return a value"
)),
// 5a. Index setters must take the subject, "index", and new value as arguments.
FnSpecialAccess::Index(Index::Set) if self.arg_count() != 3 =>
return Err(syn::Error::new(
self.signature.span(),
"index setter requires exactly 3 arguments",
)),
// 5b. Index setters must return nothing.
FnSpecialAccess::Index(Index::Set) if self.return_type().is_some() =>
return Err(syn::Error::new(
self.signature.span(),
"index setter must return no value"
)),
_ => {}
}
self.params = params;
Ok(())
}

View File

@ -1197,23 +1197,25 @@ mod generate_tests {
let input_tokens: TokenStream = quote! {
pub mod one_fn {
#[rhai_fn(set = "squared")]
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
}
};
let expected_tokens = quote! {
pub mod one_fn {
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
#[allow(unused_imports)]
use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
m.set_fn("set$squared", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
m.set_fn("set$squared", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
m
}
@ -1223,10 +1225,11 @@ mod generate_tests {
fn call(&self,
args: &mut [&mut Dynamic], pos: Position
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg1 = mem::take(args[1usize]).clone().cast::<u64>();
let arg0: &mut _ = &mut args[0usize].write_lock::<u64>().unwrap();
Ok(Dynamic::from(int_foo(arg0)))
Ok(Dynamic::from(int_foo(arg0, arg1)))
}
fn is_method_call(&self) -> bool { true }
@ -1235,7 +1238,7 @@ mod generate_tests {
Box::new(int_foo_token())
}
fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<u64>()].into_boxed_slice()
new_vec![TypeId::of::<u64>(), TypeId::of::<u64>()].into_boxed_slice()
}
}
pub fn int_foo_token_callable() -> CallableFunction {
@ -1256,25 +1259,29 @@ mod generate_tests {
let input_tokens: TokenStream = quote! {
pub mod one_fn {
#[rhai_fn(name = "set_sq", set = "squared")]
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
}
};
let expected_tokens = quote! {
pub mod one_fn {
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
#[allow(unused_imports)]
use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
m.set_fn("set_sq", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
m.set_fn("set_sq", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
m.set_fn("set$squared", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
m.set_fn("set$squared", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
m
}
@ -1284,10 +1291,11 @@ mod generate_tests {
fn call(&self,
args: &mut [&mut Dynamic], pos: Position
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg1 = mem::take(args[1usize]).clone().cast::<u64>();
let arg0: &mut _ = &mut args[0usize].write_lock::<u64>().unwrap();
Ok(Dynamic::from(int_foo(arg0)))
Ok(Dynamic::from(int_foo(arg0, arg1)))
}
fn is_method_call(&self) -> bool { true }
@ -1296,7 +1304,7 @@ mod generate_tests {
Box::new(int_foo_token())
}
fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<u64>()].into_boxed_slice()
new_vec![TypeId::of::<u64>(), TypeId::of::<u64>()].into_boxed_slice()
}
}
pub fn int_foo_token_callable() -> CallableFunction {

View File

@ -0,0 +1,28 @@
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 = "foo", set = "bar")]
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");
}
}

View File

@ -0,0 +1,11 @@
error: conflicting setter
--> $DIR/rhai_fn_getter_conflict.rs:12:42
|
12 | #[rhai_fn(name = "foo", get = "foo", set = "bar")]
| ^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_getter_conflict.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
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 = "foo", get = "bar")]
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");
}
}

View File

@ -0,0 +1,11 @@
error: conflicting getter
--> $DIR/rhai_fn_getter_multiple.rs:12:42
|
12 | #[rhai_fn(name = "foo", get = "foo", get = "bar")]
| ^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_getter_multiple.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,29 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(get = "foo")]
pub fn test_fn(input: &mut Point) {
input.x *= 2.0;
}
}
fn main() {
let mut n = Point {
x: 0.0,
y: 10.0,
};
test_module::test_fn(&mut n);
if n.x > 10.0 {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,11 @@
error: property getter must return a value
--> $DIR/rhai_fn_getter_return.rs:13:9
|
13 | pub fn test_fn(input: &mut Point) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_getter_return.rs:23:5
|
23 | test_module::test_fn(&mut n);
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(get = "foo")]
pub fn test_fn(input: Point, value: bool) -> bool {
value && input.x > input.y
}
}
fn main() {
let n = Point {
x: 0.0,
y: 10.0,
};
if test_module::test_fn(n, true) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,11 @@
error: property getter requires exactly 1 argument
--> $DIR/rhai_fn_getter_signature.rs:13:9
|
13 | pub fn test_fn(input: Point, value: bool) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_getter_signature.rs:23:8
|
23 | if test_module::test_fn(n, true) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
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", index_get, index_get)]
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");
}
}

View File

@ -0,0 +1,11 @@
error: conflicting index_get
--> $DIR/rhai_fn_index_getter_multiple.rs:12:40
|
12 | #[rhai_fn(name = "foo", index_get, index_get)]
| ^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_index_getter_multiple.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(index_get)]
pub fn test_fn(input: &mut Point, i: f32) {
input.x *= 2.0;
}
}
fn main() {
let mut n = Point {
x: 0.0,
y: 10.0,
};
if test_module::test_fn(&mut n, 5.0) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,11 @@
error: index getter must return a value
--> $DIR/rhai_fn_index_getter_return.rs:13:9
|
13 | pub fn test_fn(input: &mut Point, i: f32) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_index_getter_return.rs:23:8
|
23 | if test_module::test_fn(&mut n, 5.0) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(index_get)]
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");
}
}

View File

@ -0,0 +1,11 @@
error: index getter requires exactly 2 arguments
--> $DIR/rhai_fn_index_getter_signature.rs:13:9
|
13 | pub fn test_fn(input: Point) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_index_getter_signature.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
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", index_set, index_set)]
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");
}
}

View File

@ -0,0 +1,11 @@
error: conflicting index_set
--> $DIR/rhai_fn_index_setter_multiple.rs:12:40
|
12 | #[rhai_fn(name = "foo", index_set, index_set)]
| ^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_index_setter_multiple.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(index_set)]
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");
}
}

View File

@ -0,0 +1,11 @@
error: index setter requires exactly 3 arguments
--> $DIR/rhai_fn_setter_index_signature.rs:13:9
|
13 | pub fn test_fn(input: Point) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_setter_index_signature.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
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", set = "foo", set = "bar")]
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");
}
}

View File

@ -0,0 +1,11 @@
error: conflicting setter
--> $DIR/rhai_fn_setter_multiple.rs:12:42
|
12 | #[rhai_fn(name = "foo", set = "foo", set = "bar")]
| ^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_setter_multiple.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,29 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(set = "foo")]
pub fn test_fn(input: &mut Point, value: f32) -> bool {
let z = if value % 2 { input.x } else { input.y };
*input.x = z;
}
}
fn main() {
let mut n = Point {
x: 0.0,
y: 10.0,
};
if test_module::test_fn(&mut n, 5.0) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,11 @@
error: property setter must return no value
--> $DIR/rhai_fn_setter_return.rs:13:9
|
13 | pub fn test_fn(input: &mut Point, value: f32) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_setter_return.rs:24:8
|
24 | if test_module::test_fn(&mut n, 5.0) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,28 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(set = "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");
}
}

View File

@ -0,0 +1,11 @@
error: property setter requires exactly 2 arguments
--> $DIR/rhai_fn_setter_signature.rs:13:9
|
13 | pub fn test_fn(input: Point) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_setter_signature.rs:23:8
|
23 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`