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 codegen for codec::Compact as type parameters #651

Merged
merged 14 commits into from
Sep 21, 2022

Conversation

ascjones
Copy link
Contributor

Fixes use-ink/cargo-contract#734.

When a codec::Compact type appears as a type parameter e.g. Option<Compact<u128>> as it does in the contracts dispatchables e.g. https://github.com/paritytech/substrate/blob/4b7911607b48a5ca7ebf1d655f12d91cdd241866/frame/contracts/src/lib.rs#L498, then this will incorrectly result in a Option<u128> type being generated by subxt-codegen.

This is because the current codegen assumes that the compact value will be the type of the field directly and can generate the following:

struct S { #[codec(compact)] field: u128 }

The compact attribute will cause the conversion to be handled during encoding and decoding of the struct. This means that the "inner" type (in this case u128) can be used directly.

However when the compact type is a type parameter then it is required to use the codec::Compact<T> wrapper type directly instead of the field level attribute.

Refactoring

This PR also incorporates some refactoring of the type path codegen, specifically expanding the TypePathType to a strongly typed enum which maps from the scale_info::TypeDef but with all the information necessary to generate the hierarchical type paths.

@ascjones ascjones requested a review from jsdw September 14, 2022 10:21
@@ -9,7 +9,6 @@ format_code_in_doc_comments = false
comment_width = 80
normalize_comments = true # changed
normalize_doc_attributes = false
license_template_path = "FILE_TEMPLATE" # changed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never really noticed but does removing this mean no checking that the license header is at the top of files now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it has been removed now so it doesn't do anything other than give a warning rust-lang/rustfmt#5103

Comment on lines 378 to 380
struct S {
a: Option<<u128 as codec::HasCompact>::Type>,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding tests to check that some of the other TypePath variants lead to the output you expect?

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good; tidy!

@ascjones ascjones merged commit 3bf7ddc into master Sep 21, 2022
@ascjones ascjones deleted the aj/compact-generic-params branch September 21, 2022 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot pass --storage-deposit-limit to cargo contract call
3 participants