-
Notifications
You must be signed in to change notification settings - Fork 248
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
Conversation
@@ -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 |
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.
I never really noticed but does removing this mean no checking that the license header is at the top of files now?
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.
No it has been removed now so it doesn't do anything other than give a warning rust-lang/rustfmt#5103
codegen/src/types/tests.rs
Outdated
struct S { | ||
a: Option<<u128 as codec::HasCompact>::Type>, | ||
} |
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.
Is it worth adding tests to check that some of the other TypePath variants lead to the output you expect?
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.
Look good; tidy!
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 aOption<u128>
type being generated bysubxt-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 caseu128
) 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 thescale_info::TypeDef
but with all the information necessary to generate the hierarchical type paths.