Allow non-Dynamic in return_raw.

This commit is contained in:
Stephen Chung
2021-03-22 11:18:09 +08:00
parent b3bcd7bf79
commit a82f0fc738
23 changed files with 214 additions and 282 deletions

View File

@@ -279,7 +279,6 @@ pub struct ExportedFn {
signature: syn::Signature,
visibility: syn::Visibility,
pass_context: bool,
return_dynamic: bool,
mut_receiver: bool,
params: ExportedFnParams,
}
@@ -290,10 +289,6 @@ impl Parse for ExportedFn {
let entire_span = fn_all.span();
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
let dynamic_type_path1 = syn::parse2::<syn::Path>(quote! { Dynamic }).unwrap();
let dynamic_type_path2 = syn::parse2::<syn::Path>(quote! { rhai::Dynamic }).unwrap();
let mut return_dynamic = false;
let context_type_path1 = syn::parse2::<syn::Path>(quote! { NativeCallContext }).unwrap();
let context_type_path2 =
syn::parse2::<syn::Path>(quote! { rhai::NativeCallContext }).unwrap();
@@ -400,11 +395,6 @@ impl Parse for ExportedFn {
"Rhai functions cannot return references",
))
}
syn::Type::Path(p)
if p.path == dynamic_type_path1 || p.path == dynamic_type_path2 =>
{
return_dynamic = true
}
_ => {}
}
}
@@ -413,7 +403,6 @@ impl Parse for ExportedFn {
signature: fn_all.sig,
visibility,
pass_context,
return_dynamic,
mut_receiver,
params: Default::default(),
})
@@ -520,7 +509,7 @@ impl ExportedFn {
if params.return_raw.is_some() && self.return_type().is_none() {
return Err(syn::Error::new(
params.return_raw.unwrap(),
"functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>",
"functions marked with 'return_raw' must return Result<T, Box<EvalAltResult>>",
));
}
@@ -656,13 +645,7 @@ impl ExportedFn {
if self.params.return_raw.is_some() {
quote_spanned! { return_span =>
pub #dynamic_signature {
#name(#(#arguments),*)
}
}
} else if self.return_dynamic {
quote_spanned! { return_span =>
pub #dynamic_signature {
Ok(#name(#(#arguments),*))
#name(#(#arguments),*).map(Dynamic::from)
}
}
} else {
@@ -890,18 +873,12 @@ impl ExportedFn {
.map(|r| r.span())
.unwrap_or_else(|| proc_macro2::Span::call_site());
let return_expr = if !self.params.return_raw.is_some() {
if self.return_dynamic {
quote_spanned! { return_span =>
Ok(#sig_name(#(#unpack_exprs),*))
}
} else {
quote_spanned! { return_span =>
Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
}
quote_spanned! { return_span =>
Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
}
} else {
quote_spanned! { return_span =>
#sig_name(#(#unpack_exprs),*)
#sig_name(#(#unpack_exprs),*).map(Dynamic::from)
}
};

View File

@@ -447,7 +447,7 @@ mod generate_tests {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(return_dynamic())
Ok(Dynamic::from(return_dynamic()))
}
fn is_method_call(&self) -> bool { false }
@@ -477,7 +477,7 @@ mod generate_tests {
}
#[allow(unused)]
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(return_dynamic())
Ok(Dynamic::from(return_dynamic()))
}
}
};

View File

@@ -1,4 +1,4 @@
error: functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>
error: functions marked with 'return_raw' must return Result<T, Box<EvalAltResult>>
--> $DIR/export_fn_raw_noreturn.rs:9:13
|
9 | #[export_fn(return_raw)]

View File

@@ -1,10 +1,9 @@
error[E0308]: mismatched types
--> $DIR/export_fn_raw_return.rs:10:8
error[E0599]: the method `map` exists for type `bool`, but its trait bounds were not satisfied
--> $DIR/export_fn_raw_return.rs:10:33
|
9 | #[export_fn(return_raw)]
| ------------------------ expected `Result<rhai::Dynamic, std::boxed::Box<rhai::EvalAltResult>>` because of return type
10 | pub fn test_fn(input: Point) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found `bool`
| ^^^^ method cannot be called on `bool` due to unsatisfied trait bounds
|
= note: expected enum `Result<rhai::Dynamic, std::boxed::Box<rhai::EvalAltResult>>`
found type `bool`
= note: the following trait bounds were not satisfied:
`bool: std::iter::Iterator`
which is required by `&mut bool: std::iter::Iterator`

View File

@@ -1,4 +1,4 @@
error: functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>
error: functions marked with 'return_raw' must return Result<T, Box<EvalAltResult>>
--> $DIR/export_mod_raw_noreturn.rs:11:11
|
11 | #[rhai_fn(return_raw)]

View File

@@ -1,11 +1,9 @@
error[E0308]: mismatched types
--> $DIR/export_mod_raw_return.rs:12:8
error[E0599]: the method `map` exists for type `bool`, but its trait bounds were not satisfied
--> $DIR/export_mod_raw_return.rs:12:33
|
9 | #[export_module]
| ---------------- expected `Result<rhai::Dynamic, std::boxed::Box<EvalAltResult>>` because of return type
...
12 | pub fn test_fn(input: Point) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found `bool`
| ^^^^ method cannot be called on `bool` due to unsatisfied trait bounds
|
= note: expected enum `Result<rhai::Dynamic, std::boxed::Box<EvalAltResult>>`
found type `bool`
= note: the following trait bounds were not satisfied:
`bool: std::iter::Iterator`
which is required by `&mut bool: std::iter::Iterator`

View File

@@ -6,5 +6,5 @@ error[E0277]: the trait bound `NonClonable: Clone` is not satisfied
|
::: $WORKSPACE/src/dynamic.rs
|
| pub fn from<T: Variant + Clone>(value: T) -> Self {
| pub fn from<T: Variant + Clone>(mut value: T) -> Self {
| ----- required by this bound in `rhai::Dynamic::from`

View File

@@ -6,5 +6,5 @@ error[E0277]: the trait bound `NonClonable: Clone` is not satisfied
|
::: $WORKSPACE/src/dynamic.rs
|
| pub fn from<T: Variant + Clone>(value: T) -> Self {
| pub fn from<T: Variant + Clone>(mut value: T) -> Self {
| ----- required by this bound in `rhai::Dynamic::from`