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: strip return types of lifetimes #982

Merged
merged 7 commits into from
Jan 23, 2023
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
10 changes: 2 additions & 8 deletions near-sdk-macros/src/core_impl/info_extractor/arg_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::core_impl::info_extractor::serializer_attr::SerializerAttr;
use crate::core_impl::info_extractor::SerializerType;
use crate::core_impl::{info_extractor::serializer_attr::SerializerAttr, utils};
use quote::ToTokens;
use syn::{spanned::Spanned, Attribute, Error, Ident, Pat, PatType, Token, Type};

Expand Down Expand Up @@ -58,13 +58,7 @@ impl ArgInfo {
));
}
};
let (reference, mutability, ty) = match original.ty.as_ref() {
x @ Type::Array(_) | x @ Type::Path(_) | x @ Type::Tuple(_) => {
(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())?;
// 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{ArgInfo, BindgenArgType, InitAttr, MethodType, SerializerAttr, SerializerType};
use crate::core_impl::utils;
use proc_macro2::Span;
use quote::ToTokens;
use syn::spanned::Spanned;
Expand Down Expand Up @@ -132,7 +133,13 @@ impl AttrSigInfo {
}

*original_attrs = non_bindgen_attrs.clone();
let returns = original_sig.output.clone();
let returns = match &original_sig.output {
ReturnType::Default => ReturnType::Default,
ReturnType::Type(arrow, ty) => {
let (_, _, ty) = utils::extract_ref_mut(ty)?;
ReturnType::Type(*arrow, ty.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,4 +1,8 @@
use syn::{GenericArgument, Path, PathArguments, Type};
use syn::{
spanned::Spanned,
token::{And, Mut},
GenericArgument, Path, PathArguments, Type,
};

/// Checks whether the given path is literally "Result".
/// Note that it won't match a fully qualified name `core::result::Result` or a type alias like
Expand Down Expand Up @@ -73,3 +77,14 @@ pub(crate) fn extract_vec_type(ty: &Type) -> Option<&Type> {
_ => None,
}
}

/// 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) -> syn::Result<(Option<And>, Option<Mut>, Type)> {
match ty {
x @ Type::Array(_) | x @ Type::Path(_) | x @ Type::Tuple(_) => {
Ok((None, None, (*x).clone()))
}
Type::Reference(r) => Ok((Some(r.and_token), r.mutability, (*r.elem.as_ref()).clone())),
_ => Err(syn::Error::new(ty.span(), "Unsupported contract API type.")),
}
}
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> {
miraclx marked this conversation as resolved.
Show resolved Hide resolved
Expand Down