Skip to content

Commit

Permalink
fix: strip return types of lifetimes (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov authored Jan 23, 2023
1 parent 8846dab commit c82e595
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
9 changes: 2 additions & 7 deletions near-sdk-macros/src/core_impl/info_extractor/arg_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,8 @@ impl ArgInfo {
}
};
*original.ty.as_mut() = utils::sanitize_self(&original.ty, source_type)?;
let (reference, mutability, ty) = match original.ty.as_ref() {
x @ (Type::Array(_) | Type::Path(_) | Type::Tuple(_) | Type::Group(_)) => {
(None, None, (*x).clone())
}
Type::Reference(r) => (Some(r.and_token), r.mutability, (*r.elem.as_ref()).clone()),
_ => return Err(Error::new(original.span(), "Unsupported argument type.")),
};
let (reference, mutability, ty) =
utils::extract_ref_mut(original.ty.as_ref(), original.span())?;
// In the absence of callback attributes this is a regular argument.
let mut bindgen_ty = BindgenArgType::Regular;
// In the absence of serialization attributes this is a JSON serialization.
Expand Down
12 changes: 7 additions & 5 deletions near-sdk-macros/src/core_impl/info_extractor/attr_sig_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ impl AttrSigInfo {
}

*original_attrs = non_bindgen_attrs.clone();
let mut returns = original_sig.output.clone();

if let ReturnType::Type(_, ref mut ty) = returns {
*ty.as_mut() = utils::sanitize_self(&*ty, source_type)?;
}
let returns = match &original_sig.output {
ReturnType::Default => ReturnType::Default,
ReturnType::Type(arrow, ty) => {
let (_, _, ty) = utils::extract_ref_mut(ty, ty.span())?;
ReturnType::Type(*arrow, utils::sanitize_self(&ty, source_type)?.into())
}
};

let mut result = Self {
ident,
Expand Down
17 changes: 16 additions & 1 deletion near-sdk-macros/src/core_impl/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use proc_macro2::{Group, TokenStream as TokenStream2, TokenTree};
use proc_macro2::{Group, Span, TokenStream as TokenStream2, TokenTree};
use quote::quote;
use syn::spanned::Spanned;
use syn::token::{And, Mut};
use syn::{GenericArgument, Path, PathArguments, Signature, Type};

/// Checks whether the given path is literally "Result".
Expand Down Expand Up @@ -77,6 +78,20 @@ pub(crate) fn extract_vec_type(ty: &Type) -> Option<&Type> {
}
}

/// Extracts reference and mutability tokens from a `Type` object. Also, strips top-level lifetime binding if present.
pub(crate) fn extract_ref_mut(
ty: &Type,
span: Span,
) -> syn::Result<(Option<And>, Option<Mut>, Type)> {
match ty {
x @ (Type::Array(_) | Type::Path(_) | Type::Tuple(_) | Type::Group(_)) => {
Ok((None, None, (*x).clone()))
}
Type::Reference(r) => Ok((Some(r.and_token), r.mutability, (*r.elem.as_ref()).clone())),
_ => Err(syn::Error::new(span, "Unsupported contract API type.")),
}
}

/// Checks that the method signature is supported in the NEAR Contract API.
pub(crate) fn sig_is_supported(sig: &Signature) -> syn::Result<()> {
if sig.asyncness.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion near-sdk/compilation_tests/bad_argument.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Unsupported argument type.
error: Unsupported contract API type.
--> compilation_tests/bad_argument.rs:30:56
|
30 | pub fn insert(&mut self, key: TypeA, value: TypeB, t: impl MyTrait) -> Option<TypeB> {
Expand Down

0 comments on commit c82e595

Please sign in to comment.