Skip to content

Commit

Permalink
Merge pull request #364 from schungx/master
Browse files Browse the repository at this point in the history
Refine codegen errors.
  • Loading branch information
schungx committed Feb 27, 2021
2 parents 1fb63c3 + f3b5df0 commit 0c62a62
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 50 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,8 @@
Rhai Release Notes
==================

This version introduces functions with `Dynamic` parameters acting as wildcards.

Version 0.19.13
===============

Expand Down
80 changes: 49 additions & 31 deletions codegen/src/function.rs
Expand Up @@ -98,8 +98,8 @@ pub fn print_type(ty: &syn::Type) -> String {
#[derive(Debug, Default)]
pub struct ExportedFnParams {
pub name: Vec<String>,
pub return_raw: bool,
pub pure: bool,
pub return_raw: Option<proc_macro2::Span>,
pub pure: Option<proc_macro2::Span>,
pub skip: bool,
pub special: FnSpecialAccess,
pub namespace: FnNamespaceAccess,
Expand Down Expand Up @@ -137,8 +137,8 @@ impl ExportedParams for ExportedFnParams {
items: attrs,
} = info;
let mut name = Vec::new();
let mut return_raw = false;
let mut pure = false;
let mut return_raw = None;
let mut pure = None;
let mut skip = false;
let mut namespace = FnNamespaceAccess::Unset;
let mut special = FnSpecialAccess::None;
Expand Down Expand Up @@ -194,18 +194,28 @@ impl ExportedParams for ExportedFnParams {
return Err(syn::Error::new(s.span(), "extraneous value"))
}

("pure", None) => pure = true,
("return_raw", None) => return_raw = true,
("pure", None) => pure = Some(item_span),
("return_raw", None) => return_raw = Some(item_span),
("skip", None) => skip = true,
("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)) => {
Expand Down Expand Up @@ -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<Item = &syn::FnArg> {
Expand All @@ -503,23 +513,31 @@ 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() {
if params.return_raw.is_some() && self.return_type().is_none() {
return Err(syn::Error::new(
self.signature.span(),
params.return_raw.unwrap(),
"functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>",
));
}

// 1b. Do not allow non-method pure functions.
//
if params.pure.is_some() && !self.mutable_receiver() {
return Err(syn::Error::new(
params.pure.unwrap(),
"'pure' is not necessary on functions without 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 => {
return Err(syn::Error::new(
self.signature.span(),
self.signature.inputs.span(),
"property getter requires exactly 1 parameter",
))
}
Expand All @@ -533,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",
))
}
Expand All @@ -561,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",
))
}
_ => {}
Expand Down Expand Up @@ -635,7 +653,7 @@ impl ExportedFn {
.return_type()
.map(|r| r.span())
.unwrap_or_else(|| proc_macro2::Span::call_site());
if self.params.return_raw {
if self.params.return_raw.is_some() {
quote_spanned! { return_span =>
pub #dynamic_signature {
#name(#(#arguments),*)
Expand All @@ -659,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! {
Expand All @@ -672,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! {
Expand All @@ -685,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! {
Expand All @@ -698,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! {
Expand Down Expand Up @@ -750,7 +768,7 @@ impl ExportedFn {
})
.unwrap(),
);
if !self.params().pure {
if !self.params().pure.is_some() {
let arg_lit_str =
syn::LitStr::new(&pat.to_token_stream().to_string(), pat.span());
unpack_statements.push(
Expand Down Expand Up @@ -871,7 +889,7 @@ impl ExportedFn {
.return_type()
.map(|r| r.span())
.unwrap_or_else(|| proc_macro2::Span::call_site());
let return_expr = if !self.params.return_raw {
let return_expr = if !self.params.return_raw.is_some() {
if self.return_dynamic {
quote_spanned! { return_span =>
Ok(#sig_name(#(#unpack_exprs),*))
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/module.rs
Expand Up @@ -217,7 +217,7 @@ impl Module {

pub fn exported_name(&self) -> Cow<str> {
if !self.params.name.is_empty() {
self.params.name.as_str().into()
(&self.params.name).into()
} else {
self.module_name().to_string().into()
}
Expand Down
8 changes: 4 additions & 4 deletions codegen/ui_tests/export_fn_raw_noreturn.stderr
@@ -1,8 +1,8 @@
error: functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>
--> $DIR/export_fn_raw_noreturn.rs:10:5
|
10 | pub fn test_fn(input: &mut Point) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
--> $DIR/export_fn_raw_noreturn.rs:9:13
|
9 | #[export_fn(return_raw)]
| ^^^^^^^^^^

error[E0425]: cannot find function `test_fn` in this scope
--> $DIR/export_fn_raw_noreturn.rs:19:5
Expand Down
6 changes: 3 additions & 3 deletions codegen/ui_tests/export_mod_raw_noreturn.stderr
@@ -1,8 +1,8 @@
error: functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>
--> $DIR/export_mod_raw_noreturn.rs:12:5
--> $DIR/export_mod_raw_noreturn.rs:11:11
|
12 | pub fn test_fn(input: &mut Point) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 | #[rhai_fn(return_raw)]
| ^^^^^^^^^^

error[E0433]: failed to resolve: use of undeclared crate or module `test_mod`
--> $DIR/export_mod_raw_noreturn.rs:22:5
Expand Down
4 changes: 2 additions & 2 deletions codegen/ui_tests/rhai_fn_getter_signature.stderr
@@ -1,8 +1,8 @@
error: property getter requires exactly 1 parameter
--> $DIR/rhai_fn_getter_signature.rs:13:9
--> $DIR/rhai_fn_getter_signature.rs:13:20
|
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
Expand Down
2 changes: 1 addition & 1 deletion 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)]
Expand Down
4 changes: 2 additions & 2 deletions codegen/ui_tests/rhai_fn_index_getter_signature.stderr
@@ -1,8 +1,8 @@
error: index getter requires exactly 2 parameters
--> $DIR/rhai_fn_index_getter_signature.rs:13:9
--> $DIR/rhai_fn_index_getter_signature.rs:13:20
|
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
Expand Down
4 changes: 2 additions & 2 deletions codegen/ui_tests/rhai_fn_setter_index_signature.stderr
@@ -1,8 +1,8 @@
error: index setter requires exactly 3 parameters
--> $DIR/rhai_fn_setter_index_signature.rs:13:9
--> $DIR/rhai_fn_setter_index_signature.rs:13:20
|
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
Expand Down
4 changes: 2 additions & 2 deletions codegen/ui_tests/rhai_fn_setter_return.stderr
@@ -1,8 +1,8 @@
error: property setter cannot return any value
--> $DIR/rhai_fn_setter_return.rs:13:9
--> $DIR/rhai_fn_setter_return.rs:13:51
|
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
Expand Down
4 changes: 2 additions & 2 deletions codegen/ui_tests/rhai_fn_setter_signature.stderr
@@ -1,8 +1,8 @@
error: property setter requires exactly 2 parameters
--> $DIR/rhai_fn_setter_signature.rs:13:9
--> $DIR/rhai_fn_setter_signature.rs:13:20
|
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
Expand Down

0 comments on commit 0c62a62

Please sign in to comment.