From 643ecc86a34e7e77705b27bd5d907088d0d049b3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 20 Oct 2020 09:21:41 +0800 Subject: [PATCH] Minor refactor --- codegen/src/attrs.rs | 5 +++- codegen/src/function.rs | 28 +++++++++---------- codegen/src/test/function.rs | 14 +++++----- .../ui_tests/export_fn_raw_noreturn.stderr | 2 +- .../ui_tests/export_mod_raw_noreturn.stderr | 2 +- .../ui_tests/rhai_fn_getter_signature.stderr | 2 +- .../rhai_fn_index_getter_signature.stderr | 2 +- .../rhai_fn_setter_index_signature.stderr | 2 +- .../ui_tests/rhai_fn_setter_signature.stderr | 2 +- 9 files changed, 31 insertions(+), 28 deletions(-) diff --git a/codegen/src/attrs.rs b/codegen/src/attrs.rs index eb8d999a..a7986b6b 100644 --- a/codegen/src/attrs.rs +++ b/codegen/src/attrs.rs @@ -1,4 +1,7 @@ -use syn::{parse::ParseStream, parse::Parser, spanned::Spanned}; +use syn::{ + parse::{ParseStream, Parser}, + spanned::Spanned, +}; #[derive(Debug)] pub enum ExportScope { diff --git a/codegen/src/function.rs b/codegen/src/function.rs index d0f56f0b..c0d50588 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -13,7 +13,10 @@ use std::format; use std::borrow::Cow; use quote::{quote, quote_spanned}; -use syn::{parse::Parse, parse::ParseStream, parse::Parser, spanned::Spanned}; +use syn::{ + parse::{Parse, ParseStream, Parser}, + spanned::Spanned, +}; use crate::attrs::{ExportInfo, ExportScope, ExportedParams}; @@ -360,7 +363,7 @@ impl Parse for ExportedFn { pass_context, return_dynamic, mut_receiver, - params: ExportedFnParams::default(), + params: Default::default(), }) } } @@ -466,13 +469,10 @@ impl ExportedFn { // // 1. Do not allow non-returning raw functions. // - if params.return_raw - && mem::discriminant(&self.signature.output) - == mem::discriminant(&syn::ReturnType::Default) - { + if params.return_raw && self.return_type().is_none() { return Err(syn::Error::new( self.signature.span(), - "return_raw functions must return Result>", + "functions marked with 'return_raw' must return Result>", )); } @@ -481,7 +481,7 @@ impl ExportedFn { FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 => { return Err(syn::Error::new( self.signature.span(), - "property getter requires exactly 1 argument", + "property getter requires exactly 1 parameter", )) } // 2b. Property getters must return a value. @@ -495,7 +495,7 @@ impl ExportedFn { FnSpecialAccess::Property(Property::Set(_)) if self.arg_count() != 2 => { return Err(syn::Error::new( self.signature.span(), - "property setter requires exactly 2 arguments", + "property setter requires exactly 2 parameters", )) } // 3b. Property setters must return nothing. @@ -509,7 +509,7 @@ impl ExportedFn { FnSpecialAccess::Index(Index::Get) if self.arg_count() != 2 => { return Err(syn::Error::new( self.signature.span(), - "index getter requires exactly 2 arguments", + "index getter requires exactly 2 parameters", )) } // 4b. Index getters must return a value. @@ -523,7 +523,7 @@ impl ExportedFn { FnSpecialAccess::Index(Index::Set) if self.arg_count() != 3 => { return Err(syn::Error::new( self.signature.span(), - "index setter requires exactly 3 arguments", + "index setter requires exactly 3 parameters", )) } // 5b. Index setters must return nothing. @@ -593,19 +593,19 @@ impl ExportedFn { if self.params.return_raw { quote_spanned! { return_span=> pub #dynamic_signature { - super::#name(#(#arguments),*) + #name(#(#arguments),*) } } } else if self.return_dynamic { quote_spanned! { return_span=> pub #dynamic_signature { - Ok(super::#name(#(#arguments),*)) + Ok(#name(#(#arguments),*)) } } } else { quote_spanned! { return_span=> pub #dynamic_signature { - Ok(Dynamic::from(super::#name(#(#arguments),*))) + Ok(Dynamic::from(#name(#(#arguments),*))) } } } diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index ee5616e7..3cfdbf7b 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -297,7 +297,7 @@ mod generate_tests { Token().input_types() } pub fn dynamic_result_fn() -> Result > { - Ok(Dynamic::from(super::do_nothing())) + Ok(Dynamic::from(do_nothing())) } } }; @@ -339,7 +339,7 @@ mod generate_tests { Token().input_types() } pub fn dynamic_result_fn(x: usize) -> Result > { - Ok(Dynamic::from(super::do_something(x))) + Ok(Dynamic::from(do_something(x))) } } }; @@ -381,7 +381,7 @@ mod generate_tests { Token().input_types() } pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> Result > { - Ok(Dynamic::from(super::do_something(context, x))) + Ok(Dynamic::from(do_something(context, x))) } } }; @@ -425,7 +425,7 @@ mod generate_tests { Token().input_types() } pub fn dynamic_result_fn() -> Result > { - Ok(super::return_dynamic()) + Ok(return_dynamic()) } } }; @@ -497,7 +497,7 @@ mod generate_tests { Token().input_types() } pub fn dynamic_result_fn(x: usize, y: usize) -> Result > { - Ok(Dynamic::from(super::add_together(x, y))) + Ok(Dynamic::from(add_together(x, y))) } } }; @@ -541,7 +541,7 @@ mod generate_tests { Token().input_types() } pub fn dynamic_result_fn(x: &mut usize, y: usize) -> Result > { - Ok(Dynamic::from(super::increment(x, y))) + Ok(Dynamic::from(increment(x, y))) } } }; @@ -584,7 +584,7 @@ mod generate_tests { Token().input_types() } pub fn dynamic_result_fn(message: &str) -> Result > { - Ok(Dynamic::from(super::special_print(message))) + Ok(Dynamic::from(special_print(message))) } } }; diff --git a/codegen/ui_tests/export_fn_raw_noreturn.stderr b/codegen/ui_tests/export_fn_raw_noreturn.stderr index db719f9b..b2574e9c 100644 --- a/codegen/ui_tests/export_fn_raw_noreturn.stderr +++ b/codegen/ui_tests/export_fn_raw_noreturn.stderr @@ -1,4 +1,4 @@ -error: return_raw functions must return Result> +error: functions marked with 'return_raw' must return Result> --> $DIR/export_fn_raw_noreturn.rs:10:5 | 10 | pub fn test_fn(input: &mut Point) { diff --git a/codegen/ui_tests/export_mod_raw_noreturn.stderr b/codegen/ui_tests/export_mod_raw_noreturn.stderr index dc7bf10f..d840295f 100644 --- a/codegen/ui_tests/export_mod_raw_noreturn.stderr +++ b/codegen/ui_tests/export_mod_raw_noreturn.stderr @@ -1,4 +1,4 @@ -error: return_raw functions must return Result> +error: functions marked with 'return_raw' must return Result> --> $DIR/export_mod_raw_noreturn.rs:12:5 | 12 | pub fn test_fn(input: &mut Point) { diff --git a/codegen/ui_tests/rhai_fn_getter_signature.stderr b/codegen/ui_tests/rhai_fn_getter_signature.stderr index 0689c83f..70b42738 100644 --- a/codegen/ui_tests/rhai_fn_getter_signature.stderr +++ b/codegen/ui_tests/rhai_fn_getter_signature.stderr @@ -1,4 +1,4 @@ -error: property getter requires exactly 1 argument +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 { diff --git a/codegen/ui_tests/rhai_fn_index_getter_signature.stderr b/codegen/ui_tests/rhai_fn_index_getter_signature.stderr index 7e86413e..3bdffb28 100644 --- a/codegen/ui_tests/rhai_fn_index_getter_signature.stderr +++ b/codegen/ui_tests/rhai_fn_index_getter_signature.stderr @@ -1,4 +1,4 @@ -error: index getter requires exactly 2 arguments +error: index getter requires exactly 2 parameters --> $DIR/rhai_fn_index_getter_signature.rs:13:9 | 13 | pub fn test_fn(input: Point) -> bool { diff --git a/codegen/ui_tests/rhai_fn_setter_index_signature.stderr b/codegen/ui_tests/rhai_fn_setter_index_signature.stderr index 80cfc5fb..b7ae7dad 100644 --- a/codegen/ui_tests/rhai_fn_setter_index_signature.stderr +++ b/codegen/ui_tests/rhai_fn_setter_index_signature.stderr @@ -1,4 +1,4 @@ -error: index setter requires exactly 3 arguments +error: index setter requires exactly 3 parameters --> $DIR/rhai_fn_setter_index_signature.rs:13:9 | 13 | pub fn test_fn(input: Point) -> bool { diff --git a/codegen/ui_tests/rhai_fn_setter_signature.stderr b/codegen/ui_tests/rhai_fn_setter_signature.stderr index 55127a25..0e788c9a 100644 --- a/codegen/ui_tests/rhai_fn_setter_signature.stderr +++ b/codegen/ui_tests/rhai_fn_setter_signature.stderr @@ -1,4 +1,4 @@ -error: property setter requires exactly 2 arguments +error: property setter requires exactly 2 parameters --> $DIR/rhai_fn_setter_signature.rs:13:9 | 13 | pub fn test_fn(input: Point) -> bool {