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

Messages return TypeSpec directly #1999

Merged
merged 6 commits into from
Nov 20, 2023
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
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [E2E] Allow testing with live-chain state - [#1949](https://github.com/paritytech/ink/pull/1949)
- [E2E] Call builders and extra gas margin option - [#1917](https://github.com/paritytech/ink/pull/1917)
- Make `set_code_hash` generic - [#1906](https://github.com/paritytech/ink/pull/1906)
- Linter: `storage_never_freed` lint - [#1932](https://github.com/paritytech/ink/pull/1932)
- Linter: `strict_balance_equality` lint - [#1914](https://github.com/paritytech/ink/pull/1914)
- Clean E2E configuration parsing - [#1922](https://github.com/paritytech/ink/pull/1922)
- Make `set_code_hash` generic - [#1906](https://github.com/paritytech/ink/pull/1906)

### Changed
- Messages return `TypeSpec` directly - #[1999](https://github.com/paritytech/ink/pull/1999)
- Fail when decoding from storage and not all bytes consumed - [#1897](https://github.com/paritytech/ink/pull/1897)
- [E2E] resolve DispatchError error details for dry-runs - [#1944](https://github.com/paritytech/ink/pull/1994)

### Added
- Linter: `storage_never_freed` lint - [#1932](https://github.com/paritytech/ink/pull/1932)
- Linter: `strict_balance_equality` lint - [#1914](https://github.com/paritytech/ink/pull/1914)

## Version 5.0.0-alpha

Expand Down
30 changes: 11 additions & 19 deletions crates/ink/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ impl Metadata<'_> {
let ident = message.ident();
let args = message.inputs().map(Self::generate_dispatch_argument);
let cfg_attrs = message.get_cfg_attrs(span);
let ret_ty = Self::generate_return_type(Some(&message.wrapped_output()));
let ret_ty =
Self::generate_message_return_type(&message.wrapped_output());
quote_spanned!(span =>
#( #cfg_attrs )*
::ink::metadata::MessageSpec::from_label(::core::stringify!(#ident))
Expand Down Expand Up @@ -311,7 +312,7 @@ impl Metadata<'_> {
as #trait_path>::__ink_TraitInfo
as ::ink::reflect::TraitMessageInfo<#local_id>>::SELECTOR
}};
let ret_ty = Self::generate_return_type(Some(&message.wrapped_output()));
let ret_ty = Self::generate_message_return_type(&message.wrapped_output());
let label = [trait_ident.to_string(), message_ident.to_string()].join("::");
quote_spanned!(message_span=>
#( #cfg_attrs )*
Expand All @@ -333,19 +334,10 @@ impl Metadata<'_> {
}

/// Generates ink! metadata for the given return type.
fn generate_return_type(ret_ty: Option<&syn::Type>) -> TokenStream2 {
match ret_ty {
None => {
quote! {
::ink::metadata::ReturnTypeSpec::new(::core::option::Option::None)
}
}
Some(ty) => {
let type_spec = Self::generate_type_spec(ty);
quote! {
::ink::metadata::ReturnTypeSpec::new(#type_spec)
}
}
fn generate_message_return_type(ret_ty: &syn::Type) -> TokenStream2 {
let type_spec = Self::generate_type_spec(ret_ty);
quote! {
::ink::metadata::ReturnTypeSpec::new(#type_spec)
ascjones marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -361,13 +353,13 @@ impl Metadata<'_> {

quote_spanned!(span=>
::ink::metadata::ReturnTypeSpec::new(if #constructor_info::IS_RESULT {
::core::option::Option::Some(::ink::metadata::TypeSpec::with_name_str::<
::ink::metadata::TypeSpec::with_name_str::<
::ink::ConstructorResult<::core::result::Result<(), #constructor_info::Error>>,
>("ink_primitives::ConstructorResult"))
>("ink_primitives::ConstructorResult")
} else {
::core::option::Option::Some(::ink::metadata::TypeSpec::with_name_str::<
::ink::metadata::TypeSpec::with_name_str::<
::ink::ConstructorResult<()>,
>("ink_primitives::ConstructorResult"))
>("ink_primitives::ConstructorResult")
})
)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ink/tests/return_type_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod tests {
let message = metadata.spec().messages().iter().next().unwrap();
assert_eq!("TraitDefinition::get_value", message.label());

let type_spec = message.return_type().opt_type().unwrap();
let type_spec = message.return_type().ret_type();
let ty = resolve_type(&metadata, type_spec.ty().id);
let (ok_ty, _) = extract_result(&metadata, ty);

Expand All @@ -119,7 +119,7 @@ mod tests {
let constructor = metadata.spec().constructors().iter().next().unwrap();

assert_eq!("try_new", constructor.label());
let type_spec = constructor.return_type().opt_type().unwrap();
let type_spec = constructor.return_type().ret_type();
assert_eq!(
"ink_primitives::ConstructorResult",
format!("{}", type_spec.display_name())
Expand Down
25 changes: 13 additions & 12 deletions crates/metadata/src/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub struct ConstructorSpecBuilder<F: Form, Selector, IsPayable, Returns> {
impl<F> ConstructorSpec<F>
where
F: Form,
TypeSpec<F>: Default,
{
/// Creates a new constructor spec builder.
pub fn from_label(
Expand All @@ -484,7 +485,7 @@ where
selector: Selector::default(),
payable: Default::default(),
args: Vec::new(),
return_type: ReturnTypeSpec::new(None),
return_type: ReturnTypeSpec::new(TypeSpec::default()),
docs: Vec::new(),
default: false,
},
Expand Down Expand Up @@ -668,6 +669,7 @@ mod state {
impl<F> MessageSpec<F>
where
F: Form,
TypeSpec<F>: Default,
{
/// Creates a new message spec builder.
pub fn from_label(
Expand All @@ -686,7 +688,7 @@ where
mutates: false,
payable: false,
args: Vec::new(),
return_type: ReturnTypeSpec::new(None),
return_type: ReturnTypeSpec::new(TypeSpec::default()),
docs: Vec::new(),
default: false,
},
Expand Down Expand Up @@ -1417,45 +1419,44 @@ where
#[must_use]
pub struct ReturnTypeSpec<F: Form = MetaForm> {
#[serde(rename = "type")]
opt_type: Option<TypeSpec<F>>,
ret_type: TypeSpec<F>,
}

impl IntoPortable for ReturnTypeSpec {
type Output = ReturnTypeSpec<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
ReturnTypeSpec {
opt_type: self
.opt_type
.map(|opt_type| opt_type.into_portable(registry)),
ret_type: self.ret_type.into_portable(registry),
}
}
}

impl<F> ReturnTypeSpec<F>
where
F: Form,
TypeSpec<F>: Default,
{
/// Creates a new return type specification from the given type or `None`.
///
/// # Examples
///
/// ```no_run
/// # use ink_metadata::{TypeSpec, ReturnTypeSpec};
/// <ReturnTypeSpec<scale_info::form::MetaForm>>::new(None); // no return type;
/// <ReturnTypeSpec<scale_info::form::MetaForm>>::new(TypeSpec::default()); // no return type;
/// ```
pub fn new<T>(ty: T) -> Self
where
T: Into<Option<TypeSpec<F>>>,
T: Into<TypeSpec<F>>,
{
Self {
opt_type: ty.into(),
ret_type: ty.into(),
}
}

/// Returns the optional return type
pub fn opt_type(&self) -> Option<&TypeSpec<F>> {
self.opt_type.as_ref()
/// Returns the return type
pub fn ret_type(&self) -> &TypeSpec<F> {
&self.ret_type
}
}

Expand Down
Loading