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: prohibit NEAR function generics #980

Merged
merged 12 commits into from
Jan 23, 2023
31 changes: 21 additions & 10 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 @@ -38,16 +38,27 @@ impl AttrSigInfo {
original_attrs: &mut Vec<Attribute>,
original_sig: &mut Signature,
) -> syn::Result<Self> {
if original_sig
.generics
.params
.iter()
.any(|g| matches!(g, GenericParam::Type(_) | GenericParam::Const(_)))
{
return Err(Error::new(
original_sig.generics.span(),
"Contract API is not allowed to have generics.",
));
let mut errors = vec![];
for generic in &original_sig.generics.params {
match generic {
GenericParam::Type(type_generic) => {
errors.push(Error::new(
type_generic.span(),
"Contract API is not allowed to have generics.",
));
}
GenericParam::Const(const_generic) => {
// `generic.span()` points to the `const` part of const generics, so we use `ident` explicitly.
itegulov marked this conversation as resolved.
Show resolved Hide resolved
errors.push(Error::new(
const_generic.ident.span(),
"Contract API is not allowed to have generics.",
));
}
_ => {}
}
}
if let Some(combined_errors) = errors.into_iter().reduce(|mut l, r| (l.combine(r), l).1) {
return Err(combined_errors);
}
let ident = original_sig.ident.clone();
let mut non_bindgen_attrs = vec![];
Expand Down
4 changes: 2 additions & 2 deletions near-sdk/compilation_tests/generic_const_function.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: Contract API is not allowed to have generics.
--> compilation_tests/generic_const_function.rs:14:26
--> compilation_tests/generic_const_function.rs:14:33
|
14 | pub fn is_ident_const<const N: usize>(&self, val: [u32; N]) -> [u32; N] {
| ^
| ^
4 changes: 2 additions & 2 deletions near-sdk/compilation_tests/generic_function.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: Contract API is not allowed to have generics.
--> compilation_tests/generic_function.rs:14:20
--> compilation_tests/generic_function.rs:14:21
|
14 | pub fn is_ident<T>(&self, val: T) -> T {
| ^
| ^