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

Restore display name for event metadata arg type #2090

Merged
merged 2 commits into from
Feb 1, 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
83 changes: 41 additions & 42 deletions crates/ink/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Metadata<'_> {
let error_ty = syn::parse_quote! {
::ink::LangError
};
let error = Self::generate_type_spec(&error_ty);
let error = generate_type_spec(&error_ty);
let environment = self.generate_environment();
quote! {
::ink::metadata::ContractSpec::new()
Expand Down Expand Up @@ -177,46 +177,13 @@ impl Metadata<'_> {
syn::Pat::Ident(ident) => &ident.ident,
_ => unreachable!("encountered ink! dispatch input with missing identifier"),
};
let type_spec = Self::generate_type_spec(&pat_type.ty);
let type_spec = generate_type_spec(&pat_type.ty);
quote! {
::ink::metadata::MessageParamSpec::new(::core::stringify!(#ident))
.of_type(#type_spec)
.done()
}
}

/// Generates the ink! metadata for the given type.
fn generate_type_spec(ty: &syn::Type) -> TokenStream2 {
fn without_display_name(ty: &syn::Type) -> TokenStream2 {
quote! { ::ink::metadata::TypeSpec::of_type::<#ty>() }
}

if let syn::Type::Path(type_path) = ty {
if type_path.qself.is_some() {
return without_display_name(ty)
}
let path = &type_path.path;
if path.segments.is_empty() {
return without_display_name(ty)
}
let segs = path
.segments
.iter()
.map(|seg| &seg.ident)
.collect::<Vec<_>>();
quote! {
::ink::metadata::TypeSpec::with_name_segs::<#ty, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([ #( ::core::stringify!(#segs) ),* ]),
::core::convert::AsRef::as_ref
)
)
}
} else {
without_display_name(ty)
}
}

/// Generates the ink! metadata for all ink! smart contract messages.
fn generate_messages(&self) -> Vec<TokenStream2> {
let mut messages = Vec::new();
Expand Down Expand Up @@ -335,7 +302,7 @@ impl Metadata<'_> {

/// Generates ink! metadata for the given return type.
fn generate_message_return_type(ret_ty: &syn::Type) -> TokenStream2 {
let type_spec = Self::generate_type_spec(ret_ty);
let type_spec = generate_type_spec(ret_ty);
quote! {
::ink::metadata::ReturnTypeSpec::new(#type_spec)
}
Expand Down Expand Up @@ -374,12 +341,12 @@ impl Metadata<'_> {
let block_number: syn::Type = parse_quote!(BlockNumber);
let chain_extension: syn::Type = parse_quote!(ChainExtension);

let account_id = Self::generate_type_spec(&account_id);
let balance = Self::generate_type_spec(&balance);
let hash = Self::generate_type_spec(&hash);
let timestamp = Self::generate_type_spec(&timestamp);
let block_number = Self::generate_type_spec(&block_number);
let chain_extension = Self::generate_type_spec(&chain_extension);
let account_id = generate_type_spec(&account_id);
let balance = generate_type_spec(&balance);
let hash = generate_type_spec(&hash);
let timestamp = generate_type_spec(&timestamp);
let block_number = generate_type_spec(&block_number);
let chain_extension = generate_type_spec(&chain_extension);
let buffer_size_const = quote!(::ink::env::BUFFER_SIZE);
quote_spanned!(span=>
::ink::metadata::EnvironmentSpec::new()
Expand All @@ -396,6 +363,38 @@ impl Metadata<'_> {
}
}

/// Generates the ink! metadata for the given type.
pub fn generate_type_spec(ty: &syn::Type) -> TokenStream2 {
fn without_display_name(ty: &syn::Type) -> TokenStream2 {
quote! { ::ink::metadata::TypeSpec::of_type::<#ty>() }
}

if let syn::Type::Path(type_path) = ty {
if type_path.qself.is_some() {
return without_display_name(ty)
}
let path = &type_path.path;
if path.segments.is_empty() {
return without_display_name(ty)
}
let segs = path
.segments
.iter()
.map(|seg| &seg.ident)
.collect::<Vec<_>>();
quote! {
::ink::metadata::TypeSpec::with_name_segs::<#ty, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([ #( ::core::stringify!(#segs) ),* ]),
::core::convert::AsRef::as_ref
)
)
}
} else {
without_display_name(ty)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 4 additions & 1 deletion crates/ink/codegen/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ pub use self::{
event::Event,
ink_test::InkTest,
item_impls::ItemImpls,
metadata::Metadata,
metadata::{
generate_type_spec,
Metadata,
},
selector::{
SelectorBytes,
SelectorId,
Expand Down
2 changes: 2 additions & 0 deletions crates/ink/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ mod enforced_error;
mod generator;
mod traits;

pub use generator::generate_type_spec;

use self::{
enforced_error::EnforcedErrors,
traits::{
Expand Down
3 changes: 2 additions & 1 deletion crates/ink/macro/src/event/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ fn event_metadata_derive_struct(s: synstructure::Structure) -> syn::Result<Token
.attrs
.iter()
.filter_map(|attr| attr.extract_docs());
let ty_spec = ink_codegen::generate_type_spec(field_ty);
Ok(quote_spanned!(field_span =>
::ink::metadata::EventParamSpec::new(::core::stringify!(#field_name))
.of_type(::ink::metadata::TypeSpec::of_type::<#field_ty>())
.of_type(#ty_spec)
.indexed(#indexed)
.docs([ #( #docs ),* ])
.done()
Expand Down
42 changes: 36 additions & 6 deletions crates/ink/macro/src/tests/event_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,32 @@ fn struct_with_fields_no_topics() {
.signature_topic(<Self as ::ink::env::Event>::SIGNATURE_TOPIC)
.args([
::ink::metadata::EventParamSpec::new(::core::stringify!(field_1))
.of_type(::ink::metadata::TypeSpec::of_type::<u32>())
.of_type(::ink::metadata::TypeSpec::with_name_segs::<u32, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([::core::stringify!(u32)]),
::core::convert::AsRef::as_ref
)
))
.indexed(false)
.docs([])
.done(),
::ink::metadata::EventParamSpec::new(::core::stringify!(field_2))
.of_type(::ink::metadata::TypeSpec::of_type::<u64>())
.of_type(::ink::metadata::TypeSpec::with_name_segs::<u64, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([::core::stringify!(u64)]),
::core::convert::AsRef::as_ref
)
))
.indexed(false)
.docs([])
.done(),
::ink::metadata::EventParamSpec::new(::core::stringify!(field_3))
.of_type(::ink::metadata::TypeSpec::of_type::<u128>())
.of_type(::ink::metadata::TypeSpec::with_name_segs::<u128, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([::core::stringify!(u128)]),
::core::convert::AsRef::as_ref
)
))
.indexed(false)
.docs([])
.done()
Expand Down Expand Up @@ -125,17 +140,32 @@ fn struct_with_fields_and_some_topics() {
.signature_topic(<Self as ::ink::env::Event>::SIGNATURE_TOPIC)
.args([
::ink::metadata::EventParamSpec::new(::core::stringify!(field_1))
.of_type(::ink::metadata::TypeSpec::of_type::<u32>())
.of_type(::ink::metadata::TypeSpec::with_name_segs::<u32, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([::core::stringify!(u32)]),
::core::convert::AsRef::as_ref
)
))
.indexed(false)
.docs([])
.done(),
::ink::metadata::EventParamSpec::new(::core::stringify!(field_2))
.of_type(::ink::metadata::TypeSpec::of_type::<u64>())
.of_type(::ink::metadata::TypeSpec::with_name_segs::<u64, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([::core::stringify!(u64)]),
::core::convert::AsRef::as_ref
)
))
.indexed(true)
.docs([])
.done(),
::ink::metadata::EventParamSpec::new(::core::stringify!(field_3))
.of_type(::ink::metadata::TypeSpec::of_type::<u128>())
.of_type(::ink::metadata::TypeSpec::with_name_segs::<u128, _>(
::core::iter::Iterator::map(
::core::iter::IntoIterator::into_iter([::core::stringify!(u128)]),
::core::convert::AsRef::as_ref
)
))
.indexed(true)
.docs([])
.done()
Expand Down
Loading