From 33fa57d95b36ce45337b18e9bea5e40babfdd574 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 26 Feb 2021 20:00:25 +0800 Subject: [PATCH] Refine codegen error positions. --- CHANGELOG.md | 2 + codegen/src/function.rs | 44 ++++++++++++------- codegen/src/module.rs | 2 +- .../ui_tests/rhai_fn_getter_signature.stderr | 2 +- .../ui_tests/rhai_fn_global_multiple.stderr | 2 +- .../rhai_fn_index_getter_signature.stderr | 2 +- .../rhai_fn_setter_index_signature.stderr | 2 +- codegen/ui_tests/rhai_fn_setter_return.stderr | 2 +- .../ui_tests/rhai_fn_setter_signature.stderr | 2 +- 9 files changed, 36 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97b799ba..07ee0936 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Rhai Release Notes ================== +This version introduces functions with `Dynamic` parameters acting as wildcards. + Version 0.19.13 =============== diff --git a/codegen/src/function.rs b/codegen/src/function.rs index f45028f4..d35134db 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -200,12 +200,22 @@ impl ExportedParams for ExportedFnParams { ("global", None) => match namespace { FnNamespaceAccess::Unset => namespace = FnNamespaceAccess::Global, FnNamespaceAccess::Global => (), - _ => return Err(syn::Error::new(key.span(), "conflicting namespace")), + FnNamespaceAccess::Internal => { + return Err(syn::Error::new( + key.span(), + "namespace is already set to 'internal'", + )) + } }, ("internal", None) => match namespace { FnNamespaceAccess::Unset => namespace = FnNamespaceAccess::Internal, FnNamespaceAccess::Internal => (), - _ => return Err(syn::Error::new(key.span(), "conflicting namespace")), + FnNamespaceAccess::Global => { + return Err(syn::Error::new( + key.span(), + "namespace is already set to 'global'", + )) + } }, ("get", Some(s)) => { @@ -478,10 +488,10 @@ impl ExportedFn { } pub fn exported_name<'n>(&'n self) -> Cow<'n, str> { - self.params.name.last().map_or_else( - || self.signature.ident.to_string().into(), - |s| s.as_str().into(), - ) + self.params + .name + .last() + .map_or_else(|| self.signature.ident.to_string().into(), |s| s.into()) } pub fn arg_list(&self) -> impl Iterator { @@ -519,7 +529,7 @@ impl ExportedFn { if params.pure.is_some() && !self.mutable_receiver() { return Err(syn::Error::new( params.pure.unwrap(), - "functions marked with 'pure' must have a &mut first parameter", + "'pure' is not necessary on functions without a &mut first parameter", )); } @@ -541,21 +551,21 @@ impl ExportedFn { // 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(), + self.signature.inputs.span(), "property setter requires exactly 2 parameters", )) } // 3b. Property setters must return nothing. FnSpecialAccess::Property(Property::Set(_)) if self.return_type().is_some() => { return Err(syn::Error::new( - self.signature.span(), + self.signature.output.span(), "property setter cannot return any 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(), + self.signature.inputs.span(), "index getter requires exactly 2 parameters", )) } @@ -569,15 +579,15 @@ impl ExportedFn { // 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(), + self.signature.inputs.span(), "index setter requires exactly 3 parameters", )) } // 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 cannot return a value", + self.signature.output.span(), + "index setter cannot return any value", )) } _ => {} @@ -667,7 +677,7 @@ impl ExportedFn { pub fn generate_callable(&self, on_type_name: &str) -> proc_macro2::TokenStream { let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span()); let callable_fn_name: syn::Ident = syn::Ident::new( - format!("{}_callable", on_type_name.to_lowercase()).as_str(), + &format!("{}_callable", on_type_name.to_lowercase()), self.name().span(), ); quote! { @@ -680,7 +690,7 @@ impl ExportedFn { pub fn generate_input_names(&self, on_type_name: &str) -> proc_macro2::TokenStream { let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span()); let input_names_fn_name: syn::Ident = syn::Ident::new( - format!("{}_input_names", on_type_name.to_lowercase()).as_str(), + &format!("{}_input_names", on_type_name.to_lowercase()), self.name().span(), ); quote! { @@ -693,7 +703,7 @@ impl ExportedFn { pub fn generate_input_types(&self, on_type_name: &str) -> proc_macro2::TokenStream { let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span()); let input_types_fn_name: syn::Ident = syn::Ident::new( - format!("{}_input_types", on_type_name.to_lowercase()).as_str(), + &format!("{}_input_types", on_type_name.to_lowercase()), self.name().span(), ); quote! { @@ -706,7 +716,7 @@ impl ExportedFn { pub fn generate_return_type(&self, on_type_name: &str) -> proc_macro2::TokenStream { let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span()); let return_type_fn_name: syn::Ident = syn::Ident::new( - format!("{}_return_type", on_type_name.to_lowercase()).as_str(), + &format!("{}_return_type", on_type_name.to_lowercase()), self.name().span(), ); quote! { diff --git a/codegen/src/module.rs b/codegen/src/module.rs index dc0e8571..ca740de1 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -217,7 +217,7 @@ impl Module { pub fn exported_name(&self) -> Cow { if !self.params.name.is_empty() { - self.params.name.as_str().into() + (&self.params.name).into() } else { self.module_name().to_string().into() } diff --git a/codegen/ui_tests/rhai_fn_getter_signature.stderr b/codegen/ui_tests/rhai_fn_getter_signature.stderr index 70b42738..a5b6aaf1 100644 --- a/codegen/ui_tests/rhai_fn_getter_signature.stderr +++ b/codegen/ui_tests/rhai_fn_getter_signature.stderr @@ -2,7 +2,7 @@ error: property getter requires exactly 1 parameter --> $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 crate or module `test_module` --> $DIR/rhai_fn_getter_signature.rs:23:8 diff --git a/codegen/ui_tests/rhai_fn_global_multiple.stderr b/codegen/ui_tests/rhai_fn_global_multiple.stderr index 93da770d..f36c296d 100644 --- a/codegen/ui_tests/rhai_fn_global_multiple.stderr +++ b/codegen/ui_tests/rhai_fn_global_multiple.stderr @@ -1,4 +1,4 @@ -error: conflicting namespace +error: namespace is already set to 'global' --> $DIR/rhai_fn_global_multiple.rs:12:23 | 12 | #[rhai_fn(global, internal)] diff --git a/codegen/ui_tests/rhai_fn_index_getter_signature.stderr b/codegen/ui_tests/rhai_fn_index_getter_signature.stderr index 3bdffb28..bed303c6 100644 --- a/codegen/ui_tests/rhai_fn_index_getter_signature.stderr +++ b/codegen/ui_tests/rhai_fn_index_getter_signature.stderr @@ -2,7 +2,7 @@ error: index getter requires exactly 2 parameters --> $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 crate or module `test_module` --> $DIR/rhai_fn_index_getter_signature.rs:23:8 diff --git a/codegen/ui_tests/rhai_fn_setter_index_signature.stderr b/codegen/ui_tests/rhai_fn_setter_index_signature.stderr index b7ae7dad..4f778770 100644 --- a/codegen/ui_tests/rhai_fn_setter_index_signature.stderr +++ b/codegen/ui_tests/rhai_fn_setter_index_signature.stderr @@ -2,7 +2,7 @@ error: index setter requires exactly 3 parameters --> $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 crate or module `test_module` --> $DIR/rhai_fn_setter_index_signature.rs:23:8 diff --git a/codegen/ui_tests/rhai_fn_setter_return.stderr b/codegen/ui_tests/rhai_fn_setter_return.stderr index d55e2073..2001e7e5 100644 --- a/codegen/ui_tests/rhai_fn_setter_return.stderr +++ b/codegen/ui_tests/rhai_fn_setter_return.stderr @@ -2,7 +2,7 @@ error: property setter cannot return any 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 crate or module `test_module` --> $DIR/rhai_fn_setter_return.rs:24:8 diff --git a/codegen/ui_tests/rhai_fn_setter_signature.stderr b/codegen/ui_tests/rhai_fn_setter_signature.stderr index 0e788c9a..a6d0f947 100644 --- a/codegen/ui_tests/rhai_fn_setter_signature.stderr +++ b/codegen/ui_tests/rhai_fn_setter_signature.stderr @@ -2,7 +2,7 @@ error: property setter requires exactly 2 parameters --> $DIR/rhai_fn_setter_signature.rs:13:9 | 13 | pub fn test_fn(input: Point) -> bool { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^ error[E0433]: failed to resolve: use of undeclared crate or module `test_module` --> $DIR/rhai_fn_setter_signature.rs:23:8