#[rhai_fn(pure)] must have &mut parameter.

This commit is contained in:
Stephen Chung 2021-02-26 17:19:40 +08:00
parent d935401b03
commit 823ecef1f1

View File

@ -503,10 +503,9 @@ impl ExportedFn {
} }
pub fn set_params(&mut self, mut params: ExportedFnParams) -> syn::Result<()> { pub fn set_params(&mut self, mut params: ExportedFnParams) -> syn::Result<()> {
// Several issues are checked here to avoid issues with diagnostics caused by raising them // Several issues are checked here to avoid issues with diagnostics caused by raising them later.
// later.
// //
// 1. Do not allow non-returning raw functions. // 1a. Do not allow non-returning raw functions.
// //
if params.return_raw && self.return_type().is_none() { if params.return_raw && self.return_type().is_none() {
return Err(syn::Error::new( return Err(syn::Error::new(
@ -515,6 +514,15 @@ impl ExportedFn {
)); ));
} }
// 1b. Do not allow non-method pure functions.
//
if params.pure && !self.mutable_receiver() {
return Err(syn::Error::new(
self.signature.span(),
"functions marked with 'pure' must have a &mut first parameter",
));
}
match params.special { match params.special {
// 2a. Property getters must take only the subject as an argument. // 2a. Property getters must take only the subject as an argument.
FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 => { FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 => {