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(proc macros): don't use params as name #1363

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 30 additions & 7 deletions proc-macros/src/render_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::rpc_macro::{RpcDescription, RpcMethod, RpcSubscription};
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{AngleBracketedGenericArguments, FnArg, Pat, PatIdent, PatType, PathArguments, TypeParam};
use syn::{AngleBracketedGenericArguments, FnArg, Ident, Pat, PatIdent, PatType, PathArguments, TypeParam};

impl RpcDescription {
pub(super) fn render_client(&self) -> Result<TokenStream2, syn::Error> {
Expand Down Expand Up @@ -163,6 +163,8 @@ impl RpcDescription {
let method = quote! {
#docs
#deprecated
#[allow(non_snake_case)]
#[allow(clippy::used_underscore_binding)]
async fn #rust_method_name(#rust_method_params) -> #returns {
let params = { #parameter_builder };
self.#called_method(#rpc_method_name, params).await
Expand Down Expand Up @@ -196,6 +198,8 @@ impl RpcDescription {

let method = quote! {
#docs
#[allow(non_snake_case)]
#[allow(clippy::used_underscore_binding)]
async fn #rust_method_name(#rust_method_params) -> #returns {
let params = #parameter_builder;
self.subscribe(#rpc_sub_name, params, #rpc_unsub_name).await
Expand All @@ -210,14 +214,23 @@ impl RpcDescription {
param_kind: &ParamKind,
signature: &syn::TraitItemFn,
) -> TokenStream2 {
const ILLEGAL_PARAM_NAME: &str = "__RpcParams__";

let jsonrpsee = self.jsonrpsee_client_path.as_ref().unwrap();
let p = Ident::new(ILLEGAL_PARAM_NAME, proc_macro2::Span::call_site());

if params.is_empty() {
return quote!({
#jsonrpsee::core::params::ArrayParams::new()
});
}

if params.iter().any(|(param, _)| param.ident == p) {
panic!(
"Cannot use `{}` as a parameter name because it's overlapping with an internal variable in the generated code. Change it something else to make it work", ILLEGAL_PARAM_NAME
);
}

match param_kind {
ParamKind::Map => {
// Extract parameter names.
Expand All @@ -229,27 +242,37 @@ impl RpcDescription {
let (value, _value_type) = pair.1;
quote!(#name, #value)
});

// It's possible that the user has a parameter named `ILLEGAL_PARAM_NAME` in there API
// which would conflict with our internal parameter name
//
// We will throw an error if that is the case.
if param_names.iter().any(|name| name == ILLEGAL_PARAM_NAME) {
panic!("Cannot use `{}` as a parameter name", ILLEGAL_PARAM_NAME);
}

quote!({
let mut params = #jsonrpsee::core::params::ObjectParams::new();
let mut #p = #jsonrpsee::core::params::ObjectParams::new();
#(
if let Err(err) = params.insert( #params_insert ) {
if let Err(err) = #p.insert( #params_insert ) {
panic!("Parameter `{}` cannot be serialized: {:?}", stringify!( #params_insert ), err);
}
)*
params
#p
})
}
ParamKind::Array => {
// Throw away the type.
let params = params.iter().map(|(param, _param_type)| param);

quote!({
let mut params = #jsonrpsee::core::params::ArrayParams::new();
let mut #p = #jsonrpsee::core::params::ArrayParams::new();
#(
if let Err(err) = params.insert( #params ) {
if let Err(err) = #p.insert( #params ) {
panic!("Parameter `{}` cannot be serialized: {:?}", stringify!( #params ), err);
}
)*
params
#p
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/proc-macro-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait Api {
async fn async_call(&self, a: String) -> Result<String, ErrorObjectOwned>;

#[subscription(name = "subscribe", item = PubSubItem)]
async fn sub(&self, kind: PubSubKind, p: PubSubParams) -> SubscriptionResult;
async fn sub(&self, kind: PubSubKind, params: PubSubParams) -> SubscriptionResult;

#[subscription(name = "subscribeSync", item = String)]
fn sync_sub(&self, a: String) -> SubscriptionResult;
Expand Down
Loading