From 823ecef1f1245feb5832aa8ad02898af2d8b4882 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 26 Feb 2021 17:19:40 +0800 Subject: [PATCH] #[rhai_fn(pure)] must have &mut parameter. --- codegen/src/function.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 8738b410..ceb4cb89 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -503,10 +503,9 @@ impl ExportedFn { } 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 - // later. + // Several issues are checked here to avoid issues with diagnostics caused by raising them 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() { 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 { // 2a. Property getters must take only the subject as an argument. FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 => {