-
Notifications
You must be signed in to change notification settings - Fork 803
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
refactor: Move T:Config
into where clause in #[benchmarks]
macro if needed
#7418
base: master
Are you sure you want to change the base?
refactor: Move T:Config
into where clause in #[benchmarks]
macro if needed
#7418
Conversation
#[benchmarks]
macro if neededT:Config
into where clause in #[benchmarks]
macro if needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can always use the where clause to bound the types then? doesn't it look a bit better?
@@ -482,7 +482,19 @@ pub fn benchmarks( | |||
let mod_span = module.span(); | |||
let where_clause = match syn::parse::<Nothing>(attrs.clone()) { | |||
Ok(_) => quote!(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok(_) => quote!(), | |
Ok(_) => match instance { | |
false => quote!(T: Config), | |
true => quote!(T: Config<I>, I: 'static), | |
}, |
let type_impl_generics = if where_clause.is_empty() { | ||
match instance { | ||
false => quote!(T: Config), | ||
true => quote!(T: Config<I>, I: 'static), | ||
} | ||
} else { | ||
type_use_generics.clone() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace the type_impl_generics
with type_use_generics
in the expansion.
let type_impl_generics = if where_clause.is_empty() { | |
match instance { | |
false => quote!(T: Config), | |
true => quote!(T: Config<I>, I: 'static), | |
} | |
} else { | |
type_use_generics.clone() | |
}; |
let type_impl_generics = if where_clause.is_empty() { | ||
match is_instance { | ||
false => quote!(T: Config), | ||
true => quote!(T: Config<I>, I: 'static), | ||
} | ||
} else { | ||
type_use_generics.clone() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let type_impl_generics = if where_clause.is_empty() { | |
match is_instance { | |
false => quote!(T: Config), | |
true => quote!(T: Config<I>, I: 'static), | |
} | |
} else { | |
type_use_generics.clone() | |
}; |
…ub.com/tsenovilla/polkadot-sdk into refactor/benchmarks-macro-where-clause
Yep I agree it looks nicer. I applied the changes + removed |
Description
Currently, the
#[benchmarks]
macro always add<T:Config>
to the expanded code even if a where clause is used. Using a where clause which also includes a trait bound for the genericT
is triggering this clippy warning from Rust 1.78 onwards. We've found that here in LAOS, as we need to includeT: pallet_vesting::Config
in the where clause, here's the outcome:While this is a harmless warning, only thrown due to a trait bound for T is being defined twice in an expanded code that nobody will see, and while annotating the benchmarks module with
#[allow(clippy::multiple_bound_locations)]
is enough to get rid of it, it might cause unnecessary concerns.Hence, I think it's worth slightly modifying the macro to avoid this.
Review Notes
What I propose is to include
<T: Config>
(or its instance version) in the expanded code only if no where clause was specified, and include that trait bound in the where clause if one is present.I considered always creating a where clause which includes
<T: Config>
even if the macro doesn't specify a where clause and totally getting rid of<T: Config>
, but discarded the idea for simplicity.I also considered checking if
T:Config
is present in the provided where clause (as it's the case in the LAOS example I provided) before adding it, but discarded the idea as well due to it implies a bit more computation and havingT:Config
defined twice in the where clause is harmless: the compiler will ignore the second occurrence and nobody will see it's there.If you think this change is worth it and one of the discarded ideas would be a better approach, I'm happy to push that code instead.