Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support #[handle_result] in ABI macro #858

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions near-sdk-macros/src/core_impl/abi/abi_generator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::core_impl::utils;
use crate::core_impl::{utils, AttrSigInfo};
use crate::core_impl::{BindgenArgType, SerializerType};
use crate::{ImplItemMethodInfo, MethodType};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::spanned::Spanned;
use syn::ReturnType;

impl ImplItemMethodInfo {
Expand Down Expand Up @@ -45,8 +46,8 @@ impl ImplItemMethodInfo {
&self.attr_signature_info.method_type,
&MethodType::Init | &MethodType::InitIgnoreState
);
let is_payable = self.attr_signature_info.is_payable;
let is_private = self.attr_signature_info.is_private;
let AttrSigInfo { is_payable, is_private, is_handles_result, .. } =
self.attr_signature_info;

let mut params = Vec::<TokenStream2>::new();
let mut callbacks = Vec::<TokenStream2>::new();
Expand Down Expand Up @@ -126,6 +127,34 @@ impl ImplItemMethodInfo {
None
}
}
ReturnType::Type(_, ty) if is_handles_result && utils::type_is_result(ty) => {
let ty = if let Some(ty) = utils::extract_ok_type(ty) {
ty
} else {
return syn::Error::new_spanned(
ty,
"Function marked with #[handle_result] should have return type Result<T, E> (where E implements FunctionError).",
)
.into_compile_error();
};
Comment on lines +140 to +148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels like this is code duplication from the extraction from non-abi codegen, but probably fine for now

Copy link
Contributor Author

@itegulov itegulov Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I kind of feel like the bindgen macro needs a pre-processing step that would do all of these "type" checks (#[handle_result], #[callback_vec] etc) and produce a well-formed model. In this case, it would look something like this:

enum BindgenReturnType {
    Regular(Ident),
    HandleResult { ident: Ident, ok_type: Ident, err_type: Ident },
}

struct BindgenFunction {
    ...
    return_type: BindgenReturnType
}

So that we can just safely use these models in later steps.

let serialization_type =
abi_serialization_type(&self.attr_signature_info.result_serializer);
quote! {
Some(
near_sdk::__private::AbiType {
type_schema: gen.subschema_for::<#ty>(),
serialization_type: #serialization_type,
}
)
}
}
ReturnType::Type(_, ty) if is_handles_result => {
return syn::Error::new(
ty.span(),
"Method marked with #[handle_result] should return Result<T, E> (where E implements FunctionError).",
)
.to_compile_error();
}
ReturnType::Type(_, ty) => {
let serialization_type =
abi_serialization_type(&self.attr_signature_info.result_serializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl ImplItemMethodInfo {
ReturnType::Type(_, return_type) if *is_handles_result => {
return syn::Error::new(
return_type.span(),
"Method marked with #[handle_result] should return Result<T, E>.",
"Method marked with #[handle_result] should return Result<T, E> (where E implements FunctionError).",
)
.to_compile_error();
}
Expand Down Expand Up @@ -241,7 +241,7 @@ fn init_method_wrapper(
}
ReturnType::Type(_, return_type) if *is_handles_result => Err(syn::Error::new(
return_type.span(),
"Method marked with #[handle_result] should return Result<T, E>",
"Method marked with #[handle_result] should return Result<T, E> (where E implements FunctionError).",
)),
ReturnType::Type(_, _) => Ok(quote! {
#state_check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ mod tests {
let actual = method_info.method_wrapper();
let expected = quote!(
compile_error! {
"Method marked with #[handle_result] should return Result<T, E>."
"Method marked with #[handle_result] should return Result<T, E> (where E implements FunctionError)."
}
);
assert_eq!(expected.to_string(), actual.to_string());
Expand Down